I need to search a rather big directory hierarchy for regular files with names matching a particular filename globbing pattern. The hierarchy is so big (both very deep and with some enormous directories) that it would take far too long to take a naive approach:
find /top/dir -type f -name 'pattern'
pattern is some pattern like
Due to the nature of the directory structure, I know I could introduce an optimization to prune the search tree if
find found a file in a directory. For example, finding one or several files in a particular directory would mean that I wouldn't need to examine any of the subdirectories of that specific directory for other matches.
-prune to a regular file isn't doing the right thing, I can't just do
find /top/dir -type f -name 'pattern' -prune
Question: How do I avoid searching the subdirectories of the directory that contains the file(s) that matches the pattern?