Linux – How to restrict Linux find by prefix

findlinux

I have a directory A with a large number of sub directories and files and want to get a list of all files named foo that are inside of a directory in A that matches *bar. E.g.:

  • Yes: ./goldbar/fiz/baz/foo
  • Yes: ./leadbar/foo
  • No: ./candy/figbar/foo

I have the some additional constraints:

  • I must not descend into directories that don't match the *bar (this is a necessary optimization as it would take to long to scan those)
  • I can't allow the shell to do the glob expansion because it returns to many results: (i.e. find *bar -type f -name foo fails)

I think that the -path flag would give me the results I need but I don't know if it fits the first of the above constraints.


Edit: Assume that there are n*10k directories in A that match *bar. i.e. anything that tries to build a command with all of them (as opposed to handling each one in tern) will fail.

Best Answer

I'm not sure how -path will handle the "do not descend into directories that don't match *bar" requirement, and I'm too lazy to build an environment to find out.
I do know the following will work on pretty much any *nix platform:

for dir in `ls -1 | grep bar`; do
   find $dir -type f -name foo
done

Additional weaseling of the ls/grep bit may be required if you have plain files, sockets, etc. in your top-level directory, or if you want to revise your conditions a little.