find: Directories
1
1 2.9 Directories
1 ===============
1
1 Here is how to control which directories 'find' searches, and how it
1 searches them. These two options allow you to process a horizontal
1 slice of a directory tree.
1
1 -- Option: -maxdepth levels
1 Descend at most LEVELS (a non-negative integer) levels of
1 directories below the command line arguments. '-maxdepth 0' means
1 only apply the tests and actions to the command line arguments.
1
1 -- Option: -mindepth levels
1 Do not apply any tests or actions at levels less than LEVELS (a
1 non-negative integer). '-mindepth 1' means process all files
1 except the command line arguments.
1
1 -- Option: -depth
1 Process each directory's contents before the directory itself.
1 Doing this is a good idea when producing lists of files to archive
1 with 'cpio' or 'tar'. If a directory does not have write
1 permission for its owner, its contents can still be restored from
1 the archive since the directory's permissions are restored after
1 its contents.
1
1 -- Option: -d
1 This is a deprecated synonym for '-depth', for compatibility with
1 Mac OS X, FreeBSD and OpenBSD. The '-depth' option is a POSIX
1 feature, so it is better to use that.
1
1 -- Action: -prune
1 If the file is a directory, do not descend into it. The result is
1 true. For example, to skip the directory 'src/emacs' and all files
1 and directories under it, and print the names of the other files
1 found:
1
1 find . -wholename './src/emacs' -prune -o -print
1
1 The above command will not print './src/emacs' among its list of
1 results. This however is not due to the effect of the '-prune'
1 action (which only prevents further descent, it doesn't make sure
1 we ignore that item). Instead, this effect is due to the use of
1 '-o'. Since the left hand side of the "or" condition has succeeded
1 for './src/emacs', it is not necessary to evaluate the
1 right-hand-side ('-print') at all for this particular file. If you
1 wanted to print that directory name you could use either an extra
1 '-print' action:
1
1 find . -wholename './src/emacs' -prune -print -o -print
1
1 or use the comma operator:
1
1 find . -wholename './src/emacs' -prune , -print
1
1 If the '-depth' option is in effect, the subdirectories will have
1 already been visited in any case. Hence '-prune' has no effect in
1 this case.
1
1 Because '-delete' implies '-depth', using '-prune' in combination
1 with '-delete' may well result in the deletion of more files than
1 you intended.
1
1 -- Action: -quit
1 Exit immediately (with return value zero if no errors have
1 occurred). This is different to '-prune' because '-prune' only
1 applies to the contents of pruned directories, while '-quit' simply
1 makes 'find' stop immediately. No child processes will be left
1 running, but no more files specified on the command line will be
1 processed. For example, 'find /tmp/foo /tmp/bar -print -quit' will
1 print only '/tmp/foo'. Any command lines which have been built by
1 '-exec ... \+' or '-execdir ... \+' are invoked before the program
1 is exited.
1
1 -- Option: -noleaf
1 Do not optimize by assuming that directories contain 2 fewer
1 subdirectories than their hard link count. This option is needed
1 when searching filesystems that do not follow the Unix
1 directory-link convention, such as CD-ROM or MS-DOS filesystems or
1 AFS volume mount points. Each directory on a normal Unix
1 filesystem has at least 2 hard links: its name and its '.' entry.
1 Additionally, its subdirectories (if any) each have a '..' entry
1 linked to that directory. When 'find' is examining a directory,
1 after it has statted 2 fewer subdirectories than the directory's
1 link count, it knows that the rest of the entries in the directory
1 are non-directories ("leaf" files in the directory tree). If only
1 the files' names need to be examined, there is no need to stat
1 them; this gives a significant increase in search speed.
1
1 -- Option: -ignore_readdir_race
1 If a file disappears after its name has been read from a directory
1 but before 'find' gets around to examining the file with 'stat',
1 don't issue an error message. If you don't specify this option, an
1 error message will be issued. This option can be useful in system
1 scripts (cron scripts, for example) that examine areas of the
1 filesystem that change frequently (mail queues, temporary
1 directories, and so forth), because this scenario is common for
1 those sorts of directories. Completely silencing error messages
1 from 'find' is undesirable, so this option neatly solves the
1 problem. There is no way to search one part of the filesystem with
1 this option on and part of it with this option off, though. When
1 this option is turned on and find discovers that one of the
1 start-point files specified on the command line does not exist, no
1 error message will be issued.
1
1 -- Option: -noignore_readdir_race
1 This option reverses the effect of the '-ignore_readdir_race'
1 option.
1