Savannah bug #20803: -prune should always return true
[findutils.git] / doc / find.texi
blob0eca9a5b6859c0754495d9328c8e0748ca463de7
1 \input texinfo @c -*-texinfo-*-
2 @c %**start of header
3 @setfilename find.info
4 @settitle Finding Files
5 @c For double-sided printing, uncomment:
6 @c @setchapternewpage odd
7 @c %**end of header
9 @include version.texi
10 @include ../locate/dblocation.texi
12 @iftex
13 @finalout
14 @end iftex
16 @dircategory Basics
17 @direntry
18 * Finding files: (find).        Operating on files matching certain criteria.
19 @end direntry
21 @dircategory Individual utilities
22 @direntry
23 * find: (find)Invoking find.                    Finding and acting on files.
24 * locate: (find)Invoking locate.                Finding files in a database.
25 * updatedb: (find)Invoking updatedb.            Building the locate database.
26 * xargs: (find)Invoking xargs.                  Operating on many files.
27 @end direntry
29 @copying
31 This file documents the GNU utilities for finding files that match
32 certain criteria and performing various operations on them.
34 Copyright (C) 1994, 1996, 1998, 2000, 2001, 2003, 2004, 2005, 2006,
35 2007 Free Software Foundation, Inc.
37 Permission is granted to copy, distribute and/or modify this document
38 under the terms of the GNU Free Documentation License, Version 1.2 or
39 any later version published by the Free Software Foundation; with no
40 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
41 Texts.  A copy of the license is included in the section entitled
42 ``GNU Free Documentation License.''
43 @end copying
45 @titlepage
46 @title Finding Files
47 @subtitle Edition @value{EDITION}, for GNU @code{find} version @value{VERSION}
48 @subtitle @value{UPDATED}
49 @author by David MacKenzie and James Youngman
51 @page
52 @vskip 0pt plus 1filll
53 @insertcopying{}
54 @end titlepage
56 @contents
58 @ifnottex
59 @node Top
60 @top GNU Findutils
61 @comment  node-name,  next,  previous,  up
62 @insertcopying
64 This file documents the GNU utilities for finding files that match
65 certain criteria and performing various actions on them.
67 This is edition @value{EDITION}, for @code{find} version @value{VERSION}.
68 @end ifnottex
70 @c The master menu, created with texinfo-master-menu, goes here.
72 @menu
73 * Introduction::                Summary of the tasks this manual describes.
74 * Finding Files::               Finding files that match certain criteria.
75 * Actions::                     Doing things to files you have found.
76 * Databases::                   Maintaining file name databases.
77 * File Permissions::            How to control access to files.
78 * Date input formats::          Specifying literal times.
79 * Reference::                   Summary of how to invoke the programs.
80 * Common Tasks::                Solutions to common real-world problems.
81 * Worked Examples::             Examples demonstrating more complex points.
82 * Security Considerations::     Security issues relating to findutils.
83 * Error Messages::              Explanations of some messages you might see.
84 * GNU Free Documentation License::  Copying and sharing this manual.
85 * Primary Index::               The components of @code{find} expressions.
86 @end menu
88 @node Introduction
89 @chapter Introduction
91 This manual shows how to find files that meet criteria you specify,
92 and how to perform various actions on the files that you find.  The
93 principal programs that you use to perform these tasks are
94 @code{find}, @code{locate}, and @code{xargs}.  Some of the examples in
95 this manual use capabilities specific to the GNU versions of those
96 programs.
98 GNU @code{find} was originally written by Eric Decker, with
99 enhancements by David MacKenzie, Jay Plett, and Tim Wood.  GNU
100 @code{xargs} was originally written by Mike Rendell, with enhancements
101 by David MacKenzie.  GNU @code{locate} and its associated utilities
102 were originally written by James Woods, with enhancements by David
103 MacKenzie.  The idea for @samp{find -print0} and @samp{xargs -0} came
104 from Dan Bernstein.  The current maintainer of GNU findutils (and this
105 manual) is James Youngman.  Many other people have contributed bug
106 fixes, small improvements, and helpful suggestions.  Thanks!
108 To report a bug in GNU findutils, please use the form on the Savannah
109 web site at
110 @code{http://savannah.gnu.org/bugs/?group=findutils}.  Reporting bugs
111 this way means that you will then be able to track progress in fixing
112 the problem.  
114 If you don't have web access, you can also just send mail to the
115 mailing list.  The mailing list @email{bug-findutils@@gnu.org} carries
116 discussion of bugs in findutils, questions and answers about the
117 software and discussion of the development of the programs.  To join
118 the list, send email to @email{bug-findutils-request@@gnu.org}.
120 Please read any relevant sections of this manual before asking for
121 help on the mailing list.  You may also find it helpful to read the
122 NON-BUGS section of the @code{find} manual page.
124 If you ask for help on the mailing list, people will be able to help
125 you much more effectively if you include the following things:
127 @itemize @bullet
128 @item The version of the software you are running.  You can find this
129 out by running @samp{locate --version}.
130 @item What you were trying to do
131 @item The @emph{exact} command line you used
132 @item The @emph{exact} output you got (if this is very long, try to
133 find a smaller example which exhibits the same problem)
134 @item The output you expected to get
135 @end itemize
137 It may also be the case that the bug you are describing has already
138 been fixed, if it is a bug.  Please check the most recent findutils
139 releases at @url{ftp://ftp.gnu.org/gnu/findutils} and, if possible,
140 the development branch at @url{ftp://alpha.gnu.org/gnu/findutils}.
141 If you take the time to check that your bug still exists in current
142 releases, this will greatly help people who want to help you solve
143 your problem.  Please also be aware that if you obtained findutils as
144 part of the GNU/Linux 'distribution', the distributions often lag
145 seriously behind findutils releases, even the stable release.  Please
146 check the GNU FTP site.
148 @menu
149 * Scope::
150 * Overview::
151 * find Expressions::
152 @end menu
154 @node Scope
155 @section Scope
157 For brevity, the word @dfn{file} in this manual means a regular file,
158 a directory, a symbolic link, or any other kind of node that has a
159 directory entry.  A directory entry is also called a @dfn{file name}.
160 A file name may contain some, all, or none of the directories in a
161 path that leads to the file.  These are all examples of what this
162 manual calls ``file names'':
164 @example
165 parser.c
166 README
167 ./budget/may-94.sc
168 fred/.cshrc
169 /usr/local/include/termcap.h
170 @end example
172 A @dfn{directory tree} is a directory and the files it contains, all
173 of its subdirectories and the files they contain, etc.  It can also be
174 a single non-directory file.
176 These programs enable you to find the files in one or more directory
177 trees that:
179 @itemize @bullet
180 @item
181 have names that contain certain text or match a certain pattern;
182 @item
183 are links to certain files;
184 @item
185 were last used during a certain period of time;
186 @item
187 are within a certain size range;
188 @item
189 are of a certain type (regular file, directory, symbolic link, etc.);
190 @item
191 are owned by a certain user or group;
192 @item
193 have certain access permissions or special mode bits;
194 @item
195 contain text that matches a certain pattern;
196 @item
197 are within a certain depth in the directory tree;
198 @item
199 or some combination of the above.
200 @end itemize
202 Once you have found the files you're looking for (or files that are
203 potentially the ones you're looking for), you can do more to them than
204 simply list their names.  You can get any combination of the files'
205 attributes, or process the files in many ways, either individually or
206 in groups of various sizes.  Actions that you might want to perform on
207 the files you have found include, but are not limited to:
209 @itemize @bullet
210 @item
211 view or edit
212 @item
213 store in an archive
214 @item
215 remove or rename
216 @item
217 change access permissions
218 @item
219 classify into groups
220 @end itemize
222 This manual describes how to perform each of those tasks, and more.
224 @node Overview
225 @section Overview
227 The principal programs used for making lists of files that match given
228 criteria and running commands on them are @code{find}, @code{locate},
229 and @code{xargs}.  An additional command, @code{updatedb}, is used by
230 system administrators to create databases for @code{locate} to use.
232 @code{find} searches for files in a directory hierarchy and prints
233 information about the files it found.  It is run like this:
235 @example
236 find @r{[}@var{file}@dots{}@r{]} @r{[}@var{expression}@r{]}
237 @end example
239 @noindent
240 Here is a typical use of @code{find}.  This example prints the names
241 of all files in the directory tree rooted in @file{/usr/src} whose
242 name ends with @samp{.c} and that are larger than 100 Kilobytes.
243 @example
244 find /usr/src -name '*.c' -size +100k -print
245 @end example
247 Notice that the wildcard must be enclosed in quotes in order to
248 protect it from expansion by the shell.
250 @code{locate} searches special file name databases for file names that
251 match patterns.  The system administrator runs the @code{updatedb}
252 program to create the databases.  @code{locate} is run like this:
254 @example
255 locate @r{[}@var{option}@dots{}@r{]} @var{pattern}@dots{}
256 @end example
258 @noindent
259 This example prints the names of all files in the default file name
260 database whose name ends with @samp{Makefile} or @samp{makefile}.
261 Which file names are stored in the database depends on how the system
262 administrator ran @code{updatedb}.
263 @example
264 locate '*[Mm]akefile'
265 @end example
267 The name @code{xargs}, pronounced EX-args, means ``combine
268 arguments.''  @code{xargs} builds and executes command lines by
269 gathering together arguments it reads on the standard input.  Most
270 often, these arguments are lists of file names generated by
271 @code{find}.  @code{xargs} is run like this:
273 @example
274 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
275 @end example
277 @noindent
278 The following command searches the files listed in the file
279 @file{file-list} and prints all of the lines in them that contain the
280 word @samp{typedef}.
281 @example
282 xargs grep typedef < file-list
283 @end example
285 @node find Expressions
286 @section @code{find} Expressions
288 The expression that @code{find} uses to select files consists of one
289 or more @dfn{primaries}, each of which is a separate command line
290 argument to @code{find}.  @code{find} evaluates the expression each
291 time it processes a file.  An expression can contain any of the
292 following types of primaries:
294 @table @dfn
295 @item options
296 affect overall operation rather than the processing of a specific
297 file;
298 @item tests
299 return a true or false value, depending on the file's attributes;
300 @item actions
301 have side effects and return a true or false value; and
302 @item operators
303 connect the other arguments and affect when and whether they are
304 evaluated.
305 @end table
307 You can omit the operator between two primaries; it defaults to
308 @samp{-and}.  @xref{Combining Primaries With Operators}, for ways to
309 connect primaries into more complex expressions.  If the expression
310 contains no actions other than @samp{-prune}, @samp{-print} is
311 performed on all files for which the entire expression is true
312 (@pxref{Print File Name}).
314 Options take effect immediately, rather than being evaluated for each
315 file when their place in the expression is reached.  Therefore, for
316 clarity, it is best to place them at the beginning of the expression.
317 There are two exceptions to this; @samp{-daystart} and @samp{-follow}
318 have different effects depending on where in the command line they
319 appear.  This can be confusing, so it's best to keep them at the
320 beginning, too.
322 Many of the primaries take arguments, which immediately follow them in
323 the next command line argument to @code{find}.  Some arguments are
324 file names, patterns, or other strings; others are numbers.  Numeric
325 arguments can be specified as
327 @table @code
328 @item +@var{n}
329 for greater than @var{n},
330 @item -@var{n}
331 for less than @var{n},
332 @item @var{n}
333 for exactly @var{n}.
334 @end table
336 @node Finding Files
337 @chapter Finding Files
339 By default, @code{find} prints to the standard output the names of the
340 files that match the given criteria.  @xref{Actions}, for how to get
341 more information about the matching files.
344 @menu
345 * Name::
346 * Links::
347 * Time::
348 * Size::
349 * Type::
350 * Owner::
351 * Mode Bits::
352 * Contents::
353 * Directories::
354 * Filesystems::
355 * Combining Primaries With Operators::
356 @end menu
358 @node Name
359 @section Name
361 Here are ways to search for files whose name matches a certain
362 pattern.  @xref{Shell Pattern Matching}, for a description of the
363 @var{pattern} arguments to these tests.
365 Each of these tests has a case-sensitive version and a
366 case-insensitive version, whose name begins with @samp{i}.  In a
367 case-insensitive comparison, the patterns @samp{fo*} and @samp{F??}
368 match the file names @file{Foo}, @samp{FOO}, @samp{foo}, @samp{fOo},
369 etc.
371 @menu
372 * Base Name Patterns::
373 * Full Name Patterns::
374 * Fast Full Name Search::
375 * Shell Pattern Matching::      Wildcards used by these programs.
376 @end menu
378 @node Base Name Patterns
379 @subsection Base Name Patterns
381 @deffn Test -name pattern
382 @deffnx Test -iname pattern
383 True if the base of the file name (the path with the leading
384 directories removed) matches shell pattern @var{pattern}.  For
385 @samp{-iname}, the match is case-insensitive.@footnote{Because we
386 need to perform case-insensitive matching, the GNU fnmatch
387 implementation is always used; if the C library includes the GNU
388 implementation, we use that and otherwise we use the one from gnulib}
389 To ignore a whole directory tree, use @samp{-prune}
390 (@pxref{Directories}).  As an example, to find Texinfo source files in
391 @file{/usr/local/doc}:
393 @example
394 find /usr/local/doc -name '*.texi'
395 @end example
397 Notice that the wildcard must be enclosed in quotes in order to
398 protect it from expansion by the shell.
400 As of findutils version 4.2.2, patterns for @samp{-name} and
401 @samp{-iname} will match a file name with a leading @samp{.}.  For
402 example the command @samp{find /tmp -name \*bar} will match the file
403 @file{/tmp/.foobar}.  Braces within the pattern (@samp{@{@}}) are not
404 considered to be special (that is, @code{find . -name 'foo@{1,2@}'}
405 matches a file named @file{foo@{1,2@}}, not the files @file{foo1} and
406 @file{foo2}.
407 @end deffn
410 @node Full Name Patterns
411 @subsection Full Name Patterns
413 @deffn Test -path pattern
414 @deffnx Test -wholename pattern
415 True if the entire file name, starting with the command line argument
416 under which the file was found, matches shell pattern @var{pattern}.
417 To ignore a whole directory tree, use @samp{-prune} rather than
418 checking every file in the tree (@pxref{Directories}).  The ``entire
419 file name'' as used by @code{find} starts with the starting-point
420 specified on the command line, and is not converted to an absolute
421 pathname, so for example @code{cd /; find tmp -wholename /tmp} will
422 never match anything.   The name @samp{-wholename} is GNU-specific,
423 but @samp{-path} is more portable; it is supported by HP-UX
424 @code{find} and will soon be part of POSIX.
425 @end deffn
427 @deffn Test -ipath pattern
428 @deffnx Test -iwholename pattern
429 These tests are like @samp{-wholename} and @samp{-path}, but the match
430 is case-insensitive.
431 @end deffn
434 In the context of the tests @samp{-path}, @samp{-wholename},
435 @samp{-ipath} and @samp{-wholename}, a ``full path'' is the name of
436 all the directories traversed from @code{find}'s start point to the
437 file being tested, followed by the base name of the file itself.
438 These paths are often not absolute paths; for example
440 @example
441 $ cd /tmp
442 $ mkdir -p foo/bar/baz
443 $ find foo -path foo/bar -print
444 foo/bar
445 $ find foo -path /tmp/foo/bar -print
446 $ find /tmp/foo -path /tmp/foo/bar -print
447 /tmp/foo/bar
448 @end example
450 Notice that the second @code{find} command prints nothing, even though
451 @file{/tmp/foo/bar} exists and was examined by @code{find}.
455 @deffn Test -regex expr
456 @deffnx Test -iregex expr
457 True if the entire file name matches regular expression @var{expr}.
458 This is a match on the whole path, not a search.  For example, to
459 match a file named @file{./fubar3}, you can use the regular expression
460 @samp{.*bar.} or @samp{.*b.*3}, but not @samp{f.*r3}.  @xref{Regexps,
461 , Syntax of Regular Expressions, emacs, The GNU Emacs Manual}, for a
462 description of the syntax of regular expressions.  For @samp{-iregex},
463 the match is case-insensitive.  There are several varieties of regular
464 expressions; by default this test uses POSIX basic regular
465 expressions, but this can be changed with the option
466 @samp{-regextype}.
467 @end deffn
469 @deffn Option -regextype name
470 This option controls the variety of regular expression syntax
471 understood by the @samp{-regex} and @samp{-iregex} tests.  This option
472 is positional; that is, it only affects regular expressions which
473 occur later in the command line.  If this option is not given, GNU
474 Emacs regular expressions are assumed.  Currently-implemented types
478 @table @samp
479 @item emacs
480 Regular expressions compatible with GNU Emacs; this is also the
481 default behaviour if this option is not used.
482 @item posix-awk
483 Regular expressions compatible with the POSIX awk command (not GNU awk)
484 @item posix-basic
485 POSIX Basic Regular Expressions.
486 @item posix-egrep
487 Regular expressions compatible with the POSIX egrep command
488 @item posix-extended
489 POSIX Extended Regular Expressions
490 @end table
492 @ref{Regular Expressions} for more information on the regular
493 expression dialects understood by GNU findutils.
496 @end deffn
498 @node Fast Full Name Search
499 @subsection Fast Full Name Search
501 To search for files by name without having to actually scan the
502 directories on the disk (which can be slow), you can use the
503 @code{locate} program.  For each shell pattern you give it,
504 @code{locate} searches one or more databases of file names and
505 displays the file names that contain the pattern.  @xref{Shell Pattern
506 Matching}, for details about shell patterns.
508 If a pattern is a plain string---it contains no
509 metacharacters---@code{locate} displays all file names in the database
510 that contain that string.  If a pattern contains
511 metacharacters, @code{locate} only displays file names that match the
512 pattern exactly.  As a result, patterns that contain metacharacters
513 should usually begin with a @samp{*}, and will most often end with one
514 as well.  The exceptions are patterns that are intended to explicitly
515 match the beginning or end of a file name.
517 If you only want @code{locate} to match against the last component of
518 the file names (the ``base name'' of the files) you can use the
519 @samp{--basename} option.  The opposite behaviour is the default, but
520 can be selected explicitly by using the option @samp{--wholename}.
522 The command
523 @example
524 locate @var{pattern}
525 @end example
527 is almost equivalent to
528 @example
529 find @var{directories} -name @var{pattern}
530 @end example
532 where @var{directories} are the directories for which the file name
533 databases contain information.  The differences are that the
534 @code{locate} information might be out of date, and that @code{locate}
535 handles wildcards in the pattern slightly differently than @code{find}
536 (@pxref{Shell Pattern Matching}).
538 The file name databases contain lists of files that were on the system
539 when the databases were last updated.  The system administrator can
540 choose the file name of the default database, the frequency with which
541 the databases are updated, and the directories for which they contain
542 entries.
544 Here is how to select which file name databases @code{locate}
545 searches.  The default is system-dependent.  At the time this document
546 was generated, the default was @file{@value{LOCATE_DB}}.
548 @table @code
549 @item --database=@var{path}
550 @itemx -d @var{path}
551 Instead of searching the default file name database, search the file
552 name databases in @var{path}, which is a colon-separated list of
553 database file names.  You can also use the environment variable
554 @code{LOCATE_PATH} to set the list of database files to search.  The
555 option overrides the environment variable if both are used.
556 @end table
558 GNU @code{locate} can read file name databases generated by the
559 @code{slocate} package.  However, these generally contain a list of
560 all the files on the system, and so when using this database,
561 @code{locate} will produce output only for files which are accessible
562 to you.  @xref{Invoking locate}, for a description of the
563 @samp{--existing} option which is used to do this.
565 The @code{updatedb} program can also generate database in a format
566 compatible with @code{slocate}.  @xref{Invoking updatedb}, for a
567 description of its @samp{--dbformat} and @samp{--output} options.
570 @node Shell Pattern Matching
571 @subsection Shell Pattern Matching
573 @code{find} and @code{locate} can compare file names, or parts of file
574 names, to shell patterns.  A @dfn{shell pattern} is a string that may
575 contain the following special characters, which are known as
576 @dfn{wildcards} or @dfn{metacharacters}.
578 You must quote patterns that contain metacharacters to prevent the
579 shell from expanding them itself.  Double and single quotes both work;
580 so does escaping with a backslash.
582 @table @code
583 @item *
584 Matches any zero or more characters.
586 @item ?
587 Matches any one character.
589 @item [@var{string}]
590 Matches exactly one character that is a member of the string
591 @var{string}.  This is called a @dfn{character class}.  As a
592 shorthand, @var{string} may contain ranges, which consist of two
593 characters with a dash between them.  For example, the class
594 @samp{[a-z0-9_]} matches a lowercase letter, a number, or an
595 underscore.  You can negate a class by placing a @samp{!} or @samp{^}
596 immediately after the opening bracket.  Thus, @samp{[^A-Z@@]} matches
597 any character except an uppercase letter or an at sign.
599 @item \
600 Removes the special meaning of the character that follows it.  This
601 works even in character classes.
602 @end table
604 In the @code{find} tests that do shell pattern matching (@samp{-name},
605 @samp{-wholename}, etc.), wildcards in the pattern will match a
606 @samp{.}  at the beginning of a file name.  This is also the case for
607 @code{locate}.  Thus, @samp{find -name '*macs'} will match a file
608 named @file{.emacs}, as will @samp{locate '*macs'}.
610 Slash characters have no special significance in the shell pattern
611 matching that @code{find} and @code{locate} do, unlike in the shell,
612 in which wildcards do not match them.  Therefore, a pattern
613 @samp{foo*bar} can match a file name @samp{foo3/bar}, and a pattern
614 @samp{./sr*sc} can match a file name @samp{./src/misc}.
616 If you want to locate some files with the @samp{locate} command but
617 don't need to see the full list you can use the @samp{--limit} option
618 to see just a small number of results, or the @samp{--count} option to
619 display only the total number of matches.
621 @node Links
622 @section Links
624 There are two ways that files can be linked together.  @dfn{Symbolic
625 links} are a special type of file whose contents are a portion of the
626 name of another file.  @dfn{Hard links} are multiple directory entries
627 for one file; the file names all have the same index node
628 (@dfn{inode}) number on the disk.
630 @menu
631 * Symbolic Links::
632 * Hard Links::
633 @end menu
635 @node Symbolic Links
636 @subsection Symbolic Links
638 Symbolic links are names that reference other files.  GNU @code{find}
639 will handle symbolic links in one of two ways; firstly, it can
640 dereference the links for you - this means that if it comes across a
641 symbolic link, it examines the file that the link points to, in order
642 to see if it matches the criteria you have specified.  Secondly, it
643 can check the link itself in case you might be looking for the actual
644 link.  If the file that the symbolic link points to is also within the
645 directory hierarchy you are searching with the @code{find} command,
646 you may not see a great deal of difference between these two
647 alternatives.
649 By default, @code{find} examines symbolic links themselves when it
650 finds them (and, if it later comes across the linked-to file, it will
651 examine that, too).  If you would prefer @code{find} to dereference
652 the links and examine the file that each link points to, specify the
653 @samp{-L} option to @code{find}.  You can explicitly specify the
654 default behaviour by using the @samp{-P} option.  The @samp{-H}
655 option is a half-way-between option which ensures that any symbolic
656 links listed on the command line are dereferenced, but other symbolic
657 links are not.
659 Symbolic links are different to ``hard links'' in the sense that you
660 need permission to search the directories
661 in the linked-to file name to
662 dereference the link.  This can mean that even if you specify the
663 @samp{-L} option, @code{find} may not be able to determine the
664 properties of the file that the link points to (because you don't have
665 sufficient permission).  In this situation, @code{find} uses the
666 properties of the link itself.  This also occurs if a symbolic link
667 exists but points to a file that is missing.
669 The options controlling the behaviour of @code{find} with respect to
670 links are as follows :-
672 @table @samp
673 @item -P
674 @code{find} does not dereference symbolic links at all.  This is the
675 default behaviour.  This option must be specified before any of the
676 file names on the command line.
677 @item -H
678 @code{find} does not dereference symbolic links (except in the case of
679 file names on the command line, which are dereferenced).  If a
680 symbolic link cannot be dereferenced, the information for the symbolic
681 link itself is used.  This option must be specified before any of the
682 file names on the command line.
683 @item -L
684 @code{find} dereferences symbolic links where possible, and where this
685 is not possible it uses the properties of the symbolic link itself.
686 This option must be specified before any of the file names on the
687 command line.  Use of this option also implies the same behaviour as
688 the @samp{-noleaf} option.  If you later use the @samp{-H} or
689 @samp{-P} options, this does not turn off @samp{-noleaf}.
691 @item -follow
692 This option forms part of the ``expression'' and must be specified
693 after the file names, but it is otherwise equivalent to @samp{-L}.
694 The @samp{-follow} option affects only those tests which appear after
695 it on the command line.  This option is deprecated.  Where possible,
696 you should use @samp{-L} instead.
697 @end table
699 The following differences in behavior occur when the @samp{-L} option
700 is used:
702 @itemize @bullet
703 @item
704 @code{find} follows symbolic links to directories when searching
705 directory trees.
706 @item
707 @samp{-lname} and @samp{-ilname} always return false (unless they
708 happen to match broken symbolic links).
709 @item
710 @samp{-type} reports the types of the files that symbolic links point
711 to.  This means that in combination with @samp{-L}, @samp{-type l}
712 will be true only for broken symbolic links.  To check for symbolic
713 links when @samp{-L} has been specified, use @samp{-xtype l}.
714 @item
715 Implies @samp{-noleaf} (@pxref{Directories}).
716 @end itemize
718 If the @samp{-L} option or the @samp{-H} option is used,
719 the file names used as arguments to @samp{-newer}, @samp{-anewer}, and
720 @samp{-cnewer} are dereferenced and the timestamp from the pointed-to
721 file is used instead (if possible -- otherwise the timestamp from the
722 symbolic link is used).
724 @deffn Test -lname pattern
725 @deffnx Test -ilname pattern
726 True if the file is a symbolic link whose contents match shell pattern
727 @var{pattern}.  For @samp{-ilname}, the match is case-insensitive.
728 @xref{Shell Pattern Matching}, for details about the @var{pattern}
729 argument.  If the @samp{-L} option is in effect, this test will always
730 return false for symbolic links unless they are broken.  So, to list
731 any symbolic links to @file{sysdep.c} in the current directory and its
732 subdirectories, you can do:
734 @example
735 find . -lname '*sysdep.c'
736 @end example
737 @end deffn
739 @node Hard Links
740 @subsection Hard Links
742 Hard links allow more than one name to refer to the same file.  To
743 find all the names which refer to the same file as NAME, use
744 @samp{-samefile NAME}.  If you are not using the @samp{-L} option, you
745 can confine your search to one filesystem using the @samp{-xdev}
746 option.  This is useful because hard links cannot point outside a
747 single filesystem, so this can cut down on needless searching.
749 If the @samp{-L} option is in effect, and NAME is in fact a symbolic
750 link, the symbolic link will be dereferenced.  Hence you are searching
751 for other links (hard or symbolic) to the file pointed to by NAME.  If
752 @samp{-L} is in effect but NAME is not itself a symbolic link, other
753 symbolic links to the file NAME will be matched.
755 You can also search for files by inode number.  This can occasionally
756 be useful in diagnosing problems with filesystems for example, because
757 @code{fsck} tends to print inode numbers.  Inode numbers also
758 occasionally turn up in log messages for some types of software, and
759 are used to support the @code{ftok()} library function.
761 You can learn a file's inode number and the number of links to it by
762 running @samp{ls -li} or @samp{find -ls}.
764 You can search for hard links to inode number NUM by using @samp{-inum
765 NUM}. If there are any filesystem mount points below the directory
766 where you are starting the search, use the @samp{-xdev} option unless
767 you are also using the @samp{-L} option.  Using @samp{-xdev} this
768 saves needless searching, since hard links to a file must be on the
769 same filesystem.  @xref{Filesystems}.
771 @deffn Test -samefile NAME
772 File is a hard link to the same inode as NAME.  If the @samp{-L}
773 option is in effect, symbolic links to the same file as NAME points to
774 are also matched.
775 @end deffn
777 @deffn Test -inum n
778 File has inode number @var{n}.  The @samp{+} and @samp{-} qualifiers
779 also work, though these are rarely useful.  Much of the time it is
780 easier to use @samp{-samefile} rather than this option.
781 @end deffn
783 You can also search for files that have a certain number of links,
784 with @samp{-links}.  Directories normally have at least two hard
785 links; their @file{.} entry is the second one.  If they have
786 subdirectories, each of those also has a hard link called @file{..} to
787 its parent directory.  The @file{.} and @file{..} directory entries
788 are not normally searched unless they are mentioned on the @code{find}
789 command line.
791 @deffn Test -links n
792 File has @var{n} hard links.
793 @end deffn
795 @deffn Test -links +n
796 File has more than @var{n} hard links.
797 @end deffn
799 @deffn Test -links -n
800 File has fewer than @var{n} hard links.
801 @end deffn
803 @node Time
804 @section Time
806 Each file has three time stamps, which record the last time that
807 certain operations were performed on the file:
809 @enumerate
810 @item
811 access (read the file's contents)
812 @item
813 change the status (modify the file or its attributes)
814 @item
815 modify (change the file's contents)
816 @end enumerate
818 Some systems also provide a timestamp that indicates when a file was
819 @emph{created}.   For example, the UFS2 fileystem under NetBSD-3.1
820 records the @emph{birth time} of each file.  This information is also
821 available under other versions of BSD and some versions of Cygwin.
822 However, even on systems which support file birth time, files may
823 exist for which this information was not recorded (for example, UFS1
824 file systems simply do not contain this information).
826 You can search for files whose time stamps are within a certain age
827 range, or compare them to other time stamps.
829 @menu
830 * Age Ranges::
831 * Comparing Timestamps::
832 @end menu
834 @node Age Ranges
835 @subsection Age Ranges
837 These tests are mainly useful with ranges (@samp{+@var{n}} and
838 @samp{-@var{n}}).
840 @deffn Test -atime n
841 @deffnx Test -ctime n
842 @deffnx Test -mtime n
843 True if the file was last accessed (or its status changed, or it was
844 modified) @var{n}*24 hours ago.  The number of 24-hour periods since
845 the file's timestamp is always rounded down; therefore 0 means ``less
846 than 24 hours ago'', 1 means ``between 24 and 48 hours ago'', and so
847 forth.  Fractional values are supported but this only really makes
848 sense for the case where ranges (@samp{+@var{n}} and @samp{-@var{n}})
849 are used.
850 @end deffn
852 @deffn Test -amin n
853 @deffnx Test -cmin n
854 @deffnx Test -mmin n
855 True if the file was last accessed (or its status changed, or it was
856 modified) @var{n} minutes ago.  These tests provide finer granularity
857 of measurement than @samp{-atime} et al., but rounding is done in a
858 similar way (again, fractions are supported).  For example, to list
859 files in @file{/u/bill} that were last read from 2 to 6 minutes ago:
861 @example
862 find /u/bill -amin +2 -amin -6
863 @end example
864 @end deffn
866 @deffn Option -daystart
867 Measure times from the beginning of today rather than from 24 hours
868 ago.  So, to list the regular files in your home directory that were
869 modified yesterday, do
871 @example
872 find ~/ -daystart -type f -mtime 1
873 @end example
875 The @samp{-daystart} option is unlike most other options in that it
876 has an effect on the way that other tests are performed.  The affected
877 tests are @samp{-amin}, @samp{-cmin}, @samp{-mmin}, @samp{-atime},
878 @samp{-ctime} and @samp{-mtime}.  The @samp{-daystart} option only
879 affects the behaviour of any tests which appear after it on the
880 command line.
881 @end deffn
883 @node Comparing Timestamps
884 @subsection Comparing Timestamps
886 @deffn Test -newerXY reference
887 Succeeds if timestamp @samp{X} of the file being considered is newer
888 than timestamp @samp{Y} of the file @file{reference}.   The latters
889 @samp{X} and @samp{Y} can be any of the following letters:
891 @table @samp
892 @item a
893 Last-access time of @file{reference}
894 @item B
895 Birth time of @file{reference} (when this is not known, the test cannot succeed)
896 @item c
897 Last-change time of @file{reference}
898 @item m
899 Last-modification time of @file{reference}
900 @item t
901 The @file{reference} argument is interpreted as a literal time, rather
902 than the name of a file.  @xref{Date input formats}, for a description
903 of how the timestamp is understood.  Tests of the form @samp{-newerXt}
904 are valid but tests of the form @samp{-newertY} are not.
905 @end table
907 For example the test @code{-newerac /tmp/foo} succeeds for all files
908 which have been accessed more recently than @file{/tmp/foo} was
909 changed.   Here @samp{X} is @samp{a} and @samp{Y} is @samp{c}.   
911 Not all files have a known birth time.  If @samp{Y} is @samp{b} and
912 the birth time of @file{reference} is not available, @code{find} exits
913 with an explanatory error message.  If @samp{X} is @samp{b} and we do
914 not know the birth time the file currently being considered, the test
915 simply fails (that is, it behaves like @code{-false} does).
917 Some operating systems (for example, most implementations of Unix) do
918 not support file birth times.  Some others, for example NetBSD-3.1,
919 do.  Even on operating systems which support file birth times, the
920 information may not be available for specific files.  For example,
921 under NetBSD, file birth times are supported on UFS2 file systems, but
922 not UFS1 file systems.
924 @end deffn
928 There are two ways to list files in @file{/usr} modified after
929 February 1 of the current year.  One uses @samp{-newermt}:
931 @example
932 find /usr -newermt "Feb 1"
933 @end example
935 The other way of doing this works on the versions of find before 4.3.3:
937 @c Idea from Rick Sladkey.
938 @example
939 touch -t 02010000 /tmp/stamp$$
940 find /usr -newer /tmp/stamp$$
941 rm -f /tmp/stamp$$
942 @end example
944 @deffn Test -anewer file
945 @deffnx Test -cnewer file
946 @deffnx Test -newer file
947 True if the file was last accessed (or its status changed, or it was
948 modified) more recently than @var{file} was modified.  These tests are
949 affected by @samp{-follow} only if @samp{-follow} comes before them on
950 the command line.  @xref{Symbolic Links}, for more information on
951 @samp{-follow}.  As an example, to list any files modified since
952 @file{/bin/sh} was last modified:
954 @example
955 find . -newer /bin/sh
956 @end example
957 @end deffn
959 @deffn Test -used n
960 True if the file was last accessed @var{n} days after its status was
961 last changed.  Useful for finding files that are not being used, and
962 could perhaps be archived or removed to save disk space.
963 @end deffn
965 @node Size
966 @section Size
968 @deffn Test -size n@r{[}bckwMG@r{]}
969 True if the file uses @var{n} units of space, rounding up.  The units
970 are 512-byte blocks by default, but they can be changed by adding a
971 one-character suffix to @var{n}:
973 @table @code
974 @item b
975 512-byte blocks (never 1024)
976 @item c
977 bytes
978 @item k
979 kilobytes (1024 bytes)
980 @item w
981 2-byte words
982 @item M
983 Megabytes (units of 1048576 bytes)
984 @item G
985 Gigabytes (units of 1073741824 bytes)
986 @end table
988 The `b' suffix always considers blocks to be 512 bytes.  This is not
989 affected by the setting (or non-setting) of the POSIXLY_CORRECT
990 environment variable.  This behaviour is different to the behaviour of
991 the @samp{-ls} action).  If you want to use 1024-byte units, use the
992 `k' suffix instead.
994 The number can be prefixed with a `+' or a `-'.  A plus sign indicates
995 that the test should succeed if the file uses at least @var{n} units
996 of storage (a common use of this test) and a minus sign
997 indicates that the test should succeed if the file uses less than
998 @var{n} units of storage.  There is no `=' prefix, because that's the
999 default anyway.
1001 The size does not count indirect blocks, but it does count blocks in
1002 sparse files that are not actually allocated.  In other words, it's
1003 consistent with the result you get for @samp{ls -l} or @samp{wc -c}.
1004 This handling of sparse files differs from the output of the @samp{%k}
1005 and @samp{%b} format specifiers for the @samp{-printf} predicate.
1007 @end deffn
1009 @deffn Test -empty
1010 True if the file is empty and is either a regular file or a directory.
1011 This might help determine good candidates for deletion.  This test is
1012 useful with @samp{-depth} (@pxref{Directories}) and @samp{-delete}
1013 (@pxref{Single File}).
1014 @end deffn
1016 @node Type
1017 @section Type
1019 @deffn Test -type c
1020 True if the file is of type @var{c}:
1022 @table @code
1023 @item b
1024 block (buffered) special
1025 @item c
1026 character (unbuffered) special
1027 @item d
1028 directory
1029 @item p
1030 named pipe (FIFO)
1031 @item f
1032 regular file
1033 @item l
1034 symbolic link; if @samp{-L} is in effect, this is true only for broken
1035 symbolic links.  If you want to search for symbolic links when
1036 @samp{-L} is in effect, use @samp{-xtype} instead of @samp{-type}.
1037 @item s
1038 socket
1039 @item D
1040 door (Solaris)
1041 @end table
1042 @end deffn
1044 @deffn Test -xtype c
1045 This test behaves the same as @samp{-type} unless the file is a
1046 symbolic link.  If the file is a symbolic link, the result is as
1047 follows (in the table below, @samp{X} should be understood to
1048 represent any letter except @samp{l}):
1050 @table @samp
1051 @item @samp{-P -xtype l}
1052 True if the symbolic link is broken
1053 @item @samp{-P -xtype X}
1054 True if the (ultimate) target file is of type @samp{X}.
1055 @item @samp{-L -xtype l}
1056 Always true
1057 @item @samp{-L -xtype X}
1058 False unless the symbolic link is broken
1059 @end table
1061 In other words, for symbolic links, @samp{-xtype} checks the type of
1062 the file that @samp{-type} does not check.  
1064 The @samp{-H} option also affects the behaviour of @samp{-xtype}.
1065 When @samp{-H} is in effect, @samp{-xtype} behaves as if @samp{-L} had
1066 been specified when examining files listed on the command line, and as
1067 if @samp{-P} had been specified otherwise.  If neither @samp{-H} nor
1068 @samp{-L} was specified, @samp{-xtype} behaves as if @samp{-P} had
1069 been specified.
1071 @xref{Symbolic Links}, for more information on @samp{-follow} and
1072 @samp{-L}.
1073 @end deffn
1075 @node Owner
1076 @section Owner
1078 @deffn Test -user uname
1079 @deffnx Test -group gname
1080 True if the file is owned by user @var{uname} (belongs to group
1081 @var{gname}).  A numeric ID is allowed.
1082 @end deffn
1084 @deffn Test -uid n
1085 @deffnx Test -gid n
1086 True if the file's numeric user ID (group ID) is @var{n}.  These tests
1087 support ranges (@samp{+@var{n}} and @samp{-@var{n}}), unlike
1088 @samp{-user} and @samp{-group}.
1089 @end deffn
1091 @deffn Test -nouser
1092 @deffnx Test -nogroup
1093 True if no user corresponds to the file's numeric user ID (no group
1094 corresponds to the numeric group ID).  These cases usually mean that
1095 the files belonged to users who have since been removed from the
1096 system.  You probably should change the ownership of such files to an
1097 existing user or group, using the @code{chown} or @code{chgrp}
1098 program.
1099 @end deffn
1101 @node Mode Bits
1102 @section File Mode Bits
1104 @xref{File Permissions}, for information on how file mode bits are
1105 structured and how to specify them.
1107 Four tests determine what users can do with files.  These are
1108 @samp{-readable}, @samp{-writable}, @samp{-executable} and
1109 @samp{-perm}.  The first three tests ask the operating system if the
1110 current user can perform the relevant operation on a file, while
1111 @samp{-perm} just examines the file's mode.  The file mode may give
1112 a misleading impression of what the user can actually do, because the
1113 file may have an access control list, or exist on a read-only
1114 filesystem, for example.  Of these four tests though, only
1115 @samp{-perm} is specified by the POSIX standard.
1117 The @samp{-readable}, @samp{-writable} and @samp{-executable} tests
1118 are implemented via the @code{access} system call.  This is
1119 implemented within the operating system itself.  If the file being
1120 considered is on an NFS filesystem, the remote system may allow or
1121 forbid read or write operations for reasons of which the NFS client
1122 cannot take account.  This includes user-ID mapping, either in the
1123 general sense or the more restricted sense in which remote superusers
1124 are treated by the NFS server as if they are the local user
1125 @samp{nobody} on the NFS server.
1127 None of the tests in this section should be used to verify that a user
1128 is authorised to perform any operation (on the file being tested or
1129 any other file) because of the possibility of a race condition.  That
1130 is, the situation may change between the test and an action being
1131 taken on the basis of the result of that test.
1134 @deffn Test -readable 
1135 True if the file can be read by the invoking user.
1136 @end deffn
1138 @deffn Test -writable
1139 True if the file can be written by the invoking user.  This is an
1140 in-principle check, and other things may prevent a successful write
1141 operation; for example, the filesystem might be full.
1142 @end deffn
1144 @deffn Test -executable
1145 True if the file can be executed/searched by the invoking user.
1146 @end deffn
1148 @deffn Test -perm pmode
1150 True if the file's mode bits match @var{pmode}, which can be
1151 either a symbolic or numeric @var{mode} (@pxref{File Permissions})
1152 optionally prefixed by @samp{-} or @samp{/}.
1154 A @var{pmode} that starts with neither @samp{-} nor @samp{/} matches
1155 if @var{mode} exactly matches the file mode bits.  
1157 A @var{pmode} that starts with @samp{+} but which is not valid (for
1158 example @samp{+a+x}) is an error if the POSIXLY_CORRECT environment
1159 variable it set.  Otherwise this is treated as if the initial
1160 @samp{+} were a @samp{/}, for backward compatibility.
1162 A @var{pmode} that starts with @samp{-} matches if
1163 @emph{all} the file mode bits set in @var{mode} are set for the file;
1164 bits not set in @var{mode} are ignored.
1166 A @var{pmode} that starts with @samp{/} matches if
1167 @emph{any} of the file mode bits set in @var{mode} are set for the file;
1168 bits not set in @var{mode} are ignored.
1169 This is a GNU extension.
1171 If you don't use the @samp{/} or @samp{-} form with a symbolic mode
1172 string, you may have to specify a rather complex mode string.  For
1173 example @samp{-perm g=w} will only match files that have mode 0020
1174 (that is, ones for which group write permission is the only file mode bit
1175 set).  It is more likely that you will want to use the @samp{/} or
1176 @samp{-} forms, for example @samp{-perm -g=w}, which matches any file
1177 with group write permission.
1180 @table @samp
1181 @item -perm 664
1182 Match files that have read and write permission for their owner,
1183 and group, but that the rest of the world can read but not write to.
1184 Do not match files that meet these criteria but have other file mode
1185 bits set (for example if someone can execute/search the file).
1187 @item -perm -664
1188 Match files that have read and write permission for their owner,
1189 and group, but that the rest of the world can read but not write to,
1190 without regard to the presence of any extra file mode bits (for
1191 example the executable bit).  This matches a file with mode
1192 0777, for example.
1194 @item -perm /222
1195 Match files that are writable by somebody (their owner, or
1196 their group, or anybody else).
1198 @item -perm /022
1199 Match files that are writable by either their owner or their
1200 group.  The files don't have to be writable by both the owner and
1201 group to be matched; either will do.
1203 @item -perm /g+w,o+w
1204 As above.
1206 @item -perm /g=w,o=w
1207 As above.
1209 @item -perm -022
1210 Match files that are writable by both their owner and their
1211 group.
1213 @item -perm -444 -perm /222 ! -perm /111
1214 Match files that are readable for everybody, have at least one
1215 write bit set (i.e., somebody can write to them), but that cannot be
1216 executed/searched by anybody.  Note that in some shells the @samp{!} must be
1217 escaped;.
1219 @item -perm -a+r -perm /a+w ! -perm /a+x
1220 As above.
1223 @item -perm -g+w,o+w
1224 As above.
1225 @end table
1227 @quotation Warning
1228 If you specify @samp{-perm /000} or @samp{-perm /mode} where the
1229 symbolic mode @samp{mode} has no bits set, the test matches all files.
1230 Versions of GNU @code{find} prior to 4.3.3 matched no files in this
1231 situation.
1232 @end quotation
1234 @end deffn
1236 @node Contents
1237 @section Contents
1239 To search for files based on their contents, you can use the
1240 @code{grep} program.  For example, to find out which C source files in
1241 the current directory contain the string @samp{thing}, you can do:
1243 @example
1244 grep -l thing *.[ch]
1245 @end example
1247 If you also want to search for the string in files in subdirectories,
1248 you can combine @code{grep} with @code{find} and @code{xargs}, like
1249 this:
1251 @example
1252 find . -name '*.[ch]' | xargs grep -l thing
1253 @end example
1255 The @samp{-l} option causes @code{grep} to print only the names of
1256 files that contain the string, rather than the lines that contain it.
1257 The string argument (@samp{thing}) is actually a regular expression,
1258 so it can contain metacharacters.  This method can be refined a little
1259 by using the @samp{-r} option to make @code{xargs} not run @code{grep}
1260 if @code{find} produces no output, and using the @code{find} action
1261 @samp{-print0} and the @code{xargs} option @samp{-0} to avoid
1262 misinterpreting files whose names contain spaces:
1264 @example
1265 find . -name '*.[ch]' -print0 | xargs -r -0 grep -l thing
1266 @end example
1268 For a fuller treatment of finding files whose contents match a
1269 pattern, see the manual page for @code{grep}.
1271 @node Directories
1272 @section Directories
1274 Here is how to control which directories @code{find} searches, and how
1275 it searches them.  These two options allow you to process a horizontal
1276 slice of a directory tree.
1278 @deffn Option -maxdepth levels
1279 Descend at most @var{levels} (a non-negative integer) levels of
1280 directories below the command line arguments.  @samp{-maxdepth 0}
1281 means only apply the tests and actions to the command line arguments.
1282 @end deffn
1284 @deffn Option -mindepth levels
1285 Do not apply any tests or actions at levels less than @var{levels} (a
1286 non-negative integer).  @samp{-mindepth 1} means process all files
1287 except the command line arguments.
1288 @end deffn
1290 @deffn Option -depth
1291 Process each directory's contents before the directory itself.  Doing
1292 this is a good idea when producing lists of files to archive with
1293 @code{cpio} or @code{tar}.  If a directory does not have write
1294 permission for its owner, its contents can still be restored from the
1295 archive since the directory's permissions are restored after its
1296 contents.
1297 @end deffn
1299 @deffn Option -d
1300 This is a deprecated synonym for @samp{-depth}, for compatibility with
1301 Mac OS X, FreeBSD and OpenBSD.  The @samp{-depth} option is a POSIX
1302 feature, so it is better to use that.
1303 @end deffn
1305 @deffn Action -prune
1306 If the file is a directory, do not descend into it.  The result is
1307 true.  For example, to skip the directory @file{src/emacs} and all
1308 files and directories under it, and print the names of the other files
1309 found:
1311 @example
1312 find . -wholename './src/emacs' -prune -o -print
1313 @end example
1315 The above command will not print @file{./src/emacs} among its list of
1316 results.  This however is not due to the effect of the @samp{-prune}
1317 action (which only prevents further descent, it doesn't make sure we
1318 ignore that item).  Instead, this effect is due to the use of
1319 @samp{-o}.  Since the left hand side of the ``or'' condition has
1320 succeeded for @file{./src/emacs}, it is not necessary to evaluate the
1321 right-hand-side (@samp{-print}) at all for this particular file.  If
1322 you wanted to print that directory name you could use either an extra
1323 @samp{-print} action:
1325 @example
1326 find . -wholename './src/emacs' -prune -print -o -print
1327 @end example
1329 or use the comma operator:
1331 @example
1332 find . -wholename './src/emacs' -prune , -print
1333 @end example
1335 If the @samp{-depth} option is in effect, the subdirectories will have
1336 already been visited in any case.  Hence @samp{-prune} has no effect
1337 in this case.   
1339 Because @samp{-delete} implies @samp{-depth}, using @samp{-prune} in
1340 combination with @samp{-delete} may well result in the deletion of
1341 more files than you intended.
1342 @end deffn
1345 @deffn Action -quit
1346 Exit immediately (with return value zero if no errors have occurred).
1347 This is different to @samp{-prune} because @samp{-prune} only applies
1348 to the contents of pruned directories, whilt @samp{-quit} simply makes
1349 @code{find} stop immediately.  No child processes will be left
1350 running, but no more files specified on the command line will be
1351 processed.  For example, @code{find /tmp/foo /tmp/bar -print -quit}
1352 will print only @samp{/tmp/foo}.  Any command lines which have been
1353 built by @samp{-exec ... \+} or @samp{-execdir ... \+} are invoked
1354 before the program is exited.
1355 @end deffn
1357 @deffn Option -noleaf
1358 Do not optimize by assuming that directories contain 2 fewer
1359 subdirectories than their hard link count.  This option is needed when
1360 searching filesystems that do not follow the Unix directory-link
1361 convention, such as CD-ROM or MS-DOS filesystems or AFS volume mount
1362 points.  Each directory on a normal Unix filesystem has at least 2
1363 hard links: its name and its @file{.}  entry.  Additionally, its
1364 subdirectories (if any) each have a @file{..}  entry linked to that
1365 directory.  When @code{find} is examining a directory, after it has
1366 statted 2 fewer subdirectories than the directory's link count, it
1367 knows that the rest of the entries in the directory are
1368 non-directories (@dfn{leaf} files in the directory tree).  If only the
1369 files' names need to be examined, there is no need to stat them; this
1370 gives a significant increase in search speed.
1371 @end deffn
1373 @deffn Option -ignore_readdir_race
1374 If a file disappears after its name has been read from a directory but
1375 before @code{find} gets around to examining the file with @code{stat},
1376 don't issue an error message.  If you don't specify this option, an
1377 error message will be issued.  This option can be useful in system
1378 scripts (cron scripts, for example) that examine areas of the
1379 filesystem that change frequently (mail queues, temporary directories,
1380 and so forth), because this scenario is common for those sorts of
1381 directories.  Completely silencing error messages from @code{find} is
1382 undesirable, so this option neatly solves the problem.  There is no
1383 way to search one part of the filesystem with this option on and part
1384 of it with this option off, though.  When this option is turned on and 
1385 find discovers that one of the start-point files specified on the
1386 command line does not exist, no error message will be issued.
1388 @end deffn
1390 @deffn Option -noignore_readdir_race
1391 This option reverses the effect of the @samp{-ignore_readdir_race}
1392 option.
1393 @end deffn
1396 @node Filesystems
1397 @section Filesystems
1399 A @dfn{filesystem} is a section of a disk, either on the local host or
1400 mounted from a remote host over a network.  Searching network
1401 filesystems can be slow, so it is common to make @code{find} avoid
1402 them.
1404 There are two ways to avoid searching certain filesystems.  One way is
1405 to tell @code{find} to only search one filesystem:
1407 @deffn Option -xdev
1408 @deffnx Option -mount
1409 Don't descend directories on other filesystems.  These options are
1410 synonyms.
1411 @end deffn
1413 The other way is to check the type of filesystem each file is on, and
1414 not descend directories that are on undesirable filesystem types:
1416 @deffn Test -fstype type
1417 True if the file is on a filesystem of type @var{type}.  The valid
1418 filesystem types vary among different versions of Unix; an incomplete
1419 list of filesystem types that are accepted on some version of Unix or
1420 another is:
1421 @example
1422 ext2 ext3 proc sysfs ufs 4.2 4.3 nfs tmp mfs S51K S52K
1423 @end example
1424 You can use @samp{-printf} with the @samp{%F} directive to see the
1425 types of your filesystems.  The @samp{%D} directive shows the device
1426 number.  @xref{Print File Information}.  @samp{-fstype} is usually
1427 used with @samp{-prune} to avoid searching remote filesystems
1428 (@pxref{Directories}).
1429 @end deffn
1431 @node Combining Primaries With Operators
1432 @section Combining Primaries With Operators
1434 Operators build a complex expression from tests and actions.
1435 The operators are, in order of decreasing precedence:
1437 @table @code
1438 @item @asis{( @var{expr} )}
1439 @findex ()
1440 Force precedence.  True if @var{expr} is true.
1442 @item @asis{! @var{expr}}
1443 @itemx @asis{-not @var{expr}}
1444 @findex !
1445 @findex -not
1446 True if @var{expr} is false.  In some shells, it is necessary to
1447 protect the @samp{!} from shell interpretation by quoting it.
1449 @item @asis{@var{expr1 expr2}}
1450 @itemx @asis{@var{expr1} -a @var{expr2}}
1451 @itemx @asis{@var{expr1} -and @var{expr2}}
1452 @findex -a
1453 @findex -and
1454 And; @var{expr2} is not evaluated if @var{expr1} is false.
1456 @item @asis{@var{expr1} -o @var{expr2}}
1457 @itemx @asis{@var{expr1} -or @var{expr2}}
1458 @findex -o
1459 @findex -or
1460 Or; @var{expr2} is not evaluated if @var{expr1} is true.
1462 @item @asis{@var{expr1} , @var{expr2}}
1463 @findex ,
1464 List; both @var{expr1} and @var{expr2} are always evaluated.  True if
1465 @var{expr2} is true.  The value of @var{expr1} is discarded.  This
1466 operator lets you do multiple independent operations on one traversal,
1467 without depending on whether other operations succeeded.  The two
1468 operations @var{expr1} and @var{expr2} are not always fully
1469 independent, since @var{expr1} might have side effects like touching
1470 or deleting files, or it might use @samp{-prune} which would also
1471 affect @var{expr2}.
1472 @end table
1474 @code{find} searches the directory tree rooted at each file name by
1475 evaluating the expression from left to right, according to the rules
1476 of precedence, until the outcome is known (the left hand side is false
1477 for @samp{-and}, true for @samp{-or}), at which point @code{find}
1478 moves on to the next file name.
1480 There are two other tests that can be useful in complex expressions:
1482 @deffn Test -true
1483 Always true.
1484 @end deffn
1486 @deffn Test -false
1487 Always false.
1488 @end deffn
1490 @node Actions
1491 @chapter Actions
1493 There are several ways you can print information about the files that
1494 match the criteria you gave in the @code{find} expression.  You can
1495 print the information either to the standard output or to a file that
1496 you name.  You can also execute commands that have the file names as
1497 arguments.  You can use those commands as further filters to select
1498 files.
1500 @menu
1501 * Print File Name::
1502 * Print File Information::
1503 * Run Commands::
1504 * Delete Files::
1505 * Adding Tests::
1506 @end menu
1508 @node Print File Name
1509 @section Print File Name
1511 @deffn Action -print
1512 True; print the entire file name on the standard output, followed by a
1513 newline.  If there is the faintest possibility that one of the files
1514 for which you are searching might contain a newline, you should use
1515 @samp{-print0} instead.
1516 @end deffn
1518 @deffn Action -fprint file
1519 True; print the entire file name into file @var{file}, followed by a
1520 newline.  If @var{file} does not exist when @code{find} is run, it is
1521 created; if it does exist, it is truncated to 0 bytes.  The named
1522 output file is always created, even if no output is sent to it.  The
1523 file names @file{/dev/stdout} and @file{/dev/stderr} are handled
1524 specially; they refer to the standard output and standard error
1525 output, respectively.
1527 If there is the faintest possibility that one of the files for which
1528 you are searching might contain a newline, you should use
1529 @samp{-fprint0} instead.
1530 @end deffn
1533 @c @deffn Option -show-control-chars how
1534 @c This option affects how some of @code{find}'s actions treat
1535 @c unprintable characters in file names.  If @samp{how} is
1536 @c @samp{literal}, any subsequent actions (i.e., actions further on in the
1537 @c command line) print file names as-is.  
1538 @c 
1539 @c If this option is not specified, it currently defaults to @samp{safe}.
1540 @c If @samp{how} is @samp{safe}, C-like backslash escapes are used to
1541 @c indicate the non-printable characters for @samp{-ls} and @samp{-fls}.
1542 @c On the other hand, @samp{-print}, @samp{-fprint}, @samp{-fprintf} and
1543 @c @code{-printf} all quote unprintable characters if the data is going
1544 @c to a tty, and otherwise the data is emitted literally.
1545 @c 
1546 @c @table @code
1547 @c @item -ls
1548 @c Escaped if @samp{how} is @samp{safe}
1549 @c @item -fls
1550 @c Escaped if @samp{how} is @samp{safe}
1551 @c @item -print
1552 @c Always quoted if stdout is a tty,
1553 @c @samp{-show-control-chars} is ignored
1554 @c @item -print0
1555 @c Always literal, never escaped
1556 @c @item -fprint
1557 @c Always quoted if the destination is a tty;
1558 @c @samp{-show-control-chars} is ignored
1559 @c @item -fprint0
1560 @c Always literal, never escaped
1561 @c @item -fprintf
1562 @c If the destination is a tty, the @samp{%f}, 
1563 @c @samp{%F}, @samp{%h}, @samp{%l}, @samp{%p},
1564 @c and @samp{%P} directives produce quoted
1565 @c strings if stdout is a tty and are treated
1566 @c literally otherwise.
1567 @c @item -printf
1568 @c As for @code{-fprintf}.
1569 @c @end table
1570 @c @end deffn
1573 @node Print File Information
1574 @section Print File Information
1576 @deffn Action -ls
1577 True; list the current file in @samp{ls -dils} format on the standard
1578 output.  The output looks like this:
1580 @smallexample
1581 204744   17 -rw-r--r--   1 djm      staff       17337 Nov  2  1992 ./lwall-quotes
1582 @end smallexample
1584 The fields are:
1586 @enumerate
1587 @item
1588 The inode number of the file.  @xref{Hard Links}, for how to find
1589 files based on their inode number.
1591 @item
1592 the number of blocks in the file.  The block counts are of 1K blocks,
1593 unless the environment variable @code{POSIXLY_CORRECT} is set, in
1594 which case 512-byte blocks are used.  @xref{Size}, for how to find
1595 files based on their size.
1597 @item
1598 The file's type and file mode bits.  The type is shown as a dash for a
1599 regular file; for other file types, a letter like for @samp{-type} is
1600 used (@pxref{Type}).  The file mode bits are read, write, and execute/search for
1601 the file's owner, its group, and other users, respectively; a dash
1602 means the permission is not granted.  @xref{File Permissions}, for
1603 more details about file permissions.  @xref{Mode Bits}, for how to
1604 find files based on their file mode bits.
1606 @item
1607 The number of hard links to the file.
1609 @item
1610 The user who owns the file.
1612 @item
1613 The file's group.
1615 @item
1616 The file's size in bytes.
1618 @item
1619 The date the file was last modified.
1621 @item
1622 The file's name.  @samp{-ls} quotes non-printable characters in the
1623 file names using C-like backslash escapes.  This may change soon, as
1624 the treatment of unprintable characters is harmonised for @samp{-ls},
1625 @samp{-fls}, @samp{-print}, @samp{-fprint}, @samp{-printf} and
1626 @samp{-fprintf}.
1627 @end enumerate
1628 @end deffn
1630 @deffn Action -fls file
1631 True; like @samp{-ls} but write to @var{file} like @samp{-fprint}
1632 (@pxref{Print File Name}).  The named output file is always created,
1633 even if no output is sent to it.
1634 @end deffn
1636 @deffn Action -printf format
1637 True; print @var{format} on the standard output, interpreting @samp{\}
1638 escapes and @samp{%} directives.  Field widths and precisions can be
1639 specified as with the @code{printf} C function.  Format flags (like
1640 @samp{#} for example) may not work as you expect because many of the
1641 fields, even numeric ones, are printed with %s.  Numeric flags which
1642 are affected in this way include G, U, b, D, k and n.  This difference
1643 in behaviour means though that the format flag @samp{-} will work; it
1644 forces left-alignment of the field.  Unlike @samp{-print},
1645 @samp{-printf} does not add a newline at the end of the string.  If
1646 you want a newline at the end of the string, add a @samp{\n}.
1647 @end deffn
1649 @deffn Action -fprintf file format
1650 True; like @samp{-printf} but write to @var{file} like @samp{-fprint}
1651 (@pxref{Print File Name}).  The output file is always created, even if
1652 no output is ever sent to it.
1653 @end deffn
1655 @menu
1656 * Escapes::
1657 * Format Directives::
1658 * Time Formats::
1659 @end menu
1661 @node Escapes
1662 @subsection Escapes
1664 The escapes that @samp{-printf} and @samp{-fprintf} recognise are:
1666 @table @code
1667 @item \a
1668 Alarm bell.
1669 @item \b
1670 Backspace.
1671 @item \c
1672 Stop printing from this format immediately and flush the output.
1673 @item \f
1674 Form feed.
1675 @item \n
1676 Newline.
1677 @item \r
1678 Carriage return.
1679 @item \t
1680 Horizontal tab.
1681 @item \v
1682 Vertical tab.
1683 @item \\
1684 A literal backslash (@samp{\}).
1685 @item \0
1686 ASCII NUL.
1687 @item \NNN
1688 The character whose ASCII code is NNN (octal).
1689 @end table
1691 A @samp{\} character followed by any other character is treated as an
1692 ordinary character, so they both are printed, and a warning message is
1693 printed to the standard error output (because it was probably a typo).
1695 @node Format Directives
1696 @subsection Format Directives
1698 @samp{-printf} and @samp{-fprintf} support the following format
1699 directives to print information about the file being processed.  The C
1700 @code{printf} function, field width and precision specifiers are
1701 supported, as applied to string (%s) types. That is, you can specify
1702 "minimum field width"."maximum field width" for each directive.
1703 Format flags (like @samp{#} for example) may not work as you expect
1704 because many of the fields, even numeric ones, are printed with %s.
1705 The format flag @samp{-} does work; it forces left-alignment of the
1706 field.
1708 @samp{%%} is a literal percent sign.  A @samp{%} character followed by
1709 an unrecognised character (i.e., not a known directive or @code{printf}
1710 field width and precision specifier), is discarded (but the
1711 unrecognised character is printed), and a warning message is printed
1712 to the standard error output (because it was probably a typo).  Don't
1713 rely on this behaviour, because other directives may be added in the
1714 future.
1716 A @samp{%} at the end of the format argument causes undefined
1717 behaviour since there is no following character.  In some locales, it
1718 may hide your door keys, while in others it may remove the final page
1719 from the novel you are reading.
1721 @menu
1722 * Name Directives::
1723 * Ownership Directives::
1724 * Size Directives::
1725 * Location Directives::
1726 * Time Directives::
1727 * Formatting Flags::
1728 @end menu
1730 @node Name Directives
1731 @subsubsection Name Directives
1733 @table @code
1734 @item %p
1735 @c supports %-X.Yp
1736 File's name (not the absolute path name, but the name of the file as
1737 it was encountered by @code{find} - that is, as a relative path from
1738 one of the starting points).
1739 @item %f
1740 File's name with any leading directories removed (only the last
1741 element).
1742 @c supports %-X.Yf
1743 @item %h
1744 Leading directories of file's name (all but the last element and the
1745 slash before it).  If the file's name contains no slashes (for example
1746 because it was named on the command line and is in the current working
1747 directory), then ``%h'' expands to ``.''.  This prevents ``%h/%f''
1748 expanding to ``/foo'', which would be surprising and probably not
1749 desirable.
1750 @c supports %-X.Yh
1751 @item %P
1752 File's name with the name of the command line argument under which
1753 it was found removed from the beginning.
1754 @c supports %-X.YP
1755 @item %H
1756 Command line argument under which file was found.
1757 @c supports %-X.YH
1758 @end table
1760 @node Ownership Directives
1761 @subsubsection Ownership Directives
1763 @table @code
1764 @item %g
1765 @c supports %-X.Yg
1766 File's group name, or numeric group ID if the group has no name.
1767 @item %G
1768 @c supports %-X.Yg
1769 @c TODO: Needs to support # flag and 0 flag
1770 File's numeric group ID.
1771 @item %u
1772 @c supports %-X.Yu
1773 File's user name, or numeric user ID if the user has no name.
1774 @item %U
1775 @c supports %-X.Yu
1776 @c TODO: Needs to support # flag
1777 File's numeric user ID.
1778 @item %m
1779 @c full support, including # and 0.
1780 File's mode bits (in octal).  If you always want to have a leading
1781 zero on the number, use the '#' format flag, for example '%#m'.  
1783 The file mode bit numbers used are the traditional Unix
1784 numbers, which will be as expected on most systems, but if your
1785 system's file mode bit layout differs from the traditional Unix
1786 semantics, you will see a difference between the mode as printed by
1787 @samp{%m} and the mode as it appears in @code{struct stat}.
1789 @item %M
1790 File's type and mode bits (in symbolic form, as for @code{ls}).  This
1791 directive is supported in findutils 4.2.5 and later.
1792 @end table
1794 @node Size Directives
1795 @subsubsection Size Directives
1797 @table @code
1798 @item %k
1799 The amount of disk space used for this file in 1K blocks. Since disk
1800 space is allocated in multiples of the filesystem block size this is
1801 usually greater than %s/1024, but it can also be smaller if the file
1802 is a sparse file (that is, it has ``holes'').
1803 @item %b
1804 The amount of disk space used for this file in 512-byte blocks. Since
1805 disk space is allocated in multiples of the filesystem block size this
1806 is usually greater than %s/512, but it can also be smaller if the
1807 file is a sparse file (that is, it has ``holes'').
1808 @item %s
1809 File's size in bytes.
1810 @item %S
1811 File's sparseness.  This is calculated as @code{(BLOCKSIZE*st_blocks /
1812 st_size)}.  The exact value you will get for an ordinary file of a
1813 certain length is system-dependent.  However, normally sparse files
1814 will have values less than 1.0, and files which use indirect blocks
1815 and have few holes may have a value which is greater than 1.0.  The
1816 value used for BLOCKSIZE is system-dependent, but is usually 512
1817 bytes.  If the file size is zero, the value printed is undefined.  On
1818 systems which lack support for st_blocks, a file's sparseness is
1819 assumed to be 1.0.
1820 @end table
1822 @node Location Directives
1823 @subsubsection Location Directives
1825 @table @code
1826 @item %d
1827 File's depth in the directory tree (depth below a file named on the
1828 command line, not depth below the root directory).  Files named on the
1829 command line have a depth of 0.  Subdirectories immediately below them
1830 have a depth of 1, and so on.
1831 @item %D
1832 The device number on which the file exists (the @code{st_dev} field of
1833 @code{struct stat}), in decimal.
1834 @item %F
1835 Type of the filesystem the file is on; this value can be used for
1836 @samp{-fstype} (@pxref{Directories}).
1837 @item %l
1838 Object of symbolic link (empty string if file is not a symbolic link).
1839 @item %i
1840 File's inode number (in decimal).
1841 @item %n
1842 Number of hard links to file.
1843 @item %y
1844 Type of the file as used with @samp{-type}.  If the file is a symbolic
1845 link, @samp{l} will be printed.
1846 @item %Y
1847 Type of the file as used with @samp{-type}.  If the file is a symbolic
1848 link, it is dereferenced.  If the file is a broken symbolic link,
1849 @samp{N} is printed.
1851 @end table
1853 @node Time Directives
1854 @subsubsection Time Directives
1856 Some of these directives use the C @code{ctime} function.  Its output
1857 depends on the current locale, but it typically looks like
1859 @example
1860 Wed Nov  2 00:42:36 1994
1861 @end example
1863 @table @code
1864 @item %a
1865 File's last access time in the format returned by the C @code{ctime}
1866 function.
1867 @item %A@var{k}
1868 File's last access time in the format specified by @var{k}
1869 (@pxref{Time Formats}).
1870 @item %c
1871 File's last status change time in the format returned by the C
1872 @code{ctime} function.
1873 @item %C@var{k}
1874 File's last status change time in the format specified by @var{k}
1875 (@pxref{Time Formats}).
1876 @item %t
1877 File's last modification time in the format returned by the C
1878 @code{ctime} function.
1879 @item %T@var{k}
1880 File's last modification time in the format specified by @var{k}
1881 (@pxref{Time Formats}).
1882 @end table
1884 @node Time Formats
1885 @subsection Time Formats
1887 Below are the formats for the directives @samp{%A}, @samp{%C}, and
1888 @samp{%T}, which print the file's timestamps.  Some of these formats
1889 might not be available on all systems, due to differences in the C
1890 @code{strftime} function between systems.
1892 @menu
1893 * Time Components::
1894 * Date Components::
1895 * Combined Time Formats::
1896 @end menu
1898 @node Time Components
1899 @subsubsection Time Components
1901 The following format directives print single components of the time.
1903 @table @code
1904 @item H
1905 hour (00..23)
1906 @item I
1907 hour (01..12)
1908 @item k
1909 hour ( 0..23)
1910 @item l
1911 hour ( 1..12)
1912 @item p
1913 locale's AM or PM
1914 @item Z
1915 time zone (e.g., EDT), or nothing if no time zone is determinable
1916 @item M
1917 minute (00..59)
1918 @item S
1919 second (00..61).  There is a fractional part.
1920 @item @@
1921 seconds since Jan. 1, 1970, 00:00 GMT, with fractional part.
1922 @end table
1924 The fractional part of the seconds field is of indeterminate length
1925 and precision.  That is, the length of the fractional part of the
1926 seconds field will in general vary between findutils releases and
1927 between systems.  This means that it is unwise to assume that field
1928 has any specific length.  The length of this field is not usually a
1929 guide to the precision of timestamps in the underlying file system.
1933 @node Date Components
1934 @subsubsection Date Components
1936 The following format directives print single components of the date.
1938 @table @code
1939 @item a
1940 locale's abbreviated weekday name (Sun..Sat)
1941 @item A
1942 locale's full weekday name, variable length (Sunday..Saturday)
1943 @item b
1944 @itemx h
1945 locale's abbreviated month name (Jan..Dec)
1946 @item B
1947 locale's full month name, variable length (January..December)
1948 @item m
1949 month (01..12)
1950 @item d
1951 day of month (01..31)
1952 @item w
1953 day of week (0..6)
1954 @item j
1955 day of year (001..366)
1956 @item U
1957 week number of year with Sunday as first day of week (00..53)
1958 @item W
1959 week number of year with Monday as first day of week (00..53)
1960 @item Y
1961 year (1970@dots{})
1962 @item y
1963 last two digits of year (00..99)
1964 @end table
1966 @node Combined Time Formats
1967 @subsubsection Combined Time Formats
1969 The following format directives print combinations of time and date
1970 components.
1972 @table @code
1973 @item r
1974 time, 12-hour (hh:mm:ss [AP]M)
1975 @item T
1976 time, 24-hour (hh:mm:ss)
1977 @item X
1978 locale's time representation (H:M:S)
1979 @item c
1980 locale's date and time in ctime format (Sat Nov 04 12:02:33 EST
1981 1989).  This format does not include any fractional part in the
1982 seconds field.
1983 @item D
1984 date (mm/dd/yy)
1985 @item x
1986 locale's date representation (mm/dd/yy)
1987 @item +
1988 Date and time, separated by '+', for example
1989 `2004-04-28+22:22:05.0000000000'.
1990 The time is given in the current timezone (which may be affected by
1991 setting the TZ environment variable).  This is a GNU extension.  The
1992 seconds field includes a fractional part.
1993 @end table
1995 @node Formatting Flags
1996 @subsubsection Formatting Flags
1998 The @samp{%m} and @samp{%d} directives support the @samp{#}, @samp{0}
1999 and @samp{+} flags, but the other directives do not, even if they
2000 print numbers.  Numeric directives that do not support these flags
2001 include
2003 @samp{G},
2004 @samp{U},
2005 @samp{b},
2006 @samp{D},
2007 @samp{k} and
2008 @samp{n}.
2010 All fields support the format flag @samp{-}, which makes fields
2011 left-aligned.  That is, if the field width is greater than the actual
2012 contents of the field, the requisite number of spaces are printed
2013 after the field content instead of before it.
2015 @node Run Commands
2016 @section Run Commands
2018 You can use the list of file names created by @code{find} or
2019 @code{locate} as arguments to other commands.  In this way you can
2020 perform arbitrary actions on the files.
2022 @menu
2023 * Single File::
2024 * Multiple Files::
2025 * Querying::
2026 @end menu
2028 @node Single File
2029 @subsection Single File
2031 Here is how to run a command on one file at a time.
2033 @deffn Action -execdir command ;
2034 Execute @var{command}; true if zero status is returned.  @code{find}
2035 takes all arguments after @samp{-exec} to be part of the command until
2036 an argument consisting of @samp{;} is reached.  It replaces the string
2037 @samp{@{@}} by the current file name being processed everywhere it
2038 occurs in the command.  Both of these constructions need to be escaped
2039 (with a @samp{\}) or quoted to protect them from expansion by the
2040 shell.  The command is executed in the directory in which @code{find}
2041 was run.
2043 For example, to compare each C header file in or below the current
2044 directory with the file @file{/tmp/master}:
2046 @example
2047 find . -name '*.h' -execdir diff -u '@{@}' /tmp/master ';'
2048 @end example
2049 @end deffn
2051 If you use @samp{-execdir}, you must ensure that the @samp{$PATH}
2052 variable contains only absolute directory names.  Having an empty
2053 element in @samp{$PATH} or explicitly including @samp{.} (or any other
2054 non-absolute name) is insecure.  GNU find will refuse to run if you
2055 use @samp{-execdir} and it thinks your @samp{$PATH} setting is
2056 insecure.  For example:
2058 @table @samp
2059 @item /bin:/usr/bin:
2060 Insecure; empty path element (at the end)
2061 @item :/bin:/usr/bin:/usr/local/bin
2062 Insecure; empty path element (at the start)
2063 @item /bin:/usr/bin::/usr/local/bin
2064 Insecure; empty path element (two colons in a row)
2065 @item /bin:/usr/bin:.:/usr/local/bin
2066 Insecure; @samp{.} is a path element (@file{.} is not an absolute file name)
2067 @item /bin:/usr/bin:sbin:/usr/local/bin
2068 Insecure; @samp{sbin} is not an absolute file name
2069 @item /bin:/usr/bin:/sbin:/usr/local/bin
2070 Secure (if you control the contents of those directories and any access to them)
2071 @end table
2073 Another similar option, @samp{-exec} is supported, but is less secure.
2074 @xref{Security Considerations}, for a discussion of the security
2075 problems surrounding @samp{-exec}.
2078 @deffn Action -exec command ;
2079 This insecure variant of the @samp{-execdir} action is specified by
2080 POSIX.  The main difference is that the command is executed in the
2081 directory from which @code{find} was invoked, meaning that @samp{@{@}}
2082 is expanded to a relative path starting with the name of one of the
2083 starting directories, rather than just the basename of the matched
2084 file.
2086 While some implementations of @code{find} replace the @samp{@{@}} only
2087 where it appears on its own in an argument, GNU @code{find} replaces
2088 @samp{@{@}} wherever it appears.
2089 @end deffn
2092 @node Multiple Files
2093 @subsection Multiple Files
2095 Sometimes you need to process files one at a time.  But usually this
2096 is not necessary, and, it is faster to run a command on as many files
2097 as possible at a time, rather than once per file.  Doing this saves on
2098 the time it takes to start up the command each time.
2100 The @samp{-execdir} and @samp{-exec} actions have variants that build
2101 command lines containing as many matched files as possible.
2103 @deffn Action -execdir command @{@} +
2104 This works as for @samp{-execdir command ;}, except that the
2105 @samp{@{@}} at the end of the command is expanded to a list of names
2106 of matching files.  This expansion is done in such a way as to avoid
2107 exceeding the maximum command line length available on the system.
2108 Only one @samp{@{@}} is allowed within the command, and it must appear
2109 at the end, immediately before the @samp{+}.  A @samp{+} appearing in
2110 any position other than immediately after @samp{@{@}} is not
2111 considered to be special (that is, it does not terminate the command).
2112 @end deffn
2115 @deffn Action -exec command @{@} +
2116 This insecure variant of the @samp{-execdir} action is specified by
2117 POSIX.  The main difference is that the command is executed in the
2118 directory from which @code{find} was invoked, meaning that @samp{@{@}}
2119 is expanded to a relative path starting with the name of one of the
2120 starting directories, rather than just the basename of the matched
2121 file.
2122 @end deffn
2124 Before @code{find} exits, any partially-built command lines are
2125 executed.  This happens even if the exit was caused by the
2126 @samp{-quit} action.  However, some types of error (for example not
2127 being able to invoke @code{stat()} on the current directory) can cause
2128 an immediate fatal exit.  In this situation, any partially-built
2129 command lines will not be invoked (this prevents possible infinite
2130 loops).
2132 At first sight, it looks like the list of filenames to be processed
2133 can only be at the end of the command line, and that this might be a
2134 problem for some comamnds (@code{cp} and @code{rsync} for example).
2136 However, there is a slightly obscure but powerful workarouund for this
2137 problem which takes advantage of the behaviour of @code{sh -c}:-
2139 @example
2140 find startpoint -tests @dots{} -exec sh -c 'scp "$@@" remote:/dest' sh @{@} + 
2141 @end example
2143 In the example above, the filenames we want to work on need to occur
2144 on the @code{scp} command line before the name of the destination.  We
2145 use the shell to invoke the command @code{scp "$@@" remote:/dest} and
2146 the shell expands @code{"$@@"} to the list of filenames we want to
2147 process.
2149 Another, but less secure, way to run a command on more than one file
2150 at once, is to use the @code{xargs} command, which is invoked like
2151 this:
2153 @example
2154 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
2155 @end example
2157 @code{xargs} normally reads arguments from the standard input.  These
2158 arguments are delimited by blanks (which can be protected with double
2159 or single quotes or a backslash) or newlines.  It executes the
2160 @var{command} (default is @file{/bin/echo}) one or more times with any
2161 @var{initial-arguments} followed by arguments read from standard
2162 input.  Blank lines on the standard input are ignored.  If the
2163 @samp{-L} option is in use, trailing blanks indicate that @code{xargs}
2164 should consider the following line to be part of this one.
2166 Instead of blank-delimited names, it is safer to use @samp{find
2167 -print0} or @samp{find -fprint0} and process the output by giving the
2168 @samp{-0} or @samp{--null} option to GNU @code{xargs}, GNU @code{tar},
2169 GNU @code{cpio}, or @code{perl}.  The @code{locate} command also has a
2170 @samp{-0} or @samp{--null} option which does the same thing.
2172 You can use shell command substitution (backquotes) to process a list
2173 of arguments, like this:
2175 @example
2176 grep -l sprintf `find $HOME -name '*.c' -print`
2177 @end example
2179 However, that method produces an error if the length of the @samp{.c}
2180 file names exceeds the operating system's command line length limit.
2181 @code{xargs} avoids that problem by running the command as many times
2182 as necessary without exceeding the limit:
2184 @example
2185 find $HOME -name '*.c' -print | xargs grep -l sprintf
2186 @end example
2188 However, if the command needs to have its standard input be a terminal
2189 (@code{less}, for example), you have to use the shell command
2190 substitution method or use the @samp{--arg-file} option of
2191 @code{xargs}.
2193 The @code{xargs} command will process all its input, building command
2194 lines and executing them, unless one of the commands exits with a
2195 status of 255 (this will cause xargs to issue an error message and
2196 stop) or it reads a line contains the end of file string specified
2197 with the @samp{--eof} option.
2199 @menu
2200 * Unsafe File Name Handling::
2201 * Safe File Name Handling::
2202 * Unusual Characters in File Names::
2203 * Limiting Command Size::
2204 * Interspersing File Names::
2205 @end menu
2207 @node Unsafe File Name Handling
2208 @subsubsection Unsafe File Name Handling
2210 Because file names can contain quotes, backslashes, blank characters,
2211 and even newlines, it is not safe to process them using @code{xargs}
2212 in its default mode of operation.  But since most files' names do not
2213 contain blanks, this problem occurs only infrequently.  If you are
2214 only searching through files that you know have safe names, then you
2215 need not be concerned about it.
2217 Error messages issued by @code{find} and @code{locate} quote unusual
2218 characters in file names in order to prevent unwanted changes in the
2219 terminal's state.
2222 @c This example is adapted from:
2223 @c From: pfalstad@stone.Princeton.EDU (Paul John Falstad)
2224 @c Newsgroups: comp.unix.shell
2225 @c Subject: Re: Beware xargs security holes
2226 @c Date: 16 Oct 90 19:12:06 GMT
2228 In many applications, if @code{xargs} botches processing a file
2229 because its name contains special characters, some data might be lost.
2230 The importance of this problem depends on the importance of the data
2231 and whether anyone notices the loss soon enough to correct it.
2232 However, here is an extreme example of the problems that using
2233 blank-delimited names can cause.  If the following command is run
2234 daily from @code{cron}, then any user can remove any file on the
2235 system:
2237 @example
2238 find / -name '#*' -atime +7 -print | xargs rm
2239 @end example
2241 For example, you could do something like this:
2243 @example
2244 eg$ echo > '#
2245 vmunix'
2246 @end example
2248 @noindent
2249 and then @code{cron} would delete @file{/vmunix}, if it ran
2250 @code{xargs} with @file{/} as its current directory.
2252 To delete other files, for example @file{/u/joeuser/.plan}, you could
2253 do this:
2255 @example
2256 eg$ mkdir '#
2258 eg$ cd '#
2260 eg$ mkdir u u/joeuser u/joeuser/.plan'
2262 eg$ echo > u/joeuser/.plan'
2263 /#foo'
2264 eg$ cd ..
2265 eg$ find . -name '#*' -print | xargs echo
2266 ./# ./# /u/joeuser/.plan /#foo
2267 @end example
2269 @node Safe File Name Handling
2270 @subsubsection Safe File Name Handling
2272 Here is how to make @code{find} output file names so that they can be
2273 used by other programs without being mangled or misinterpreted.  You
2274 can process file names generated this way by giving the @samp{-0} or
2275 @samp{--null} option to GNU @code{xargs}, GNU @code{tar}, GNU
2276 @code{cpio}, or @code{perl}.
2278 @deffn Action -print0
2279 True; print the entire file name on the standard output, followed by a
2280 null character.
2281 @end deffn
2283 @deffn Action -fprint0 file
2284 True; like @samp{-print0} but write to @var{file} like @samp{-fprint}
2285 (@pxref{Print File Name}).  The output file is always created.
2286 @end deffn
2288 As of findutils version 4.2.4, the @code{locate} program also has a
2289 @samp{--null} option which does the same thing.  For similarity with
2290 @code{xargs}, the short form of the option @samp{-0} can also be used.
2292 If you want to be able to handle file names safely but need to run
2293 commands which want to be connected to a terminal on their input, you
2294 can use the @samp{--arg-file} option to @code{xargs} like this:
2296 @example
2297 find / -name xyzzy -print0 > list
2298 xargs --null --arg-file=list munge
2299 @end example
2301 The example above runs the @code{munge} program on all the files named
2302 @file{xyzzy} that we can find, but @code{munge}'s input will still be
2303 the terminal (or whatever the shell was using as standard input).  If
2304 your shell has the ``process substitution'' feature @samp{<(...)}, you
2305 can do this in just one step:
2307 @example
2308 xargs --null --arg-file=<(find / -name xyzzy -print0) munge
2309 @end example
2311 @node Unusual Characters in File Names
2312 @subsubsection Unusual Characters in File Names
2313 As discussed above, you often need to be careful about how the names
2314 of files are handled by @code{find} and other programs.  If the output
2315 of @code{find} is not going to another program but instead is being
2316 shown on a terminal, this can still be a problem.  For example, some
2317 character sequences can reprogram the function keys on some terminals.
2318 @xref{Security Considerations}, for a discussion of other security
2319 problems relating to @code{find}.
2321 Unusual characters are handled differently by various
2322 actions, as described below.
2324 @table @samp
2325 @item -print0
2326 @itemx -fprint0
2327 Always print the exact file name, unchanged, even if the output is
2328 going to a terminal.
2329 @item -ok
2330 @itemx -okdir
2331 Always print the exact file name, unchanged.  This will probably
2332 change in a future release.
2333 @item -ls
2334 @itemx -fls
2335 Unusual characters are always escaped.  White space, backslash, and
2336 double quote characters are printed using C-style escaping (for
2337 example @samp{\f}, @samp{\"}).  Other unusual characters are printed
2338 using an octal escape.  Other printable characters (for @samp{-ls} and
2339 @samp{-fls} these are the characters between octal 041 and 0176) are
2340 printed as-is.
2341 @item -printf
2342 @itemx -fprintf
2343 If the output is not going to a terminal, it is printed as-is.
2344 Otherwise, the result depends on which directive is in use:
2346 @table @asis
2347 @item %D, %F, %H, %Y, %y
2348 These expand to values which are not under control of files' owners,
2349 and so are printed as-is.
2350 @item  %a, %b, %c, %d, %g, %G, %i, %k, %m, %M, %n, %s, %t, %u, %U
2351 These have values which are under the control of files' owners but
2352 which cannot be used to send arbitrary data to the terminal, and so
2353 these are printed as-is.
2354 @item %f, %h, %l, %p, %P
2355 The output of these directives is quoted if the output is going to a
2356 terminal.
2358 This quoting is performed in the same way as for GNU @code{ls}.  This
2359 is not the same quoting mechanism as the one used for @samp{-ls} and
2360 @samp{fls}.  If you are able to decide what format to use for the
2361 output of @code{find} then it is normally better to use @samp{\0} as a
2362 terminator than to use newline, as file names can contain white space
2363 and newline characters.
2364 @end table
2365 @item -print
2366 @itemx -fprint
2367 Quoting is handled in the same way as for the @samp{%p} directive of
2368 @samp{-printf} and @samp{-fprintf}.  If you are using @code{find} in a
2369 script or in a situation where the matched files might have arbitrary
2370 names, you should consider using @samp{-print0} instead of
2371 @samp{-print}.
2372 @end table
2375 The @code{locate} program quotes and escapes unusual characters in
2376 file names in the same way as @code{find}'s @samp{-print} action.
2378 The behaviours described above may change soon, as the treatment of
2379 unprintable characters is harmonised for @samp{-ls}, @samp{-fls},
2380 @samp{-print}, @samp{-fprint}, @samp{-printf} and @samp{-fprintf}.
2382 @node Limiting Command Size
2383 @subsubsection Limiting Command Size
2385 @code{xargs} gives you control over how many arguments it passes to
2386 the command each time it executes it.  By default, it uses up to
2387 @code{ARG_MAX} - 2k, or 128k, whichever is smaller, characters per
2388 command.  It uses as many lines and arguments as fit within that
2389 limit.  The following options modify those values.
2391 @table @code
2392 @item --no-run-if-empty
2393 @itemx -r
2394 If the standard input does not contain any nonblanks, do not run the
2395 command.  By default, the command is run once even if there is no
2396 input.  This option is a GNU extension.
2398 @item --max-lines@r{[}=@var{max-lines}@r{]}
2399 @itemx -L @var{max-lines}
2400 @itemx -l@r{[}@var{max-lines}@r{]}
2401 Use at most @var{max-lines} nonblank input lines per command line;
2402 @var{max-lines} defaults to 1 if omitted; omitting the argument is not
2403 allowed in the case of the @samp{-L} option.  Trailing blanks cause an
2404 input line to be logically continued on the next input line, for the
2405 purpose of counting the lines.  Implies @samp{-x}.  The preferred name
2406 for this option is @samp{-L} as this is specified by POSIX.  
2408 @item --max-args=@var{max-args}
2409 @itemx -n @var{max-args}
2410 Use at most @var{max-args} arguments per command line.  Fewer than
2411 @var{max-args} arguments will be used if the size (see the @samp{-s}
2412 option) is exceeded, unless the @samp{-x} option is given, in which
2413 case @code{xargs} will exit.
2415 @item --max-chars=@var{max-chars}
2416 @itemx -s @var{max-chars}
2417 Use at most @var{max-chars} characters per command line, including the
2418 command initial arguments and the terminating nulls at the ends of the
2419 argument strings.  If you specify a value for this option which is too
2420 large or small, a warning message is printed and the appropriate upper
2421 or lower limit is used instead.  You can use @samp{--show-limits}
2422 option to understand the command-line limits applying to @code{xargs}
2423 and how this is affected by any other options.  The POSIX limits shown
2424 when you do this have already been adjusted to take into account the
2425 size of your environment variables.  
2427 The largest allowed value is system-dependent, and is calculated as
2428 the argument length limit for exec, less the size of your environment,
2429 less 2048 bytes of headroom.  If this value is more than 128KiB,
2430 128Kib is used as the default value; otherwise, the default value is
2431 the maximum.
2434 @item --max-procs=@var{max-procs}
2435 @itemx -P @var{max-procs}
2436 Run up to @var{max-procs} processes at a time; the default is 1.  If
2437 @var{max-procs} is 0, @code{xargs} will run as many processes as
2438 possible at a time.  Use the @samp{-n}, @samp{-s}, or @samp{-L} option
2439 with @samp{-P}; otherwise chances are that the command will be run
2440 only once.
2441 @end table
2443 @node Interspersing File Names
2444 @subsubsection Interspersing File Names
2446 @code{xargs} can insert the name of the file it is processing between
2447 arguments you give for the command.  Unless you also give options to
2448 limit the command size (@pxref{Limiting Command Size}), this mode of
2449 operation is equivalent to @samp{find -exec} (@pxref{Single File}).
2451 @table @code
2452 @item --replace@r{[}=@var{replace-str}@r{]}
2453 @itemx -I @var{replace-str}
2454 @itemx -i @var{replace-str}
2455 Replace occurrences of @var{replace-str} in the initial arguments with
2456 names read from the input.  Also, unquoted blanks do not terminate
2457 arguments; instead, the input is split at newlines only.  For the
2458 @samp{-i} option, if @var{replace-str} is omitted for @samp{--replace}
2459 or @samp{-i}, it defaults to @samp{@{@}} (like for @samp{find -exec}).
2460 Implies @samp{-x} and @samp{-l 1}.  @samp{-i} is deprecated in favour
2461 of @samp{-I}. As an example, to sort each file in the @file{bills}
2462 directory, leaving the output in that file name with @file{.sorted}
2463 appended, you could do:
2465 @example
2466 find bills -type f | xargs -I XX sort -o XX.sorted XX
2467 @end example
2469 @noindent
2470 The equivalent command using @samp{find -execdir} is:
2472 @example
2473 find bills -type f -execdir sort -o '@{@}.sorted' '@{@}' ';'
2474 @end example
2475 @end table
2478 When you use the @samp{-I} option, each line read from the input is
2479 buffered internally.  This means that there is an upper limit on the
2480 length of input line that xargs will accept when used with the
2481 @samp{-I} option.  To work around this limitation, you can use the
2482 @samp{-s} option to increase the amount of buffer space that xargs
2483 uses, and you can also use an extra invocation of xargs to ensure that
2484 very long lines do not occur.  For example:
2486 @example
2487 somecommand | xargs -s 50000 echo | xargs -I '@{@}' -s 100000 rm '@{@}'
2488 @end example
2490 Here, the first invocation of @code{xargs} has no input line length
2491 limit because it doesn't use the @samp{-I} option. The second
2492 invocation of @code{xargs} does have such a limit, but we have ensured
2493 that the it never encounters a line which is longer than it can
2494 handle.  
2496 This is not an ideal solution.  Instead, the @samp{-I} option should
2497 not impose a line length limit (apart from any limit imposed by the
2498 operating system) and so one might consider this limitation to be a
2499 bug.  A better solution would be to allow @code{xargs -I} to
2500 automatically move to a larger value for the @samp{-s} option when
2501 this is needed.
2503 This sort of problem doesn't occur with the output of @code{find}
2504 because it emits just one filename per line.
2506 @node Querying
2507 @subsection Querying
2509 To ask the user whether to execute a command on a single file, you can
2510 use the @code{find} primary @samp{-okdir} instead of @samp{-execdir},
2511 and the @code{find} primary @samp{-ok} instead of @samp{-exec}:
2513 @deffn Action -okdir command ;
2514 Like @samp{-execdir} (@pxref{Single File}), but ask the user first (on
2515 the standard input); if the response does not start with @samp{y} or
2516 @samp{Y}, do not run the command, and return false.  If the command is
2517 run, its standard input is redirected from @file{/dev/null}.
2518 @end deffn
2520 @deffn Action -ok command ;
2521 This insecure variant of the @samp{-okdir} action is specified by
2522 POSIX.  The main difference is that the command is executed in the
2523 directory from which @code{find} was invoked, meaning that @samp{@{@}}
2524 is expanded to a relative path starting with the name of one of the
2525 starting directories, rather than just the basename of the matched
2526 file.  If the command is run, its standard input is redirected from
2527 @file{/dev/null}.
2528 @end deffn
2530 When processing multiple files with a single command, to query the
2531 user you give @code{xargs} the following option.  When using this
2532 option, you might find it useful to control the number of files
2533 processed per invocation of the command (@pxref{Limiting Command
2534 Size}).
2536 @table @code
2537 @item --interactive
2538 @itemx -p
2539 Prompt the user about whether to run each command line and read a line
2540 from the terminal.  Only run the command line if the response starts
2541 with @samp{y} or @samp{Y}.  Implies @samp{-t}.
2542 @end table
2544 @node Delete Files
2545 @section Delete Files
2547 @deffn Action -delete
2548 Delete files or directories; true if removal succeeded.  If the
2549 removal failed, an error message is issued.
2551 The use of the @samp{-delete} action on the command line automatically
2552 turns on the @samp{-depth} option (@pxref{find Expressions}).  This
2553 can be surprising if you were previously just testing with
2554 @samp{-print}, so it is usually best to remember to use @samp{-depth}
2555 explicitly.
2556 @end deffn
2558 @node Adding Tests
2559 @section Adding Tests
2561 You can test for file attributes that none of the @code{find} builtin
2562 tests check.  To do this, use @code{xargs} to run a program that
2563 filters a list of files printed by @code{find}.  If possible, use
2564 @code{find} builtin tests to pare down the list, so the program run by
2565 @code{xargs} has less work to do.  The tests builtin to @code{find}
2566 will likely run faster than tests that other programs perform.
2568 For reasons of efficiency it is often useful to limit the number of
2569 times an external program has to be run.  For this reason, it is often
2570 a good idea to implement ``extended'' tests by using @code{xargs}.
2572 For example, here is a way to print the names of all of the unstripped
2573 binaries in the @file{/usr/local} directory tree.  Builtin tests avoid
2574 running @code{file} on files that are not regular files or are not
2575 executable.
2577 @example
2578 find /usr/local -type f -perm /a=x | xargs file |
2579   grep 'not stripped' | cut -d: -f1
2580 @end example
2582 @noindent
2583 The @code{cut} program removes everything after the file name from the
2584 output of @code{file}.
2586 However, using @code{xargs} can present important security problems
2587 (@pxref{Security Considerations}).  These can be avoided by using
2588 @samp{-execdir}.  The @samp{-execdir} action is also a useful way of
2589 putting your own test in the middle of a set of other tests or actions
2590 for @code{find} (for example, you might want to use @samp{-prune}).
2592 @c Idea from Martin Weitzel.
2593 To place a special test somewhere in the middle of a @code{find}
2594 expression, you can use @samp{-execdir} (or, less securely,
2595 @samp{-exec}) to run a program that performs the test.  Because
2596 @samp{-execdir} evaluates to the exit status of the executed program,
2597 you can use a program (which can be a shell script) that tests for a
2598 special attribute and make it exit with a true (zero) or false
2599 (non-zero) status.  It is a good idea to place such a special test
2600 @emph{after} the builtin tests, because it starts a new process which
2601 could be avoided if a builtin test evaluates to false.
2603 Here is a shell script called @code{unstripped} that checks whether
2604 its argument is an unstripped binary file:
2606 @example
2607 #! /bin/sh
2608 file "$1" | grep -q "not stripped"
2609 @end example
2612 This script relies on the shell exiting with the status of
2613 the last command in the pipeline, in this case @code{grep}.  The
2614 @code{grep} command exits with a true status if it found any matches,
2615 false if not.  Here is an example of using the script (assuming it is
2616 in your search path).  It lists the stripped executables (and shell
2617 scripts) in the file @file{sbins} and the unstripped ones in
2618 @file{ubins}.
2620 @example
2621 find /usr/local -type f -perm /a=x \
2622   \( -execdir unstripped '@{@}' \; -fprint ubins -o -fprint sbins \)
2623 @end example
2626 @node Databases
2627 @chapter File Name Databases
2629 The file name databases used by @code{locate} contain lists of files
2630 that were in particular directory trees when the databases were last
2631 updated.  The file name of the default database is determined when
2632 @code{locate} and @code{updatedb} are configured and installed.  The
2633 frequency with which the databases are updated and the directories for
2634 which they contain entries depend on how often @code{updatedb} is run,
2635 and with which arguments.
2637 You can obtain some statistics about the databases by using
2638 @samp{locate --statistics}.
2640 @menu
2641 * Database Locations::
2642 * Database Formats::
2643 * Newline Handling::
2644 @end menu
2647 @node Database Locations
2648 @section Database Locations
2650 There can be multiple file name databases.  Users can select which
2651 databases @code{locate} searches using the @code{LOCATE_PATH}
2652 environment variable or a command line option.  The system
2653 administrator can choose the file name of the default database, the
2654 frequency with which the databases are updated, and the directories
2655 for which they contain entries.  File name databases are updated by
2656 running the @code{updatedb} program, typically nightly.
2658 In networked environments, it often makes sense to build a database at
2659 the root of each filesystem, containing the entries for that
2660 filesystem.  @code{updatedb} is then run for each filesystem on the
2661 fileserver where that filesystem is on a local disk, to prevent
2662 thrashing the network.
2664 @xref{Invoking updatedb}, for the description of the options to
2665 @code{updatedb}.  These options can be used to specify which
2666 directories are indexed by each database file.
2668 The default location for the locate database depends on how findutils
2669 is built, but the findutils installation accompanying this manual uses
2670 the default location @file{@value{LOCATE_DB}}.
2672 If no database exists at @file{@value{LOCATE_DB}} but the user did not
2673 specify where to look (by using @samp{-d} or setting
2674 @code{LOCATE_PATH}), then @code{locate} will also check for a
2675 ``secure'' database in @file{/var/lib/slocate/slocate.db}.
2677 @node Database Formats
2678 @section Database Formats
2680 The file name databases contain lists of files that were in particular
2681 directory trees when the databases were last updated.  The file name
2682 database format changed starting with GNU @code{locate} version 4.0 to
2683 allow machines with different byte orderings to share the databases.
2685 GNU @code{locate} can read both the old and new database formats.
2686 However, old versions of @code{locate} (on other Unix systems, or GNU
2687 @code{locate} before version 4.0) produce incorrect results if run
2688 against a database in something other than the old format.
2690 Support for the old database format will eventually be discontinued,
2691 first in @code{updatedb} and later in @code{locate}.
2693 If you run @samp{locate --statistics}, the resulting summary indicates
2694 the type of each @code{locate} database.   You select which database
2695 format @code{updatedb} will use with the @samp{--dbformat} option.
2698 @menu
2699 * LOCATE02 Database Format::
2700 * Sample LOCATE02 Database::
2701 * slocate Database Format::
2702 * Old Database Format::
2703 @end menu
2705 @node LOCATE02 Database Format
2706 @subsection LOCATE02 Database Format
2708 @code{updatedb} runs a program called @code{frcode} to
2709 @dfn{front-compress} the list of file names, which reduces the
2710 database size by a factor of 4 to 5.  Front-compression (also known as
2711 incremental encoding) works as follows.
2713 The database entries are a sorted list (case-insensitively, for users'
2714 convenience).  Since the list is sorted, each entry is likely to share
2715 a prefix (initial string) with the previous entry.  Each database
2716 entry begins with an offset-differential count byte, which is the
2717 additional number of characters of prefix of the preceding entry to
2718 use beyond the number that the preceding entry is using of its
2719 predecessor.  (The counts can be negative.)  Following the count is a
2720 null-terminated ASCII remainder---the part of the name that follows
2721 the shared prefix.
2723 If the offset-differential count is larger than can be stored in a
2724 byte (+/-127), the byte has the value 0x80 and the count follows in a
2725 2-byte word, with the high byte first (network byte order).
2727 Every database begins with a dummy entry for a file called
2728 @file{LOCATE02}, which @code{locate} checks for to ensure that the
2729 database file has the correct format; it ignores the entry in doing
2730 the search.
2732 Databases cannot be concatenated together, even if the first (dummy)
2733 entry is trimmed from all but the first database.  This is because the
2734 offset-differential count in the first entry of the second and
2735 following databases will be wrong.
2737 In the output of @samp{locate --statistics}, the new database format
2738 is referred to as @samp{LOCATE02}.
2740 @node Sample LOCATE02 Database
2741 @subsection Sample LOCATE02 Database
2743 Sample input to @code{frcode}:
2744 @c with nulls changed to newlines:
2746 @example
2747 /usr/src
2748 /usr/src/cmd/aardvark.c
2749 /usr/src/cmd/armadillo.c
2750 /usr/tmp/zoo
2751 @end example
2753 Length of the longest prefix of the preceding entry to share:
2755 @example
2756 0 /usr/src
2757 8 /cmd/aardvark.c
2758 14 rmadillo.c
2759 5 tmp/zoo
2760 @end example
2762 Output from @code{frcode}, with trailing nulls changed to newlines
2763 and count bytes made printable:
2765 @example
2766 0 LOCATE02
2767 0 /usr/src
2768 8 /cmd/aardvark.c
2769 6 rmadillo.c
2770 -9 tmp/zoo
2771 @end example
2773 (6 = 14 - 8, and -9 = 5 - 14)
2775 @node slocate Database Format
2776 @subsection slocate Database Format
2778 The @code{slocate} program uses a database format similar to, but not
2779 quite the same as, GNU @code{locate}.  The first byte of the database
2780 specifies its @dfn{security level}.  If the security level is 0,
2781 @code{slocate} will read, match and print filenames on the basis of
2782 the information in the database only.  However, if the security level
2783 byte is 1, @code{slocate} omits entries from its output if the
2784 invoking user is unable to access them.  The second byte of the
2785 database is zero.  The second byte is immediately followed by the
2786 first database entry.  The first entry in the database is not preceded
2787 by any differential count or dummy entry.  Instead the differential
2788 count for the first item is assumed to be zero.
2790 Starting with the second entry (if any) in the database, data is
2791 interpreted as for the GNU LOCATE02 format.
2793 @node Old Database Format
2794 @subsection Old Database Format
2796 The old database format is used by Unix @code{locate} and @code{find}
2797 programs and earlier releases of the GNU ones.  @code{updatedb}
2798 produces this format if given the @samp{--old-format} option.
2800 @code{updatedb} runs programs called @code{bigram} and @code{code} to
2801 produce old-format databases.  The old format differs from the new one
2802 in the following ways.  Instead of each entry starting with an
2803 offset-differential count byte and ending with a null, byte values
2804 from 0 through 28 indicate offset-differential counts from -14 through
2805 14.  The byte value indicating that a long offset-differential count
2806 follows is 0x1e (30), not 0x80.  The long counts are stored in host
2807 byte order, which is not necessarily network byte order, and host
2808 integer word size, which is usually 4 bytes.  They also represent a
2809 count 14 less than their value.  The database lines have no
2810 termination byte; the start of the next line is indicated by its first
2811 byte having a value <= 30.
2813 In addition, instead of starting with a dummy entry, the old database
2814 format starts with a 256 byte table containing the 128 most common
2815 bigrams in the file list.  A bigram is a pair of adjacent bytes.
2816 Bytes in the database that have the high bit set are indexes (with the
2817 high bit cleared) into the bigram table.  The bigram and
2818 offset-differential count coding makes these databases 20-25% smaller
2819 than the new format, but makes them not 8-bit clean.  Any byte in a
2820 file name that is in the ranges used for the special codes is replaced
2821 in the database by a question mark, which not coincidentally is the
2822 shell wildcard to match a single character.
2824 The old format therefore cannot faithfully store entries with
2825 non-ASCII characters. It therefore should not be used in
2826 internationalised environments.  That is, most installations should
2827 not use it.
2829 Because the long counts are stored by the @code{code} program as
2830 native-order machine words, the database format is not eaily used in
2831 environments which differ in terms of byte order.  If locate databases
2832 are to be shared between machines, the LOCATE02 database format should
2833 be used.  This has other benefits as discussed above.  However, the
2834 length of the filename currently being processed can normally be used
2835 to place reasonable limits on the long counts and so this information
2836 is used by locate to help it guess the byte ordering of the old format
2837 database.  Unless it finds evidence to the contrary, @code{locate}
2838 will assume that the byte order of the database is the same as the
2839 native byte order of the machine running @code{locate}.  The output of
2840 @samp{locate --statistics} also includes information about the byte
2841 order of old-format databases.
2843 The output of @samp{locate --statistics} will give an incorrect count
2844 of the number of file names containing newlines or high-bit characters
2845 for old-format databases.
2847 Old versions of GNU @code{locate} fail to correctly handle very long
2848 file names, possibly leading to security problems relating to a heap
2849 buffer overrun.  @xref{Security Considerations for locate}, for a
2850 detailed explanation.
2852 @node Newline Handling
2853 @section Newline Handling
2855 Within the database, file names are terminated with a null character.
2856 This is the case for both the old and the new format.
2858 When the new database format is being used, the compression technique
2859 used to generate the database though relies on the ability to sort the
2860 list of files before they are presented to @code{frcode}.
2862 If the system's sort command allows its input list of files to be
2863 separated with null characters via the @samp{-z} option, this option
2864 is used and therefore @code{updatedb} and @code{locate} will both
2865 correctly handle file names containing newlines.  If the @code{sort}
2866 command lacks support for this, the list of files is delimited with
2867 the newline character, meaning that parts of file names containing
2868 newlines will be incorrectly sorted.  This can result in both
2869 incorrect matches and incorrect failures to match.
2871 On the other hand, if you are using the old database format, file
2872 names with embedded newlines are not correctly handled.  There is no
2873 technical limitation which enforces this, it's just that the
2874 @code{bigram} program has not been updated to support lists of file
2875 names separated by nulls.
2877 So, if you are using the new database format (this is the default) and
2878 your system uses GNU @code{sort}, newlines will be correctly handled
2879 at all times.  Otherwise, newlines may not be correctly handled.
2881 @node File Permissions
2882 @chapter File Permissions
2884 @include perm.texi
2886 @include getdate.texi
2888 @node Reference
2889 @chapter Reference
2891 Below are summaries of the command line syntax for the programs
2892 discussed in this manual.
2894 @menu
2895 * Invoking find::
2896 * Invoking locate::
2897 * Invoking updatedb::
2898 * Invoking xargs::
2899 * Regular Expressions::
2900 * Environment Variables::
2901 @end menu
2903 @node Invoking find
2904 @section Invoking @code{find}
2906 @example
2907 find @r{[-H] [-L] [-P] [-D @var{debugoptions}] [-O@var{level}]} @r{[}@var{file}@dots{}@r{]} @r{[}@var{expression}@r{]}
2908 @end example
2910 @code{find} searches the directory tree rooted at each file name
2911 @var{file} by evaluating the @var{expression} on each file it finds in
2912 the tree.
2914 The command line may begin with the @samp{-H}, @samp{-L}, @samp{-P},
2915 @samp{-D} and @samp{-O} options.  These are followed by a list of
2916 files or directories that should be searched.  If no files to search
2917 are specified, the current directory (@file{.}) is used.
2919 This list of files to search is followed by a list of expressions
2920 describing the files we wish to search for.  The first part of the
2921 expression is recognised by the fact that it begins with @samp{-}
2922 followed by some other letters (for example @samp{-print}), or is
2923 either @samp{(} or @samp{!}.  Any arguments after it are the rest of
2924 the expression.  
2926 If no expression is given, the expression @samp{-print} is used.
2928 The @code{find} command exits with status zero if all files matched
2929 are processed successfully, greater than zero if errors occur.
2931 The @code{find} program also recognises two options for administrative
2932 use:
2934 @table @samp
2935 @item --help
2936 Print a summary of the command line usage and exit.
2937 @item --version
2938 Print the version number of @code{find} and exit.
2939 @end table
2941 The @samp{-version} option is a synonym for @samp{--version}
2944 @menu
2945 * Filesystem Traversal Options::
2946 * Warning Messages::
2947 * Optimisation Options::
2948 * Debug Options::
2949 * Find Expressions::
2950 @end menu
2952 @node Filesystem Traversal Options
2953 @subsection Filesystem Traversal Options
2955 The options @samp{-H}, @samp{-L} or @samp{-P} may be specified at the
2956 start of the command line (if none of these is specified, @samp{-P} is
2957 assumed).  If you specify more than one of these options, the last one
2958 specified takes effect (but note that the @samp{-follow} option is
2959 equivalent to @samp{-L}).  
2961 @table @code
2962 @item -P
2963 Never follow symbolic links (this is the default), except in the case
2964 of the @samp{-xtype} predicate.
2965 @item -L
2966 Always follow symbolic links, except in the case of the @samp{-xtype}
2967 predicate.
2968 @item -H
2969 Follow symbolic links specified in the list of files to search, or
2970 which are otherwise specified on the command line.
2971 @end table
2973 If @code{find} would follow a symbolic link, but cannot for any reason
2974 (for example, because it has insufficient permissions or the link is
2975 broken), it falls back on using the properties of the symbolic link
2976 itself.  @ref{Symbolic Links} for a more complete description of how
2977 symbolic links are handled.
2979 @node Warning Messages
2980 @subsection Warning Messages
2982 If there is an error on the @code{find} command line, an error message
2983 is normally issued.  However, there are some usages that are
2984 inadvisable but which @code{find} should still accept.  Under these
2985 circumstances, @code{find} may issue a warning message.  
2987 By default, warnings are enabled only if @code{find} is being run
2988 interactively (specifically, if the standard input is a terminal) and
2989 the POSIXLY_CORRECT environment variable is not set.  Warning messages
2990 can be controlled explicitly by the use of options on the command
2991 line:
2993 @table @code
2994 @item -warn
2995 Issue warning messages where appropriate.
2996 @item -nowarn
2997 Do not issue warning messages.
2998 @end table
3000 These options take effect at the point on the command line where they
3001 are specified.  Therefore it's not useful to specify @samp{-nowarn} at
3002 the end of the command line.  The warning messages affected by the
3003 above options are triggered by:
3005 @itemize @minus
3006 @item
3007 Use of the @samp{-d} option which is deprecated; please use
3008 @samp{-depth} instead, since the latter is POSIX-compliant.
3009 @item
3010 Use of the @samp{-ipath} option which is deprecated; please use
3011 @samp{-iwholename} instead.
3012 @item
3013 Specifying an option (for example @samp{-mindepth}) after a non-option
3014 (for example @samp{-type} or @samp{-print}) on the command line.
3015 @item
3016 Use of the @samp{-name} or @samp{-iname} option with a slash character
3017 in the pattern.  Since the name predicates only compare against the
3018 basename of the visited files, the only file that can match a slash is
3019 the root directory itself.
3020 @end itemize
3022 The default behaviour above is designed to work in that way so that
3023 existing shell scripts don't generate spurious errors, but people will
3024 be made aware of the problem.
3026 Some warning messages are issued for less common or more serious
3027 problems, and consequently cannot be turned off:
3029 @itemize @minus
3030 @item
3031 Use of an unrecognised backslash escape sequence with @samp{-fprintf}
3032 @item
3033 Use of an unrecognised formatting directive with @samp{-fprintf}
3034 @end itemize
3036 @node Optimisation Options
3037 @subsection Optimisation Options
3039 The @samp{-O@var{level}} option sets @code{find}'s optimisation level
3040 to @var{level}.  The default optimisation level is 1.
3042 At certain optimisation levels, @code{find} reorders tests to speed up
3043 execution while preserving the overall effect; that is, predicates
3044 with side effects are not reordered relative to each other.  The
3045 optimisations performed at each optimisation level are as follows.
3047 @table @samp
3048 @item 0
3049 Currently equivalent to optimisation level 1.
3051 @item 1
3052 This is the default optimisation level and corresponds to the
3053 traditional behaviour.  Expressions are reordered so that tests based
3054 only on the names of files (for example@samp{ -name} and
3055 @samp{-regex}) are performed first.
3057 @item 2 
3058 Any @samp{-type} or @samp{-xtype} tests are performed after any tests
3059 based only on the names of files, but before any tests that require
3060 information from the inode.  On many modern versions of Unix, file
3061 types are returned by @code{readdir()} and so these predicates are
3062 faster to evaluate than predicates which need to stat the file first.
3064 @item 3 
3065 At this optimisation level, the full cost-based query optimiser is
3066 enabled.  The order of tests is modified so that cheap (i.e., fast)
3067 tests are performed first and more expensive ones are performed later,
3068 if necessary.  Within each cost band, predicates are evaluated earlier
3069 or later according to whether they are likely to succeed or not.  For
3070 @samp{-o}, predicates which are likely to succeed are evaluated
3071 earlier, and for @samp{-a}, predicates which are likely to fail are
3072 evaluated earlier.
3073 @end table
3076 @node Debug Options
3077 @subsection Debug Options
3079 The @samp{-D} option makes @code{find} produce diagnostic output.
3080 Much of the information is useful only for diagnosing problems, and so
3081 most people will not find this option helpful.
3083 The list of debug options should be comma separated.  Compatibility of
3084 the debug options is not guaranteed between releases of findutils.
3085 For a complete list of valid debug options, see the output of
3086 @code{find -D help}.  Valid debug options include:
3087 @table @samp
3088 @item help
3089 Explain the debugging options.
3090 @item tree   
3091 Show the expression tree in its original and optimised form.
3092 @item stat   
3093 Print messages as files are examined with the stat and lstat system
3094 calls.  The find program tries to minimise such calls.
3095 @item opt    
3096 Prints diagnostic information relating to the optimisation of the
3097 expression tree; see the @samp{-O} option.
3098 @item rates
3099 Prints a summary indicating how often each predicate succeeded or
3100 failed.
3101 @end table
3103 @node Find Expressions
3104 @subsection Find Expressions
3106 The final part of the @code{find} command line is a list of
3107 expressions.  @xref{Primary Index}, for a summary of all of the tests,
3108 actions, and options that the expression can contain.  If the
3109 expression is missing, @samp{-print} is assumed.
3111 @node Invoking locate
3112 @section Invoking @code{locate}
3114 @example
3115 locate @r{[}@var{option}@dots{}@r{]} @var{pattern}@dots{}
3116 @end example
3118 For each @var{pattern} given @code{locate} searches one or more file
3119 name databases returning each match of @var{pattern}.
3121 For each @var{pattern} given @code{locate} searches one or more file
3122 name databases returning each match of @var{pattern}.
3124 @table @code
3125 @item --all
3126 @itemx -A
3127 Print only names which match all non-option arguments, not those
3128 matching one or more non-option arguments.
3130 @item --basename
3131 @itemx -b
3132 The specified pattern is matched against just the last component of
3133 the name of a file in the @code{locate} database.  This last
3134 component is also called the ``base name''.  For example, the base
3135 name of @file{/tmp/mystuff/foo.old.c} is @file{foo.old.c}.  If the
3136 pattern contains metacharacters, it must match the base name exactly.
3137 If not, it must match part of the base name.
3139 @item --count
3140 @itemx -c
3141 Instead of printing the matched file names, just print the total
3142 number of matches found, unless @samp{--print} (@samp{-p}) is also
3143 present.
3146 @item --database=@var{path}
3147 @itemx -d @var{path}
3148 Instead of searching the default @code{locate} database
3149 @file{@value{LOCATE_DB}}, @code{locate} searches the file
3150 name databases in @var{path}, which is a colon-separated list of
3151 database file names.  You can also use the environment variable
3152 @code{LOCATE_PATH} to set the list of database files to search.  The
3153 option overrides the environment variable if both are used.  Empty
3154 elements in @var{path} (that is, a leading or trailing colon, or two
3155 colons in a row) are taken to stand for the default database.
3156 A database can be supplied on stdin, using @samp{-} as an element
3157 of @samp{path}. If more than one element of @samp{path} is @samp{-},
3158 later instances are ignored (but a warning message is printed).
3160 @item --existing
3161 @itemx -e
3162 Only print out such names which currently exist (instead of such names
3163 which existed when the database was created).  Note that this may slow
3164 down the program a lot, if there are many matches in the database.
3165 The way in which broken symbolic links are treated is affected by the
3166 @samp{-L}, @samp{-P} and @samp{-H} options.  Please note that it is
3167 possible for the file to be deleted after @code{locate} has checked
3168 that it exists, but before you use it.  This option is automatically
3169 turned on when reading an @code{slocate} database in secure mode
3170 (@pxref{slocate Database Format}).
3172 @item --non-existing
3173 @itemx -E
3174 Only print out such names which currently do not exist (instead of
3175 such names which existed when the database was created).  Note that
3176 this may slow down the program a lot, if there are many matches in the
3177 database.  The way in which broken symbolic links are treated is
3178 affected by the @samp{-L}, @samp{-P} and @samp{-H} options.  Please
3179 note that @code{locate} checks that the file does not exist, but a
3180 file of the same name might be created after @code{locate}'s check but
3181 before you read @code{locate}'s output.
3183 @item --follow
3184 @itemx -L
3185 If testing for the existence of files (with the @samp{-e} or @samp{-E}
3186 options), consider broken symbolic links to be non-existing.  This is
3187 the default behaviour.  
3189 @item --nofollow
3190 @itemx -P
3191 @itemx -H
3192 If testing for the existence of files (with the @samp{-e} or @samp{-E}
3193 options), treat broken symbolic links as if they were existing files.
3194 The @samp{-H} form of this option is provided purely for similarity
3195 with @code{find}; the use of @samp{-P} is recommended over @samp{-H}.
3197 @item --ignore-case
3198 @itemx -i
3199 Ignore case distinctions in both the pattern and the file names.
3201 @item --limit=N
3202 @itemx -l N
3203 Limit the number of results printed to N.  When used with the
3204 @samp{--count} option, the value printed will never be larger than
3205 this limit.
3206 @item --max-database-age=D
3207 Normally, @code{locate} will issue a warning message when it searches
3208 a database which is more than 8 days old.  This option changes that
3209 value to something other than 8.  The effect of specifying a negative
3210 value is undefined.
3211 @item --mmap
3212 @itemx -m
3213 Accepted but does nothing.  The option is supported only to provide
3214 compatibility with BSD's @code{locate}.
3216 @item --null
3217 @itemx -0
3218 Results are separated with the ASCII NUL character rather than the
3219 newline character.  To get the full benefit of the use of this option,
3220 use the new @code{locate} database format (that is the default
3221 anyway).
3223 @item --print
3224 @itemx -p
3225 Print search results when they normally would not, because of the
3226 presence of @samp{--statistics} (@samp{-S}) or @samp{--count}
3227 (@samp{-c}).
3229 @item --wholename
3230 @itemx -w
3231 The specified pattern is matched against the whole name of the file in
3232 the @code{locate} database.  If the pattern contains metacharacters,
3233 it must match exactly.  If not, it must match part of the whole file
3234 name.  This is the default behaviour.
3236 @item --regex
3237 @itemx -r
3238 Instead of using substring or shell glob matching, the pattern
3239 specified on the command line is understood to be a regular
3240 expression.  GNU Emacs-style regular expressions are assumed unless
3241 the @samp{--regextype} option is also given.  File names from the
3242 @code{locate} database are matched using the specified regular
3243 expression.  If the @samp{-i} flag is also given, matching is
3244 case-insensitive.  Matches are performed against the whole path name,
3245 and so by default a pathname will be matched if any part of it matches
3246 the specified regular expression.  The regular expression may use
3247 @samp{^} or @samp{$} to anchor a match at the beginning or end of a
3248 pathname.
3250 @item --regextype
3251 This option changes the regular expression syntax and behaviour used
3252 by the @samp{--regex} option.  @ref{Regular Expressions} for more
3253 information on the regular expression dialects understood by GNU
3254 findutils.
3256 @item --stdio
3257 @itemx -s
3258 Accepted but does nothing.  The option is supported only to provide
3259 compatibility with BSD's @code{locate}.
3261 @item --statistics
3262 @itemx -S
3263 Print some summary information for each @code{locate} database.  No
3264 search is performed unless non-option arguments are given.
3265 Although the BSD version of locate also has this option, the format of the
3266 output is different.
3268 @item --help
3269 Print a summary of the command line usage for @code{locate} and exit.
3271 @item --version
3272 Print the version number of @code{locate} and exit.
3273 @end table
3275 @node Invoking updatedb
3276 @section Invoking @code{updatedb}
3278 @example
3279 updatedb @r{[}@var{option}@dots{}@r{]}
3280 @end example
3282 @code{updatedb} creates and updates the database of file names used by
3283 @code{locate}.  @code{updatedb} generates a list of files similar to
3284 the output of @code{find} and then uses utilities for optimizing the
3285 database for performance.  @code{updatedb} is often run periodically
3286 as a @code{cron} job and configured with environment variables or
3287 command options.  Typically, operating systems have a shell script
3288 that ``exports'' configurations for variable definitions and uses
3289 another shell script that ``sources'' the configuration file into the
3290 environment and then executes @code{updatedb} in the environment.
3292 @code{updatedb} creates and updates the database of file names used by
3293 @code{locate}.  @code{updatedb} generates a list of files similar to
3294 the output of @code{find} and then uses utilities for optimizing the
3295 database for performance.  @code{updatedb} is often run periodically
3296 as a @code{cron} job and configured with environment variables or
3297 command options.  Typically, operating systems have a shell script
3298 that ``exports'' configurations for variable definitions and uses
3299 another shell script that ``sources'' the configuration file into the
3300 environment and then executes @code{updatedb} in the environment.
3302 @table @code
3303 @item --findoptions='@var{OPTION}@dots{}'
3304 Global options to pass on to @code{find}.
3305 The environment variable @code{FINDOPTIONS} also sets this value.
3306 Default is none.
3308 @item --localpaths='@var{path}@dots{}'
3309 Non-network directories to put in the database.
3310 Default is @file{/}.
3312 @item --netpaths='@var{path}@dots{}'
3313 Network (NFS, AFS, RFS, etc.) directories to put in the database.
3314 The environment variable @code{NETPATHS} also sets this value.
3315 Default is none.
3317 @item --prunepaths='@var{path}@dots{}'
3318 Directories to omit from the database, which would otherwise be
3319 included.  The environment variable @code{PRUNEPATHS} also sets this
3320 value.  Default is @file{/tmp /usr/tmp /var/tmp /afs}.  The paths are
3321 used as regular expressions (with @code{find ... -regex}, so you need
3322 to specify these paths in the same way that @code{find} will encounter
3323 them.  This means for example that the paths must not include trailing
3324 slashes.
3326 @item --prunefs='@var{path}@dots{}'
3327 Filesystems to omit from the database, which would otherwise be
3328 included.  Note that files are pruned when a filesystem is reached;
3329 Any filesystem mounted under an undesired filesystem will be ignored.
3330 The environment variable @code{PRUNEFS} also sets this value.  Default
3331 is @file{nfs NFS proc}.
3333 @item --output=@var{dbfile}
3334 The database file to build.  The default is system-dependent, but
3335 when this document was formatted it was @file{@value{LOCATE_DB}}.
3337 @item --localuser=@var{user}
3338 The user to search the non-network directories as, using @code{su}.
3339 Default is to search the non-network directories as the current user.
3340 You can also use the environment variable @code{LOCALUSER} to set this user.
3342 @item --netuser=@var{user}
3343 The user to search network directories as, using @code{su}.  Default
3344 @code{user} is @code{daemon}.  You can also use the environment variable
3345 @code{NETUSER} to set this user.
3347 @item --old-format
3348 Generate a @code{locate} database in the old format, for compatibility
3349 with versions of @code{locate} other than GNU @code{locate}.  Using
3350 this option means that @code{locate} will not be able to properly
3351 handle non-ASCII characters in file names (that is, file names
3352 containing characters which have the eighth bit set, such as many of
3353 the characters from the ISO-8859-1 character set).  @xref{Database
3354 Formats}, for a detailed description of the supported database
3355 formats.
3357 @item --dbformat=@var{FORMAT}
3358 Generate the locate database in format @code{FORMAT}.  Supported
3359 database formats include @code{LOCATE02} (which is the default),
3360 @code{old} and @code{slocate}.  The @code{old} format exists for
3361 compatibility with implementations of @code{locate} on other Unix
3362 systems.  The @code{slocate} format exists for compatibility with
3363 @code{slocate}.  @xref{Database Formats}, for a detailed description
3364 of each format.
3366 @item --help
3367 Print a summary of the command line usage and exit.
3368 @item --version
3369 Print the version number of @code{updatedb} and exit.
3370 @end table
3372 @node Invoking xargs
3373 @section Invoking @code{xargs}
3375 @example
3376 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
3377 @end example
3379 @code{xargs} exits with the following status:
3381 @table @asis
3382 @item 0
3383 if it succeeds
3384 @item 123
3385 if any invocation of the command exited with status 1-125
3386 @item 124
3387 if the command exited with status 255
3388 @item 125
3389 if the command is killed by a signal
3390 @item 126
3391 if the command cannot be run
3392 @item 127
3393 if the command is not found
3394 @item 1
3395 if some other error occurred.
3396 @end table
3398 Exit codes greater than 128 are used by the shell to indicate that 
3399 a program died due to a fatal signal.
3400   
3401 @table @code
3402 @item --arg-file@r{=@var{inputfile}}
3403 @itemx -a @r{@var{inputfile}}
3404 Read names from the file @var{inputfile} instead of standard input.
3405 If you use this option, the standard input stream remains unchanged
3406 when commands are run. Otherwise, stdin is redirected from
3407 @file{/dev/null}.
3409 @item --null
3410 @itemx -0
3411 Input file names are terminated by a null character instead of by
3412 whitespace, and any quotes and backslash characters are not considered
3413 special (every character is taken literally).  Disables the end of
3414 file string, which is treated like any other argument.
3416 @item --delimiter @var{delim}
3417 @itemx -d @var{delim}
3419 Input file names are terminated by the specified character @var{delim}
3420 instead of by whitespace, and any quotes and backslash characters are
3421 not considered special (every character is taken literally).  Disables
3422 the end of file string, which is treated like any other argument.
3424 The specified delimiter may be a single character, a C-style character
3425 escape such as @samp{\n}, or an octal or hexadecimal escape code.
3426 Octal and hexadecimal escape codes are understood as for the
3427 @code{printf} command.  Multibyte characters are not supported.
3430 @item -E @var{eof-str}
3431 @itemx --eof@r{[}=@var{eof-str}@r{]}
3432 @itemx -e@r{[}@var{eof-str}@r{]}
3433 Set the end of file string to @var{eof-str}.  If the end of file
3434 string occurs as a line of input, the rest of the input is ignored.
3435 If @var{eof-str} is omitted (@samp{-e}) or blank (either @samp{-e} or
3436 @samp{-E}), there is no end of file string.  The @samp{-e} form of
3437 this option is deprecated in favour of the POSIX-compliant @samp{-E}
3438 option, which you should use instead.  As of GNU xargs version 4.2.9,
3439 the default behaviour of xargs is not to have a logical end-of-file
3440 marker.  The POSIX standard (IEEE Std 1003.1, 2004 Edition) allows
3441 this.
3443 @item --help
3444 Print a summary of the options to @code{xargs} and exit.
3446 @item -I @var{replace-str}
3447 @itemx --replace@r{[}=@var{replace-str}@r{]}
3448 @itemx -i@r{[}@var{replace-str}@r{]}
3449 Replace occurrences of @var{replace-str} in the initial arguments with
3450 names read from standard input.  Also, unquoted blanks do not
3451 terminate arguments; instead, the input is split at newlines only.  If
3452 @var{replace-str} is omitted (omitting it is allowed only for
3453 @samp{-i}), it defaults to @samp{@{@}} (like for @samp{find -exec}).
3454 Implies @samp{-x} and @samp{-l 1}.  The @samp{-i} option is deprecated
3455 in favour of the @samp{-I} option.
3457 @item -L @var{max-lines}
3458 @itemx --max-lines@r{[}=@var{max-lines}@r{]}
3459 @itemx -l@r{[}@var{max-lines}@r{]}
3460 Use at most @var{max-lines} non-blank input lines per command line.
3461 For @samp{-l}, @var{max-lines} defaults to 1 if omitted.  For
3462 @samp{-L}, the argument is mandatory.  Trailing blanks cause an input
3463 line to be logically continued on the next input line, for the purpose
3464 of counting the lines.  Implies @samp{-x}.  The @samp{-l} form of this
3465 option is deprecated in favour of the POSIX-compliant @samp{-L}
3466 option.
3468 @item --max-args=@var{max-args}
3469 @itemx -n @var{max-args}
3470 Use at most @var{max-args} arguments per command line.  Fewer than
3471 @var{max-args} arguments will be used if the size (see the @samp{-s}
3472 option) is exceeded, unless the @samp{-x} option is given, in which
3473 case @code{xargs} will exit.
3475 @item --interactive
3476 @itemx -p
3477 Prompt the user about whether to run each command line and read a line
3478 from the terminal.  Only run the command line if the response starts
3479 with @samp{y} or @samp{Y}.  Implies @samp{-t}.
3481 @item --no-run-if-empty
3482 @itemx -r
3483 If the standard input is completely empty, do not run the
3484 command.  By default, the command is run once even if there is no
3485 input.
3487 @item --max-chars=@var{max-chars}
3488 @itemx -s @var{max-chars}
3489 Use at most @var{max-chars} characters per command line, including the
3490 command, initial arguments and any terminating nulls at the ends of
3491 the argument strings.
3493 @item --show-limits
3494 Display the limits on the command-line length which are imposed by the
3495 operating system, @code{xargs}' choice of buffer size and the
3496 @samp{-s} option.  Pipe the input from @file{/dev/null} (and perhaps
3497 specify @samp{--no-run-if-empty}) if you don't want @code{xargs} to do
3498 anything.
3500 @item --verbose
3501 @itemx -t
3502 Print the command line on the standard error output before executing
3505 @item --version
3506 Print the version number of @code{xargs} and exit.
3508 @item --exit
3509 @itemx -x
3510 Exit if the size (see the @samp{-s} option) is exceeded.
3513 @item --max-procs=@var{max-procs}
3514 @itemx -P @var{max-procs}
3515 Run simultaneously up to @var{max-procs} processes at once; the default is 1.  If
3516 @var{max-procs} is 0, @code{xargs} will run as many processes as
3517 possible simultaneously.
3518 @end table
3521 @node Regular Expressions
3522 @section Regular Expressions
3524 The @samp{-regex} and @samp{-iregex} tests of @code{find} allow
3525 matching by regular expression, as does the @samp{--regex} option of
3526 @code{locate}.  There are many different types of Regular Expression,
3527 but the type used by @code{find} and @code{locate} is the same as is
3528 used in GNU Emacs.  Both programs provide an option which allows you
3529 to select an alternative regular expression syntax; for @code{find}
3530 this is the @samp{-regextype} option, and for @code{locate} this is
3531 the @samp{--regextype} option.
3533 These options take a single argument, which indicates the specific
3534 regular expression syntax and behaviour that should be used.  This
3535 should be one of the following:
3537 @include regexprops.texi
3539 @node Environment Variables
3540 @section Environment Variables
3541 @table @var
3542 @item LANG
3543 Provides a default value for the internationalisation variables that
3544 are unset or null.
3545 @item LC_ALL
3546 If set to a non-empty string value, override the values of all the
3547 other internationalisation variables.
3548 @item LC_COLLATE
3549 The POSIX standard specifies that this variable affects the pattern
3550 matching to be used for the `\-name' option.  GNU find uses the
3551 GNU version of the @code{fnmatch} library function.
3553 POSIX also specifies that the `LC_COLLATE' environment 
3554 variable affects the interpretation of the user's response to the 
3555 query issued by `\-ok', but this is not the case for GNU find.
3556 @item LC_CTYPE 
3557 This variable affects the treatment of character classes used with 
3558 the @samp{-name} test, if the system's 
3559 @code{fnmatch} library function supports this.   It has no effect on the behaviour 
3560 of the @samp{-ok} expression.
3561 @item LC_MESSAGES
3562 Determines the locale to be used for internationalised messages.
3563 @item NLSPATH
3564 Determines the location of the internationalisation message catalogues.
3565 @item PATH
3566 Affects the directories which are searched to find the executables
3567 invoked by @samp{-exec}, @samp{-execdir} @samp{-ok} and @samp{-okdir}.
3568 If the @var{PATH} environment variable includes the current directory
3569 (by explicitly including @samp{.} or by having an empty element), and
3570 the find command line includes @samp{-execdir} or @samp{-okdir},
3571 @code{find} will refuse to run.  @xref{Security Considerations}, for a
3572 more detailed discussion of security matters.
3574 @item POSIXLY_CORRECT 
3575 Determines the block size used by @samp{-ls} and @samp{-fls}.  
3576 If @var{POSIXLY_CORRECT} is set, blocks are units of 512 bytes.  Otherwise
3577 they are units of 1024 bytes.
3579 Setting this variable also turns off warning messages (that is, implies
3580 @samp{-nowarn}) by default, because POSIX requires that apart from
3581 the output for @samp{-ok}, all messages printed on stderr are
3582 diagnositcs and must result in a non-zero exit status.
3584 Arguments to @samp{-perm} beginning with @samp{+} are treated
3585 differently when POSIXLY_CORRECT is set. See 
3586 @ref{Mode Bits,-perm,File Mode Bits}.
3588 @item TZ 
3589 Affects the time zone used for some of the time-related format
3590 directives of @samp{-printf} and @samp{-fprintf}.
3591 @end table
3595 @node Common Tasks
3596 @chapter Common Tasks
3598 The sections that follow contain some extended examples that both give
3599 a good idea of the power of these programs, and show you how to solve
3600 common real-world problems.
3602 @menu
3603 * Viewing And Editing::
3604 * Archiving::
3605 * Cleaning Up::
3606 * Strange File Names::
3607 * Fixing Permissions::
3608 * Classifying Files::
3609 @end menu
3611 @node Viewing And Editing
3612 @section Viewing And Editing
3614 To view a list of files that meet certain criteria, simply run your
3615 file viewing program with the file names as arguments.  Shells
3616 substitute a command enclosed in backquotes with its output, so the
3617 whole command looks like this:
3619 @example
3620 less `find /usr/include -name '*.h' | xargs grep -l mode_t`
3621 @end example
3623 @noindent
3624 You can edit those files by giving an editor name instead of a file
3625 viewing program:
3627 @example
3628 emacs `find /usr/include -name '*.h' | xargs grep -l mode_t`
3629 @end example
3631 Because there is a limit to the length of any individual command line,
3632 there is a limit to the number of files that can be handled in this
3633 way.  We can get around this difficulty by using xargs like this:
3635 @example
3636 find /usr/include -name '*.h' | xargs grep -l mode_t > todo
3637 xargs --arg-file=todo emacs
3638 @end example
3640 Here, @code{xargs} will run @code{emacs} as many times as necessary to
3641 visit all of the files listed in the file @file{todo}.
3643 @node Archiving
3644 @section Archiving
3646 You can pass a list of files produced by @code{find} to a file
3647 archiving program.  GNU @code{tar} and @code{cpio} can both read lists
3648 of file names from the standard input---either delimited by nulls (the
3649 safe way) or by blanks (the lazy, risky default way).  To use
3650 null-delimited names, give them the @samp{--null} option.  You can
3651 store a file archive in a file, write it on a tape, or send it over a
3652 network to extract on another machine.
3654 One common use of @code{find} to archive files is to send a list of
3655 the files in a directory tree to @code{cpio}.  Use @samp{-depth} so if
3656 a directory does not have write permission for its owner, its contents
3657 can still be restored from the archive since the directory's
3658 permissions are restored after its contents.  Here is an example of
3659 doing this using @code{cpio}; you could use a more complex @code{find}
3660 expression to archive only certain files.
3662 @example
3663 find . -depth -print0 |
3664   cpio --create --null --format=crc --file=/dev/nrst0
3665 @end example
3667 You could restore that archive using this command:
3669 @example
3670 cpio --extract --null --make-dir --unconditional \
3671   --preserve --file=/dev/nrst0
3672 @end example
3674 Here are the commands to do the same things using @code{tar}:
3676 @example
3677 find . -depth -print0 |
3678   tar --create --null --files-from=- --file=/dev/nrst0
3680 tar --extract --null --preserve-perm --same-owner \
3681   --file=/dev/nrst0
3682 @end example
3684 @c Idea from Rick Sladkey.
3685 Here is an example of copying a directory from one machine to another:
3687 @example
3688 find . -depth -print0 | cpio -0o -Hnewc |
3689   rsh @var{other-machine} "cd `pwd` && cpio -i0dum"
3690 @end example
3692 @node Cleaning Up
3693 @section Cleaning Up
3695 @c Idea from Jim Meyering.
3696 This section gives examples of removing unwanted files in various
3697 situations.  Here is a command to remove the CVS backup files created
3698 when an update requires a merge:
3700 @example
3701 find . -name '.#*' -print0 | xargs -0r rm -f
3702 @end example
3704 The command above works, but the following is safer:
3706 @example
3707 find . -name '.#*' -depth -delete
3708 @end example
3710 @c Idea from Franc,ois Pinard.
3711 You can run this command to clean out your clutter in @file{/tmp}.
3712 You might place it in the file your shell runs when you log out
3713 (@file{.bash_logout}, @file{.logout}, or @file{.zlogout}, depending on
3714 which shell you use).
3716 @example
3717 find /tmp -depth -user "$LOGNAME" -type f -delete
3718 @end example
3720 If your @code{find} command removes directories, you may find that
3721 you get a spurious error message when @code{find} tries to recurse
3722 into a directory that has now been removed.  Using the @samp{-depth}
3723 option will normally resolve this problem.
3725 @c Idea from Noah Friedman.
3726 To remove old Emacs backup and auto-save files, you can use a command
3727 like the following.  It is especially important in this case to use
3728 null-terminated file names because Emacs packages like the VM mailer
3729 often create temporary file names with spaces in them, like
3730 @file{#reply to David J. MacKenzie<1>#}.
3732 @example
3733 find ~ \( -name '*~' -o -name '#*#' \) -print0 |
3734   xargs --no-run-if-empty --null rm -vf
3735 @end example
3737 Removing old files from @file{/tmp} is commonly done from @code{cron}:
3739 @c Idea from Kaveh Ghazi.
3740 @example
3741 find /tmp /var/tmp -depth -not        -type d -mtime +3 -delete
3742 find /tmp /var/tmp -depth -mindepth 1 -type d -empty    -delete
3743 @end example
3745 The second @code{find} command above cleans out empty directories
3746 depth-first (@samp{-delete} implies @samp{-depth} anyway), hoping that
3747 the parents become empty and can be removed too.  It uses
3748 @samp{-mindepth} to avoid removing @file{/tmp} itself if it becomes
3749 totally empty.
3752 Lastly, an example of a program that almost certainly does not do what
3753 the user intended:
3755 @c inspired by Savannah bug #20865 (Bruno De Fraine)
3756 @example
3757 find dirname -delete -name quux
3758 @end example
3760 If the user hoped to delete only files named @file{quux} they will get
3761 an unpleasant surprise; this command will attempt to delete everything
3762 at or below the starting point @file{dirname}.  This is because
3763 @code{find} evaluates the items on the command line as an expression.
3764 The @code{find} program will normally execute an action if the
3765 preceeding action succeeds.  Here, there is no action or test before
3766 the @samp{-delete} so it will always be executed.  The @samp{-name
3767 quux} test will be performed for files we successfully deleted, but
3768 that test has no effect since @samp{-delete} also disables the default
3769 @samp{-print} operation.   So the above example will probably delete a
3770 lot of files the user didn't want to delete.
3772 This command is also likely to do something you did not intend:
3773 @example
3774 find dirname -path dirname/foo -prune -o -delete
3775 @end example
3777 Because @samp{-delete} turns on @samp{-depth}, the @samp{-prune}
3778 action has no effect and files in @file{dirname/foo} will be deleted
3779 too.
3782 @node Strange File Names
3783 @section Strange File Names
3785 @c Idea from:
3786 @c From: tmatimar@isgtec.com (Ted Timar)
3787 @c Newsgroups: comp.unix.questions,comp.unix.shell,comp.answers,news.answers
3788 @c Subject: Unix - Frequently Asked Questions (2/7) [Frequent posting]
3789 @c Subject: How do I remove a file with funny characters in the filename ?
3790 @c Date: Thu Mar 18 17:16:55 EST 1993
3791 @code{find} can help you remove or rename a file with strange
3792 characters in its name.  People are sometimes stymied by files whose
3793 names contain characters such as spaces, tabs, control characters, or
3794 characters with the high bit set.  The simplest way to remove such
3795 files is:
3797 @example
3798 rm -i @var{some*pattern*that*matches*the*problem*file}
3799 @end example
3801 @code{rm} asks you whether to remove each file matching the given
3802 pattern.  If you are using an old shell, this approach might not work
3803 if the file name contains a character with the high bit set; the shell
3804 may strip it off.  A more reliable way is:
3806 @example
3807 find . -maxdepth 1 @var{tests} -okdir rm '@{@}' \;
3808 @end example
3810 @noindent
3811 where @var{tests} uniquely identify the file.  The @samp{-maxdepth 1}
3812 option prevents @code{find} from wasting time searching for the file
3813 in any subdirectories; if there are no subdirectories, you may omit
3814 it.  A good way to uniquely identify the problem file is to figure out
3815 its inode number; use
3817 @example
3818 ls -i
3819 @end example
3821 Suppose you have a file whose name contains control characters, and
3822 you have found that its inode number is 12345.  This command prompts
3823 you for whether to remove it:
3825 @example
3826 find . -maxdepth 1 -inum 12345 -okdir rm -f '@{@}' \;
3827 @end example
3829 If you don't want to be asked, perhaps because the file name may
3830 contain a strange character sequence that will mess up your screen
3831 when printed, then use @samp{-execdir} instead of @samp{-okdir}.
3833 If you want to rename the file instead, you can use @code{mv} instead
3834 of @code{rm}:
3836 @example
3837 find . -maxdepth 1 -inum 12345 -okdir mv '@{@}' @var{new-file-name} \;
3838 @end example
3840 @node Fixing Permissions
3841 @section Fixing Permissions
3843 Suppose you want to make sure that everyone can write to the
3844 directories in a certain directory tree.  Here is a way to find
3845 directories lacking either user or group write permission (or both),
3846 and fix their permissions:
3848 @example
3849 find . -type d -not -perm -ug=w | xargs chmod ug+w
3850 @end example
3852 @noindent
3853 You could also reverse the operations, if you want to make sure that
3854 directories do @emph{not} have world write permission.
3856 @node Classifying Files
3857 @section Classifying Files
3859 @c Idea from:
3860 @c From: martin@mwtech.UUCP (Martin Weitzel)
3861 @c Newsgroups: comp.unix.wizards,comp.unix.questions
3862 @c Subject: Advanced usage of 'find' (Re: Unix security automating script)
3863 @c Date: 22 Mar 90 15:05:19 GMT
3864 If you want to classify a set of files into several groups based on
3865 different criteria, you can use the comma operator to perform multiple
3866 independent tests on the files.  Here is an example:
3868 @example
3869 find / -type d \( -perm -o=w -fprint allwrite , \
3870   -perm -o=x -fprint allexec \)
3872 echo "Directories that can be written to by everyone:"
3873 cat allwrite
3874 echo ""
3875 echo "Directories with search permissions for everyone:"
3876 cat allexec
3877 @end example
3879 @code{find} has only to make one scan through the directory tree
3880 (which is one of the most time consuming parts of its work).
3882 @node Worked Examples
3883 @chapter Worked Examples
3885 The tools in the findutils package, and in particular @code{find},
3886 have a large number of options.  This means that quite often,
3887 there is more than one way to do things.  Some of the options
3888 and facilities only exist for compatibility with other tools, and
3889 findutils provides improved ways of doing things.
3891 This chapter describes a number of useful tasks that are commonly
3892 performed, and compares the different ways of achieving them.
3894 @menu
3895 * Deleting Files::
3896 * Copying A Subset of Files::
3897 * Updating A Timestamp File::
3898 @end menu
3900 @node Deleting Files
3901 @section Deleting Files
3903 One of the most common tasks that @code{find} is used for is locating
3904 files that can be deleted.  This might include:
3906 @itemize
3907 @item 
3908 Files last modified more than 3 years ago which haven't been accessed
3909 for at least 2 years
3910 @item
3911 Files belonging to a certain user
3912 @item
3913 Temporary files which are no longer required
3914 @end itemize
3916 This example concentrates on the actual deletion task rather than on
3917 sophisticated ways of locating the files that need to be deleted.
3918 We'll assume that the files we want to delete are old files underneath
3919 @file{/var/tmp/stuff}.
3921 @subsection The Traditional Way
3923 The traditional way to delete files in @file{/var/tmp/stuff} that have
3924 not been modified in over 90 days would have been:
3926 @smallexample
3927 find /var/tmp/stuff -mtime +90 -exec /bin/rm @{@} \;
3928 @end smallexample
3930 The above command uses @samp{-exec} to run the @code{/bin/rm} command
3931 to remove each file.  This approach works and in fact would have
3932 worked in Version 7 Unix in 1979.  However, there are a number of
3933 problems with this approach.
3936 The most obvious problem with the approach above is that it causes
3937 @code{find} to fork every time it finds a file that needs to delete,
3938 and the child process then has to use the @code{exec} system call to
3939 launch @code{/bin/rm}.   All this is quite inefficient.  If we are
3940 going to use @code{/bin/rm} to do this job, it is better to make it
3941 delete more than one file at a time.  
3943 The most obvious way of doing this is to use the shell's command
3944 expansion feature:
3946 @smallexample
3947 /bin/rm `find /var/tmp/stuff -mtime +90 -print`
3948 @end smallexample
3949 or you could use the more modern form
3950 @smallexample
3951 /bin/rm $(find /var/tmp/stuff -mtime +90 -print)
3952 @end smallexample
3954 The commands above are much more efficient than the first attempt.
3955 However, there is a problem with them.  The shell has a maximum
3956 command length which is imposed by the operating system (the actual
3957 limit varies between systems).  This means that while the command
3958 expansion technique will usually work, it will suddenly fail when
3959 there are lots of files to delete.  Since the task is to delete
3960 unwanted files, this is precisely the time we don't want things to go
3961 wrong.
3963 @subsection Making Use of xargs
3965 So, is there a way to be more efficient in the use of @code{fork()}
3966 and @code{exec()} without running up against this limit?
3967 Yes, we can be almost optimally efficient by making use
3968 of the @code{xargs} command.  The @code{xargs} command reads arguments
3969 from its standard input and builds them into command lines.  We can
3970 use it like this:
3972 @smallexample
3973 find /var/tmp/stuff -mtime +90 -print | xargs /bin/rm 
3974 @end smallexample
3976 For example if the files found by @code{find} are
3977 @file{/var/tmp/stuff/A}, 
3978 @file{/var/tmp/stuff/B} and 
3979 @file{/var/tmp/stuff/C} then @code{xargs} might issue the commands 
3981 @smallexample
3982 /bin/rm /var/tmp/stuff/A /var/tmp/stuff/B
3983 /bin/rm /var/tmp/stuff/C
3984 @end smallexample
3986 The above assumes that @code{xargs} has a very small maximum command
3987 line length.  The real limit is much larger but the idea is that
3988 @code{xargs} will run @code{/bin/rm} as many times as necessary to get
3989 the job done, given the limits on command line length.
3991 This usage of @code{xargs} is pretty efficient, and the @code{xargs}
3992 command is widely implemented (all modern versions of Unix offer it).
3993 So far then, the news is all good.  However, there is bad news too.
3995 @subsection Unusual characters in filenames
3997 Unix-like systems allow any characters to appear in file names with
3998 the exception of the ASCII NUL character and the backslash.
3999 Backslashes can occur in path names (as the directory separator) but
4000 not in the names of actual directory entries.  This means that the
4001 list of files that @code{xargs} reads could in fact contain white space
4002 characters --- spaces, tabs and newline characters.  Since by default,
4003 @code{xargs} assumes that the list of files it is reading uses white
4004 space as an argument separator, it cannot correctly handle the case
4005 where a filename actually includes white space.  This makes the
4006 default behaviour of @code{xargs} almost useless for handling
4007 arbitrary data.
4009 To solve this problem, GNU findutils introduced the @samp{-print0}
4010 action for @code{find}.  This uses the ASCII NUL character to separate
4011 the entries in the file list that it produces.  This is the ideal
4012 choice of separator since it is the only character that cannot appear
4013 within a path name.  The @samp{-0} option to @code{xargs} makes it
4014 assume that arguments are separated with ASCII NUL instead of white
4015 space.  It also turns off another misfeature in the default behaviour
4016 of @code{xargs}, which is that it pays attention to quote characters
4017 in its input.  Some versions of @code{xargs} also terminate when they
4018 see a lone @samp{_} in the input, but GNU @code{find} no longer does
4019 that (since it has become an optional behaviour in the Unix standard).
4021 So, putting @code{find -print0} together with @code{xargs -0} we get
4022 this command:
4024 @smallexample
4025 find /var/tmp/stuff -mtime +90 -print0 | xargs -0 /bin/rm 
4026 @end smallexample
4028 The result is an efficient way of proceeding that
4029 correctly handles all the possible characters that could appear in the
4030 list of files to delete.  This is good news.  However, there is, as
4031 I'm sure you're expecting, also more bad news.  The problem is that
4032 this is not a portable construct; although other versions of Unix
4033 (notably BSD-derived ones) support @samp{-print0}, it's not
4034 universal.  So, is there a more universal mechanism?
4036 @subsection Going back to -exec
4038 There is indeed a more universal mechanism, which is a slight
4039 modification to the @samp{-exec} action.  The normal @samp{-exec}
4040 action assumes that the command to run is terminated with a semicolon
4041 (the semicolon normally has to be quoted in order to protect it from
4042 interpretation as the shell command separator).  The SVR4 edition of
4043 Unix introduced a slight variation, which involves terminating the
4044 command with @samp{+} instead:
4046 @smallexample
4047 find /var/tmp/stuff -mtime +90 -exec /bin/rm @{@} \+
4048 @end smallexample
4050 The above use of @samp{-exec} causes @code{find} to build up a long
4051 command line and then issue it.  This can be less efficient than some
4052 uses of @code{xargs}; for example @code{xargs} allows new command
4053 lines to be built up while the previous command is still executing, and
4054 allows you to specify a number of commands to run in parallel.
4055 However, the @code{find @dots{} -exec @dots{} +} construct has the advantage
4056 of wide portability.  GNU findutils did not support @samp{-exec @dots{} +}
4057 until version 4.2.12; one of the reasons for this is that it already
4058 had the @samp{-print0} action in any case.
4061 @subsection A more secure version of -exec
4063 The command above seems to be efficient and portable.  However,
4064 within it lurks a security problem.  The problem is shared with
4065 all the commands we've tried in this worked example so far, too.  The
4066 security problem is a race condition; that is, if it is possible for
4067 somebody to manipulate the filesystem that you are searching while you
4068 are searching it, it is possible for them to persuade your @code{find}
4069 command to cause the deletion of a file that you can delete but they
4070 normally cannot.  
4072 The problem occurs because the @samp{-exec} action is defined by the
4073 @acronym{POSIX} standard to invoke its command with the same working directory
4074 as @code{find} had when it was started.  This means that the arguments
4075 which replace the @{@} include a relative path from @code{find}'s
4076 starting point down the file that needs to be deleted.  For example,
4078 @smallexample
4079 find /var/tmp/stuff -mtime +90 -exec /bin/rm @{@} \+
4080 @end smallexample
4082 might actually issue the command:
4084 @smallexample
4085 /bin/rm /var/tmp/stuff/A /var/tmp/stuff/B /var/tmp/stuff/passwd
4086 @end smallexample
4088 Notice the file @file{/var/tmp/stuff/passwd}.  Likewise, the command:
4090 @smallexample
4091 cd /var/tmp && find stuff -mtime +90 -exec /bin/rm @{@} \+
4092 @end smallexample
4094 might actually issue the command:
4096 @smallexample
4097 /bin/rm stuff/A stuff/B stuff/passwd
4098 @end smallexample
4100 If an attacker can rename @file{stuff} to something else (making use
4101 of their write permissions in @file{/var/tmp}) they can replace it
4102 with a symbolic link to @file{/etc}.  That means that the
4103 @code{/bin/rm} command will be invoked on @file{/etc/passwd}.  If you
4104 are running your @code{find} command as root, the attacker has just managed
4105 to delete a vital file.  All they needed to do to achieve this was
4106 replace a subdirectory with a symbolic link at the vital moment.
4108 There is however, a simple solution to the problem.  This is an action
4109 which works a lot like @code{-exec} but doesn't need to traverse a
4110 chain of directories to reach the file that it needs to work on.  This
4111 is the @samp{-execdir} action, which was introduced by the BSD family
4112 of operating systems.   The command,
4114 @smallexample
4115 find /var/tmp/stuff -mtime +90 -execdir /bin/rm @{@} \+
4116 @end smallexample
4118 might delete a set of files by performing these actions:
4120 @enumerate
4121 @item 
4122 Change directory to /var/tmp/stuff/foo
4123 @item 
4124 Invoke @code{/bin/rm ./file1 ./file2 ./file3}
4125 @item
4126 Change directory to /var/tmp/stuff/bar
4127 @item 
4128 Invoke @code{/bin/rm ./file99 ./file100 ./file101}
4129 @end enumerate
4131 This is a much more secure method.  We are no longer exposed to a race
4132 condition.  For many typical uses of @code{find}, this is the best
4133 strategy.   It's reasonably efficient, but the length of the command
4134 line is limited not just by the operating system limits, but also by
4135 how many files we actually need to delete from each directory.
4137 Is it possible to do any better?   In the case of general file
4138 processing, no.  However, in the specific case of deleting files it is
4139 indeed possible to do better.  
4141 @subsection Using the -delete action
4143 The most efficient and secure method of solving this problem is to use
4144 the @samp{-delete} action:
4146 @smallexample
4147 find /var/tmp/stuff -mtime +90 -delete
4148 @end smallexample
4150 This alternative is more efficient than any of the @samp{-exec} or
4151 @samp{-execdir} actions, since it entirely avoids the overhead of
4152 forking a new process and using @code{exec} to run @code{/bin/rm}.  It
4153 is also normally more efficient than @code{xargs} for the same
4154 reason.   The file deletion is performed from the directory containing
4155 the entry to be deleted, so the @samp{-delete} action has the same
4156 security advantages as the @samp{-execdir} action has.  
4158 The @samp{-delete} action was introduced by the BSD family of
4159 operating systems.
4161 @subsection Improving things still further
4163 Is it possible to improve things still further?  Not without either
4164 modifying the system library to the operating system or having more specific
4165 knowledge of the layout of the filesystem and disk I/O subsystem, or
4166 both.
4168 The @code{find} command traverses the filesystem, reading
4169 directories.  It then issues a separate system call for each file to
4170 be deleted.  If we could modify the operating system, there are
4171 potential gains that could be made:
4173 @itemize
4174 @item
4175 We could have a system call to which we pass more than one filename
4176 for deletion
4177 @item
4178 Alternatively, we could pass in a list of inode numbers (on GNU/Linux
4179 systems, @code{readdir()} also returns the inode number of each
4180 directory entry) to be deleted.
4181 @end itemize
4183 The above possibilities sound interesting, but from the kernel's point
4184 of view it is difficult to enforce standard Unix access controls for
4185 such processing by inode number.  Such a facility would probably
4186 need to be restricted to the superuser.
4188 Another way of improving performance would be to increase the
4189 parallelism of the process.  For example if the directory hierarchy we
4190 are searching is actually spread across a number of disks, we might
4191 somehow be able to arrange for @code{find} to process each disk in
4192 parallel.  In practice GNU @code{find} doesn't have such an intimate
4193 understanding of the system's filesystem layout and disk I/O
4194 subsystem.
4196 However, since the system administrator can have such an understanding
4197 they can take advantage of it like so:
4199 @smallexample
4200 find /var/tmp/stuff1 -mtime +90 -delete &
4201 find /var/tmp/stuff2 -mtime +90 -delete &
4202 find /var/tmp/stuff3 -mtime +90 -delete &
4203 find /var/tmp/stuff4 -mtime +90 -delete &
4204 wait
4205 @end smallexample
4207 In the example above, four separate instances of @code{find} are used
4208 to search four subdirectories in parallel.  The @code{wait} command
4209 simply waits for all of these to complete.  Whether this approach is
4210 more or less efficient than a single instance of @code{find} depends
4211 on a number of things:
4213 @itemize
4214 @item
4215 Are the directories being searched in parallel actually on separate
4216 disks?  If not, this parallel search might just result in a lot of
4217 disk head movement and so the speed might even be slower.
4218 @item
4219 Other activity - are other programs also doing things on those disks?
4220 @end itemize
4223 @subsection Conclusion
4225 The fastest and most secure way to delete files with the help of
4226 @code{find} is to use @samp{-delete}.  Using @code{xargs -0 -P N} can
4227 also make effective use of the disk, but it is not as secure.
4229 In the case where we're doing things other than deleting files, the
4230 most secure alternative is @samp{-execdir @dots{} +}, but this is not as
4231 portable as the insecure action @samp{-exec @dots{} +}.
4233 The @samp{-delete} action is not completely portable, but the only
4234 other possibility which is as secure (@samp{-execdir}) is no more
4235 portable.  The most efficient portable alternative is @samp{-exec
4236 @dots{}+}, but this is insecure and isn't supported by versions of GNU
4237 findutils prior to 4.2.12.
4239 @node Copying A Subset of Files
4240 @section Copying A Subset of Files
4242 Suppose you want to copy some files from @file{/source-dir} to
4243 @file{/dest-dir}, but there are a small number of files in
4244 @file{/source-dir} you don't want to copy. 
4246 One option of course is @code{cp /source-dir /dest-dir} followed by
4247 deletion of the unwanted material under @file{/dest-dir}.  But often
4248 that can be inconvenient, because for example we would have copied a
4249 large amount of extraneous material, or because @file{/dest-dir} is
4250 too small.  Naturally there are many other possible reasons why this
4251 strategy may be unsuitable.
4253 So we need to have some way of identifying which files we want to
4254 copy, and we need to have a way of copying that file list.  The second
4255 part of this condition is met by @code{cpio -p}.  Of course, we can
4256 identify the files we wish to copy by using @code{find}.  Here is a
4257 command that solves our problem:
4259 @example
4260 cd /source-dir
4261 find . -name '.snapshot' -prune -o \( \! -name '*~' -print0 \) | 
4262 cpio -pmd0   /dest-dir
4263 @end example
4265 The first part of the @code{find} command here identifies files or
4266 directoires named @file{.snapshot} and tells @code{find} not to
4267 recurse into them (since they do not need to be copied).  The
4268 combination @code{-name '.snapshot' -prune} yields false for anything
4269 that didn't get pruned, but it is exactly those files we want to
4270 copy.  Therefore we need to use an OR (@samp{-o}) condition to
4271 introduce the rest of our expression.  The remainder of the expression
4272 simply arranges for the name of any file not ending in @samp{~} to be
4273 printed.  
4275 Using @code{-print0} ensures that white space characters in file names
4276 do not pose a problem.  The @code{cpio} command does the actual work
4277 of copying files.  The program as a whole fails if the @code{cpio}
4278 program returns nonzero.  If the @code{find} command returns non-zero
4279 on the other hand, the Unix shell will not diagnose a problem (since
4280 @code{find} is not the last command in the pipeline).
4283 @node Updating A Timestamp File
4284 @section Updating A Timestamp File
4286 Suppose we have a directory full of files which is maintained with a
4287 set of automated tools; perhaps one set of tools updates them and
4288 another set of tools uses the result.  In this situation, it might be
4289 useful for the second set of tools to know if the files have recently
4290 been changed.  It might be useful, for example, to have a 'timestamp'
4291 file which gives the timestamp on the newest file in the collection.
4293 We can use @code{find} to achieve this, but there are several
4294 different ways to do it.
4296 @subsection Updating the Timestamp The Wrong Way
4298 The obvious but wrong answer is just to use @samp{-newer}:-
4300 @smallexample
4301 find subdir -newer timestamp -exec touch -r @{@} timestamp \; 
4302 @end smallexample
4304 This does the right sort of thing but has a bug.  Suppose that two
4305 files in the subdirectory have been updated, and that these are called
4306 @file{file1} and @file{file2}.  The command above will update
4307 @file{timestamp} with the modification time of @file{file1} or that of
4308 @file{file2}, but we don't know which one.  Since the timestamps on
4309 @file{file1} and @file{file2} will in general be different, this could
4310 well be the wrong value.
4312 One solution to this problem is to modify @code{find} to recheck the
4313 modification time of @file{timestamp} every time a file is to be
4314 compared against it, but that will reduce the performance of
4315 @code{find}.
4317 @subsection Using the test utility to compare timestamps
4319 The @code{test} command can be used to compare timestamps:
4321 @smallexample
4322 find subdir -exec test @{@} -nt timestamp \; -exec touch -r @{@} timestamp \; 
4323 @end smallexample
4325 This will ensure that any changes made to the modification time of
4326 @file{timestamp} that take place during the execution of @code{find}
4327 are taken into account.  This resolves our earlier problem, but
4328 unfortunately this runs much more slowly.
4330 @subsection A combined approach
4332 We can of course still use @samp{-newer} to cut down on the number of
4333 calls to @code{test}:
4335 @smallexample
4336 find subdir -newer timestamp -a \
4337      -exec test @{@} -nt timestamp \; -a \
4338      -exec touch -r @{@} timestamp \; 
4339 @end smallexample
4341 Here, the @samp{-newer} test excludes all the files which are
4342 definitely older than the timestamp, but all the files which are newer
4343 than the old value of the timestamp are compared against the current
4344 updated timestamp.
4346 This is indeed faster in general, but the speed difference will depend
4347 on how many updated files there are.
4349 @subsection Using -printf and sort to compare timestamps
4351 It is possible to use the @samp{-printf} action to abandon the use of
4352 @code{test} entirely:
4354 @smallexample
4355 newest=$(find subdir -newer timestamp -printf "%A@:%p\n" | 
4356            sort -n | 
4357            tail -1 | 
4358            cut -d: -f2- ) 
4359 touch -r "$@{newest:-timestamp@}" timestamp
4360 @end smallexample
4362 The command above works by generating a list of the timestamps and
4363 names of all the files which are newer than the timestamp.  The
4364 @code{sort}, @code{tail} and @code{cut} commands simply pull out the
4365 name of the file with the largest timestamp value (that is, the latest
4366 file).  The @code{touch} command is then used to update the timestamp,
4368 The @code{"$@{newest:-timestamp@}"} expression simply expands to the
4369 value of @code{$newest} if that variable is set, but to
4370 @file{timestamp} otherwise.  This ensures that an argument is always
4371 given to the @samp{-r} option of the @code{touch} command.
4373 This approach seems quite efficient, but unfortunately it has a
4374 problem.  Many operating systems now keep file modification time
4375 information at a granularity which is finer than one second.
4376 Findutils version 4.3.3 and later will print a fractional part with
4377 %A@@, but older versions will not.
4380 @subsection Solving the problem with make
4382 Another tool which often works with timestamps is @code{make}.  We can
4383 use @code{find} to generate a @file{Makefile} file on the fly and then
4384 use @code{make} to update the timestamps:
4386 @smallexample
4387 makefile=$(mktemp)
4388 find subdir \
4389         \( \! -xtype l \) \
4390         -newer timestamp \
4391         -printf "timestamp:: %p\n\ttouch -r %p timestamp\n\n" > "$makefile"
4392 make -f "$makefile"
4393 rm   -f "$makefile"
4394 @end smallexample
4396 Unfortunately although the solution above is quite elegant, it fails
4397 to cope with white space within file names, and adjusting it to do so
4398 would require a rather complex shell script.
4401 @subsection Coping with odd filenames too
4403 We can fix both of these problems (looping and problems with white
4404 space), and do things more efficiently too.  The following command
4405 works with newlines and doesn't need to sort the list of filenames.
4407 @smallexample
4408 find subdir -newer timestamp -printf "%A@@:%p\0" | 
4409    perl -0 newest.pl |
4410    xargs --no-run-if-empty --null -i \
4411       find @{@} -maxdepth 0 -newer timestamp -exec touch -r @{@} timestamp \;
4412 @end smallexample
4414 The first @code{find} command generates a list of files which are
4415 newer than the original timestamp file, and prints a list of them with
4416 their timestamps.  The @file{newest.pl} script simply filters out all
4417 the filenames which have timestamps which are older than whatever the
4418 newest file is:-
4420 @smallexample
4421 @verbatim
4422 #! /usr/bin/perl -0
4423 my @newest = ();
4424 my $latest_stamp = undef;
4425 while (<>) {
4426     my ($stamp, $name) = split(/:/);
4427     if (!defined($latest_stamp) || ($tstamp > $latest_stamp)) {
4428         $latest_stamp = $stamp;
4429         @newest = ();
4430     }
4431     if ($tstamp >= $latest_stamp) {
4432         push @newest, $name;
4433     }
4435 print join("\0", @newest);
4436 @end verbatim
4437 @end smallexample
4439 This prints a list of zero or more files, all of which are newer than
4440 the original timestamp file, and which have the same timestamp as each
4441 other, to the nearest second.  The second @code{find} command takes
4442 each resulting file one at a time, and if that is newer than the
4443 timestamp file, the timestamp is updated.
4445 @node Security Considerations
4446 @chapter Security Considerations
4448 Security considerations are important if you are using @code{find} or
4449 @code{xargs} to search for or process files that don't belong to you
4450 or which other people have control.  Security considerations
4451 relating to @code{locate} may also apply if you have files which you
4452 do not want others to see.
4454 The most severe forms of security problems affecting
4455 @code{find} and related programs are when third parties bring
4456 about a situation allowing them to do something
4457 they would normally not be able to accomplish.  This is called @emph{privilege
4458 elevation}.  This might include deleting files they would not normally
4459 be able to delete.  It is common for the operating system to periodically
4460 invoke @code{find} for self-maintenance purposes.  These invocations of
4461 @code{find} are particularly problematic from a security point of view
4462 as these are often invoked by the superuser and search the entire
4463 filesystem hierarchy.  Generally, the severity of any associated problem depends
4464 on what the system is going to do with the files found by @code{find}.
4466 @menu
4467 * Levels of Risk::      What is your level of exposure to security problems?
4468 * Security Considerations for find::  Security problems with find
4469 * Security Considerations for xargs:: Security problems with xargs
4470 * Security Considerations for locate:: Security problems with locate
4471 * Security Summary:: That was all very complex, what does it boil down to?
4472 @end menu
4475 @node Levels of Risk
4476 @section Levels of Risk
4478 There are some security risks inherent in the use of @code{find},
4479 @code{xargs} and (to a lesser extent) @code{locate}.  The severity of
4480 these risks depends on what sort of system you are using:
4482 @table @strong
4483 @item High risk
4484 Multi-user systems where you do not control (or trust) the other
4485 users, and on which you execute @code{find}, including areas where
4486 those other users can manipulate the filesystem (for example beneath
4487 @file{/home} or @file{/tmp}).
4489 @item Medium Risk
4490 Systems where the actions of other users can create file names chosen
4491 by them, but to which they don't have access while @code{find} is
4492 being run.  This access might include leaving programs running (shell
4493 background jobs, @code{at} or @code{cron} tasks, for example).  On
4494 these sorts of systems, carefully written commands (avoiding use of
4495 @samp{-print} for example) should not expose you to a high degree of
4496 risk.  Most systems fall into this category.
4498 @item Low Risk
4499 Systems to which untrusted parties do not have access, cannot create
4500 file names of their own choice (even remotely) and which contain no
4501 security flaws which might enable an untrusted third party to gain
4502 access.  Most systems do not fall into this category because there are
4503 many ways in which external parties can affect the names of files that
4504 are created on your system.  The system on which I am writing this for
4505 example automatically downloads software updates from the Internet;
4506 the names of the files in which these updates exist are chosen by
4507 third parties@footnote{Of course, I trust these parties to a large
4508 extent anyway, because I install software provided by them; I choose
4509 to trust them in this way, and that's a deliberate choice}.
4510 @end table
4512 In the discussion above, ``risk'' denotes the likelihood that someone
4513 can cause @code{find}, @code{xargs}, @code{locate} or some other
4514 program which is controlled by them to do something you did not
4515 intend.  The levels of risk suggested do not take any account of the
4516 consequences of this sort of event.  That is, if you operate a ``low
4517 risk'' type system, but the consequences of a security problem are
4518 disastrous, then you should still give serious thought to all the
4519 possible security problems, many of which of course will not be
4520 discussed here -- this section of the manual is intended to be
4521 informative but not comprehensive or exhaustive.
4523 If you are responsible for the operation of a system where the
4524 consequences of a security problem could be very important, you should
4525 do two things:-
4527 @enumerate
4528 @item Define a security policy which defines who is allowed to do what
4529 on your system.
4530 @item Seek competent advice on how to enforce your policy, detect
4531 breaches of that policy, and take account of any potential problems
4532 that might fall outside the scope of your policy.
4533 @end enumerate
4536 @node Security Considerations for find
4537 @section Security Considerations for @code{find}
4540 Some of the actions @code{find} might take have a direct effect;
4541 these include @code{-exec} and @code{-delete}.  However, it is also
4542 common to use @code{-print} explicitly or implicitly, and so if
4543 @code{find} produces the wrong list of file names, that can also be a
4544 security problem; consider the case for example where @code{find} is
4545 producing a list of files to be deleted.
4547 We normally assume that the @code{find} command line expresses the
4548 file selection criteria and actions that the user had in mind -- that
4549 is, the command line is ``trusted'' data.
4551 From a security analysis point of view, the output of @code{find}
4552 should be correct; that is, the output should contain only the names
4553 of those files which meet the user's criteria specified on the command
4554 line.  This applies for the @code{-exec} and @code{-delete} actions;
4555 one can consider these to be part of the output.
4557 On the other hand, the contents of the filesystem can be manipulated
4558 by other people, and hence we regard this as ``untrusted'' data.  This
4559 implies that the @code{find} command line is a filter which converts
4560 the untrusted contents of the filesystem into a correct list of output
4561 files.
4563 The filesystem will in general change while @code{find} is searching
4564 it; in fact, most of the potential security problems with @code{find}
4565 relate to this issue in some way.
4567 @dfn{Race conditions} are a general class of security problem where the
4568 relative ordering of actions taken by @code{find} (for example) and
4569 something else are critically important in getting the correct and expected result@footnote{This is more or less the
4570 definition of the term ``race condition''} .
4572 For @code{find}, an attacker might move or rename files or directories in
4573 the hope that an action might be taken against a file which was not
4574 normally intended to be affected.  Alternatively, this sort of attack
4575 might be intended to persuade @code{find} to search part of the
4576 filesystem which would not normally be included in the search
4577 (defeating the @code{-prune} action for example).
4579 @menu
4580 * Problems with -exec and filenames::
4581 * Changing the Current Working Directory::
4582 * Race Conditions with -exec::
4583 * Race Conditions with -print and -print0::
4584 @end menu
4586 @node Problems with -exec and filenames
4587 @subsection Problems with -exec and filenames
4589 It is safe in many cases to use the @samp{-execdir} action with any
4590 file name.  Because @samp{-execdir} prefixes the arguments it passes
4591 to programs with @samp{./}, you will not accidentally pass an argument
4592 which is interpreted as an option.  For example the file @file{-f}
4593 would be passed to @code{rm} as @file{./-f}, which is harmless.
4595 However, your degree of safety does depend on the nature of the
4596 program you are running.  For example constructs such as these two commands
4598 @example
4599 # risky
4600 find -exec sh -c "something @{@}" \;
4601 find -execdir sh -c "something @{@}" \;
4602 @end example
4604 are very dangerous.  The reason for this is that the @samp{@{@}} is
4605 expanded to a filename which might contain a semicolon or other
4606 characters special to the shell.  If for example someone creates the
4607 file @file{/tmp/foo; rm -rf $HOME} then the two commands above could
4608 delete someone's home directory.
4610 So for this reason do not run any command which will pass untrusted
4611 data (such as the names of files) to commands which interpret
4612 arguments as commands to be further interpreted (for example
4613 @samp{sh}).
4615 In the case of the shell, there is a clever workaround for this
4616 problem:
4618 @example
4619 # safer 
4620 find -exec sh -c 'something "$@@"' @{@} \;
4621 find -execdir sh -c 'something "$@@"' @{@}\;
4622 @end example
4624 This approach is not guaranteed to avoid every problem, but it is much
4625 safer than substituting data of an attacker's choice into the text of
4626 a shell command.
4628 @node Changing the Current Working Directory
4629 @subsection Changing the Current Working Directory
4631 As @code{find} searches the filesystem, it finds subdirectories and
4632 then searches within them by changing its working directory.  First,
4633 @code{find} reaches and recognises a subdirectory.  It then decides if that
4634 subdirectory meets the criteria for being searched; that is, any
4635 @samp{-xdev} or @samp{-prune} expressions are taken into account.  The
4636 @code{find} program will then change working directory and proceed to
4637 search the directory.
4639 A race condition attack might take the form that once the checks
4640 relevant to @samp{-xdev} and @samp{-prune} have been done, an attacker
4641 might rename the directory that was being considered, and put in its
4642 place a symbolic link that actually points somewhere else.
4644 The idea behind this attack is to fool @code{find} into going into the
4645 wrong directory.  This would leave @code{find} with a working
4646 directory chosen by an attacker, bypassing any protection apparently
4647 provided by @samp{-xdev} and @samp{-prune}, and any protection
4648 provided by being able to @emph{not} list particular directories on
4649 the @code{find} command line.  This form of attack is particularly
4650 problematic if the attacker can predict when the @code{find} command
4651 will be run, as is the case with @code{cron} tasks for example.
4653 GNU @code{find} has specific safeguards to prevent this general class
4654 of problem.  The exact form of these safeguards depends on the
4655 properties of your system.
4657 @menu
4658 * O_NOFOLLOW::                     Safely changing directory using fchdir().
4659 * Systems without O_NOFOLLOW::     Checking for symbolic links after chdir().
4660 @end menu
4662 @node O_NOFOLLOW
4663 @subsubsection O_NOFOLLOW
4665 If your system supports the O_NOFOLLOW flag @footnote{GNU/Linux
4666 (kernel version 2.1.126 and later) and FreeBSD (3.0-CURRENT and later)
4667 support this} to the @code{open(2)} system call, @code{find} uses it
4668 when safely changing directory.  The target directory is first opened
4669 and then @code{find} changes working directory with the
4670 @code{fchdir()} system call.  This ensures that symbolic links are not
4671 followed, preventing the sort of race condition attack in which use
4672 is made of symbolic links.
4674 If for any reason this approach does not work, @code{find} will fall
4675 back on the method which is normally used if O_NOFOLLOW is not
4676 supported.
4678 You can tell if your system supports O_NOFOLLOW by running
4680 @example
4681 find --version
4682 @end example
4684 This will tell you the version number and which features are enabled.
4685 For example, if I run this on my system now, this gives:
4686 @example
4687 GNU find version 4.2.18-CVS
4688 Features enabled: D_TYPE O_NOFOLLOW(enabled)
4689 @end example
4691 Here, you can see that I am running a version of @code{find} which was
4692 built from the development (CVS) code prior to the release of
4693 findutils-4.2.18, and that the D_TYPE and O_NOFOLLOW features are
4694 present.  O_NOFOLLOW is qualified with ``enabled''.  This simply means
4695 that the current system seems to support O_NOFOLLOW.  This check is
4696 needed because it is possible to build @code{find} on a system that
4697 defines O_NOFOLLOW and then run it on a system that ignores the
4698 O_NOFOLLOW flag.  We try to detect such cases at startup by checking
4699 the operating system and version number; when this happens you will
4700 see ``O_NOFOLLOW(disabled)'' instead.
4702 @node Systems without O_NOFOLLOW
4703 @subsubsection Systems without O_NOFOLLOW
4705 The strategy for preventing this type of problem on systems that lack
4706 support for the O_NOFOLLOW flag is more complex.  Each time
4707 @code{find} changes directory, it examines the directory it is about
4708 to move to, issues the @code{chdir()} system call, and then checks
4709 that it has ended up in the subdirectory it expected.  If all is as
4710 expected, processing continues as normal.  However, there are two main
4711 reasons why the directory might change: the use of an automounter and
4712 the someone removing the old directory and replacing it with something
4713 else while @code{find} is trying to descend into it.
4715 Where a filesystem ``automounter'' is in use it can be the case that
4716 the use of the @code{chdir()} system call can itself cause a new
4717 filesystem to be mounted at that point.  On systems that do not
4718 support O_NOFOLLOW, this will cause @code{find}'s security check to
4719 fail.
4721 However, this does not normally represent a security problem, since
4722 the automounter configuration is normally set up by the system
4723 administrator.  Therefore, if the @code{chdir()} sanity check fails,
4724 @code{find} will make one more attempt@footnote{This may not be the
4725 case for the fts-based executable}.  If that succeeds, execution
4726 carries on as normal.  This is the usual case for automounters.
4728 Where an attacker is trying to exploit a race condition, the problem
4729 may not have gone away on the second attempt.  If this is the case,
4730 @code{find} will issue a warning message and then ignore that
4731 subdirectory.  When this happens, actions such as @samp{-exec} or
4732 @samp{-print} may already have taken place for the problematic
4733 subdirectory.  This is because @code{find} applies tests and actions
4734 to directories before searching within them (unless @samp{-depth} was
4735 specified).
4737 Because of the nature of the directory-change operation and security
4738 check, in the worst case the only things that @code{find} would have
4739 done with the directory are to move into it and back out to the
4740 original parent.  No operations would have been performed within that
4741 directory.
4743 @node Race Conditions with -exec
4744 @subsection Race Conditions with -exec
4746 The @samp{-exec} action causes another program to be run.  It passes
4747 to the program the name of the file which is being considered at the
4748 time.  The invoked program will typically then perform some action
4749 on that file.  Once again, there is a race condition which can be
4750 exploited here.  We shall take as a specific example the command
4752 @example
4753 find /tmp -path /tmp/umsp/passwd -exec /bin/rm
4754 @end example
4756 In this simple example, we are identifying just one file to be deleted
4757 and invoking @code{/bin/rm} to delete it.  A problem exists because
4758 there is a time gap between the point where @code{find} decides that
4759 it needs to process the @samp{-exec} action and the point where the
4760 @code{/bin/rm} command actually issues the @code{unlink()} system
4761 call to delete the file from the filesystem.  Within this time period, an attacker can rename the
4762 @file{/tmp/umsp} directory, replacing it with a symbolic link to
4763 @file{/etc}.  There is no way for @code{/bin/rm} to determine that it
4764 is working on the same file that @code{find} had in mind.  Once the
4765 symbolic link is in place, the attacker has persuaded @code{find} to
4766 cause the deletion of the @file{/etc/passwd} file, which is not the
4767 effect intended by the command which was actually invoked.
4769 One possible defence against this type of attack is to modify the
4770 behaviour of @samp{-exec} so that the @code{/bin/rm} command is run
4771 with the argument @file{./passwd} and a suitable choice of working
4772 directory.  This would allow the normal sanity check that @code{find}
4773 performs to protect against this form of attack too.  Unfortunately,
4774 this strategy cannot be used as the POSIX standard specifies that the
4775 current working directory for commands invoked with @samp{-exec} must
4776 be the same as the current working directory from which @code{find}
4777 was invoked.  This means that the @samp{-exec} action is inherently
4778 insecure and can't be fixed.
4780 GNU @code{find} implements a more secure variant of the @samp{-exec}
4781 action, @samp{-execdir}.  The @samp{-execdir} action
4782 ensures that it is not necessary to dereference subdirectories to
4783 process target files.  The current directory used to invoke programs
4784 is the same as the directory in which the file to be processed exists
4785 (@file{/tmp/umsp} in our example, and only the basename of the file to
4786 be processed is passed to the invoked command, with a @samp{./}
4787 prepended (giving @file{./passwd} in our example).
4789 The @samp{-execdir} action refuses to do anything if the current
4790 directory is included in the @var{$PATH} environment variable.  This
4791 is necessary because @samp{-execdir} runs programs in the same
4792 directory in which it finds files -- in general, such a directory
4793 might be writable by untrusted users.  For similar reasons,
4794 @samp{-execdir} does not allow @samp{@{@}} to appear in the name of
4795 the command to be run.
4797 @node Race Conditions with -print and -print0
4798 @subsection Race Conditions with -print and -print0
4800 The @samp{-print} and @samp{-print0} actions can be used to produce a
4801 list of files matching some criteria, which can then be used with some
4802 other command, perhaps with @code{xargs}.  Unfortunately, this means
4803 that there is an unavoidable time gap between @code{find} deciding
4804 that one or more files meet its criteria and the relevant command
4805 being executed.  For this reason, the @samp{-print} and @samp{-print0}
4806 actions are just as insecure as @samp{-exec}.
4808 In fact, since the construction
4810 @example
4811 find @dots{}  -print | xargs @enddots{}
4812 @end example
4814 does not cope correctly with newlines or other ``white space'' in
4815 file names, and copes poorly with file names containing quotes, the
4816 @samp{-print} action is less secure even than @samp{-print0}.
4819 @comment  node-name,  next,  previous,  up
4820 @comment @node Security Considerations for xargs
4821 @node Security Considerations for xargs
4822 @section Security Considerations for @code{xargs}
4824 The description of the race conditions affecting the @samp{-print}
4825 action of @code{find} shows that @code{xargs} cannot be secure if it
4826 is possible for an attacker to modify a filesystem after @code{find}
4827 has started but before @code{xargs} has completed all its actions.
4829 However, there are other security issues that exist even if it is not
4830 possible for an attacker to have access to the filesystem in real
4831 time.  Firstly, if it is possible for an attacker to create files with
4832 names of their choice on the filesystem, then @code{xargs} is
4833 insecure unless the @samp{-0} option is used.  If a file with the name
4834 @file{/home/someuser/foo/bar\n/etc/passwd} exists (assume that
4835 @samp{\n} stands for a newline character), then @code{find @dots{} -print}
4836 can be persuaded to print three separate lines:
4838 @example
4839 /home/someuser/foo/bar
4841 /etc/passwd
4842 @end example
4844 If it finds a blank line in the input, @code{xargs} will ignore it.
4845 Therefore, if some action is to be taken on the basis of this list of
4846 files, the @file{/etc/passwd} file would be included even if this was
4847 not the intent of the person running find.  There are circumstances in
4848 which an attacker can use this to their advantage.  The same
4849 consideration applies to file names containing ordinary spaces rather
4850 than newlines, except that of course the list of file names will no
4851 longer contain an ``extra'' newline.
4853 This problem is an unavoidable consequence of the default behaviour of
4854 the @code{xargs} command, which is specified by the POSIX standard.
4855 The only ways to avoid this problem are either to avoid all use of
4856 @code{xargs} in favour for example of @samp{find -exec} or (where
4857 available) @samp{find -execdir}, or to use the @samp{-0} option, which
4858 ensures that @code{xargs} considers file names to be separated by
4859 ASCII NUL characters rather than whitespace.  However, useful as this
4860 option is, the POSIX standard does not make it mandatory.
4862 POSIX also specifies that @code{xargs} interprets quoting and trailing
4863 whitespace specially in filenames, too.  This means that using
4864 @code{find ... -print | xargs ...} can cause the commands run by
4865 @code{xargs} to receive a list of file names which is not the same as
4866 the list printed by @code{find}.   The interpretation of quotes and
4867 trailing whitespace is turned off by the @samp{-0} argument to
4868 @code{xargs}, which is another reason to use that option.
4870 @comment  node-name,  next,  previous,  up
4871 @node Security Considerations for locate
4872 @section Security Considerations for @code{locate}
4874 @subsection Race Conditions
4875 It is fairly unusual for the output of @code{locate} to be fed into
4876 another command.  However, if this were to be done, this would raise
4877 the same set of security issues as the use of @samp{find @dots{} -print}.
4878 Although the problems relating to whitespace in file names can be
4879 resolved by using @code{locate}'s @samp{-0} option, this still leaves
4880 the race condition problems associated with @samp{find @dots{} -print0}.
4881 There is no way to avoid these problems in the case of @code{locate}.
4883 @subsection Long File Name Bugs with Old-Format Databases
4884 Old versions of @code{locate} have a bug in the way that old-format
4885 databases are read.  This bug affects the following versions of
4886 @code{locate}:
4888 @enumerate
4889 @item All releases prior to 4.2.31
4890 @item All 4.3.x releases prior to 4.3.7
4891 @end enumerate
4893 The affected versions of @code{locate} read file names into a
4894 fixed-length 1026 byte buffer, allocated on the heap.  This buffer is
4895 not extended if file names are too long to fit into the buffer.  No
4896 range checking on the length of the filename is performed.  This could
4897 in theory lead to a privilege escalation attack.  Findutils versions
4898 4.3.0 to 4.3.6 are also affected.
4900 On systems using the old database format and affected versions of
4901 @code{locate}, carefully-chosen long file names could in theory allow
4902 malicious users to run code of their choice as any user invoking
4903 locate.
4905 If remote users can choose the names of files stored on your system,
4906 and these files are indexed by @code{updatedb}, this may be a remote
4907 security vulnerability.  Findutils version 4.2.31 and findutils
4908 version 4.3.7 include fixes for this problem.  The @code{updatedb},
4909 @code{bigram} and @code{code} programs do no appear to be affected.
4911 If you are also using GNU coreutils, you can use the following command
4912 to determine the length of the longest file name on a given system:
4914 @example
4915 find / -print0 | tr -c '\0' 'x' | tr '\0' '\n' | wc -L
4916 @end example
4918 Although this problem is significant, the old database format is not
4919 the default, and use of the old database format is not common.  Most
4920 installations and most users will not be affected by this problem.  
4924 @node Security Summary
4925 @section Summary
4927 Where untrusted parties can create files on the system, or affect the
4928 names of files that are created, all uses for @code{find},
4929 @code{locate} and @code{xargs} have known security problems except the
4930 following:
4932 @table @asis
4933 @item Informational use only
4934 Uses where the programs are used to prepare lists of file names upon
4935 which no further action will ever be taken.
4937 @item @samp{-delete}
4938 Use of the @samp{-delete} action with @code{find} to delete files
4939 which meet specified criteria
4941 @item @samp{-execdir}
4942 Use of the @samp{-execdir} action with @code{find} where the
4943 @env{PATH} environment variable contains directories which contain
4944 only trusted programs.
4945 @end table
4947 @comment  node-name,  next,  previous,  up
4948 @node Error Messages
4949 @chapter Error Messages
4951 This section describes some of the error messages sometimes made by
4952 @code{find}, @code{xargs}, or @code{locate}, explains them and in some
4953 cases provides advice as to what you should do about this.
4955 This manual is written in English.  The GNU findutils software
4956 features translations of error messages for many languages.  For this
4957 reason the error messages produced by the programs are made to be as
4958 self-explanatory as possible.  This approach avoids leaving people to
4959 figure out which test an English-language error message corresponds
4960 to.  Error messages which are self-explanatory will not normally be
4961 mentioned in this document.  For those messages mentioned in this
4962 document, only the English-language version of the message will be
4963 listed.
4965 @menu
4966 * Error Messages From find::
4967 * Error Messages From xargs::
4968 * Error Messages From locate::
4969 * Error Messages From updatedb::
4970 @end menu
4972 @node Error Messages From find
4973 @section Error Messages From @code{find}
4975 Most error messages produced by find are self-explanatory.  Error
4976 messages sometimes include a filename.  When this happens, the
4977 filename is quoted in order to prevent any unusual characters in the
4978 filename making unwanted changes in the state of the terminal.
4980 @table @samp
4981 @item invalid predicate `-foo'
4982 This means that the @code{find} command line included something that
4983 started with a dash or other special character.  The @code{find}
4984 program tried to interpret this as a test, action or option, but
4985 didn't recognise it.  If it was intended to be a test, check what was
4986 specified against the documentation.  If, on the other hand, the
4987 string is the name of a file which has been expanded from a wildcard
4988 (for example because you have a @samp{*} on the command line),
4989 consider using @samp{./*} or just @samp{.} instead.
4991 @item unexpected extra predicate
4992 This usually happens if you have an extra bracket on the command line
4993 (for example @samp{find . -print \)}).
4995 @item Warning: filesystem /path/foo has recently been mounted
4996 @itemx Warning: filesystem /path/foo has recently been unmounted
4997 These messages might appear when @code{find} moves into a directory
4998 and finds that the device number and inode are different to what it
4999 expected them to be.  If the directory @code{find} has moved into is
5000 on an network filesystem (NFS), it will not issue this message, because
5001 @code{automount} frequently mounts new filesystems on directories as
5002 you move into them (that is how it knows you want to use the
5003 filesystem).  So, if you do see this message, be wary ---
5004 @code{automount} may not have been responsible.  Consider the
5005 possibility that someone else is manipulating the filesystem while
5006 @code{find} is running.  Some people might do this in order to mislead
5007 @code{find} or persuade it to look at one set of files when it thought
5008 it was looking at another set.
5010 @item /path/foo changed during execution of find (old device number 12345, new device number 6789, filesystem type is <whatever>) [ref XXX]
5011 This message is issued when @code{find} moves into a directory and ends up
5012 somewhere it didn't expect to be.  This happens in one of two
5013 circumstances.  Firstly, this happens when @code{automount} intervenes
5014 on a system where @code{find} doesn't know how to determine what
5015 the current set of mounted filesystems is.
5017 Secondly, this can happen when the device number of a directory
5018 appears to change during a change of current directory, but
5019 @code{find} is moving up the filesystem hierarchy rather than down into it.
5020 In order to prevent @code{find} wandering off into some unexpected
5021 part of the filesystem, we stop it at this point.
5023 @item Don't know how to use getmntent() to read `/etc/mtab'.  This is a bug.
5024 This message is issued when a problem similar to the above occurs on a
5025 system where @code{find} doesn't know how to figure out the current
5026 list of mount points.  Ask for help on @email{bug-findutils@@gnu.org}.
5028 @item /path/foo/bar changed during execution of find (old inode number 12345, new inode number 67893, filesystem type is <whatever>) [ref XXX]"),
5029 This message is issued when @code{find} moves into a directory and
5030 discovers that the inode number of that directory
5031 is different from the inode number that it obtained when it examined the
5032 directory previously.  This usually means that while
5033 @code{find} was deep in a directory hierarchy doing a
5034 time consuming operation, somebody has moved one of the parent directories to
5035 another location in the same filesystem.  This may or may not have been done
5036 maliciously.  In any case, @code{find} stops at this point
5037 to avoid traversing parts of the filesystem that it wasn't
5038 intended.  You can use @code{ls -li} or @code{find /path -inum
5039 12345 -o -inum 67893} to find out more about what has happened.
5041 @item sanity check of the fnmatch() library function failed.
5042 Please submit a bug report.  You may well be asked questions about
5043 your system, and if you compiled the @code{findutils} code yourself,
5044 you should keep your copy of the build tree around.  The likely
5045 explanation is that your system has a buggy implementation of
5046 @code{fnmatch} that looks enough like the GNU version to fool
5047 @code{configure}, but which doesn't work properly.
5049 @item cannot fork
5050 This normally happens if you use the @code{-exec} action or
5051 something similar (@code{-ok} and so forth) but the system has run out
5052 of free process slots.  This is either because the system is very busy
5053 and the system has reached its maximum process limit, or because you
5054 have a resource limit in place and you've reached it.  Check the
5055 system for runaway processes (with @code{ps}, if possible).  Some process
5056 slots are normally reserved for use by @samp{root}.
5058 @item some-program terminated by signal 99
5059 Some program which was launched with @code{-exec} or similar was killed
5060 with a fatal signal.  This is just an advisory message.
5061 @end table
5064 @node Error Messages From xargs
5065 @section Error Messages From xargs
5067 @table @samp
5068 @item environment is too large for exec
5069 This message means that you have so many environment variables set (or
5070 such large values for them) that there is no room within the
5071 system-imposed limits on program command line argument length to
5072 invoke any program.  This is an unlikely situation and is more likely
5073 result of an attempt to test the limits of @code{xargs}, or break it.
5074 Please try unsetting some environment variables, or exiting the
5075 current shell.  You can also use @samp{xargs --show-limits} to
5076 understand the relevant sizes.
5078 @item can not fit single argument within argument list size limit
5079 You are using the @samp{-I} option and @code{xargs} doesn't have
5080 enough space to build a command line because it has read a really
5081 large item and it doesn't fit.  You can probably work around this
5082 problem with the @samp{-s} option, but the default size is pretty
5083 large.  This is a rare situation and is more likely an attempt to test
5084 the limits of @code{xargs}, or break it.  Otherwise, you will need to
5085 try to shorten the problematic argument or not use @code{xargs}.
5087 @item cannot fork
5088 See the description of the similar message for @code{find}.
5090 @item <program>: exited with status 255; aborting
5091 When a command run by @code{xargs} exits with status 255, @code{xargs}
5092 is supposed to stop.  If this is not what you intended, wrap the
5093 program you are trying to invoke in a shell script which doesn't
5094 return status 255.
5096 @item <program>: terminated by signal 99
5097 See the description of the similar message for @code{find}.
5098 @end table
5100 @node Error Messages From locate
5101 @section Error Messages From @code{locate}
5103 @table @samp
5104 @item warning: database @file{@value{LOCATE_DB}} is more than 8 days old
5105 The @code{locate} program relies on a database which is periodically
5106 built by the @code{updatedb} program.  That hasn't happened in a long
5107 time.  To fix this problem, run @code{updatedb} manually.  This can
5108 often happen on systems that are generally not left on, so the
5109 periodic ``cron'' task which normally does this doesn't get a chance
5110 to run.
5112 @item locate database @file{@value{LOCATE_DB}} is corrupt or invalid
5113 This should not happen.  Re-run @code{updatedb}.  If that works, but
5114 @code{locate} still produces this error, run @code{locate --version}
5115 and @code{updatedb --version}.  These should produce the same output.
5116 If not, you are using a mixed toolset; check your @samp{$PATH}
5117 environment variable and your shell aliases (if you have any).  If
5118 both programs claim to be GNU versions, this is a bug; all versions of
5119 these programs should interoperate without problem.  Ask for help on
5120 @email{bug-findutils@@gnu.org}.
5121 @end table
5124 @node Error Messages From updatedb
5125 @section Error Messages From updatedb
5127 The @code{updatedb} program (and the programs it invokes) do issue
5128 error messages, but none seem to be candidates for guidance.  If
5129 you are having a problem understanding one of these, ask for help on
5130 @email{bug-findutils@@gnu.org}.
5132 @node GNU Free Documentation License
5133 @appendix GNU Free Documentation License
5134 @include fdl.texi
5136 @node Primary Index
5137 @unnumbered @code{find} Primary Index
5139 This is a list of all of the primaries (tests, actions, and options)
5140 that make up @code{find} expressions for selecting files.  @xref{find
5141 Expressions}, for more information on expressions.
5143 @printindex fn
5145 @bye
5147 @comment texi related words used by Emacs' spell checker ispell.el
5149 @comment LocalWords: texinfo setfilename settitle setchapternewpage
5150 @comment LocalWords: iftex finalout ifinfo DIR titlepage vskip pt
5151 @comment LocalWords: filll dir samp dfn noindent xref pxref
5152 @comment LocalWords: var deffn texi deffnx itemx emph asis
5153 @comment LocalWords: findex smallexample subsubsection cindex
5154 @comment LocalWords: dircategory direntry itemize
5156 @comment other words used by Emacs' spell checker ispell.el
5157 @comment LocalWords: README fred updatedb xargs Plett Rendell akefile
5158 @comment LocalWords: args grep Filesystems fo foo fOo wildcards iname
5159 @comment LocalWords: ipath regex iregex expr fubar regexps
5160 @comment LocalWords: metacharacters macs sr sc inode lname ilname
5161 @comment LocalWords: sysdep noleaf ls inum xdev filesystems usr atime
5162 @comment LocalWords: ctime mtime amin cmin mmin al daystart Sladkey rm
5163 @comment LocalWords: anewer cnewer bckw rf xtype uname gname uid gid
5164 @comment LocalWords: nouser nogroup chown chgrp perm ch maxdepth
5165 @comment LocalWords: mindepth cpio src CD AFS statted stat fstype ufs
5166 @comment LocalWords: nfs tmp mfs printf fprint dils rw djm Nov lwall
5167 @comment LocalWords: POSIXLY fls fprintf strftime locale's EDT GMT AP
5168 @comment LocalWords: EST diff perl backquotes sprintf Falstad Oct cron
5169 @comment LocalWords: eg vmunix mkdir afs allexec allwrite ARG bigram
5170 @comment LocalWords: bigrams cd chmod comp crc CVS dbfile dum eof
5171 @comment LocalWords: fileserver filesystem fn frcode Ghazi Hnewc iXX
5172 @comment LocalWords: joeuser Kaveh localpaths localuser LOGNAME
5173 @comment LocalWords: Meyering mv netpaths netuser nonblank nonblanks
5174 @comment LocalWords: ois ok Pinard printindex proc procs prunefs
5175 @comment LocalWords: prunepaths pwd RFS rmadillo rmdir rsh sbins str
5176 @comment LocalWords: su Timar ubins ug unstripped vf VM Weitzel
5177 @comment LocalWords: wildcard zlogout basename execdir wholename iwholename
5178 @comment LocalWords: timestamp timestamps Solaris FreeBSD OpenBSD POSIX