Skip to main content
4 of 4
incorrect use of match()
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k

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 as it misses things like ./a/foo/b/foo/c.

If you can guarantee that file and directory names won't contain newline characters, you could post-process the output with awk like:

find . -path "*/foo/*" -type f | awk -F/ '$(NF-1) == "foo"' 
Scottie H
  • 744
  • 3
  • 12