UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 17.23 Finding Files with -prune Chapter 17
Finding Files with find
Next: 17.25 Keeping find From Searching Networked Filesystems
 

17.24 Skipping Some Parts of a Tree in find (A More Selective -prune)

Q: I want to run find across a directory tree, skipping standard directories like /usr/spool and /usr/local/bin. A -name dirname -prune clause won't do it because  - name doesn't match the whole pathname - just each part of it, such as spool or local. How can I make find match the whole pathname, like /usr/local/bin/, instead of all directories named bin?

A: It cannot be done directly. You can do this:

A:
test 
find /path -exec test {} = /foo/bar -o {} = /foo/baz \; -prune -o pred

A: This will not perform pred on /foo/bar and /foo/baz; if you want them done, but not any files within them, try:

A:

find path \( -exec test test-exprs \; ! -prune \) -o pred

A: The second version is worth close study, keeping the manual for find at hand for reference. It shows a great deal about how find works.

A: The -prune operator simply says "do not search the current path any deeper," and then succeeds a la -print.

Q: I only want a list of pathnames; the pred I use in your answer above will be just -print. I think I could solve my particular problem by piping the find output through a sed (27.15) or egrep -v (27.3) filter that deletes the pathnames I don't want to see.

A: That would probably be fastest. Using test runs the test program for each file name, which is quite slow. There's more about complex find expressions in other articles, especially 17.6 and 17.12.

- CT, JP


Previous: 17.23 Finding Files with -prune UNIX Power ToolsNext: 17.25 Keeping find From Searching Networked Filesystems
17.23 Finding Files with -prune Book Index17.25 Keeping find From Searching Networked Filesystems

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System