Skip to main content
3 of 4
Updated for OPs clarification
Scottie H
  • 744
  • 3
  • 12

I'm on RHEL 7. This works for me:

find . -path "*/foo/*" ! -path '*/foo/*/*' -type f

Note the DOT before the -path. (Or substitute your path there, such as /home/$USER)

The DOT says "Start looking in the current directory"

the -path says "Look for anything, followed by a sub-directory named foo, followed by anything" except for directories nested under "foo".

The -type f says give me only the files in a matching directory.
Looks like

-path "*/foo/*" ! -path '*/foo/*/*'  

Doesn't get everything. Not elegant, but it works:

find . -path "*/foo/*" -type f | awk -F'/' '{if (match$(NF-1),"foo")) print $0}'  
Scottie H
  • 744
  • 3
  • 12