递归查找与特定模式匹配的所有文件 [英] Recursively find all files that match a certain pattern

查看:73
本文介绍了递归查找与特定模式匹配的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要查找(或更具体地,计数)与此模式匹配的所有文件:

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.

这篇关于递归查找与特定模式匹配的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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