tdao
tdao

Reputation: 17713

find recursively, but with specific sub-folder name

This command find all files name "log_7" recursively in current folder.

find . -name log_7

Assume many sub-folders under the current folder tree has a file with that same name "log_7":

./am/f1/log_7
./ke/f2/log_7
./sa/f6/log_7
..
./xx/f97/log_7 

Is there a way to explicitly say that we only want to search for "log_7" in a folder name "f2" ? such that the result from find will only list only one entry:

./ke/f2/log_7

Upvotes: 3

Views: 2249

Answers (3)

mickp
mickp

Reputation: 1819

Simple glob should do:

printf '%s\n' */f2/log_7

If there is a possibility for more leading folders, you can use the globstar option:

shopt -s globstar
printf '%s\n' **/f2/log_7

Upvotes: 0

anand
anand

Reputation: 163

There is a different way is to use xargs for same thing

find . -name filename | xargs grep -e refectory

But find with built in regex is preferable.

Upvotes: 0

tmcnicol
tmcnicol

Reputation: 566

You could use a regular expression.

find . -regex '.*/f2/log_7'

This will only match if log_7 is directly nested under f2

Upvotes: 5

Related Questions