/222 or -perm /a+w) but are not executable for anybody (! -perm /111
or ! -perm /a+x respectively).
Pruning ‐ omitting files and subdirectories
• Copy the contents of /source‐dir to /dest‐dir, but omit files and directories named .snapshot (and anything in them). It also omits files or directories whose name ends in ‘~’, but not their contents.
$ cd /source‐dir
$ find . -name .snapshot -prune -o \( \! -name '*~' -print0 \) \
| cpio -pmd0 /dest‐dir
The construct -prune -o \( ... -print0 \) is quite common. The idea here is that the expression before -prune matches things which are to be pruned. However, the -prune action itself returns true, so the fol‐
lowing -o ensures that the right hand side is evaluated only for those directories which didn’t get pruned (the contents of the pruned directories are not even visited, so their contents are irrelevant). The
expression on the right hand side of the -o is in parentheses only for clarity. It emphasises that the -print0 action takes place only for things that didn’t have -prune applied to them. Because the default
‘and’ condition between tests binds more tightly than -o, this is the default anyway, but the parentheses help to show what is going on.
• Given the following directory of projects and their associated SCM administrative directories, perform an efficient search for the projects’ roots:
$ find repo/ \
\( -exec test -d '{}/.svn' \; \
-or -exec test -d '{}/.git' \; \
-or -exec test -d '{}/CVS' \; \
\) -print -prune
Sample output:
repo/project1/CVS
repo/gnu/project2/.svn
repo/gnu/project3/.svn
repo/gnu/project3/src/.svn
repo/project4/.git
In this example, -prune prevents unnecessary descent into directories that have already been discovered (for example we do not search project3/src because we already found project3/.svn), but ensures sibling
directories (project2 and project3) are found.
Other useful examples
• Search for several file types.
$ find /tmp -type f,d,l
Search for files, directories, and symbolic links in the directory /tmp passing these types as a comma‐separated list (GNU extension), which is otherwise equivalent to the longer, yet more portable:
$ find /tmp \( -type f -o -type d -o -type l \)
• Search for files with the particular name needle and stop immediately when we find the first one.
$ find / ‐name needle ‐print ‐quit
• Demonstrate the interpretation of the %f and %h format directives of the -printf action for some corner‐cases. Here is an example including some output.
$ find . .. / /tmp /tmp/TRACE compile compile/64/tests/find ‐maxdepth 0 ‐printf ’[%h][%f]\n’
[.][.]
[.][..]
[][/]
[][tmp]
[/tmp][TRACE]
[.][compile]
[compile/64/tests][find]
EXIT STATUS
find exits with status 0 if all files are processed successfully, greater than 0 if errors occur. This is deliberately a very broad description, but if the return value is non‐zero, you should not rely on the cor‐
rectness of the results of find.
When some error occurs, find may stop immediately, without completing all the actions specified. For example, some starting points may not have been examined or some pending program invocations for -exec ... {} + or
-execdir ... {} + may not have been performed.
HISTORY
As of findutils‐4.2.2, shell metacharacters (‘*’, ‘?’ or ‘[]’ for example) used in filename