我想列出以数字开头并以“.c”扩展名结尾的文件。下面是使用的find命令。但是,它并没有给出 预期输出。
命令:
find -type f -regex "^[0-9].*\\.c$"
最佳答案
这是因为正则表达式选项适用于完整路径,而您仅指定了文件名。来自man find
:
-regex pattern File name matches regular expression pattern. This is a match on the whole path, not a search. For example, to match a file named './fubar3', you can use the regular expression '.*bar.' or '.*b.*3', but not 'f.*r3'. The regular expressions understood by find are by default Emacs Regular Expressions, but this can be changed with the -regextype option.
试试这个:
find -type f -regex ".*/[0-9][^/]+\.c$"
您明确查找一个字符串,其中“文件名的格式遵循任何以斜杠结尾的字符串”
更新:我对正则表达式进行了更正。我将文件名中的 .*
更改为 [^\]+
,因为我们不希望在 “任何以斜杠结尾的字符串” 之后在字符串的该部分中查找斜杠,因为它不是文件名而是另一个目录!
注意:匹配的.*
可能非常有害......
关于regex - Find 命令中的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30780731/