FreeBSD 指定部分目录名称以使用 find 进行搜索

FreeBSD specify partial directory names to search using find

我有以下目录:

/usr/smac/results/june/

在这个目录中我有很多目录。我只对以 'ceg' 开头的那些感兴趣,例如

ceg20160611234
gfd20160611493
ceg20160611543
ceg20160612193
flc20160612873

其中每个都有以下内容:

run1/logfile run2/logfile run3/logfile 等等

我知道如何使用 'find' 命令在每个日志文件中搜索字符串,即

find . -maxdepth 2 -type f -name logfile -exec egrep -l 'passed' {} \;

但是这将搜索所有文件,即

ceg20160611234/run1/logfile
ceg20160611234/run2/logfile
ceg20160611234/run3/logfile
[...]
gfd20160611493/run1/logfile
gfd20160611493/run2/logfile
[...]
ceg20160611543/run1/logfile
ceg20160611543/run2/logfile
[...]
ceg20160612193/run1/logfile
ceg20160612193/run2/logfile
[...]
flc20160612873/run1/logfile
flc20160612873/run2/logfile
[...]

我想最小化搜索以仅搜索以 'ceg' 开头的目录,例如

ceg20160611234/run1/logfile
ceg20160611234/run2/logfile
ceg20160611234/run3/logfile
[...]
ceg20160611543/run1/logfile
ceg20160611543/run2/logfile
[...]
ceg20160612193/run1/logfile
ceg20160612193/run2/logfile
[...]

find ./ceg* -maxdepth 2 -type f -name logfile -exec egrep -l 'passed' {} \;

您可以使用 find 命令作为

find ceg* -maxdepth 2 -type f -name "logfile" -exec egrep -l 'passed' {} \;

将对以 ceg* 开头的目录名称执行上述操作,并对这些目录中名为 logfile 的文件执行 egrep

这样做:

查找/usr/smac/results/june/-名称'ceg*'

基本上,-name 的参数支持shell 特殊字符,例如“*”和“?”;但是,一定要用引号 ('') 将它们传递给查找命令,而不是被 shell.

展开