Skip to main content

Find all files in directories named foo [Resolved]

I would like to find all files which reside in any directory foo. For example, consider the following files:

foo/w
a/foo/x
a/b/foo/y
a/b/c/foo/z
a/b/c/foo/bar/n

I would like to find the files w,x,y,z and get them listed as above, i.e., with their relative paths. My attempt was something like

$ find . -path '**/foo' -type f

which doesn't work. The search should not include file n, i.e., only files whose parent directory is named foo we are interested in.


Question Credit: Max Maier
Question Reference
Asked September 19, 2019
Posted Under: Unix Linux
17 views
6 Answers

The first thing that comes to mind is:

find $ROOT_PATH -type d -name foo | xargs -n1 -I{} find {} -type f


credit: Jeff Schaller
Answered September 19, 2019

Tried with Below approach

find  . -type d -iname "*foo* -exec ls -ltr {} \; 2>/dev/null| awk '!/^d/||/^l/{print $0}'

credit: Praveen Kumar BS
Answered September 19, 2019
Your Answer