使用 find 匹配以数字开头的文件名 [英] use find to match filenames starting with number

查看:19
本文介绍了使用 find 匹配以数字开头的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找文件名中只有数字的文件.例如,如果我有文件 123.txt、45.doc、alpha_123、beta_123.txt、45 和 123,我只想匹配文件 45 和 123.我已尝试使用 find 的各种选项和 grep 或它们的组合,但没有按我的意愿工作或给我语法错误.

I am trying to find files that have only numbers in their filenames. For example, if I have the files 123.txt, 45.doc, alpha_123, beta_123.txt, 45 and 123, I only want to match files 45 and 123. I have tried various options with find and grep or combinations of them, but nothing worked as I wanted or gave me syntax errors.

还有没有机会我只能匹配空文件?我尝试使用空"选项,但也没有成功,尽管这可能是由于使用了错误的语法.

Also is there any chance that I can only match for empty files? I tried using the option "empty" but also was unsuccessful, although this was probably due to using the wrong syntax.

这个找到 .-name "[0-9]*" kind of 起作用了,因为它找到了文件名中带有数字的文件,例如文件 123 和文件 45,但它也找到了文件扩展名,例如 123.txt 和 45.doc,不幸的是这不是我想要的.

This find . -name "[0-9]*" kind of worked, as in it found files with numbers in their filenames, for example file 123 and file 45, but it also found files with an extension, for example 123.txt and 45.doc, and unfortunately this is not what I want.

根据下面的建议(因为我的原始问题在发布答案后被大量编辑),完全符合我的要求的是 $ find .-type f -name '[[:digit:]]*' |grep -E '.*/[0-9]+$'.

From the suggestions below (as my original question was heavily edited after answers were posted), what worked exactly as I wanted was $ find . -type f -name '[[:digit:]]*' | grep -E '.*/[0-9]+$'.

对于最初的误导性/模棱两可的帖子,我深表歉意,我希望编辑后的版本对其他也对如何将 find 与正则表达式一起使用感到困惑的用户有所帮助.

I apologise for the initial misleading/ambiguous post, I hope the edited version will prove helpful for other users also confused about how to use find with regex.

推荐答案

要么:

$ find -E .  -type f -regex '.*/[[:digit:]]+$'

或者这个:

$ find .  -type f -name '[[:digit:]]*' | grep -E '.*/[0-9]+$'

努力查找名称完全由数字组成的文件(即没有扩展名)

work to find files with names composed entirely of digits (i.e., no extension)

这篇关于使用 find 匹配以数字开头的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆