2.11 Directories

Here is how to control which directories find searches, and how it searches them. These two options allow you to process a horizontal slice of a directory tree.

Option: -maxdepth levels

Descend at most levels (a non-negative integer) levels of directories below the command line arguments. Using ‘-maxdepth 0’ means only apply the tests and actions to the command line arguments.

$ mkdir -p dir/d1/d2/d3/d4/d5/d6

$ find dir -maxdepth 1
dir
dir/d1

$ find dir -mindepth 5
dir/d1/d2/d3/d4/d5
dir/d1/d2/d3/d4/d5/d6

$ find dir -mindepth 2 -maxdepth 4
dir/d1/d2
dir/d1/d2/d3
dir/d1/d2/d3/d4
Option: -mindepth levels

Do not apply any tests or actions at levels less than levels (a non-negative integer). Using ‘-mindepth 1’ means process all files except the command line arguments.

See ‘-maxdepth’ for examples.

Option: -depth

Process each directory’s contents before the directory itself. Doing this is a good idea when producing lists of files to archive with cpio or tar. If a directory does not have write permission for its owner, its contents can still be restored from the archive since the directory’s permissions are restored after its contents.

Option: -d

This is a deprecated synonym for ‘-depth’, for compatibility with Mac OS X, FreeBSD and OpenBSD. The ‘-depth’ option is a POSIX feature, so it is better to use that.

Action: -prune

If the file is a directory, do not descend into it. The result is true. For example, to skip the directory src/emacs and all files and directories under it, and print the names of the other files found:

find . -wholename './src/emacs' -prune -o -print

The above command will not print ./src/emacs among its list of results. This however is not due to the effect of the ‘-prune’ action (which only prevents further descent, it doesn’t make sure we ignore that item). Instead, this effect is due to the use of ‘-o’. Since the left hand side of the “or” condition has succeeded for ./src/emacs, it is not necessary to evaluate the right-hand-side (‘-print’) at all for this particular file. If you wanted to print that directory name you could use either an extra ‘-print’ action:

find . -wholename './src/emacs' -prune -print -o -print

or use the comma operator:

find . -wholename './src/emacs' -prune , -print

If the ‘-depth’ option is in effect, the subdirectories will have already been visited in any case. Hence ‘-prune’ has no effect in this case.

Because ‘-delete’ implies ‘-depth’, using ‘-prune’ in combination with ‘-delete’ may well result in the deletion of more files than you intended.

Action: -quit

Exit immediately (with return value zero if no errors have occurred). This is different to ‘-prune’ because ‘-prune’ only applies to the contents of pruned directories, while ‘-quit’ simply makes find stop immediately. No child processes will be left running. Any command lines which have been built by ‘-exec ... \+’ or ‘-execdir ... \+’ are invoked before the program is exited. After ‘-quit’ is executed, no more files specified on the command line will be processed. For example, ‘find /tmp/foo /tmp/bar -print -quit’ will print only ‘/tmp/foo’. One common use of ‘-quit’ is to stop searching the file system once we have found what we want. For example, if we want to find just a single file we can do this:

find / -name needle -print -quit
Option: -noleaf

Do not optimize by assuming that directories contain 2 fewer subdirectories than their hard link count. This option is needed when searching filesystems that do not follow the Unix directory-link convention, such as CD-ROM or MS-DOS filesystems or AFS volume mount points. Each directory on a normal Unix filesystem has at least 2 hard links: its name and its . entry. Additionally, its subdirectories (if any) each have a .. entry linked to that directory. When find is examining a directory, after it has statted 2 fewer subdirectories than the directory’s link count, it knows that the rest of the entries in the directory are non-directories (leaf files in the directory tree). If only the files’ names need to be examined, there is no need to stat them; this gives a significant increase in search speed.

Option: -ignore_readdir_race

If a file disappears after its name has been read from a directory but before find gets around to examining the file with stat, don’t issue an error message. If you don’t specify this option, an error message will be issued.

Furthermore, find with the ‘-ignore_readdir_race’ option will ignore errors of the ‘-delete’ action in the case the file has disappeared since the parent directory was read: it will not output an error diagnostic, and the return code of the ‘-delete’ action will be true.

This option can be useful in system scripts (cron scripts, for example) that examine areas of the filesystem that change frequently (mail queues, temporary directories, and so forth), because this scenario is common for those sorts of directories. Completely silencing error messages from find is undesirable, so this option neatly solves the problem. There is no way to search one part of the filesystem with this option on and part of it with this option off, though. When this option is turned on and find discovers that one of the start-point files specified on the command line does not exist, no error message will be issued.

Option: -noignore_readdir_race

This option reverses the effect of the ‘-ignore_readdir_race’ option.