递归查找与特定模式匹配的所有文件
我需要查找(或更具体地,计数)与此模式匹配的所有文件:
I need to find (or more specifically, count) all files that match this pattern:
*/foo/*.doc
*/foo/*.doc
第一个通配符星号包含可变数量的子目录.
Where the first wildcard asterisk includes a variable number of subdirectories.
通过gnu find,您可以使用正则表达式,它(与-name
不同)与整个路径匹配:
With gnu find you can use regex, which (unlike -name
) match the entire path:
find . -regex '.*/foo/[^/]*.doc'
仅计算文件数:
find . -regex '.*/foo/[^/]*.doc' -printf '%i\n' | wc -l
(格式代码%i
导致find
打印inode号而不是文件名;与文件名不同,保证inode号不包含换行符之类的字符,因此计数更加可靠.感谢@该建议的三元组.)
(The %i
format code causes find
to print the inode number instead of the filename; unlike the filename, the inode number is guaranteed to not have characters like a newline, so counting is more reliable. Thanks to @tripleee for the suggestion.)
不过,我不知道这是否可以在OSX上使用.
I don't know if that will work on OSX, though.