stevo stevo - 5 months ago 11
Linux Question

FreeBSD specify partial directory names to search using find

I have the following directory:

/usr/smac/results/june/


In this directory I have a lot of directories. I'm only interested in the ones that start with 'ceg' e.g.

ceg20160611234
gfd20160611493
ceg20160611543
ceg20160612193
flc20160612873


Each of these has the following:

run1/logfile
run2/logfile
run3/logfile
etc.

I know how to use the 'find' command to search for a string within each logfile i.e.

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


However this will search all files i.e.

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
[...]


I want to minimise the search to only search the directories starting with 'ceg' e.g.

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

Answer

You can use the find command as

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

will do the above operation on the directory names starting with ceg* and performs the egrep on the files named logfile inside those directories.

Comments