Merged changes from the 4.2.x branch
[findutils.git] / doc / find.texi
blob97bd7c5b468d2be949a43755b750270408c4202e
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
11 @iftex
12 @finalout
13 @end iftex
15 @dircategory Basics
16 @direntry
17 * Finding files: (find).        Operating on files matching certain criteria.
18 @end direntry
20 @dircategory Individual utilities
21 @direntry
22 * find: (find)Invoking find.                    Finding and acting on files.
23 * locate: (find)Invoking locate.                Finding files in a database.
24 * updatedb: (find)Invoking updatedb.            Building the locate database.
25 * xargs: (find)Invoking xargs.                  Operating on many files.
26 @end direntry
28 @copying
30 This file documents the GNU utilities for finding files that match
31 certain criteria and performing various operations on them.
33 Copyright (C) 1994, 1996, 1998, 2000, 2001, 2003, 2004, 2005 Free
34 Software Foundation, Inc.
36 Permission is granted to make and distribute verbatim copies of
37 this manual provided the copyright notice and this permission notice
38 are preserved on all copies.
40 @ignore
41 Permission is granted to process this file through TeX and print the
42 results, provided the printed document carries copying permission
43 notice identical to this one except for the removal of this paragraph
44 (this paragraph not being relevant to the printed manual).
46 @end ignore
47 Permission is granted to copy and distribute modified versions of this
48 manual under the conditions for verbatim copying, provided that the
49 entire resulting derived work is distributed under the terms of a
50 permission notice identical to this one.
52 Permission is granted to copy and distribute translations of this
53 manual into another language, under the above conditions for modified
54 versions, except that this permission notice may be stated in a
55 translation approved by the Foundation.
56 @end copying
58 @titlepage
59 @title Finding Files
60 @subtitle Edition @value{EDITION}, for GNU @code{find} version @value{VERSION}
61 @subtitle @value{UPDATED}
62 @author by David MacKenzie
64 @page
65 @vskip 0pt plus 1filll
66 @insertcopying{}
67 @end titlepage
69 @contents
71 @ifnottex
72 @node Top, Introduction, , (dir)
73 @comment  node-name,  next,  previous,  up
75 This file documents the GNU utilities for finding files that match
76 certain criteria and performing various actions on them.
78 This is edition @value{EDITION}, for @code{find} version @value{VERSION}.
79 @end ifnottex
81 @c The master menu, created with texinfo-master-menu, goes here.
83 @menu
84 * Introduction::                Summary of the tasks this manual describes.
85 * Finding Files::               Finding files that match certain criteria.
86 * Actions::                     Doing things to files you have found.
87 * Databases::                   Maintaining file name databases.
88 * File Permissions::            How to control access to files.
89 * Reference::                   Summary of how to invoke the programs.
90 * Common Tasks::                Solutions to common real-world problems.
91 * Worked Examples::             Examples demonstrating more complex points.
92 * Security Considerations::     Security issues relating to findutils.
93 * Error Messages::              Explanations of some messages you might see.
94 * Primary Index::               The components of @code{find} expressions.
95 @end menu
97 @node Introduction, Finding Files, Top, Top
98 @chapter Introduction
100 This manual shows how to find files that meet criteria you specify,
101 and how to perform various actions on the files that you find.  The
102 principal programs that you use to perform these tasks are
103 @code{find}, @code{locate}, and @code{xargs}.  Some of the examples in
104 this manual use capabilities specific to the GNU versions of those
105 programs.
107 GNU @code{find} was originally written by Eric Decker, with
108 enhancements by David MacKenzie, Jay Plett, and Tim Wood.  GNU
109 @code{xargs} was originally written by Mike Rendell, with enhancements
110 by David MacKenzie.  GNU @code{locate} and its associated utilities
111 were originally written by James Woods, with enhancements by David
112 MacKenzie.  The idea for @samp{find -print0} and @samp{xargs -0} came
113 from Dan Bernstein.  The current maintainer of GNU findutils (and this
114 manual) is James Youngman.  Many other people have contributed bug
115 fixes, small improvements, and helpful suggestions.  Thanks!
117 Mail suggestions and bug reports for these programs to
118 @code{bug-findutils@@gnu.org}.  Please include the version
119 number, which you can get by running @samp{find --version}.
121 @menu
122 * Scope::
123 * Overview::
124 * find Expressions::
125 @end menu
127 @node Scope
128 @section Scope
130 For brevity, the word @dfn{file} in this manual means a regular file,
131 a directory, a symbolic link, or any other kind of node that has a
132 directory entry.  A directory entry is also called a @dfn{file name}.
133 A file name may contain some, all, or none of the directories in a
134 path that leads to the file.  These are all examples of what this
135 manual calls ``file names'':
137 @example
138 parser.c
139 README
140 ./budget/may-94.sc
141 fred/.cshrc
142 /usr/local/include/termcap.h
143 @end example
145 A @dfn{directory tree} is a directory and the files it contains, all
146 of its subdirectories and the files they contain, etc.  It can also be
147 a single non-directory file.
149 These programs enable you to find the files in one or more directory
150 trees that:
152 @itemize @bullet
153 @item
154 have names that contain certain text or match a certain pattern;
155 @item
156 are links to certain files;
157 @item
158 were last used during a certain period of time;
159 @item
160 are within a certain size range;
161 @item
162 are of a certain type (regular file, directory, symbolic link, etc.);
163 @item
164 are owned by a certain user or group;
165 @item
166 have certain access permissions;
167 @item
168 contain text that matches a certain pattern;
169 @item
170 are within a certain depth in the directory tree;
171 @item
172 or some combination of the above.
173 @end itemize
175 Once you have found the files you're looking for (or files that are
176 potentially the ones you're looking for), you can do more to them than
177 simply list their names.  You can get any combination of the files'
178 attributes, or process the files in many ways, either individually or
179 in groups of various sizes.  Actions that you might want to perform on
180 the files you have found include, but are not limited to:
182 @itemize @bullet
183 @item
184 view or edit
185 @item
186 store in an archive
187 @item
188 remove or rename
189 @item
190 change access permissions
191 @item
192 classify into groups
193 @end itemize
195 This manual describes how to perform each of those tasks, and more.
197 @node Overview
198 @section Overview
200 The principal programs used for making lists of files that match given
201 criteria and running commands on them are @code{find}, @code{locate},
202 and @code{xargs}.  An additional command, @code{updatedb}, is used by
203 system administrators to create databases for @code{locate} to use.
205 @code{find} searches for files in a directory hierarchy and prints
206 information about the files it found.  It is run like this:
208 @example
209 find @r{[}@var{file}@dots{}@r{]} @r{[}@var{expression}@r{]}
210 @end example
212 @noindent
213 Here is a typical use of @code{find}.  This example prints the names
214 of all files in the directory tree rooted in @file{/usr/src} whose
215 name ends with @samp{.c} and that are larger than 100 Kilobytes.
216 @example
217 find /usr/src -name '*.c' -size +100k -print
218 @end example
220 Notice that the wildcard must be enclosed in quotes in order to
221 protect it from expansion by the shell.
223 @code{locate} searches special file name databases for file names that
224 match patterns.  The system administrator runs the @code{updatedb}
225 program to create the databases.  @code{locate} is run like this:
227 @example
228 locate @r{[}@var{option}@dots{}@r{]} @var{pattern}@dots{}
229 @end example
231 @noindent
232 This example prints the names of all files in the default file name
233 database whose name ends with @samp{Makefile} or @samp{makefile}.
234 Which file names are stored in the database depends on how the system
235 administrator ran @code{updatedb}.
236 @example
237 locate '*[Mm]akefile'
238 @end example
240 The name @code{xargs}, pronounced EX-args, means ``combine
241 arguments.''  @code{xargs} builds and executes command lines by
242 gathering together arguments it reads on the standard input.  Most
243 often, these arguments are lists of file names generated by
244 @code{find}.  @code{xargs} is run like this:
246 @example
247 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
248 @end example
250 @noindent
251 The following command searches the files listed in the file
252 @file{file-list} and prints all of the lines in them that contain the
253 word @samp{typedef}.
254 @example
255 xargs grep typedef < file-list
256 @end example
258 @node find Expressions
259 @section @code{find} Expressions
261 The expression that @code{find} uses to select files consists of one
262 or more @dfn{primaries}, each of which is a separate command line
263 argument to @code{find}.  @code{find} evaluates the expression each
264 time it processes a file.  An expression can contain any of the
265 following types of primaries:
267 @table @dfn
268 @item options
269 affect overall operation rather than the processing of a specific
270 file;
271 @item tests
272 return a true or false value, depending on the file's attributes;
273 @item actions
274 have side effects and return a true or false value; and
275 @item operators
276 connect the other arguments and affect when and whether they are
277 evaluated.
278 @end table
280 You can omit the operator between two primaries; it defaults to
281 @samp{-and}.  @xref{Combining Primaries With Operators}, for ways to
282 connect primaries into more complex expressions.  If the expression
283 contains no actions other than @samp{-prune}, @samp{-print} is
284 performed on all files for which the entire expression is true
285 (@pxref{Print File Name}).
287 Options take effect immediately, rather than being evaluated for each
288 file when their place in the expression is reached.  Therefore, for
289 clarity, it is best to place them at the beginning of the expression.
291 Many of the primaries take arguments, which immediately follow them in
292 the next command line argument to @code{find}.  Some arguments are
293 file names, patterns, or other strings; others are numbers.  Numeric
294 arguments can be specified as
296 @table @code
297 @item +@var{n}
298 for greater than @var{n},
299 @item -@var{n}
300 for less than @var{n},
301 @item @var{n}
302 for exactly @var{n}.
303 @end table
305 @node Finding Files, Actions, Introduction, Top
306 @chapter Finding Files
308 By default, @code{find} prints to the standard output the names of the
309 files that match the given criteria.  @xref{Actions}, for how to get
310 more information about the matching files.
313 @menu
314 * Name::
315 * Links::
316 * Time::
317 * Size::
318 * Type::
319 * Owner::
320 * Permissions::
321 * Contents::
322 * Directories::
323 * Filesystems::
324 * Combining Primaries With Operators::
325 @end menu
327 @node Name
328 @section Name
330 Here are ways to search for files whose name matches a certain
331 pattern.  @xref{Shell Pattern Matching}, for a description of the
332 @var{pattern} arguments to these tests.
334 Each of these tests has a case-sensitive version and a
335 case-insensitive version, whose name begins with @samp{i}.  In a
336 case-insensitive comparison, the patterns @samp{fo*} and @samp{F??}
337 match the file names @file{Foo}, @samp{FOO}, @samp{foo}, @samp{fOo},
338 etc.
340 @menu
341 * Base Name Patterns::
342 * Full Name Patterns::
343 * Fast Full Name Search::
344 * Shell Pattern Matching::      Wildcards used by these programs.
345 @end menu
347 @node Base Name Patterns
348 @subsection Base Name Patterns
350 @deffn Test -name pattern
351 @deffnx Test -iname pattern
352 True if the base of the file name (the path with the leading
353 directories removed) matches shell pattern @var{pattern}.  For
354 @samp{-iname}, the match is case-insensitive.  To ignore a whole
355 directory tree, use @samp{-prune} (@pxref{Directories}).  As an
356 example, to find Texinfo source files in @file{/usr/local/doc}:
358 @example
359 find /usr/local/doc -name '*.texi'
360 @end example
361 @end deffn
363 Notice that the wildcard must be enclosed in quotes in order to
364 protect it from expansion by the shell.
366 Patterns for @samp{-name} and @samp{-iname} will match a file name
367 with a leading @samp{.}.  For example the command @samp{find /tmp
368 -name \*bar} will match the file @file{/tmp/.foobar}.
371 @node Full Name Patterns
372 @subsection Full Name Patterns
374 @deffn Test -wholename pattern
375 @deffnx Test -iwholename pattern
376 True if the entire file name, starting with the command line argument
377 under which the file was found, matches shell pattern @var{pattern}.
378 For @samp{-iwholename}, the match is case-insensitive.  To ignore a
379 whole directory tree, use @samp{-prune} rather than checking every
380 file in the tree (@pxref{Directories}).  The ``entire file name'' as
381 used by @code{find} starts with the starting-point specified on the
382 command line, and is not converted to an absolute pathname, so for
383 example @code{cd /; find tmp -wholename /tmp} will never match
384 anything.
385 @end deffn
387 @deffn Test -path pattern
388 @deffnx Test -ipath pattern
389 These tests are deprecated, but work as for @samp{-wholename} and
390 @samp{-iwholename}, respectively.  The @samp{-ipath} test is a GNU
391 extension, but @samp{-path} is also provided by HP-UX @code{find}.
392 @end deffn
394 @deffn Test -regex expr
395 @deffnx Test -iregex expr
396 True if the entire file name matches regular expression @var{expr}.
397 This is a match on the whole path, not a search.  For example, to
398 match a file named @file{./fubar3}, you can use the regular expression
399 @samp{.*bar.} or @samp{.*b.*3}, but not @samp{f.*r3}.  @xref{Regexps,
400 , Syntax of Regular Expressions, emacs, The GNU Emacs Manual}, for a
401 description of the syntax of regular expressions.  For @samp{-iregex},
402 the match is case-insensitive.  There are several varieties of regular
403 expressions; by default this test uses POSIX basic regular
404 expressions, but this can be changed with the option
405 @samp{-regextype}.
406 @end deffn
408 @deffn Option -regextype name
409 This option controls the variety of regular expression syntax
410 understood by the @samp{-regex} and @samp{-iregex} tests.  This option
411 is positional; that is, it only affects regular expressions which
412 occur later in the command line.  If this option is not given, GNU
413 Emacs regular expressions are assumed.  Currently-implemented types
417 @table @samp
418 @item emacs
419 Regular expressions compatible with GNU Emacs; this is also the
420 default behaviour if this option is not used.
421 @item posix-awk
422 Regular expressions compatible with the POSIX awk command (not GNU awk)
423 @item posix-basic
424 POSIX Basic Regular Expressions.
425 @item posix-egrep
426 Regular expressions compatible with the POSIX egrep command
427 @item posix-extended
428 POSIX Extended Regular Expressions
429 @end table
431 @ref{Regular Expressions} for more information on the regular
432 expression dialects understood by GNU findutils.
435 @end deffn
437 @node Fast Full Name Search
438 @subsection Fast Full Name Search
440 To search for files by name without having to actually scan the
441 directories on the disk (which can be slow), you can use the
442 @code{locate} program.  For each shell pattern you give it,
443 @code{locate} searches one or more databases of file names and
444 displays the file names that contain the pattern.  @xref{Shell Pattern
445 Matching}, for details about shell patterns.
447 If a pattern is a plain string---it contains no
448 metacharacters---@code{locate} displays all file names in the database
449 that contain that string.  If a pattern contains
450 metacharacters, @code{locate} only displays file names that match the
451 pattern exactly.  As a result, patterns that contain metacharacters
452 should usually begin with a @samp{*}, and will most often end with one
453 as well.  The exceptions are patterns that are intended to explicitly
454 match the beginning or end of a file name.
456 If you only want @code{locate} to match against the last component of
457 the file names (the ``base name'' of the files) you can use the
458 @samp{--basename} option.  The opposite behaviour is the default, but
459 can be selected explicitly by using the option @samp{--wholename}.
461 The command
462 @example
463 locate @var{pattern}
464 @end example
466 is almost equivalent to
467 @example
468 find @var{directories} -name @var{pattern}
469 @end example
471 where @var{directories} are the directories for which the file name
472 databases contain information.  The differences are that the
473 @code{locate} information might be out of date, and that @code{locate}
474 handles wildcards in the pattern slightly differently than @code{find}
475 (@pxref{Shell Pattern Matching}).
477 The file name databases contain lists of files that were on the system
478 when the databases were last updated.  The system administrator can
479 choose the file name of the default database, the frequency with which
480 the databases are updated, and the directories for which they contain
481 entries.
483 Here is how to select which file name databases @code{locate}
484 searches.  The default is system-dependent.
486 @table @code
487 @item --database=@var{path}
488 @itemx -d @var{path}
489 Instead of searching the default file name database, search the file
490 name databases in @var{path}, which is a colon-separated list of
491 database file names.  You can also use the environment variable
492 @code{LOCATE_PATH} to set the list of database files to search.  The
493 option overrides the environment variable if both are used.
494 @end table
496 @node Shell Pattern Matching
497 @subsection Shell Pattern Matching
499 @code{find} and @code{locate} can compare file names, or parts of file
500 names, to shell patterns.  A @dfn{shell pattern} is a string that may
501 contain the following special characters, which are known as
502 @dfn{wildcards} or @dfn{metacharacters}.
504 You must quote patterns that contain metacharacters to prevent the
505 shell from expanding them itself.  Double and single quotes both work;
506 so does escaping with a backslash.
508 @table @code
509 @item *
510 Matches any zero or more characters.
512 @item ?
513 Matches any one character.
515 @item [@var{string}]
516 Matches exactly one character that is a member of the string
517 @var{string}.  This is called a @dfn{character class}.  As a
518 shorthand, @var{string} may contain ranges, which consist of two
519 characters with a dash between them.  For example, the class
520 @samp{[a-z0-9_]} matches a lowercase letter, a number, or an
521 underscore.  You can negate a class by placing a @samp{!} or @samp{^}
522 immediately after the opening bracket.  Thus, @samp{[^A-Z@@]} matches
523 any character except an uppercase letter or an at sign.
525 @item \
526 Removes the special meaning of the character that follows it.  This
527 works even in character classes.
528 @end table
530 In the @code{find} tests that do shell pattern matching (@samp{-name},
531 @samp{-wholename}, etc.), wildcards in the pattern will match a
532 @samp{.}  at the beginning of a file name.  This is also the case for
533 @code{locate}.  Thus, @samp{find -name '*macs'} will match a file
534 named @file{.emacs}, as will @samp{locate '*macs'}.
536 Slash characters have no special significance in the shell pattern
537 matching that @code{find} and @code{locate} do, unlike in the shell,
538 in which wildcards do not match them.  Therefore, a pattern
539 @samp{foo*bar} can match a file name @samp{foo3/bar}, and a pattern
540 @samp{./sr*sc} can match a file name @samp{./src/misc}.
542 If you want to locate some files with the @samp{locate} command but
543 don't need to see the full list you can use the @samp{--limit} option
544 to see just a small number of results, or the @samp{--count} option to
545 display only the total number of matches.
547 @node Links
548 @section Links
550 There are two ways that files can be linked together.  @dfn{Symbolic
551 links} are a special type of file whose contents are a portion of the
552 name of another file.  @dfn{Hard links} are multiple directory entries
553 for one file; the file names all have the same index node
554 (@dfn{inode}) number on the disk.
556 @menu
557 * Symbolic Links::
558 * Hard Links::
559 @end menu
561 @node Symbolic Links
562 @subsection Symbolic Links
564 Symbolic links are names that reference other files.  GNU @code{find}
565 will handle symbolic links in one of two ways; firstly, it can
566 dereference the links for you - this means that if it comes across a
567 symbolic link, it examines the file that the link points to, in order
568 to see if it matches the criteria you have specified.  Secondly, it
569 can check the link itself in case you might be looking for the actual
570 link.  If the file that the symbolic link points to is also within the
571 directory hierarchy you are searching with the @code{find} command,
572 you may not see a great deal of difference between these two
573 alternatives.
575 By default, @code{find} examines symbolic links themselves when it
576 finds them (and, if it later comes across the linked-to file, it will
577 examine that, too).  If you would prefer @code{find} to dereference
578 the links and examine the file that each link points to, specify the
579 @samp{-L} option to @code{find}.  You can explicitly specify the
580 default behaviour by using the @samp{-P} option.  The @samp{-H}
581 option is a half-way-between option which ensures that any symbolic
582 links listed on the command line are dereferenced, but other symbolic
583 links are not.
585 Symbolic links are different to ``hard links'' in the sense that you
586 need permissions upon the linked-to file in order to be able to
587 dereference the link.  This can mean that even if you specify the
588 @samp{-L} option, @code{find} may not be able to determine the
589 properties of the file that the link points to (because you don't have
590 sufficient permissions).  In this situation, @code{find} uses the
591 properties of the link itself.  This also occurs if a symbolic link
592 exists but points to a file that is missing.
594 The options controlling the behaviour of @code{find} with respect to
595 links are as follows :-
597 @table @samp
598 @item -P
599 @code{find} does not dereference symbolic links at all.  This is the
600 default behaviour.  This option must be specified before any of the
601 file names on the command line.
602 @item -H
603 @code{find} does not dereference symbolic links (except in the case of
604 file names on the command line, which are dereferenced).  If a
605 symbolic link cannot be dereferenced, the information for the symbolic
606 link itself is used.  This option must be specified before any of the
607 file names on the command line.
608 @item -L
609 @code{find} dereferences symbolic links where possible, and where this
610 is not possible it uses the properties of the symbolic link itself.
611 This option must be specified before any of the file names on the
612 command line.  Use of this option also implies the same behaviour as
613 the @samp{-noleaf} option.  If you later use the @samp{-H} or
614 @samp{-P} options, this does not turn off @samp{-noleaf}.
616 @item -follow
617 This option forms part of the ``expression'' and must be specified
618 after the file names, but it is otherwise equivalent to @samp{-L}.
619 @end table
621 The following differences in behavior occur when the @samp{-L} option
622 is used:
624 @itemize @bullet
625 @item
626 @code{find} follows symbolic links to directories when searching
627 directory trees.
628 @item
629 @samp{-lname} and @samp{-ilname} always return false (unless they
630 happen to match broken symbolic links).
631 @item
632 @samp{-type} reports the types of the files that symbolic links point
633 to.  This means that in combination with @samp{-L}, @samp{-type l}
634 will be true only for broken symbolic links.  To check for symbolic
635 links when @samp{-L} has been specified, use @samp{-xtype l}.
636 @item
637 Implies @samp{-noleaf} (@pxref{Directories}).
638 @end itemize
640 If the @samp{-L} option or the @samp{-H} option is used,
641 the file names used as arguments to @samp{-newer}, @samp{-anewer}, and
642 @samp{-cnewer} are dereferenced and the timestamp from the pointed-to
643 file is used instead (if possible -- otherwise the timestamp from the
644 symbolic link is used).
646 @deffn Test -lname pattern
647 @deffnx Test -ilname pattern
648 True if the file is a symbolic link whose contents match shell pattern
649 @var{pattern}.  For @samp{-ilname}, the match is case-insensitive.
650 @xref{Shell Pattern Matching}, for details about the @var{pattern}
651 argument.  If the @samp{-L} option is in effect, this test will always
652 return false for symbolic links unless they are broken.  So, to list
653 any symbolic links to @file{sysdep.c} in the current directory and its
654 subdirectories, you can do:
656 @example
657 find . -lname '*sysdep.c'
658 @end example
659 @end deffn
661 @node Hard Links
662 @subsection Hard Links
664 Hard links allow more than one name to refer to the same file.  To
665 find all the names which refer to the same file as NAME, use
666 @samp{-samefile NAME}.  If you are not using the @samp{-L} option, you
667 can confine your search to one filesystem using the @samp{-xdev}
668 option.  This is useful because hard links cannot point outside a
669 single filesystem, so this can cut down on needless searching.
671 If the @samp{-L} option is in effect, and NAME is in fact a symbolic
672 link, the symbolic link will be dereferenced.  Hence you are searching
673 for other links (hard or symbolic) to the file pointed to by NAME.  If
674 @samp{-L} is in effect but NAME is not itself a symbolic link, other
675 symbolic links to the file NAME will be matched.
677 You can also search for files by inode number.  This can occasionally
678 be useful in diagnosing problems with filesystems for example, because
679 @code{fsck} tends to print inode numbers.  Inode numbers also
680 occasionally turn up in log messages for some types of software, and
681 are used to support the @code{ftok()} library function.
683 You can learn a file's inode number and the number of links to it by
684 running @samp{ls -li} or @samp{find -ls}.
686 You can search for hard links to inode number NUM by using @samp{-inum
687 NUM}. If there are any filesystem mount points below the directory
688 where you are starting the search, use the @samp{-xdev} option unless
689 you are also using the @samp{-L} option.  Using @samp{-xdev} this
690 saves needless searching, since hard links to a file must be on the
691 same filesystem.  @xref{Filesystems}.
693 @deffn Test -samefile NAME
694 File is a hard link to the same inode as NAME.  If the @samp{-L}
695 option is in effect, symbolic links to the same file as NAME points to
696 are also matched.
697 @end deffn
699 @deffn Test -inum n
700 File has inode number @var{n}.  The @samp{+} and @samp{-} qualifiers
701 also work, though these are rarely useful.
702 @end deffn
704 You can also search for files that have a certain number of links,
705 with @samp{-links}.  Directories normally have at least two hard
706 links; their @file{.} entry is the second one.  If they have
707 subdirectories, each of those also has a hard link called @file{..} to
708 its parent directory.  The @file{.} and @file{..} directory entries
709 are not normally searched unless they are mentioned on the @code{find}
710 command line.
712 @deffn Test -links n
713 File has @var{n} hard links.
714 @end deffn
716 @deffn Test -links +n
717 File has more than @var{n} hard links.
718 @end deffn
720 @deffn Test -links -n
721 File has fewer than @var{n} hard links.
722 @end deffn
724 @node Time
725 @section Time
727 Each file has three time stamps, which record the last time that
728 certain operations were performed on the file:
730 @enumerate
731 @item
732 access (read the file's contents)
733 @item
734 change the status (modify the file or its attributes)
735 @item
736 modify (change the file's contents)
737 @end enumerate
739 There is no timestamp that indicates when a file was @emph{created}.
741 You can search for files whose time stamps are within a certain age
742 range, or compare them to other time stamps.
744 @menu
745 * Age Ranges::
746 * Comparing Timestamps::
747 @end menu
749 @node Age Ranges
750 @subsection Age Ranges
752 These tests are mainly useful with ranges (@samp{+@var{n}} and
753 @samp{-@var{n}}).
755 @deffn Test -atime n
756 @deffnx Test -ctime n
757 @deffnx Test -mtime n
758 True if the file was last accessed (or its status changed, or it was
759 modified) @var{n}*24 hours ago.  The number of 24-hour periods since
760 the file's timestamp is always rounded down; therefore 0 means ``less
761 than 24 hours ago'', 1 means ``between 24 and 48 hours ago'', and so
762 forth.
763 @end deffn
765 @deffn Test -amin n
766 @deffnx Test -cmin n
767 @deffnx Test -mmin n
768 True if the file was last accessed (or its status changed, or it was
769 modified) @var{n} minutes ago.  These tests provide finer granularity
770 of measurement than @samp{-atime} et al., but rounding is done in a
771 similar way.  For example, to list files in @file{/u/bill} that were
772 last read from 2 to 6 minutes ago:
774 @example
775 find /u/bill -amin +2 -amin -6
776 @end example
777 @end deffn
779 @deffn Option -daystart
780 Measure times from the beginning of today rather than from 24 hours
781 ago.  So, to list the regular files in your home directory that were
782 modified yesterday, do
784 @example
785 find ~ -daystart -type f -mtime 1
786 @end example
787 @end deffn
789 The @samp{-daystart} option is unlike most other options in that it
790 has an effect on the way that other tests are performed.  The affected
791 tests are @samp{-amin}, @samp{-cmin}, @samp{-mmin}, @samp{-atime},
792 @samp{-ctime} and @samp{-mtime}.
794 @node Comparing Timestamps
795 @subsection Comparing Timestamps
797 As an alternative to comparing timestamps to the current time, you can
798 compare them to another file's timestamp.  That file's timestamp could
799 be updated by another program when some event occurs.  Or you could
800 set it to a particular fixed date using the @code{touch} command.  For
801 example, to list files in @file{/usr} modified after February 1 of the
802 current year:
804 @c Idea from Rick Sladkey.
805 @example
806 touch -t 02010000 /tmp/stamp$$
807 find /usr -newer /tmp/stamp$$
808 rm -f /tmp/stamp$$
809 @end example
811 @deffn Test -anewer file
812 @deffnx Test -cnewer file
813 @deffnx Test -newer file
814 True if the file was last accessed (or its status changed, or it was
815 modified) more recently than @var{file} was modified.  These tests are
816 affected by @samp{-follow} only if @samp{-follow} comes before them on
817 the command line.  @xref{Symbolic Links}, for more information on
818 @samp{-follow}.  As an example, to list any files modified since
819 @file{/bin/sh} was last modified:
821 @example
822 find . -newer /bin/sh
823 @end example
824 @end deffn
826 @deffn Test -used n
827 True if the file was last accessed @var{n} days after its status was
828 last changed.  Useful for finding files that are not being used, and
829 could perhaps be archived or removed to save disk space.
830 @end deffn
832 @node Size
833 @section Size
835 @deffn Test -size n@r{[}bckwMG@r{]}
836 True if the file uses @var{n} units of space, rounding up.  The units
837 are 512-byte blocks by default, but they can be changed by adding a
838 one-character suffix to @var{n}:
840 @table @code
841 @item b
842 512-byte blocks (never 1024)
843 @item c
844 bytes
845 @item k
846 kilobytes (1024 bytes)
847 @item w
848 2-byte words
849 @item M
850 Megabytes
851 @item G
852 Gigabytes
853 @end table
855 The `b' suffix always considers blocks to be 512 bytes.  This is not
856 affected by the setting (or non-setting) of the POSIXLY_CORRECT
857 environment variable.  This behaviour is different to the behaviour of
858 the @samp{-ls} action).  If you want to use 1024-byte units, use the
859 `k' suffix instead.
861 The number can be prefixed with a `+' or a `-'.  A plus sign indicates
862 that the test should succeed if the file uses at least @var{n} units
863 of storage (a common use of this test) and a minus sign
864 indicates that the test should succeed if the file uses less than
865 @var{n} units of storage.  There is no `=' prefix, because that's the
866 default anyway.
868 The size does not count indirect blocks, but it does count blocks in
869 sparse files that are not actually allocated.  In other words, it's
870 consistent with the result you get for @samp{ls -l} or @samp{wc -c}.
871 This handling of sparse files differs from the output of the @samp{%k}
872 and @samp{%b} format specifiers for the @samp{-printf} predicate.
874 @end deffn
876 @deffn Test -empty
877 True if the file is empty and is either a regular file or a directory.
878 This might help determine good candidates for deletion.  This test is
879 useful with @samp{-depth} (@pxref{Directories}) and @samp{-delete}
880 (@pxref{Single File}).
881 @end deffn
883 @node Type
884 @section Type
886 @deffn Test -type c
887 True if the file is of type @var{c}:
889 @table @code
890 @item b
891 block (buffered) special
892 @item c
893 character (unbuffered) special
894 @item d
895 directory
896 @item p
897 named pipe (FIFO)
898 @item f
899 regular file
900 @item l
901 symbolic link; if @samp{-L} is in effect, this is true only for broken
902 symbolic links.  If you want to search for symbolic links when
903 @samp{-L} is in effect, use @samp{-xtype} instead of @samp{-type}.
904 @item s
905 socket
906 @item D
907 door (Solaris)
908 @end table
909 @end deffn
911 @deffn Test -xtype c
912 This test behaves the same as @samp{-type} unless the file is a
913 symbolic link.  If the file is a symbolic link, the result is as
914 follows (in the table below, @samp{X} should be understood to
915 represent any letter except @samp{l}):
917 @table @samp
918 @item @samp{-P -xtype l}
919 True if the symbolic link is broken
920 @item @samp{-P -xtype X}
921 True if the (ultimate) target file is of type @samp{X}.
922 @item @samp{-L -xtype l}
923 Always true
924 @item @samp{-L -xtype X}
925 False unless the symbolic link is broken
926 @end table
928 In other words, for symbolic links, @samp{-xtype} checks the type of
929 the file that @samp{-type} does not check.  
931 The @samp{-H} option also affects the behaviour of @samp{-xtype}.
932 When @samp{-H} is in effect, @samp{-xtype} behaves as if @samp{-L} had
933 been specified when examining files listed on the command line, and as
934 if @samp{-P} had been specified otherwise.  If neither @samp{-H} nor
935 @samp{-L} was specified, @samp{-xtype} behaves as if @samp{-P} had
936 been specified.
938 @xref{Symbolic Links}, for more information on @samp{-follow} and
939 @samp{-L}.
940 @end deffn
942 @node Owner
943 @section Owner
945 @deffn Test -user uname
946 @deffnx Test -group gname
947 True if the file is owned by user @var{uname} (belongs to group
948 @var{gname}).  A numeric ID is allowed.
949 @end deffn
951 @deffn Test -uid n
952 @deffnx Test -gid n
953 True if the file's numeric user ID (group ID) is @var{n}.  These tests
954 support ranges (@samp{+@var{n}} and @samp{-@var{n}}), unlike
955 @samp{-user} and @samp{-group}.
956 @end deffn
958 @deffn Test -nouser
959 @deffnx Test -nogroup
960 True if no user corresponds to the file's numeric user ID (no group
961 corresponds to the numeric group ID).  These cases usually mean that
962 the files belonged to users who have since been removed from the
963 system.  You probably should change the ownership of such files to an
964 existing user or group, using the @code{chown} or @code{chgrp}
965 program.
966 @end deffn
968 @node Permissions
969 @section Permissions
971 @xref{File Permissions}, for information on how file permissions are
972 structured and how to specify them.
974 Four tests determine what users can do with files.  These are
975 @samp{-readable}, @samp{-writable}, @samp{-executable} and
976 @samp{-perm}.  The first three tests ask the operating system if the
977 current user can perform the relevant operation on a file, while
978 @samp{-perm} just examines the file's mode.  The file mode may give
979 a misleading impression of what the user can actually do, because the
980 file may have an access control list, or exist on a read-only
981 filesystem, for example.  Of these four tests though, only
982 @samp{-perm} is specified by the POSIX standard.
984 The @samp{-readable}, @samp{-writable} and @samp{-executable} tests
985 are implemented via the @code{access} system call.  This is
986 implemented within the operating system itself.  If the file being
987 considered is on an NFS filesystem, the remote system may allow or
988 forbid read or write operations for reasons of which the NFS client
989 cannot take account.  This includes user-ID mapping, either in the
990 general sense or the more restricted sense in which remote superusers
991 are treated by the NFS server as if they are the local user
992 @samp{nobody} on the NFS server.
994 None of the tests in this section should be used to verify that a user
995 is authorised to perform any operation (on the file being tested or
996 any other file) because of the possibility of a race condition.  That
997 is, the situation may change between the test and an action being
998 taken on the basis of the result of that test.
1001 @deffn Test -readable 
1002 True if the file can be read by the invoking user.
1003 @end deffn
1005 @deffn Test -writable
1006 True if the file can be written by the invoking user.  This is an
1007 in-principle check, and other things may prevent a successful write
1008 operation; for example, the filesystem might be full.
1009 @end deffn
1011 @deffn Test -executable
1012 True if the file can be executed by the invoking user.
1013 @end deffn
1015 @deffn Test -perm mode
1017 True if the file's permissions are exactly @var{mode}, which can be
1018 numeric or symbolic.
1020 If @var{mode} starts with @samp{-}, true if
1021 @emph{all} of the permissions set in @var{mode} are set for the file;
1022 permissions not set in @var{mode} are ignored.
1024 If @var{mode} starts with @samp{/}, true if
1025 @emph{any} of the permissions set in @var{mode} are set for the file;
1026 permissions not set in @var{mode} are ignored.
1027 This is a GNU extension.
1029 If you don't use the @samp{/} or @samp{-} form with a symbolic mode
1030 string, you may have to specify a rather complex mode string.  For
1031 example @samp{-perm g=w} will only match files which have mode 0020
1032 (that is, ones for which group write permission is the only permission
1033 set).  It is more likely that you will want to use the @samp{/} or
1034 @samp{-} forms, for example @samp{-perm -g=w}, which matches any file
1035 with group write permission.
1038 @table @samp
1039 @item -perm 664
1040 Match files which have read and write permission for their owner,
1041 and group, but which the rest of the world can read but not write to.
1042 Files which meet these criteria but have other permissions bits set
1043 (for example if someone can execute the file) will not be matched.
1045 @item -perm -664
1046 Match files which have read and write permission for their owner,
1047 and group, but which the rest of the world can read but not write to,
1048 without regard to the presence of any extra permission bits (for
1049 example the executable bit).  This will match a file which has mode
1050 0777, for example.
1052 @item -perm /222
1053 Match files which are writeable by somebody (their owner, or
1054 their group, or anybody else).
1056 @item -perm /022
1057 Match files which are writeable by either their owner or their
1058 group.  The files don't have to be writeable by both the owner and
1059 group to be matched; either will do.
1061 @item -perm /g+w,o+w
1062 As above.
1064 @item -perm /g=w,o=w
1065 As above
1067 @item -perm -022
1068 Search for files which are writeable by both their owner and their
1069 group.
1071 @item -perm -444 -perm /222 ! -perm /111
1072 Search for files which are readable for everybody, have at least one
1073 write bit set (i.e. somebody can write to them), but which cannot be
1074 executed by anybody.  Note that in some shells the @samp{!} must be
1075 escaped;.
1077 @item -perm -a+r -perm /a+w ! -perm /a+x
1078 As above.
1081 @item -perm -g+w,o+w
1082 As above.
1083 @end table
1084 @end deffn
1086 @node Contents
1087 @section Contents
1089 To search for files based on their contents, you can use the
1090 @code{grep} program.  For example, to find out which C source files in
1091 the current directory contain the string @samp{thing}, you can do:
1093 @example
1094 grep -l thing *.[ch]
1095 @end example
1097 If you also want to search for the string in files in subdirectories,
1098 you can combine @code{grep} with @code{find} and @code{xargs}, like
1099 this:
1101 @example
1102 find . -name '*.[ch]' | xargs grep -l thing
1103 @end example
1105 The @samp{-l} option causes @code{grep} to print only the names of
1106 files that contain the string, rather than the lines that contain it.
1107 The string argument (@samp{thing}) is actually a regular expression,
1108 so it can contain metacharacters.  This method can be refined a little
1109 by using the @samp{-r} option to make @code{xargs} not run @code{grep}
1110 if @code{find} produces no output, and using the @code{find} action
1111 @samp{-print0} and the @code{xargs} option @samp{-0} to avoid
1112 misinterpreting files whose names contain spaces:
1114 @example
1115 find . -name '*.[ch]' -print0 | xargs -r -0 grep -l thing
1116 @end example
1118 For a fuller treatment of finding files whose contents match a
1119 pattern, see the manual page for @code{grep}.
1121 @node Directories
1122 @section Directories
1124 Here is how to control which directories @code{find} searches, and how
1125 it searches them.  These two options allow you to process a horizontal
1126 slice of a directory tree.
1128 @deffn Option -maxdepth levels
1129 Descend at most @var{levels} (a non-negative integer) levels of
1130 directories below the command line arguments.  @samp{-maxdepth 0}
1131 means only apply the tests and actions to the command line arguments.
1132 @end deffn
1134 @deffn Option -mindepth levels
1135 Do not apply any tests or actions at levels less than @var{levels} (a
1136 non-negative integer).  @samp{-mindepth 1} means process all files
1137 except the command line arguments.
1138 @end deffn
1140 @deffn Option -depth
1141 Process each directory's contents before the directory itself.  Doing
1142 this is a good idea when producing lists of files to archive with
1143 @code{cpio} or @code{tar}.  If a directory does not have write
1144 permission for its owner, its contents can still be restored from the
1145 archive since the directory's permissions are restored after its
1146 contents.
1147 @end deffn
1149 @deffn Option -d
1150 This is a deprecated synonym for @samp{-depth}, for compatibility with
1151 Mac OS X, FreeBSD and OpenBSD.  The @samp{-depth} option is a POSIX
1152 feature, so it is better to use that.
1153 @end deffn
1155 @deffn Action -prune
1156 If the file is a directory, do not descend into it.  The result is
1157 true.  For example, to skip the directory @file{src/emacs} and all
1158 files and directories under it, and print the names of the other files
1159 found:
1161 @example
1162 find . -wholename './src/emacs' -prune -o -print
1163 @end example
1165 The above command will not print @file{./src/emacs} among its list of
1166 results.  This however is not due to the effect of the @samp{-prune}
1167 action (which only prevents further descent, it doesn't make sure we
1168 ignore that item).  Instead, this effect is due to the use of
1169 @samp{-o}.  Since the left hand side of the ``or'' condition has
1170 succeeded for @file{./src/emacs}, it is not necessary to evaluate the
1171 right-hand-side (@samp{-print}) at all for this particular file.  If
1172 you wanted to print that directory name you could use either an extra
1173 @samp{-print} action:
1175 @example
1176 find . -wholename './src/emacs' -prune -print -o -print
1177 @end example
1179 or use the comma operator:
1181 @example
1182 find . -wholename './src/emacs' -prune , -print
1183 @end example
1185 If the @samp{-depth} option is in effect, the subdirectories will have
1186 already been visited in any case.  Hence @samp{-prune} has no effect
1187 and returns false.
1188 @end deffn
1191 @deffn Action -quit
1192 Exit immediately (with return value zero if no errors have occurred).
1193 No child processes will be left running, but no more files specified
1194 on the command line will be processed.  For example, @code{find
1195 /tmp/foo /tmp/bar -print -quit} will print only @samp{/tmp/foo}.
1196 @end deffn
1198 @deffn Option -noleaf
1199 Do not optimize by assuming that directories contain 2 fewer
1200 subdirectories than their hard link count.  This option is needed when
1201 searching filesystems that do not follow the Unix directory-link
1202 convention, such as CD-ROM or MS-DOS filesystems or AFS volume mount
1203 points.  Each directory on a normal Unix filesystem has at least 2
1204 hard links: its name and its @file{.}  entry.  Additionally, its
1205 subdirectories (if any) each have a @file{..}  entry linked to that
1206 directory.  When @code{find} is examining a directory, after it has
1207 statted 2 fewer subdirectories than the directory's link count, it
1208 knows that the rest of the entries in the directory are
1209 non-directories (@dfn{leaf} files in the directory tree).  If only the
1210 files' names need to be examined, there is no need to stat them; this
1211 gives a significant increase in search speed.
1212 @end deffn
1214 @deffn Option -ignore_readdir_race
1215 If a file disappears after its name has been read from a directory but
1216 before @code{find} gets around to examining the file with @code{stat},
1217 don't issue an error message.  If you don't specify this option, an
1218 error message will be issued.  This option can be useful in system
1219 scripts (cron scripts, for example) that examine areas of the
1220 filesystem that change frequently (mail queues, temporary directories,
1221 and so forth), because this scenario is common for those sorts of
1222 directories.  Completely silencing error messages from @code{find} is
1223 undesirable, so this option neatly solves the problem.  There is no
1224 way to search one part of the filesystem with this option on and part
1225 of it with this option off, though.
1226 @end deffn
1228 @deffn Option -noignore_readdir_race
1229 This option reverses the effect of the @samp{-ignore_readdir_race}
1230 option.
1231 @end deffn
1234 @node Filesystems
1235 @section Filesystems
1237 A @dfn{filesystem} is a section of a disk, either on the local host or
1238 mounted from a remote host over a network.  Searching network
1239 filesystems can be slow, so it is common to make @code{find} avoid
1240 them.
1242 There are two ways to avoid searching certain filesystems.  One way is
1243 to tell @code{find} to only search one filesystem:
1245 @deffn Option -xdev
1246 @deffnx Option -mount
1247 Don't descend directories on other filesystems.  These options are
1248 synonyms.
1249 @end deffn
1251 The other way is to check the type of filesystem each file is on, and
1252 not descend directories that are on undesirable filesystem types:
1254 @deffn Test -fstype type
1255 True if the file is on a filesystem of type @var{type}.  The valid
1256 filesystem types vary among different versions of Unix; an incomplete
1257 list of filesystem types that are accepted on some version of Unix or
1258 another is:
1259 @example
1260 ext2 ext3 proc sysfs ufs 4.2 4.3 nfs tmp mfs S51K S52K
1261 @end example
1262 You can use @samp{-printf} with the @samp{%F} directive to see the
1263 types of your filesystems.  The @samp{%D} directive shows the device
1264 number.  @xref{Print File Information}.  @samp{-fstype} is usually
1265 used with @samp{-prune} to avoid searching remote filesystems
1266 (@pxref{Directories}).
1267 @end deffn
1269 @node Combining Primaries With Operators
1270 @section Combining Primaries With Operators
1272 Operators build a complex expression from tests and actions.
1273 The operators are, in order of decreasing precedence:
1275 @table @code
1276 @item @asis{( @var{expr} )}
1277 @findex ()
1278 Force precedence.  True if @var{expr} is true.
1280 @item @asis{! @var{expr}}
1281 @itemx @asis{-not @var{expr}}
1282 @findex !
1283 @findex -not
1284 True if @var{expr} is false.  In some shells, it is necessary to
1285 protect the @samp{!} from shell interpretation by quoting it.
1287 @item @asis{@var{expr1 expr2}}
1288 @itemx @asis{@var{expr1} -a @var{expr2}}
1289 @itemx @asis{@var{expr1} -and @var{expr2}}
1290 @findex -a
1291 @findex -and
1292 And; @var{expr2} is not evaluated if @var{expr1} is false.
1294 @item @asis{@var{expr1} -o @var{expr2}}
1295 @itemx @asis{@var{expr1} -or @var{expr2}}
1296 @findex -o
1297 @findex -or
1298 Or; @var{expr2} is not evaluated if @var{expr1} is true.
1300 @item @asis{@var{expr1} , @var{expr2}}
1301 @findex ,
1302 List; both @var{expr1} and @var{expr2} are always evaluated.  True if
1303 @var{expr2} is true.  The value of @var{expr1} is discarded.  This
1304 operator lets you do multiple independent operations on one traversal,
1305 without depending on whether other operations succeeded.  The two
1306 operations @var{expr1} and @var{expr2} are not always fully
1307 independent, since @var{expr1} might have side effects like touching
1308 or deleting files, or it might use @samp{-prune} which would also
1309 affect @var{expr2}.
1310 @end table
1312 @code{find} searches the directory tree rooted at each file name by
1313 evaluating the expression from left to right, according to the rules
1314 of precedence, until the outcome is known (the left hand side is false
1315 for @samp{-and}, true for @samp{-or}), at which point @code{find}
1316 moves on to the next file name.
1318 There are two other tests that can be useful in complex expressions:
1320 @deffn Test -true
1321 Always true.
1322 @end deffn
1324 @deffn Test -false
1325 Always false.
1326 @end deffn
1328 @node Actions, Databases, Finding Files, Top
1329 @chapter Actions
1331 There are several ways you can print information about the files that
1332 match the criteria you gave in the @code{find} expression.  You can
1333 print the information either to the standard output or to a file that
1334 you name.  You can also execute commands that have the file names as
1335 arguments.  You can use those commands as further filters to select
1336 files.
1338 @menu
1339 * Print File Name::
1340 * Print File Information::
1341 * Run Commands::
1342 * Delete Files::
1343 * Adding Tests::
1344 @end menu
1346 @node Print File Name
1347 @section Print File Name
1349 @deffn Action -print
1350 True; print the entire file name on the standard output, followed by a
1351 newline.
1352 @end deffn
1354 @deffn Action -fprint file
1355 True; print the entire file name into file @var{file}, followed by a
1356 newline.  If @var{file} does not exist when @code{find} is run, it is
1357 created; if it does exist, it is truncated to 0 bytes.  The file names
1358 @file{/dev/stdout} and @file{/dev/stderr} are handled specially; they
1359 refer to the standard output and standard error output, respectively.
1360 @end deffn
1362 @node Print File Information
1363 @section Print File Information
1365 @deffn Action -ls
1366 True; list the current file in @samp{ls -dils} format on the standard
1367 output.  The output looks like this:
1369 @smallexample
1370 204744   17 -rw-r--r--   1 djm      staff       17337 Nov  2  1992 ./lwall-quotes
1371 @end smallexample
1373 The fields are:
1375 @enumerate
1376 @item
1377 The inode number of the file.  @xref{Hard Links}, for how to find
1378 files based on their inode number.
1380 @item
1381 the number of blocks in the file.  The block counts are of 1K blocks,
1382 unless the environment variable @code{POSIXLY_CORRECT} is set, in
1383 which case 512-byte blocks are used.  @xref{Size}, for how to find
1384 files based on their size.
1386 @item
1387 The file's type and permissions.  The type is shown as a dash for a
1388 regular file; for other file types, a letter like for @samp{-type} is
1389 used (@pxref{Type}).  The permissions are read, write, and execute for
1390 the file's owner, its group, and other users, respectively; a dash
1391 means the permission is not granted.  @xref{File Permissions}, for
1392 more details about file permissions.  @xref{Permissions}, for how to
1393 find files based on their permissions.
1395 @item
1396 The number of hard links to the file.
1398 @item
1399 The user who owns the file.
1401 @item
1402 The file's group.
1404 @item
1405 The file's size in bytes.
1407 @item
1408 The date the file was last modified.
1410 @item
1411 The file's name.  @samp{-ls} quotes non-printable characters in the
1412 file names using C-like backslash escapes.  This may change soon, as
1413 the treatment of unprintable characters is harmonised for @samp{-ls},
1414 @samp{-fls}, @samp{-print}, @samp{-fprint}, @samp{-printf} and
1415 @samp{-fprintf}.
1416 @end enumerate
1417 @end deffn
1419 @deffn Action -fls file
1420 True; like @samp{-ls} but write to @var{file} like @samp{-fprint}
1421 (@pxref{Print File Name}).
1422 @end deffn
1424 @deffn Action -printf format
1425 True; print @var{format} on the standard output, interpreting @samp{\}
1426 escapes and @samp{%} directives.  Field widths and precisions can be
1427 specified as with the @code{printf} C function.  Format flags (like
1428 @samp{#} for example) may not work as you expect because many of the
1429 fields, even numeric ones, are printed with %s.  This means though
1430 that the format flag @samp{-} will work; it forces left-alignment of
1431 the field.  Unlike @samp{-print}, @samp{-printf} does not add a
1432 newline at the end of the string.  If you want a newline at the end of
1433 the string, add a @samp{\n}.
1434 @end deffn
1436 @deffn Action -fprintf file format
1437 True; like @samp{-printf} but write to @var{file} like @samp{-fprint}
1438 (@pxref{Print File Name}).
1439 @end deffn
1441 @menu
1442 * Escapes::
1443 * Format Directives::
1444 * Time Formats::
1445 @end menu
1447 @node Escapes
1448 @subsection Escapes
1450 The escapes that @samp{-printf} and @samp{-fprintf} recognize are:
1452 @table @code
1453 @item \a
1454 Alarm bell.
1455 @item \b
1456 Backspace.
1457 @item \c
1458 Stop printing from this format immediately and flush the output.
1459 @item \f
1460 Form feed.
1461 @item \n
1462 Newline.
1463 @item \r
1464 Carriage return.
1465 @item \t
1466 Horizontal tab.
1467 @item \v
1468 Vertical tab.
1469 @item \\
1470 A literal backslash (@samp{\}).
1471 @item \NNN
1472 The character whose ASCII code is NNN (octal).
1473 @end table
1475 A @samp{\} character followed by any other character is treated as an
1476 ordinary character, so they both are printed, and a warning message is
1477 printed to the standard error output (because it was probably a typo).
1479 @node Format Directives
1480 @subsection Format Directives
1482 @samp{-printf} and @samp{-fprintf} support the following format
1483 directives to print information about the file being processed.  The C
1484 @code{printf} function, field width and precision specifiers are
1485 supported, as applied to string (%s) types. That is, you can specify
1486 "minimum field width"."maximum field width" for each directive.
1487 Format flags (like @samp{#} for example) may not work as you expect
1488 because many of the fields, even numeric ones, are printed with %s.
1489 The format flag @samp{-} does work; it forces left-alignment of the
1490 field.
1492 @samp{%%} is a literal percent sign.  A @samp{%} character followed by
1493 an unrecognised character (i.e. not a known directive or printf field
1494 width and precision specifier), is discarded (but the unrecognised
1495 character is printed), and a warning message is printed to the
1496 standard error output (because it was probably a typo).
1498 @menu
1499 * Name Directives::
1500 * Ownership Directives::
1501 * Size Directives::
1502 * Location Directives::
1503 * Time Directives::
1504 * Formatting Flags::
1505 @end menu
1507 @node Name Directives
1508 @subsubsection Name Directives
1510 @table @code
1511 @item %p
1512 @c supports %-X.Yp
1513 File's name (not the absolute path name, but the name of the file as
1514 it was encountered by @code{find} - that is, as a relative path from
1515 one of the starting points).
1516 @item %f
1517 File's name with any leading directories removed (only the last
1518 element).
1519 @c supports %-X.Yf
1520 @item %h
1521 Leading directories of file's name (all but the last element and the
1522 slash before it).  If the file's name contains no slashes (for example
1523 because it was named on the command line and is in the current working
1524 directory), then ``%h'' expands to ``.''.  This prevents ``%h/%f''
1525 expanding to ``/foo'', which would be surprising and probably not
1526 desirable.
1527 @c supports %-X.Yh
1528 @item %P
1529 File's name with the name of the command line argument under which
1530 it was found removed from the beginning.
1531 @c supports %-X.YP
1532 @item %H
1533 Command line argument under which file was found.
1534 @c supports %-X.YH
1535 @end table
1537 @node Ownership Directives
1538 @subsubsection Ownership Directives
1540 @table @code
1541 @item %g
1542 @c supports %-X.Yg
1543 File's group name, or numeric group ID if the group has no name.
1544 @item %G
1545 @c supports %-X.Yg
1546 @c TODO: Needs to support # flag and 0 flag
1547 File's numeric group ID.
1548 @item %u
1549 @c supports %-X.Yu
1550 File's user name, or numeric user ID if the user has no name.
1551 @item %U
1552 @c supports %-X.Yu
1553 @c TODO: Needs to support # flag
1554 File's numeric user ID.
1555 @item %m
1556 @c full support, including # and 0.
1557 File's permissions (in octal).  If you always want to have a leading
1558 zero on the number, use the '#' format flag, for example '%#m'.
1559 @item %M
1560 File's permissions (in symbolic form, as for @code{ls}).  This
1561 directive is supported in findutils 4.2.5 and later.
1562 @end table
1564 @node Size Directives
1565 @subsubsection Size Directives
1567 @table @code
1568 @item %k
1569 The amount of disk space used for this file in 1K blocks. Since disk
1570 space is allocated in multiples of the filesystem block size this is
1571 usually greater than %s/1024, but it can also be smaller if the file
1572 is a sparse file (that is, it has ``holes'').
1573 @item %b
1574 The amount of disk space used for this file in 512-byte blocks. Since
1575 disk space is allocated in multiples of the filesystem block size this
1576 is usually greater than %s/1024, but it can also be smaller if the
1577 file is a sparse file (that is, it has ``holes'').
1578 @item %s
1579 File's size in bytes.
1580 @end table
1582 @node Location Directives
1583 @subsubsection Location Directives
1585 @table @code
1586 @item %d
1587 File's depth in the directory tree (depth below a file named on the
1588 command line, not depth below the root directory).  Files named on the
1589 command line have a depth of 0.  Subdirectories immediately below them
1590 have a depth of 1, and so on.
1591 @item %D
1592 The device number on which the file exists (the @code{st_dev} field of
1593 @code{struct stat}), in decimal.
1594 @item %F
1595 Type of the filesystem the file is on; this value can be used for
1596 @samp{-fstype} (@pxref{Directories}).
1597 @item %l
1598 Object of symbolic link (empty string if file is not a symbolic link).
1599 @item %i
1600 File's inode number (in decimal).
1601 @item %n
1602 Number of hard links to file.
1603 @item %y
1604 Type of the file as used with @samp{-type}.  If the file is a symbolic
1605 link, @samp{l} will be printed.
1606 @item %Y
1607 Type of the file as used with @samp{-type}.  If the file is a symbolic
1608 link, it is dereferenced.  If the file is a broken symbolic link,
1609 @samp{N} is printed.
1611 @end table
1613 @node Time Directives
1614 @subsubsection Time Directives
1616 Some of these directives use the C @code{ctime} function.  Its output
1617 depends on the current locale, but it typically looks like
1619 @example
1620 Wed Nov  2 00:42:36 1994
1621 @end example
1623 @table @code
1624 @item %a
1625 File's last access time in the format returned by the C @code{ctime}
1626 function.
1627 @item %A@var{k}
1628 File's last access time in the format specified by @var{k}
1629 (@pxref{Time Formats}).
1630 @item %c
1631 File's last status change time in the format returned by the C
1632 @code{ctime} function.
1633 @item %C@var{k}
1634 File's last status change time in the format specified by @var{k}
1635 (@pxref{Time Formats}).
1636 @item %t
1637 File's last modification time in the format returned by the C
1638 @code{ctime} function.
1639 @item %T@var{k}
1640 File's last modification time in the format specified by @var{k}
1641 (@pxref{Time Formats}).
1642 @end table
1644 @node Time Formats
1645 @subsection Time Formats
1647 Below are the formats for the directives @samp{%A}, @samp{%C}, and
1648 @samp{%T}, which print the file's timestamps.  Some of these formats
1649 might not be available on all systems, due to differences in the C
1650 @code{strftime} function between systems.
1652 @menu
1653 * Time Components::
1654 * Date Components::
1655 * Combined Time Formats::
1656 @end menu
1658 @node Time Components
1659 @subsubsection Time Components
1661 The following format directives print single components of the time.
1663 @table @code
1664 @item H
1665 hour (00..23)
1666 @item I
1667 hour (01..12)
1668 @item k
1669 hour ( 0..23)
1670 @item l
1671 hour ( 1..12)
1672 @item p
1673 locale's AM or PM
1674 @item Z
1675 time zone (e.g., EDT), or nothing if no time zone is determinable
1676 @item M
1677 minute (00..59)
1678 @item S
1679 second (00..61)
1680 @item @@
1681 seconds since Jan. 1, 1970, 00:00 GMT.
1682 @end table
1684 @node Date Components
1685 @subsubsection Date Components
1687 The following format directives print single components of the date.
1689 @table @code
1690 @item a
1691 locale's abbreviated weekday name (Sun..Sat)
1692 @item A
1693 locale's full weekday name, variable length (Sunday..Saturday)
1694 @item b
1695 @itemx h
1696 locale's abbreviated month name (Jan..Dec)
1697 @item B
1698 locale's full month name, variable length (January..December)
1699 @item m
1700 month (01..12)
1701 @item d
1702 day of month (01..31)
1703 @item w
1704 day of week (0..6)
1705 @item j
1706 day of year (001..366)
1707 @item U
1708 week number of year with Sunday as first day of week (00..53)
1709 @item W
1710 week number of year with Monday as first day of week (00..53)
1711 @item Y
1712 year (1970@dots{})
1713 @item y
1714 last two digits of year (00..99)
1715 @end table
1717 @node Combined Time Formats
1718 @subsubsection Combined Time Formats
1720 The following format directives print combinations of time and date
1721 components.
1723 @table @code
1724 @item r
1725 time, 12-hour (hh:mm:ss [AP]M)
1726 @item T
1727 time, 24-hour (hh:mm:ss)
1728 @item X
1729 locale's time representation (H:M:S)
1730 @item c
1731 locale's date and time (Sat Nov 04 12:02:33 EST 1989)
1732 @item D
1733 date (mm/dd/yy)
1734 @item x
1735 locale's date representation (mm/dd/yy)
1736 @item +
1737 Date and time, separated by '+', for example `2004-04-28+22:22:05'.
1738 The time is given in the current timezone (which may be affected by
1739 setting the TZ environment variable).  This is a GNU extension.
1740 @end table
1742 @node Formatting Flags
1743 @subsubsection Formatting Flags
1745 The @samp{%m} and @samp{%d} directives support the @samp{#}, @samp{0}
1746 and @samp{+} flags, but the other directives do not, even if they
1747 print numbers.  Numeric directives that do not support these flags
1748 include
1750 @samp{G},
1751 @samp{U},
1752 @samp{b},
1753 @samp{D},
1754 @samp{k} and
1755 @samp{n}.
1757 All fields support the format flag @samp{-}, which makes fields
1758 left-aligned.  That is, if the field width is greater than the actual
1759 contents of the field, the requisite number of spaces are printed
1760 after the field content instead of before it.
1762 @node Run Commands
1763 @section Run Commands
1765 You can use the list of file names created by @code{find} or
1766 @code{locate} as arguments to other commands.  In this way you can
1767 perform arbitrary actions on the files.
1769 @menu
1770 * Single File::
1771 * Multiple Files::
1772 * Querying::
1773 @end menu
1775 @node Single File
1776 @subsection Single File
1778 Here is how to run a command on one file at a time.
1780 @deffn Action -execdir command ;
1781 Execute @var{command}; true if zero status is returned.  @code{find}
1782 takes all arguments after @samp{-exec} to be part of the command until
1783 an argument consisting of @samp{;} is reached.  It replaces the string
1784 @samp{@{@}} by the current file name being processed everywhere it
1785 occurs in the command.  Both of these constructions need to be escaped
1786 (with a @samp{\}) or quoted to protect them from expansion by the
1787 shell.  The command is executed in the directory in which @code{find}
1788 was run.
1790 For example, to compare each C header file in the current directory
1791 with the file @file{/tmp/master}:
1793 @example
1794 find . -name '*.h' -execdir diff -u '@{@}' /tmp/master ';'
1795 @end example
1796 @end deffn
1799 Another similar option, @samp{-exec} is supported, but is less secure.
1800 @xref{Security Considerations}, for a discussion of the security
1801 problems surrounding @samp{-exec}.
1804 @deffn Action -exec command ;
1805 This insecure variant of the @samp{-execdir} action is specified by
1806 POSIX.  The main difference is that the command is executed in the
1807 directory from which @code{find} was invoked, meaning that @samp{@{@}}
1808 is expanded to a relative path starting with the name of one of the
1809 starting directories, rather than just the basename of the matched
1810 file.
1811 @end deffn
1814 @node Multiple Files
1815 @subsection Multiple Files
1817 Sometimes you need to process files one of the time.  But usually this
1818 is not necessary, and, it is faster to run a command on as many files
1819 as possible at a time, rather than once per file.  Doing this saves on
1820 the time it takes to start up the command each time.
1822 The @samp{-execdir} and @samp{-exec} actions have variants that build
1823 command lines containing as many matched files as possible.
1825 @deffn Action -execdir command @{@} +
1826 This works as for @samp{-execdir command ;}, except that the
1827 @samp{@{@}} at the end of the command is expanded to a list of names
1828 of matching files.  This expansion is done in such a way as to avoid
1829 exceeding the maximum command line length available on the system.
1830 Only one @samp{@{@}} is allowed within the command, and it must appear
1831 at the end, immediately before the @samp{+}.  A @samp{+} appearing in
1832 any position other than immediately after @samp{@{@}} is not
1833 considered to be special (that is, it does not terminate the command).
1834 @end deffn
1837 @deffn Action -exec command @{@} +
1838 This insecure variant of the @samp{-execdir} action is specified by
1839 POSIX.  The main difference is that the command is executed in the
1840 directory from which @code{find} was invoked, meaning that @samp{@{@}}
1841 is expanded to a relative path starting with the name of one of the
1842 starting directories, rather than just the basename of the matched
1843 file.
1844 @end deffn
1846 Before @code{find} exits, any partially-built command lines are
1847 executed.  This happens even if the exit was caused by the
1848 @samp{-quit} action.  However, some types of error (for example not
1849 being able to invoke @code{stat()} on the current directory) can cause
1850 an immediate fatal exit.  In this situation, any partially-built
1851 command lines will not be invoked (this prevents possible infinite
1852 loops).
1854 Another, but less secure, way to run a command on more than one file
1855 at once, is to use the @code{xargs} command, which is invoked like
1856 this:
1858 @example
1859 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
1860 @end example
1862 @code{xargs} normally reads arguments from the standard input.  These
1863 arguments are delimited by blanks (which can be protected with double
1864 or single quotes or a backslash) or newlines.  It executes the
1865 @var{command} (default is @file{/bin/echo}) one or more times with any
1866 @var{initial-arguments} followed by arguments read from standard
1867 input.  Blank lines on the standard input are ignored.
1869 Instead of blank-delimited names, it is safer to use @samp{find
1870 -print0} or @samp{find -fprint0} and process the output by giving the
1871 @samp{-0} or @samp{--null} option to GNU @code{xargs}, GNU @code{tar},
1872 GNU @code{cpio}, or @code{perl}.  The @code{locate} command also has a
1873 @samp{-0} or @samp{--null} option which does the same thing.
1875 You can use shell command substitution (backquotes) to process a list
1876 of arguments, like this:
1878 @example
1879 grep -l sprintf `find $HOME -name '*.c' -print`
1880 @end example
1882 However, that method produces an error if the length of the @samp{.c}
1883 file names exceeds the operating system's command line length limit.
1884 @code{xargs} avoids that problem by running the command as many times
1885 as necessary without exceeding the limit:
1887 @example
1888 find $HOME -name '*.c' -print | xargs grep -l sprintf
1889 @end example
1891 However, if the command needs to have its standard input be a terminal
1892 (@code{less}, for example), you have to use the shell command
1893 substitution method or use the @samp{--arg-file} option of
1894 @code{xargs}.
1896 The @code{xargs} command will process all its input, building command
1897 lines and executing them, unless one of the commands exits with a
1898 status of 255 (this will cause xargs to issue an error message and
1899 stop) or it reads a line contains the end of file string specified
1900 with the @samp{--eof} option.
1902 @menu
1903 * Unsafe File Name Handling::
1904 * Safe File Name Handling::
1905 * Unusual Characters in File Names::
1906 * Limiting Command Size::
1907 * Interspersing File Names::
1908 @end menu
1910 @node Unsafe File Name Handling
1911 @subsubsection Unsafe File Name Handling
1913 Because file names can contain quotes, backslashes, blank characters,
1914 and even newlines, it is not safe to process them using @code{xargs}
1915 in its default mode of operation.  But since most files' names do not
1916 contain blanks, this problem occurs only infrequently.  If you are
1917 only searching through files that you know have safe names, then you
1918 need not be concerned about it.
1920 @c This example is adapted from:
1921 @c From: pfalstad@stone.Princeton.EDU (Paul John Falstad)
1922 @c Newsgroups: comp.unix.shell
1923 @c Subject: Re: Beware xargs security holes
1924 @c Date: 16 Oct 90 19:12:06 GMT
1926 In many applications, if @code{xargs} botches processing a file
1927 because its name contains special characters, some data might be lost.
1928 The importance of this problem depends on the importance of the data
1929 and whether anyone notices the loss soon enough to correct it.
1930 However, here is an extreme example of the problems that using
1931 blank-delimited names can cause.  If the following command is run
1932 daily from @code{cron}, then any user can remove any file on the
1933 system:
1935 @example
1936 find / -name '#*' -atime +7 -print | xargs rm
1937 @end example
1939 For example, you could do something like this:
1941 @example
1942 eg$ echo > '#
1943 vmunix'
1944 @end example
1946 @noindent
1947 and then @code{cron} would delete @file{/vmunix}, if it ran
1948 @code{xargs} with @file{/} as its current directory.
1950 To delete other files, for example @file{/u/joeuser/.plan}, you could
1951 do this:
1953 @example
1954 eg$ mkdir '#
1956 eg$ cd '#
1958 eg$ mkdir u u/joeuser u/joeuser/.plan'
1960 eg$ echo > u/joeuser/.plan'
1961 /#foo'
1962 eg$ cd ..
1963 eg$ find . -name '#*' -print | xargs echo
1964 ./# ./# /u/joeuser/.plan /#foo
1965 @end example
1967 @node Safe File Name Handling
1968 @subsubsection Safe File Name Handling
1970 Here is how to make @code{find} output file names so that they can be
1971 used by other programs without being mangled or misinterpreted.  You
1972 can process file names generated this way by giving the @samp{-0} or
1973 @samp{--null} option to GNU @code{xargs}, GNU @code{tar}, GNU
1974 @code{cpio}, or @code{perl}.
1976 @deffn Action -print0
1977 True; print the entire file name on the standard output, followed by a
1978 null character.
1979 @end deffn
1981 @deffn Action -fprint0 file
1982 True; like @samp{-print0} but write to @var{file} like @samp{-fprint}
1983 (@pxref{Print File Name}).
1984 @end deffn
1986 As of findutils version 4.2.4, the @code{locate} program also has a
1987 @samp{--null} option which does the same thing.  For similarity with
1988 @code{xargs}, the short form of the option @samp{-0} can also be used.
1990 If you want to be able to handle file names safely but need to run
1991 commands which want to be connected to a terminal on their input, you
1992 can use the @samp{--arg-file} option to @code{xargs} like this:
1994 @example
1995 find / -name xyzzy -print0 > list
1996 xargs --null --arg-file=list munge
1997 @end example
1999 The example above runs the @code{munge} program on all the files named
2000 @file{xyzzy} that we can find, but @code{munge}'s input will still be
2001 the terminal (or whatever the shell was using as standard input).  If
2002 your shell has the ``process substitution'' feature @samp{<(...)}, you
2003 can do this in just one step:
2005 @example
2006 xargs --null --arg-file=<(find / -name xyzzy -print0) munge
2007 @end example
2009 @node Unusual Characters in File Names
2010 @subsubsection Unusual Characters in File Names
2011 As discussed above, you often need to be careful about how the names
2012 of files are handled by @code{find} and other programs.  If the output
2013 of @code{find} is not going to another program but instead is being
2014 shown on a terminal, this can still be a problem.  For example, some
2015 character sequences can reprogram the function keys on some terminals.
2016 @xref{Security Considerations}, for a discussion of other security
2017 problems relating to @code{find}.
2019 Unusual characters are handled differently by various
2020 actions, as described below.
2022 @table @samp
2023 @item -print0
2024 @itemx -fprint0
2025 Always print the exact file name, unchanged, even if the output is
2026 going to a terminal.
2027 @item -ok
2028 @itemx -okdir
2029 Always print the exact file name, unchanged.  This will probably
2030 change in a future release.
2031 @item -ls
2032 @itemx -fls
2033 Unusual characters are always escaped.  White space, backslash, and
2034 double quote characters are printed using C-style escaping (for
2035 example @samp{\f}, @samp{\"}).  Other unusual characters are printed
2036 using an octal escape.  Other Printable characters (for @samp{-ls} and
2037 @samp{-fls} these are the characters between octal 041 and 0176) are
2038 printed as-is.
2039 @item -printf
2040 @itemx -fprintf
2041 If the output is not going to a terminal, it is printed as-is.
2042 Otherwise, the result depends on which directive is in use:
2044 @table @asis
2045 @item %D, %F, %H, %Y, %y
2046 These expand to values which are not under control of files' owners,
2047 and so are printed as-is.
2048 @item  %a, %b, %c, %d, %g, %G, %i, %k, %m, %M, %n, %s, %t, %u, %U
2049 These have values which are under the control of files' owners but
2050 which cannot be used to send arbitrary data to the terminal, and so
2051 these are printed as-is.
2052 @item %f, %h, %l, %p, %P
2053 The output of these directives is quoted if the output is going to a
2054 terminal.
2056 This quoting is performed in the same way as for GNU @code{ls}.  This
2057 is not the same quoting mechanism as the one used for @samp{-ls} and
2058 @samp{fls}.  If you are able to decide what format to use for the
2059 output of @code{find} then it is normally better to use @samp{\0} as a
2060 terminator than to use newline, as file names can contain white space
2061 and newline characters.
2062 @end table
2063 @item -print
2064 @itemx -fprint
2065 Quoting is handled in the same way as for the @samp{%p} directive of
2066 @samp{-printf} and @samp{-fprintf}.  If you are using @code{find} in a
2067 script or in a situation where the matched files might have arbitrary
2068 names, you should consider using @samp{-print0} instead of
2069 @samp{-print}.
2070 @end table
2073 The @code{locate} program quotes and escapes unusual characters in
2074 file names in the same way as @code{find}'s @samp{-print} action.
2076 The behaviours described above may change soon, as the treatment of
2077 unprintable characters is harmonised for @samp{-ls}, @samp{-fls},
2078 @samp{-print}, @samp{-fprint}, @samp{-printf} and @samp{-fprintf}.
2080 @node Limiting Command Size
2081 @subsubsection Limiting Command Size
2083 @code{xargs} gives you control over how many arguments it passes to
2084 the command each time it executes it.  By default, it uses up to
2085 @code{ARG_MAX} - 2k, or 128k, whichever is smaller, characters per
2086 command.  It uses as many lines and arguments as fit within that
2087 limit.  The following options modify those values.
2089 @table @code
2090 @item --no-run-if-empty
2091 @itemx -r
2092 If the standard input does not contain any nonblanks, do not run the
2093 command.  By default, the command is run once even if there is no
2094 input.
2096 @item --max-lines@r{[}=@var{max-lines}@r{]}
2097 @itemx -L @var{max-lines}
2098 @itemx -l@r{[}@var{max-lines}@r{]}
2099 Use at most @var{max-lines} nonblank input lines per command line;
2100 @var{max-lines} defaults to 1 if omitted; omitting the argument is not
2101 allowed in the case of the @samp{-L} option.  Trailing blanks cause an
2102 input line to be logically continued on the next input line, for the
2103 purpose of counting the lines.  Implies @samp{-x}.  The preferred name
2104 for this option is @samp{-L} as this is specified by POSIX.  
2106 @item --max-args=@var{max-args}
2107 @itemx -n @var{max-args}
2108 Use at most @var{max-args} arguments per command line.  Fewer than
2109 @var{max-args} arguments will be used if the size (see the @samp{-s}
2110 option) is exceeded, unless the @samp{-x} option is given, in which
2111 case @code{xargs} will exit.
2113 @item --max-chars=@var{max-chars}
2114 @itemx -s @var{max-chars}
2115 Use at most @var{max-chars} characters per command line, including the
2116 command initial arguments and the terminating nulls at the ends of
2117 the argument strings.  If you specify a value for this option which is
2118 too large or small, a warning message is printed and the appropriate
2119 upper or lower limit is used instead.
2121 @item --max-procs=@var{max-procs}
2122 @itemx -P @var{max-procs}
2123 Run up to @var{max-procs} processes at a time; the default is 1.  If
2124 @var{max-procs} is 0, @code{xargs} will run as many processes as
2125 possible at a time.  Use the @samp{-n}, @samp{-s}, or @samp{-L} option
2126 with @samp{-P}; otherwise chances are that the command will be run
2127 only once.
2128 @end table
2130 @node Interspersing File Names
2131 @subsubsection Interspersing File Names
2133 @code{xargs} can insert the name of the file it is processing between
2134 arguments you give for the command.  Unless you also give options to
2135 limit the command size (@pxref{Limiting Command Size}), this mode of
2136 operation is equivalent to @samp{find -exec} (@pxref{Single File}).
2138 @table @code
2139 @item --replace@r{[}=@var{replace-str}@r{]}
2140 @itemx -I @var{replace-str}
2141 @itemx -i @var{replace-str}
2142 Replace occurrences of @var{replace-str} in the initial arguments with
2143 names read from the input.  Also, unquoted blanks do not terminate
2144 arguments; instead, the input is split at newlines only.  For the
2145 @samp{-i} option, if @var{replace-str} is omitted for @samp{--replace}
2146 or @samp{-i}, it defaults to @samp{@{@}} (like for @samp{find -exec}).
2147 Implies @samp{-x} and @samp{-l 1}.  @samp{-i} is deprecated in favour
2148 of @samp{-I}. As an example, to sort each file in the @file{bills}
2149 directory, leaving the output in that file name with @file{.sorted}
2150 appended, you could do:
2152 @example
2153 find bills -type f | xargs -I XX sort -o XX.sorted XX
2154 @end example
2156 @noindent
2157 The equivalent command using @samp{find -execdir} is:
2159 @example
2160 find bills -type f -execdir sort -o '@{@}.sorted' '@{@}' ';'
2161 @end example
2162 @end table
2164 @node Querying
2165 @subsection Querying
2167 To ask the user whether to execute a command on a single file, you can
2168 use the @code{find} primary @samp{-okdir} instead of @samp{-execdir},
2169 and the @code{find} primary @samp{-ok} instead of @samp{-exec}:
2171 @deffn Action -okdir command ;
2172 Like @samp{-execdir} (@pxref{Single File}), but ask the user first (on
2173 the standard input); if the response does not start with @samp{y} or
2174 @samp{Y}, do not run the command, and return false.  If the command is
2175 run, its standard input is redirected from @file{/dev/null}.
2176 @end deffn
2178 @deffn Action -ok command ;
2179 This insecure variant of the @samp{-okdir} action is specified by
2180 POSIX.  The main difference is that the command is executed in the
2181 directory from which @code{find} was invoked, meaning that @samp{@{@}}
2182 is expanded to a relative path starting with the name of one of the
2183 starting directories, rather than just the basename of the matched
2184 file.  If the command is run, its standard input is redirected from
2185 @file{/dev/null}.
2186 @end deffn
2188 When processing multiple files with a single command, to query the
2189 user you give @code{xargs} the following option.  When using this
2190 option, you might find it useful to control the number of files
2191 processed per invocation of the command (@pxref{Limiting Command
2192 Size}).
2194 @table @code
2195 @item --interactive
2196 @itemx -p
2197 Prompt the user about whether to run each command line and read a line
2198 from the terminal.  Only run the command line if the response starts
2199 with @samp{y} or @samp{Y}.  Implies @samp{-t}.
2200 @end table
2202 @node Delete Files
2203 @section Delete Files
2205 @deffn Action -delete
2206 Delete files or directories; true if removal succeeded.  If the
2207 removal failed, an error message is issued.
2209 The use of the @samp{-delete} action on the command line automatically
2210 turns on the @samp{-depth} option (@pxref{find Expressions}).
2211 @end deffn
2213 @node Adding Tests
2214 @section Adding Tests
2216 You can test for file attributes that none of the @code{find} builtin
2217 tests check.  To do this, use @code{xargs} to run a program that
2218 filters a list of files printed by @code{find}.  If possible, use
2219 @code{find} builtin tests to pare down the list, so the program run by
2220 @code{xargs} has less work to do.  The tests builtin to @code{find}
2221 will likely run faster than tests that other programs perform.
2223 For reasons of efficiency it is often useful to limit the number of
2224 times an external program has to be run.  For this reason, it is often
2225 a good idea to implement ``extended'' tests by using @code{xargs}.
2227 For example, here is a way to print the names of all of the unstripped
2228 binaries in the @file{/usr/local} directory tree.  Builtin tests avoid
2229 running @code{file} on files that are not regular files or are not
2230 executable.
2232 @example
2233 find /usr/local -type f -perm /a=x | xargs file |
2234   grep 'not stripped' | cut -d: -f1
2235 @end example
2237 @noindent
2238 The @code{cut} program removes everything after the file name from the
2239 output of @code{file}.
2241 However, using @code{xargs} can present important security problems
2242 (@pxref{Security Considerations}).  These can be avoided by using
2243 @samp{-execdir}.  The @samp{-execdir} action is also a useful way of
2244 putting your own test in the middle of a set of other tests or actions
2245 for @code{find} (for example, you might want to use @samp{-prune}).
2247 @c Idea from Martin Weitzel.
2248 To place a special test somewhere in the middle of a @code{find}
2249 expression, you can use @samp{-execdir} (or, less securely,
2250 @samp{-exec}) to run a program that performs the test.  Because
2251 @samp{-execdir} evaluates to the exit status of the executed program,
2252 you can use a program (which can be a shell script) that tests for a
2253 special attribute and make it exit with a true (zero) or false
2254 (non-zero) status.  It is a good idea to place such a special test
2255 @emph{after} the builtin tests, because it starts a new process which
2256 could be avoided if a builtin test evaluates to false.
2258 Here is a shell script called @code{unstripped} that checks whether
2259 its argument is an unstripped binary file:
2261 @example
2262 #! /bin/sh
2263 file "$1" | grep -q "not stripped"
2264 @end example
2267 This script relies on the shell exiting with the status of
2268 the last command in the pipeline, in this case @code{grep}.  The
2269 @code{grep} command exits with a true status if it found any matches,
2270 false if not.  Here is an example of using the script (assuming it is
2271 in your search path).  It lists the stripped executables (and shell
2272 scripts) in the file @file{sbins} and the unstripped ones in
2273 @file{ubins}.
2275 @example
2276 find /usr/local -type f -perm /a=x \
2277   \( -execdir unstripped '@{@}' \; -fprint ubins -o -fprint sbins \)
2278 @end example
2281 @node Databases, File Permissions, Actions, Top
2282 @chapter File Name Databases
2284 The file name databases used by @code{locate} contain lists of files
2285 that were in particular directory trees when the databases were last
2286 updated.  The file name of the default database is determined when
2287 @code{locate} and @code{updatedb} are configured and installed.  The
2288 frequency with which the databases are updated and the directories for
2289 which they contain entries depend on how often @code{updatedb} is run,
2290 and with which arguments.
2292 You can obtain some statistics about the databases by using
2293 @samp{locate --statistics}.
2295 @menu
2296 * Database Locations::
2297 * Database Formats::
2298 * Newline Handling::
2299 @end menu
2302 @node Database Locations
2303 @section Database Locations
2305 There can be multiple file name databases.  Users can select which
2306 databases @code{locate} searches using the @code{LOCATE_PATH}
2307 environment variable or a command line option.  The system
2308 administrator can choose the file name of the default database, the
2309 frequency with which the databases are updated, and the directories
2310 for which they contain entries.  File name databases are updated by
2311 running the @code{updatedb} program, typically nightly.
2313 In networked environments, it often makes sense to build a database at
2314 the root of each filesystem, containing the entries for that
2315 filesystem.  @code{updatedb} is then run for each filesystem on the
2316 fileserver where that filesystem is on a local disk, to prevent
2317 thrashing the network.
2319 @xref{Invoking updatedb},
2320 for the description of the options to @code{updatedb}, which specify
2321 which directories would each database contain entries for.
2324 @node Database Formats
2325 @section Database Formats
2327 The file name databases contain lists of files that were in particular
2328 directory trees when the databases were last updated.  The file name
2329 database format changed starting with GNU @code{locate} version 4.0 to
2330 allow machines with different byte orderings to share the databases.
2331 The new GNU @code{locate} can read both the old and new database
2332 formats.  However, old versions of @code{locate} and @code{find}
2333 produce incorrect results if given a new-format database.
2335 If you run @samp{locate --statistics}, the resulting summary indicates
2336 the type of each @code{locate} database.
2339 @menu
2340 * New Database Format::
2341 * Sample Database::
2342 * Old Database Format::
2343 @end menu
2345 @node New Database Format
2346 @subsection New Database Format
2348 @code{updatedb} runs a program called @code{frcode} to
2349 @dfn{front-compress} the list of file names, which reduces the
2350 database size by a factor of 4 to 5.  Front-compression (also known as
2351 incremental encoding) works as follows.
2353 The database entries are a sorted list (case-insensitively, for users'
2354 convenience).  Since the list is sorted, each entry is likely to share
2355 a prefix (initial string) with the previous entry.  Each database
2356 entry begins with an offset-differential count byte, which is the
2357 additional number of characters of prefix of the preceding entry to
2358 use beyond the number that the preceding entry is using of its
2359 predecessor.  (The counts can be negative.)  Following the count is a
2360 null-terminated ASCII remainder---the part of the name that follows
2361 the shared prefix.
2363 If the offset-differential count is larger than can be stored in a
2364 byte (+/-127), the byte has the value 0x80 and the count follows in a
2365 2-byte word, with the high byte first (network byte order).
2367 Every database begins with a dummy entry for a file called
2368 @file{LOCATE02}, which @code{locate} checks for to ensure that the
2369 database file has the correct format; it ignores the entry in doing
2370 the search.
2372 Databases cannot be concatenated together, even if the first (dummy)
2373 entry is trimmed from all but the first database.  This is because the
2374 offset-differential count in the first entry of the second and
2375 following databases will be wrong.
2377 In the output of @samp{locate --statistics}, the new database format
2378 is referred to as @samp{LOCATE02}.
2380 @node Sample Database
2381 @subsection Sample Database
2383 Sample input to @code{frcode}:
2384 @c with nulls changed to newlines:
2386 @example
2387 /usr/src
2388 /usr/src/cmd/aardvark.c
2389 /usr/src/cmd/armadillo.c
2390 /usr/tmp/zoo
2391 @end example
2393 Length of the longest prefix of the preceding entry to share:
2395 @example
2396 0 /usr/src
2397 8 /cmd/aardvark.c
2398 14 rmadillo.c
2399 5 tmp/zoo
2400 @end example
2402 Output from @code{frcode}, with trailing nulls changed to newlines
2403 and count bytes made printable:
2405 @example
2406 0 LOCATE02
2407 0 /usr/src
2408 8 /cmd/aardvark.c
2409 6 rmadillo.c
2410 -9 tmp/zoo
2411 @end example
2413 (6 = 14 - 8, and -9 = 5 - 14)
2415 @node Old Database Format
2416 @subsection Old Database Format
2418 The old database format is used by Unix @code{locate} and @code{find}
2419 programs and earlier releases of the GNU ones.  @code{updatedb}
2420 produces this format if given the @samp{--old-format} option.
2422 @code{updatedb} runs programs called @code{bigram} and @code{code} to
2423 produce old-format databases.  The old format differs from the new one
2424 in the following ways.  Instead of each entry starting with an
2425 offset-differential count byte and ending with a null, byte values
2426 from 0 through 28 indicate offset-differential counts from -14 through
2427 14.  The byte value indicating that a long offset-differential count
2428 follows is 0x1e (30), not 0x80.  The long counts are stored in host
2429 byte order, which is not necessarily network byte order, and host
2430 integer word size, which is usually 4 bytes.  They also represent a
2431 count 14 less than their value.  The database lines have no
2432 termination byte; the start of the next line is indicated by its first
2433 byte having a value <= 30.
2435 In addition, instead of starting with a dummy entry, the old database
2436 format starts with a 256 byte table containing the 128 most common
2437 bigrams in the file list.  A bigram is a pair of adjacent bytes.
2438 Bytes in the database that have the high bit set are indexes (with the
2439 high bit cleared) into the bigram table.  The bigram and
2440 offset-differential count coding makes these databases 20-25% smaller
2441 than the new format, but makes them not 8-bit clean.  Any byte in a
2442 file name that is in the ranges used for the special codes is replaced
2443 in the database by a question mark, which not coincidentally is the
2444 shell wildcard to match a single character.
2446 The old format therefore cannot faithfully store entries with
2447 non-ASCII characters. It therefore should not be used in
2448 internationalized environments.
2450 The output of @samp{locate --statistics} will give an incorrect count
2451 of the number of file names containing newlines or high-bit characters
2452 for old-format databases.
2454 @node Newline Handling
2455 @section Newline Handling
2457 Within the database, file names are terminated with a null character.
2458 This is the case for both the old and the new format.
2460 When the new database format is being used, the compression technique
2461 used to generate the database though relies on the ability to sort the
2462 list of files before they are presented to @code{frcode}.
2464 If the system's sort command allows its input list of files to be
2465 separated with null characters via the @samp{-z} option, this option
2466 is used and therefore @code{updatedb} and @code{locate} will both
2467 correctly handle file names containing newlines.  If the @code{sort}
2468 command lacks support for this, the list of files is delimited with
2469 the newline character, meaning that parts of file names containing
2470 newlines will be incorrectly sorted.  This can result in both
2471 incorrect matches and incorrect failures to match.
2473 On the other hand, if you are using the old database format, file
2474 names with embedded newlines are not correctly handled.  There is no
2475 technical limitation which enforces this, it's just that the
2476 @code{bigram} program has not been updated to support lists of file
2477 names separated by nulls.
2479 So, if you are using the new database format (this is the default) and
2480 your system uses GNU @code{sort}, newlines will be correctly handled
2481 at all times.  Otherwise, newlines may not be correctly handled.
2483 @node File Permissions, Reference, Databases, Top
2484 @chapter File Permissions
2486 @include perm.texi
2488 @node Reference, Common Tasks, File Permissions, Top
2489 @chapter Reference
2491 Below are summaries of the command line syntax for the programs
2492 discussed in this manual.
2494 @menu
2495 * Invoking find::
2496 * Invoking locate::
2497 * Invoking updatedb::
2498 * Invoking xargs::
2499 * Regular Expressions::
2500 @end menu
2502 @node Invoking find, Invoking locate, , Reference
2503 @section Invoking @code{find}
2505 @example
2506 find @r{[-H] [-L] [-P]} @r{[}@var{file}@dots{}@r{]} @r{[}@var{expression}@r{]}
2507 @end example
2509 @code{find} searches the directory tree rooted at each file name
2510 @var{file} by evaluating the @var{expression} on each file it finds in
2511 the tree.
2513 The options @samp{-H}, @samp{-L} or @samp{-P} may be specified at the
2514 start of the command line (if none of these is specified, @samp{-P} is
2515 assumed).  The arguments after these are a list of files or
2516 directories that should be searched.
2518 This list of files to search is followed by a list of expressions
2519 describing the files we wish to search for.  The first part of the
2520 expression is recognised by the fact that it begins with @samp{-},
2521 @samp{(}, @samp{)}, @samp{,}, or @samp{!}.  Any arguments after it are
2522 the rest of the expression.  If no files are given, the current
2523 directory is used.  If no expression is given, the expression
2524 @samp{-print} is used.
2526 @code{find} exits with status zero if all files matched are processed
2527 successfully, greater than 0 if errors occur.
2529 Three options can precede the list of files.  They determine the
2530 way that symbolic links are handled.
2532 @table @code
2533 @item -P
2534 Never follow symbolic links (this is the default), except in the case
2535 of the @samp{-xtype} predicate.
2536 @item -L
2537 Always follow symbolic links, except in the case of the @samp{-xtype}
2538 predicate.
2539 @item -H
2540 Follow symbolic links specified in the list of files to search, or
2541 which are otherwise specified on the command line.
2542 @end table
2544 If @code{find} would follow a symbolic link, but cannot for any reason
2545 (for example, because it has insufficient permissions or the link is
2546 broken), it falls back on using the properties of the symbolic link
2547 itself.  @ref{Symbolic Links} for a more complete description of how
2548 symbolic links are handled.
2550 @xref{Primary Index}, for a summary of all of the tests, actions, and
2551 options that the expression can contain.  If the expression is
2552 missing, @samp{-print} is assumed.
2555 @code{find} also recognizes two options for administrative use:
2557 @table @code
2558 @item --help
2559 Print a summary of the command line usage and exit.
2560 @item --version
2561 Print the version number of @code{find} and exit.
2562 @end table
2565 @menu
2566 * Warning Messages::
2567 @end menu
2570 @node Warning Messages,,, Invoking find
2571 @subsection Warning Messages
2573 If there is an error on the @code{find} command line, an error message
2574 is normally issued.  However, there are some usages that are
2575 inadvisable but which @code{find} should still accept.  Under these
2576 circumstances, @code{find} may issue a warning message.  By default,
2577 warnings are enabled only if @code{find} is being run interactively
2578 (specifically, if the standard input is a terminal).  Warning messages
2579 can be controlled explicitly by the use of options on the command
2580 line:
2582 @table @code
2583 @item -warn
2584 Issue warning messages where appropriate.
2585 @item -nowarn
2586 Do not issue warning messages.
2587 @end table
2589 These options take effect at the point on the command line where they
2590 are specified.  Therefore if you specify @samp{-nowarn} at the end of
2591 the command line, you will not see warning messages for any problems
2592 occurring before that.  The warning messages affected by the above
2593 options are triggered by:
2595 @itemize @minus
2596 @item
2597 Use of the @samp{-d} option which is deprecated; please use
2598 @samp{-depth} instead, since the latter is POSIX-compliant.
2599 @item
2600 Use of the @samp{-ipath} option which is deprecated; please use
2601 @samp{-iwholename} instead.
2602 @item
2603 Specifying an option (for example @samp{-mindepth}) after a non-option
2604 (for example @samp{-type} or @samp{-print}) on the command line.
2605 @end itemize
2608 The default behaviour above is designed to work in that way so that
2609 existing shell scripts which use such constructs don't generate
2610 spurious errors, but people will be made aware of the problem.
2612 Some warning messages are issued for less common or more serious
2613 problems, and consequently cannot be turned off:
2615 @itemize @minus
2616 @item
2617 Use of an unrecognised backslash escape sequence with @samp{-fprintf}
2618 @item
2619 Use of an unrecognised formatting directive with @samp{-fprintf}
2620 @end itemize
2622 @node Invoking locate, Invoking updatedb, Invoking find, Reference
2623 @section Invoking @code{locate}
2625 @example
2626 locate @r{[}@var{option}@dots{}@r{]} @var{pattern}@dots{}
2627 @end example
2629 For each @var{pattern} given @code{locate} searches one or more file
2630 name databases returning each match of @var{pattern}.
2632 For each @var{pattern} given @code{locate} searches one or more file
2633 name databases returning each match of @var{pattern}.
2635 @table @code
2636 @item --all
2637 @itemx -A
2638 Print only names which match all non-option arguments, not those
2639 matching one or more non-option arguments.
2641 @item --basename
2642 @itemx -b
2643 The specified pattern is matched against just the last component of
2644 the name of a file in the @code{locate} database.  This last
2645 component is also called the ``base name''.  For example, the base
2646 name of @file{/tmp/mystuff/foo.old.c} is @file{foo.old.c}.  If the
2647 pattern contains metacharacters, it must match the base name exactly.
2648 If not, it must match part of the base name.
2650 @item --count
2651 @itemx -c
2652 Instead of printing the matched file names, just print the total
2653 number of matches found, unless @samp{--print} (@samp{-p}) is also
2654 present.
2657 @item --database=@var{path}
2658 @itemx -d @var{path}
2659 Instead of searching the default @code{locate} database, @code{locate} search the file
2660 name databases in @var{path}, which is a colon-separated list of
2661 database file names.  You can also use the environment variable
2662 @code{LOCATE_PATH} to set the list of database files to search.  The
2663 option overrides the environment variable if both are used.  Empty
2664 elements in @var{path} (that is, a leading or trailing colon, or two
2665 colons in a row) are taken to stand for the default database.
2666 A database can be supplied on stdin, using @samp{-} as an element
2667 of @samp{path}. If more than one element of @samp{path} is @samp{-},
2668 later instances are ignored (but a warning message is printed).
2670 @item --existing
2671 @itemx -e
2672 Only print out such names which currently exist (instead of such names
2673 which existed when the database was created).  Note that this may slow
2674 down the program a lot, if there are many matches in the database.
2675 The way in which broken symbolic links are treated is affected by the
2676 @samp{-L}, @samp{-P} and @samp{-H} options.
2678 @item --non-existing
2679 @itemx -E
2680 Only print out such names which currently do not exist (instead of
2681 such names which existed when the database was created).  Note that
2682 this may slow down the program a lot, if there are many matches in the
2683 database.  The way in which broken symbolic links are treated is
2684 affected by the @samp{-L}, @samp{-P} and @samp{-H} options.
2686 @item --follow
2687 @itemx -L
2688 If testing for the existence of files (with the @samp{-e} or @samp{-E}
2689 options), consider broken symbolic links to be non-existing.  This is
2690 the default behaviour.
2693 @item --nofollow
2694 @itemx -P
2695 @itemx -H
2696 If testing for the existence of files (with the @samp{-e} or @samp{-E}
2697 options), treat broken symbolic links as if they were existing files.
2698 The @samp{-H} form of this option is provided purely for similarity
2699 with @code{find}; the use of @samp{-P} is recommended over @samp{-H}.
2701 @item --ignore-case
2702 @itemx -i
2703 Ignore case distinctions in both the pattern and the file names.
2705 @item --limit=N
2706 @itemx -l N
2707 Limit the number of results printed to N.  When used with the
2708 @samp{--count} option, the value printed will never be larger than
2709 this limit.
2711 @item --mmap
2712 @itemx -m
2713 Accepted but does nothing.  The option is supported only to provide
2714 compatibility with BSD's @code{locate}.
2716 @item --null
2717 @itemx -0
2718 Results are separated with the ASCII NUL character rather than the
2719 newline character.  To get the full benefit of the use of this option,
2720 use the new @code{locate} database format (that is the default
2721 anyway).
2723 @item --print
2724 @itemx -p
2725 Print search results when they normally would not, because of the
2726 presence of @samp{--statistics} (@samp{-S}) or @samp{--count}
2727 (@samp{-c}).
2729 @item --wholename
2730 @itemx -w
2731 The specified pattern is matched against the whole name of the file in
2732 the @code{locate} database.  If the pattern contains metacharacters,
2733 it must match exactly.  If not, it must match part of the whole file
2734 name.  This is the default behaviour.
2736 @item --regex
2737 @itemx -r
2738 Instead of using substring or shell glob matching, the pattern
2739 specified on the command line is understood to be a regular
2740 expression.  GNU Emacs-style regular expressions are assumed unless
2741 the @samp{--regextype} option is also given.  File names from the
2742 @code{locate} database are matched using the specified regular
2743 expression.  If the @samp{-i} flag is also given, matching is
2744 case-insensitive.  Matches are performed against the whole path name,
2745 and so by default a pathname will be matched if any part of it matches
2746 the specified regular expression.  The regular expression may use
2747 @samp{^} or @samp{$} to anchor a match at the beginning or end of a
2748 pathname.
2750 @item --regextype
2751 This option changes the regular expression syntax and behaviour used
2752 by the @samp{--regex} option.  @ref{Regular Expressions} for more
2753 information on the regular expression dialects understood by GNU
2754 findutils.
2756 @item --stdio
2757 @itemx -s
2758 Accepted but does nothing.  The option is supported only to provide
2759 compatibility with BSD's @code{locate}.
2761 @item --statistics
2762 @itemx -S
2763 Print some summary information for each @code{locate} database.  No
2764 search is performed unless non-option arguments are given.
2766 @item --help
2767 Print a summary of the command line usage for @code{locate} and exit.
2769 @item --version
2770 Print the version number of @code{locate} and exit.
2771 @end table
2773 @node Invoking updatedb, Invoking xargs, Invoking locate, Reference
2774 @section Invoking @code{updatedb}
2776 @example
2777 updatedb @r{[}@var{option}@dots{}@r{]}
2778 @end example
2780 @code{updatedb} creates and updates the database of file names used by
2781 @code{locate}.  @code{updatedb} generates a list of files similar to
2782 the output of @code{find} and then uses utilities for optimizing the
2783 database for performance.  @code{updatedb} is often run periodically
2784 as a @code{cron} job and configured with environment variables or
2785 command options.  Typically, operating systems have a shell script
2786 that ``exports'' configurations for variable definitions and uses
2787 another schell script that ``sources'' the configuration file into the
2788 environment and then executes @code{updatedb} in the environment.
2790 @code{updatedb} creates and updates the database of file names used by
2791 @code{locate}.  @code{updatedb} generates a list of files similar to
2792 the output of @code{find} and then uses utilities for optimizing the
2793 database for performance.  @code{updatedb} is often run periodically
2794 as a @code{cron} job and configured with environment variables or
2795 command options.  Typically, operating systems have a shell script
2796 that ``exports'' configurations for variable definitions and uses
2797 another schell script that ``sources'' the configuration file into the
2798 environment and then executes @code{updatedb} in the environment.
2800 @table @code
2801 @item --findoptions='@var{OPTION}@dots{}'
2802 Global options to pass on to @code{find}.
2803 The environment variable @code{FINDOPTIONS} also sets this value.
2804 Default is none.
2806 @item --localpaths='@var{path}@dots{}'
2807 Non-network directories to put in the database.
2808 Default is @file{/}.
2810 @item --netpaths='@var{path}@dots{}'
2811 Network (NFS, AFS, RFS, etc.) directories to put in the database.
2812 The environment variable @code{NETPATHS} also sets this value.
2813 Default is none.
2815 @item --prunepaths='@var{path}@dots{}'
2816 Directories to omit from the database, which would otherwise be
2817 included.  The environment variable @code{PRUNEPATHS} also sets this
2818 value.  Default is @file{/tmp /usr/tmp /var/tmp /afs}.  The paths are
2819 used as regular expressions (with @code{find ... -regex}, so you need
2820 to specify these paths in the same way that @code{find} will encounter
2821 them.  This means for example that the paths must not include trailing
2822 slashes.
2824 @item --prunefs='@var{path}@dots{}'
2825 Filesystems to omit from the database, which would otherwise be
2826 included.  Note that files are pruned when a filesystem is reached;
2827 Any filesystem mounted under an undesired filesystem will be ignored.
2828 The environment variable @code{PRUNEFS} also sets this value.  Default
2829 is @file{nfs NFS proc}.
2831 @item --output=@var{dbfile}
2832 The database file to build.  Default is system-dependent, but
2833 typically @file{/usr/local/var/locatedb}.
2835 @item --localuser=@var{user}
2836 The user to search the non-network directories as, using @code{su}.
2837 Default is to search the non-network directories as the current user.
2838 You can also use the environment variable @code{LOCALUSER} to set this user.
2840 @item --netuser=@var{user}
2841 The user to search network directories as, using @code{su}.  Default
2842 @code{user} is @code{daemon}.  You can also use the environment variable
2843 @code{NETUSER} to set this user.
2845 @item --old-format
2846 Generate a @code{locate} database in the old format, for compatibility
2847 with versions of @code{locate} other than GNU @code{locate}.  Using
2848 this option means that @code{locate} will not be able to properly
2849 handle non-ASCII characters in file names (that is, file names
2850 containing characters which have the eighth bit set, such as many of
2851 the characters from the ISO-8859-1 character set).
2852 @item --help
2853 Print a summary of the command line usage and exit.
2854 @item --version
2855 Print the version number of @code{updatedb} and exit.
2856 @end table
2858 @node Invoking xargs, Regular Expressions,  Invoking updatedb, Reference
2859 @section Invoking @code{xargs}
2861 @example
2862 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
2863 @end example
2865 @code{xargs} exits with the following status:
2867 @table @asis
2868 @item 0
2869 if it succeeds
2870 @item 123
2871 if any invocation of the command exited with status 1-125
2872 @item 124
2873 if the command exited with status 255
2874 @item 125
2875 if the command is killed by a signal
2876 @item 126
2877 if the command cannot be run
2878 @item 127
2879 if the command is not found
2880 @item 1
2881 if some other error occurred.
2882 @end table
2884 @table @code
2885 @item --arg-file@r{=@var{inputfile}}
2886 @itemx -a o@r{@var{inputfile}}
2887 Read names from the file @var{inputfile} instead of standard input.
2889 @item --null
2890 @itemx -0
2891 Input file names are terminated by a null character instead of by
2892 whitespace, and any quotes and backslash characters are not considered
2893 special (every character is taken literally).  Disables the end of
2894 file string, which is treated like any other argument.
2896 @item --delimiter @var{delim}
2897 @itemx -d @var{delim}
2899 Input file names are terminated by the specified character @var{delim}
2900 instead of by whitespace, and any quotes and backslash characters are
2901 not considered special (every character is taken literally).  Disables
2902 the end of file string, which is treated like any other argument.
2904 The specified delimiter may be a single character, a C-style character
2905 escape such as @samp{\n}, or an octal or hexadecimal escape code.
2906 Octal and hexadecimal escape codes are understood as for the
2907 @code{printf} command.  Multibyte characters are not supported.
2910 @item --eof@r{[}=@var{eof-str}@r{]}
2911 @itemx -e@r{[}@var{eof-str}@r{]}
2912 Set the end of file string to @var{eof-str}.  If the end of file
2913 string occurs as a line of input, the rest of the input is ignored.
2914 If @var{eof-str} is omitted, there is no end of file string.  If this
2915 option is not given, the end of file string defaults to @samp{_}.
2917 @item --help
2918 Print a summary of the options to @code{xargs} and exit.
2920 @item --replace@r{[}=@var{replace-str}@r{]}
2921 @itemx -I @var{replace-str}
2922 @itemx -i@r{[}@var{replace-str}@r{]}
2923 Replace occurrences of @var{replace-str} in the initial arguments with
2924 names read from standard input.  Also, unquoted blanks do not
2925 terminate arguments; instead, the input is split at newlines only.  If
2926 @var{replace-str} is omitted (omitting it is allowed only for
2927 @samp{-i}), it defaults to @samp{@{@}} (like for @samp{find -exec}).
2928 Implies @samp{-x} and @samp{-l 1}.  @samp{-i} is deprecated in favour
2929 of @samp{-I}.
2931 @item --max-lines@r{[}=@var{max-lines}@r{]}
2932 @itemx -l@r{[}@var{max-lines}@r{]}
2933 Use at most @var{max-lines} non-blank input lines per command line;
2934 @var{max-lines} defaults to 1 if omitted.  Trailing blanks cause an
2935 input line to be logically continued on the next input line, for the
2936 purpose of counting the lines.  Implies @samp{-x}.
2938 @item --max-args=@var{max-args}
2939 @itemx -n @var{max-args}
2940 Use at most @var{max-args} arguments per command line.  Fewer than
2941 @var{max-args} arguments will be used if the size (see the @samp{-s}
2942 option) is exceeded, unless the @samp{-x} option is given, in which
2943 case @code{xargs} will exit.
2945 @item --interactive
2946 @itemx -p
2947 Prompt the user about whether to run each command line and read a line
2948 from the terminal.  Only run the command line if the response starts
2949 with @samp{y} or @samp{Y}.  Implies @samp{-t}.
2951 @item --no-run-if-empty
2952 @itemx -r
2953 If the standard input is completely empty, do not run the
2954 command.  By default, the command is run once even if there is no
2955 input.
2957 @item --max-chars=@var{max-chars}
2958 @itemx -s @var{max-chars}
2959 Use at most @var{max-chars} characters per command line, including the
2960 command, initial arguments and any terminating nulls at the ends of
2961 the argument strings.
2963 @item --verbose
2964 @itemx -t
2965 Print the command line on the standard error output before executing
2968 @item --version
2969 Print the version number of @code{xargs} and exit.
2971 @item --exit
2972 @itemx -x
2973 Exit if the size (see the @samp{-s} option) is exceeded.
2976 @item --max-procs=@var{max-procs}
2977 @itemx -P @var{max-procs}
2978 Run simultaneously up to @var{max-procs} processes at once; the default is 1.  If
2979 @var{max-procs} is 0, @code{xargs} will run as many processes as
2980 possible simultaneously.
2981 @end table
2984 @node Regular Expressions,, Invoking xargs, Reference
2985 @section Regular Expressions
2987 The @samp{-regex} and @samp{-iregex} tests of @code{find} allow
2988 matching by regular expression, as does the @samp{--regex} option of
2989 @code{locate}.  There are many different types of Regular Expression,
2990 but the type used by @code{find} and @code{locate} is the same as is
2991 used in GNU Emacs.  Both programs provide an option which allows you
2992 to select an alternative regular expression syntax; for @code{find}
2993 this is the @samp{-regextype} option, and for @code{locate} this is
2994 the @samp{--regextype} option.
2996 These options take a single argument, which indicates the specific
2997 regular expression syntax and behaviour that should be used.  This
2998 should be one of the following:
3000 @include regexprops.texi
3003 @node Common Tasks, Worked Examples, Reference, Top
3004 @chapter Common Tasks
3006 The sections that follow contain some extended examples that both give
3007 a good idea of the power of these programs, and show you how to solve
3008 common real-world problems.
3010 @menu
3011 * Viewing And Editing::
3012 * Archiving::
3013 * Cleaning Up::
3014 * Strange File Names::
3015 * Fixing Permissions::
3016 * Classifying Files::
3017 @end menu
3019 @node Viewing And Editing
3020 @section Viewing And Editing
3022 To view a list of files that meet certain criteria, simply run your
3023 file viewing program with the file names as arguments.  Shells
3024 substitute a command enclosed in backquotes with its output, so the
3025 whole command looks like this:
3027 @example
3028 less `find /usr/include -name '*.h' | xargs grep -l mode_t`
3029 @end example
3031 @noindent
3032 You can edit those files by giving an editor name instead of a file
3033 viewing program:
3035 @example
3036 emacs `find /usr/include -name '*.h' | xargs grep -l mode_t`
3037 @end example
3039 Because there is a limit to the length of any individual command line,
3040 there is a limit to the number of files that can be handled in this
3041 way.  We can get around this difficulty by using xargs like this:
3043 @example
3044 find /usr/include -name '*.h' | xargs grep -l mode_t > todo
3045 xargs --arg-file=todo emacs
3046 @end example
3048 Here, @code{xargs} will run @code{emacs} as many times as necessary to
3049 visit all of the files listed in the file @file{todo}.
3051 @node Archiving
3052 @section Archiving
3054 You can pass a list of files produced by @code{find} to a file
3055 archiving program.  GNU @code{tar} and @code{cpio} can both read lists
3056 of file names from the standard input---either delimited by nulls (the
3057 safe way) or by blanks (the lazy, risky default way).  To use
3058 null-delimited names, give them the @samp{--null} option.  You can
3059 store a file archive in a file, write it on a tape, or send it over a
3060 network to extract on another machine.
3062 One common use of @code{find} to archive files is to send a list of
3063 the files in a directory tree to @code{cpio}.  Use @samp{-depth} so if
3064 a directory does not have write permission for its owner, its contents
3065 can still be restored from the archive since the directory's
3066 permissions are restored after its contents.  Here is an example of
3067 doing this using @code{cpio}; you could use a more complex @code{find}
3068 expression to archive only certain files.
3070 @example
3071 find . -depth -print0 |
3072   cpio --create --null --format=crc --file=/dev/nrst0
3073 @end example
3075 You could restore that archive using this command:
3077 @example
3078 cpio --extract --null --make-dir --unconditional \
3079   --preserve --file=/dev/nrst0
3080 @end example
3082 Here are the commands to do the same things using @code{tar}:
3084 @example
3085 find . -depth -print0 |
3086   tar --create --null --files-from=- --file=/dev/nrst0
3088 tar --extract --null --preserve-perm --same-owner \
3089   --file=/dev/nrst0
3090 @end example
3092 @c Idea from Rick Sladkey.
3093 Here is an example of copying a directory from one machine to another:
3095 @example
3096 find . -depth -print0 | cpio -0o -Hnewc |
3097   rsh @var{other-machine} "cd `pwd` && cpio -i0dum"
3098 @end example
3100 @node Cleaning Up
3101 @section Cleaning Up
3103 @c Idea from Jim Meyering.
3104 This section gives examples of removing unwanted files in various
3105 situations.  Here is a command to remove the CVS backup files created
3106 when an update requires a merge:
3108 @example
3109 find . -name '.#*' -print0 | xargs -0r rm -f
3110 @end example
3112 The command above works, but the following is safer:
3114 @example
3115 find . -name '.#*' -depth -delete
3116 @end example
3118 @c Idea from Franc,ois Pinard.
3119 You can run this command to clean out your clutter in @file{/tmp}.
3120 You might place it in the file your shell runs when you log out
3121 (@file{.bash_logout}, @file{.logout}, or @file{.zlogout}, depending on
3122 which shell you use).
3124 @example
3125 find /tmp -depth -user "$LOGNAME" -type f -delete
3126 @end example
3128 If your @code{find} command removes directories, you may find that
3129 you get a spurious error message when @code{find} tries to recurse
3130 into a directory that has now been removed.  Using the @samp{-depth}
3131 option will normally resolve this problem.
3133 @c Idea from Noah Friedman.
3134 To remove old Emacs backup and auto-save files, you can use a command
3135 like the following.  It is especially important in this case to use
3136 null-terminated file names because Emacs packages like the VM mailer
3137 often create temporary file names with spaces in them, like
3138 @file{#reply to David J. MacKenzie<1>#}.
3140 @example
3141 find ~ \( -name '*~' -o -name '#*#' \) -print0 |
3142   xargs --no-run-if-empty --null rm -vf
3143 @end example
3145 Removing old files from @file{/tmp} is commonly done from @code{cron}:
3147 @c Idea from Kaveh Ghazi.
3148 @example
3149 find /tmp /var/tmp -not -type d -mtime +3 -delete
3150 find /tmp /var/tmp -depth -mindepth 1 -type d -empty -delete
3151 @end example
3153 The second @code{find} command above uses @samp{-depth} so it cleans
3154 out empty directories depth-first, hoping that the parents become
3155 empty and can be removed too.  It uses @samp{-mindepth} to avoid
3156 removing @file{/tmp} itself if it becomes totally empty.
3158 @node Strange File Names
3159 @section Strange File Names
3161 @c Idea from:
3162 @c From: tmatimar@isgtec.com (Ted Timar)
3163 @c Newsgroups: comp.unix.questions,comp.unix.shell,comp.answers,news.answers
3164 @c Subject: Unix - Frequently Asked Questions (2/7) [Frequent posting]
3165 @c Subject: How do I remove a file with funny characters in the filename ?
3166 @c Date: Thu Mar 18 17:16:55 EST 1993
3167 @code{find} can help you remove or rename a file with strange
3168 characters in its name.  People are sometimes stymied by files whose
3169 names contain characters such as spaces, tabs, control characters, or
3170 characters with the high bit set.  The simplest way to remove such
3171 files is:
3173 @example
3174 rm -i @var{some*pattern*that*matches*the*problem*file}
3175 @end example
3177 @code{rm} asks you whether to remove each file matching the given
3178 pattern.  If you are using an old shell, this approach might not work
3179 if the file name contains a character with the high bit set; the shell
3180 may strip it off.  A more reliable way is:
3182 @example
3183 find . -maxdepth 1 @var{tests} -okdir rm '@{@}' \;
3184 @end example
3186 @noindent
3187 where @var{tests} uniquely identify the file.  The @samp{-maxdepth 1}
3188 option prevents @code{find} from wasting time searching for the file
3189 in any subdirectories; if there are no subdirectories, you may omit
3190 it.  A good way to uniquely identify the problem file is to figure out
3191 its inode number; use
3193 @example
3194 ls -i
3195 @end example
3197 Suppose you have a file whose name contains control characters, and
3198 you have found that its inode number is 12345.  This command prompts
3199 you for whether to remove it:
3201 @example
3202 find . -maxdepth 1 -inum 12345 -okdir rm -f '@{@}' \;
3203 @end example
3205 If you don't want to be asked, perhaps because the file name may
3206 contain a strange character sequence that will mess up your screen
3207 when printed, then use @samp{-execdir} instead of @samp{-okdir}.
3209 If you want to rename the file instead, you can use @code{mv} instead
3210 of @code{rm}:
3212 @example
3213 find . -maxdepth 1 -inum 12345 -okdir mv '@{@}' @var{new-file-name} \;
3214 @end example
3216 @node Fixing Permissions
3217 @section Fixing Permissions
3219 Suppose you want to make sure that everyone can write to the
3220 directories in a certain directory tree.  Here is a way to find
3221 directories lacking either user or group write permission (or both),
3222 and fix their permissions:
3224 @example
3225 find . -type d -not -perm -ug=w | xargs chmod ug+w
3226 @end example
3228 @noindent
3229 You could also reverse the operations, if you want to make sure that
3230 directories do @emph{not} have world write permission.
3232 @node Classifying Files
3233 @section Classifying Files
3235 @c Idea from:
3236 @c From: martin@mwtech.UUCP (Martin Weitzel)
3237 @c Newsgroups: comp.unix.wizards,comp.unix.questions
3238 @c Subject: Advanced usage of 'find' (Re: Unix security automating script)
3239 @c Date: 22 Mar 90 15:05:19 GMT
3240 If you want to classify a set of files into several groups based on
3241 different criteria, you can use the comma operator to perform multiple
3242 independent tests on the files.  Here is an example:
3244 @example
3245 find / -type d \( -perm -o=w -fprint allwrite , \
3246   -perm -o=x -fprint allexec \)
3248 echo "Directories that can be written to by everyone:"
3249 cat allwrite
3250 echo ""
3251 echo "Directories with search permissions for everyone:"
3252 cat allexec
3253 @end example
3255 @code{find} has only to make one scan through the directory tree
3256 (which is one of the most time consuming parts of its work).
3258 @node Worked Examples, Security Considerations, Common Tasks, Top
3259 @chapter Worked Examples
3261 The tools in the findutils package, and in particular @code{find},
3262 have a large number of options.  This means that quite often,
3263 there is more than one way to do things.  Some of the options
3264 and facilities only exist for compatibility with other tools, and
3265 findutils provides improved ways of doing things.
3267 This chapter describes a number of useful tasks that are commonly
3268 performed, and compares the different ways of achieving them.
3270 @menu
3271 * Deleting Files::
3272 * Updating A Timestamp File::
3273 @end menu
3275 @node Deleting Files
3276 @section Deleting Files
3278 One of the most common tasks that @code{find} is used for is locating
3279 files that can be deleted.  This might include:
3281 @itemize
3282 @item 
3283 Files last modified more than 3 years ago which haven't been accessed
3284 for at least 2 years
3285 @item
3286 Files belonging to a certain user
3287 @item
3288 Temporary files which are no longer required
3289 @end itemize
3291 This example concentrates on the actual deletion task rather than on
3292 sophisticated ways of locatng the files that need to be deleted.
3293 We'll assume that the files we want to delete are old files underneath
3294 @file{/var/tmp/stuff}.
3296 @subsection The Traditional Way
3298 The traditional way to delete files in @file{var/tmp/stuff} that have
3299 not been modified in over 90 days would have been:
3301 @smallexample
3302 find /var/tmp/stuff -mtime +90 -exec /bin/rm @{@} \;
3303 @end smallexample
3305 The above command uses @samp{-exec} to run the @code{/bin/rm} command
3306 to remove each file.  This approach works and in fact would have
3307 worked in Version 7 Unix in 1979.  However, there are a number of
3308 problems with this approach.
3311 The most obvious problem with the approach above is that it causes
3312 @code{find} to fork every time it finds a file that needs to delete,
3313 and the child process then has to use the @code{exec} system call to
3314 launch @code{/bin/rm}.   All this is quite inefficient.  If we are
3315 going to use @code{/bin/rm} to do this job, it is better to make it
3316 delete more than one file at a time.  
3318 The most obvious way of doing this is to use the shell's command
3319 expansion feature:
3321 @smallexample
3322 /bin/rm `find /var/tmp/stuff -mtime +90 -print`
3323 @end smallexample
3324 or you could use the more modern form
3325 @smallexample
3326 /bin/rm $(find /var/tmp/stuff -mtime +90 -print)
3327 @end smallexample
3329 The commands above are much more efficient than the first attempt.
3330 However, there is a problem with them.  The shell has a maximum
3331 command length which is imposed by the operating system (the actual
3332 limit varies between systems).  This means that while the command
3333 expansion technique will usually work, it will suddenly fail when
3334 there are lots of files to delete.  Since the task is to delete
3335 unwanted files, this is precisely the time we don't want things to go
3336 wrong.
3338 @subsection Making Use of xargs
3340 So, is there a way to be more efficient in the use of @code{fork()}
3341 and @code{exec()} without running up against this limit?
3342 Yes, we can be almost optimally efficient by making use
3343 of the @code{xargs} command.  The @code{xargs} command reads arguments
3344 from its standard input and builds them into command lines.  We can
3345 use it like this:
3347 @smallexample
3348 find /var/tmp/stuff -mtime +90 -print | xargs /bin/rm 
3349 @end smallexample
3351 For example if the files found by @code{find} are
3352 @file{/var/tmp/stuff/A}, 
3353 @file{/var/tmp/stuff/B} and 
3354 @file{/var/tmp/stuff/C} then @code{xargs} might issue the commands 
3356 @smallexample
3357 /bin/rm /var/tmp/stuff/A /var/tmp/stuff/B
3358 /bin/rm /var/tmp/stuff/C
3359 @end smallexample
3361 The above assumes that @code{xargs} has a very small maximum command
3362 line length.  The real limit is much larger but the idea is that
3363 @code{xargs} will run @code{/bin/rm} as many times as necessary to get
3364 the job done, given the limits on command line length.
3366 This usage of @code{xargs} is pretty efficient, and the @code{xargs}
3367 command is widely implemented (all modern versions of Unix offer it).
3368 So far then, the news is all good.  However, there is bad news too.
3370 @subsection Unusual characters in filenames
3372 Unix-like systems allow any characters to appear in file names with
3373 the exception of the ASCII NUL character and the backslash.
3374 Backslashes can occur in path names (as the directory separator) but
3375 not in the names of actual directory entries.  This means that the
3376 list of files that @code{xargs} reads could in fact contain white space
3377 characters --- spaces, tabs and newline characters.  Since by default,
3378 @code{xargs} assumes that the list of files it is reading uses white
3379 space as an argument separator, it cannot correctly handle the case
3380 where a filename actually includes white space.  This makes the
3381 default behaviour of @code{xargs} almost useless for handling
3382 arbitrary data.
3384 To solve this problem, GNU findutils introduced the @samp{-print0}
3385 action for @code{find}.  This uses the ASCII NUL character to separate
3386 the entries in the file list that it produces.  This is the ideal
3387 choice of separator since it is the only character that cannot appear
3388 within a path name.  The @samp{-0} option to @code{xargs} makes it
3389 assume that arguments are separated with ASCII NUL instead of white
3390 space.  It also turns off another misfeature in the default behaviour
3391 of @code{xargs}, which is that it pays attention to quote characters
3392 in its input.  Some versions of @code{xargs} also terminate when they
3393 see a lone @samp{_} in the input, but GNU @code{find} no longer does
3394 that (since it has become an optional behaviour in the Unix standard).
3396 So, putting @code{find -print0} together with @code{xargs -0} we get
3397 this command:
3399 @smallexample
3400 find /var/tmp/stuff -mtime +90 -print0 | xargs -0 /bin/rm 
3401 @end smallexample
3403 The result is an efficient way of proceeding that
3404 correctly handles all the possible characters that could appear in the
3405 list of files to delete.  This is good news.  However, there is, as
3406 I'm sure you're expecting, also more bad news.  The problem is that
3407 this is not a portable construct; although other versions of Unix
3408 (notable BSD-derived ones) support @samp{-print0}, it's not
3409 universal.  So, is there a more universal mechanism?
3411 @subsection Going back to -exec
3413 There is indeed a more universal mechanism, which is a slight
3414 modification to the @samp{-exec} action.  The normal @samp{-exec}
3415 action assumes that the command to run is terminated with a semicolon
3416 (the semicolon normally has to be quoted in order to protect it from
3417 interpretation as the shell command separator).  The SVR4 edition of
3418 Unix introduced a slight variation, which involves terminating the
3419 command with @samp{+} instead:
3421 @smallexample
3422 find /var/tmp/stuff -mtime +90 -exec /bin/rm @{@} \+
3423 @end smallexample
3425 The above use of @samp{-exec} causes @code{find} to build up a long
3426 command line and then issue it.  This can be less efficient than some
3427 uses of @code{xargs}; for example @code{xargs} allows new command
3428 lines to be built up while the previous command is still executing, and
3429 allows you to specify a number of commands to run in parallel.
3430 However, the @code{find @dots{} -exec @dots{} +} construct has the advantage
3431 of wide portability.  GNU findutils did not support @samp{-exec @dots{} +}
3432 until version 4.2.12; one of the reasons for this is that it already
3433 had the @samp{-print0} action in any case.
3436 @subsection A more secure version of -exec
3438 The command above seems to be efficient and portable.  However,
3439 within it lurks a security problem.  The problem is shared with
3440 all the commands we've tried in this worked example so far, too.  The
3441 security problem is a race condition; that is, if it is possible for
3442 somebody to manipulate the filesystem that you are searching while you
3443 are searching it, it is possible for them to persuade your @code{find}
3444 command to cause the deletion of a file that you can delete but they
3445 normally cannot.  
3447 The problem occurs because the @samp{-exec} action is defined by the
3448 @acronym{POSIX} standard to invoke its command with the same working directory
3449 as @code{find} had when it was started.  This means that the arguments
3450 which replace the @{@} include a relative path from @code{find}'s
3451 starting point down the file that needs to be deleted.  For example,
3453 @smallexample
3454 find /var/tmp/stuff -mtime +90 -exec /bin/rm @{@} \+
3455 @end smallexample
3457 might actually issue the command:
3459 @smallexample
3460 /bin/rm /var/tmp/stuff/A /var/tmp/stuff/B /var/tmp/stuff/passwd
3461 @end smallexample
3463 Notice the file @file{/var/tmp/stuff/passwd}.  Likewise, the command:
3465 @smallexample
3466 cd /var/tmp && find stuff -mtime +90 -exec /bin/rm @{@} \+
3467 @end smallexample
3469 might actually issue the command:
3471 @smallexample
3472 /bin/rm stuff/A stuff/B stuff/passwd
3473 @end smallexample
3475 If an attacker can rename @file{stuff} to something else (making use
3476 of their write permissions in @file{/var/tmp}) they can replace it
3477 with a symbolic link to @file{/etc}.  That means that the
3478 @code{/bin/rm} command will be invoked on @file{/etc/passwd}.  If you
3479 are running your @code{find} command as root, the attacker has just managed
3480 to delete a vital file.  All they needed to do to achieve this was
3481 replace a subdirectory with a symbolic link at the vital moment.
3483 There is however, a simple solution to the problem.  This is an action
3484 which works a lot like @code{-exec} but doesn't need to traverse a
3485 chain of directories to reach the file that it needs to work on.  This
3486 is the @samp{-execdir} action, which was introduced by the BSD family
3487 of operating systems.   The command,
3489 @smallexample
3490 find /var/tmp/stuff -mtime +90 -execdir /bin/rm @{@} \+
3491 @end smallexample
3493 might delete a set of files by performing these actions:
3495 @enumerate
3496 @item 
3497 Change directory to /var/tmp/stuff/foo
3498 @item 
3499 Invoke @code{/bin/rm ./file1 ./file2 ./file3}
3500 @item
3501 Change directory to /var/tmp/stuff/bar
3502 @item 
3503 Invoke @code{/bin/rm ./file99 ./file100 ./file101}
3504 @end enumerate
3506 This is a much more secure method.  We are no longer exposed to a race
3507 condition.  For many typical uses of @code{find}, this is the best
3508 strategy.   It's reasonably efficient, but the length of the command
3509 line is limited not just by the operating system limits, but also by
3510 how many files we actually need to delete from each directory.
3512 Is it possible to do any better?   In the case of general file
3513 processing, no.  However, in the specific case of deleting files it is
3514 indeed possible to do better.  
3516 @subsection Using the -delete action
3518 The most efficient and secure method of solving this problem is to use
3519 the @samp{-delete} action:
3521 @smallexample
3522 find /var/tmp/stuff -mtime +90 -delete
3523 @end smallexample
3525 This alternative is more efficient than any of the @samp{-exec} or
3526 @samp{-execdir} actions, since it entirely avoids the overhead of
3527 forking a new process and using @code{exec} to run @code{/bin/rm}.  It
3528 is also normally more efficient than @code{xargs} for the same
3529 reason.   The file deletion is performed from the directory containing
3530 the entry to be deleted, so the @samp{-delete} action has the same
3531 security advantages as the @samp{-execdir} action has.  
3533 The @samp{-delete} action was introduced by the BSD family of
3534 operating systems.
3536 @subsection Improving things still further
3538 Is it possible to improve things still further?  Not without either
3539 modifying the system library to the operating system or having more specific
3540 knowledge of the layout of the filesystem and disk I/O subsystem, or
3541 both.
3543 The @code{find} command traverses the filesystem, reading
3544 directories.  It then issues a separate system call for each file to
3545 be deleted.  If we could modify the operating system, there are
3546 potential gains that could be made:
3548 @itemize
3549 @item
3550 We could have a system call to which we pass more than one filename
3551 for deletion
3552 @item
3553 Alternatively, we could pass in a list of inode numbers (on GNU/Linux
3554 systems, @code{readdir()} also returns the inode number of each
3555 directory entry) to be deleted.
3556 @end itemize
3558 The above possibilities sound interesting, but from the kernel's point
3559 of view it is difficult to enforce standard Unix access controls for
3560 such processing by inode number.  Such a facility would probably
3561 need to be restricted to the superuser.
3563 Another way of improving performance would be to increase the
3564 parallelism of the process.  For example if the directory hierarchy we
3565 are searching is actually spread across a number of disks, we might
3566 somehow be able to arrange for @code{find} to process each disk in
3567 parallel.  In practice GNU @code{find} doesn't have such an intimate
3568 understanding of the system's filesystem layout and disk I/O
3569 subsystem.
3571 However, since the system administrator can have such an understanding
3572 they can take advantage of it like so:
3574 @smallexample
3575 find /var/tmp/stuff1 -mtime +90 -delete &
3576 find /var/tmp/stuff2 -mtime +90 -delete &
3577 find /var/tmp/stuff3 -mtime +90 -delete &
3578 find /var/tmp/stuff4 -mtime +90 -delete &
3579 wait
3580 @end smallexample
3582 In the example above, four spearate instances of @code{find} are used
3583 to search four subdirectories in parallel.  The @code{wait} command
3584 simply waits for all of these to complete.  Whether this approach is
3585 more or less efficient than a single instance of @code{find} depends
3586 on a number of things:
3588 @itemize
3589 @item
3590 Are the directories being searched in parallel actually on separate
3591 disks?  If not, this parallel search might just result in a lot of
3592 disk head movement and so the speed might even be slower.
3593 @item
3594 Other activity - are other programs also doing things on those disks?
3595 @end itemize
3598 @subsection Conclusion
3600 The fastest and most secure way to delete files with the help of
3601 @code{find} is to use @samp{-delete}.  Using @code{xargs -0 -P N} can
3602 also make effective use of the disk, but it is not as secure.
3604 In the case where we're doing things other than deleting files, the
3605 most secure alternative is @samp{-execdir @dots{} +}, but this is not as
3606 portable as the insecure action @samp{-exec @dots{} +}.
3608 The @samp{-delete} action is not completely portable, but the only
3609 other possiblility which is as secure (@samp{-execdir}) is no more
3610 portable.  The most efficient portable alternative is @samp{-exec
3611 @dots{}+}, but this is insecure and isn't supported by versions of GNU
3612 findutils prior to 4.2.12.
3615 @node Updating A Timestamp File
3616 @section Updating A Timestamp File
3618 Suppose we have a directory full of files which is maintained with a
3619 set of automated tools; perhaps one set of tools updates them and
3620 another set of tools uses the result.  In this situation, it might be
3621 useful for the second set of tools to know if the files have recently
3622 been changed.  It might be useful, for example, to have a 'timestamp'
3623 file which gives the timestamp on the newest file in the collection.
3625 We can use @code{find} to achieve this, but there are several
3626 different ways to do it.
3628 @subsection Updating the Timestamp The Wrong Way
3630 The obvious but wrong answer is just to use @samp{-newer}:-
3632 @smallexample
3633 find subdir -newer timestamp -exec touch -r @{@} timestamp \; 
3634 @end smallexample
3636 This does the right sort of thing but has a bug.  Suppose that two
3637 files in the subdirectory have been updated, and that these are called
3638 @file{file1} and @file{file2}.  The command above will update
3639 @file{timestamp} with the modification time of @file{file1} or that of
3640 @file{file2}, but we don't know which one.  Since the timestamps on
3641 @file{file1} and @file{file2} will in general be different, this could
3642 well be the wrong value.
3644 One solution to this problem is to modify @code{find} to recheck the
3645 modification time of @file{timestamp} every time a file is to be
3646 compared against it, but that will reduce the performence of
3647 @code{find}.
3649 @subsection Using the test utlity to compare timestamps
3651 The @code{test} command can be used to compare timestamps:
3653 @smallexample
3654 find subdir -exec test @{@} -nt timestamp \; -exec touch -r @{@} timestamp \; 
3655 @end smallexample
3657 This will ensure that any changes made to the modification time of
3658 @file{timestamp} that take place during the execution of @code{find}
3659 are taken into account.  This resolves our earlier problem, but
3660 unfortunately this runs much more slowly.
3662 @subsection A combined approach
3664 We can of course still use @samp{-newer} to cut down on the number of
3665 calls to @code{test}:
3667 @smallexample
3668 find subdir -newer timestamp -a \
3669      -exec test @{@} -nt timestamp \; -a \
3670      -exec touch -r @{@} timestamp \; 
3671 @end smallexample
3673 Here, the @samp{-newer} test excludes all the files which are
3674 definitely older than the timestamp, but all the files which are newer
3675 than the old value of the timestamp are compared against the current
3676 updated timestamp.
3678 This is indeed faster in general, but the speed difference will depend
3679 on how many updated files there are.
3681 @subsection Using -printf and sort to compare timestamps
3683 It is possible to use the @samp{-printf} action to abandon the use of
3684 @code{test} entirely:
3686 @smallexample
3687 newest=$(find subdir -newer timestamp -printf "%A@:%p\n" | 
3688            sort -n | 
3689            tail -1 | 
3690            cut -d: -f2- ) 
3691 touch -r "$@{newest:-timestamp@}" timestamp
3692 @end smallexample
3694 The command above works by generating a list of the timestamps and
3695 names of all the files which are newer than the timestamp.  The
3696 @code{sort}, @code{tail} and @code{cut} comands simply pull out the
3697 name of the file with the largest timestamp value (that is, the latest
3698 file).  The @code{touch} command is then used to update the timestamp,
3700 The @code{"$@{newest:-timestamp@}"} expression simply expands to the
3701 value of @code{$newest} if that variable is set, but to
3702 @file{timestamp} otherwise.  This ensures that an argument is always
3703 given to the @samp{-r} option of the @code{touch} command.
3705 This approach seems quite efficient, but unfortunately it has a bug.
3706 Many operating systems now keep file modification time information at
3707 a granularity which is finer than one second.  Unfortunately the
3708 @samp{%A@@} format for @samp{-printf} only prints a whole-number value
3709 currently; that is, these values are at a one-second granularity.
3710 This means that in our example above, @samp{$newest} wil be the name
3711 of a file which is no more than one second older than the newest file,
3712 but may indeed be older.
3714 It would be possible to solve this problem with some kind of loop:
3716 @smallexample
3717 while true; do
3718         newest=$(find subdir -newer timestamp -printf "%A@@:%p\n" | 
3719            sort -n | 
3720            tail -1 | 
3721            cut -d: -f2- ) 
3722         if test -z "$newest" ; then
3723                 break
3724         else
3725                 touch -r "$newest" timestamp
3726         fi
3727 done
3728 @end smallexample
3730 A better fix for this problem would be to allow the @samp{%A@@} format
3731 to produce a result having a fractional part, too.  While this is
3732 planned for GNU @code{find}, it hasn't been done yet.
3734 @subsection Coping with sub-second timestamp resolution
3736 Another tool which often works with timestamps is @code{make}.  We can
3737 use @code{find} to generate a @file{Makefile} file on the fly and then
3738 use @code{make} to update the timestamps:
3740 @smallexample
3741 makefile=$(mktemp)
3742 find subdir \
3743         \( \! -xtype l \) \
3744         -newer timestamp \
3745         -printf "timestamp:: %p\n\ttouch -r %p timestamp\n\n" > "$makefile"
3746 make -f "$makefile"
3747 rm   -f "$makefile"
3748 @end smallexample
3750 Unfortunately although the solution above is quite elegant, it fails
3751 to cope with white space within file names, and adjusting it to do so
3752 would require a rather complex shell script.
3755 @subsection Coping with odd filenames too
3757 We can fix both of these problems (looping and problems with white
3758 space), and do things more efficiently too.  The following command
3759 works with newlines and doesn't need to sort the list of filenames.
3761 @smallexample
3762 find subdir -newer timestamp -printf "%A@@:%p\0" | 
3763    perl -0 newest.pl |
3764    xargs --no-run-if-empty --null -i \
3765       find @{@} -maxdepth 0 -newer timestamp -exec touch -r @{@} timestamp \;
3766 @end smallexample
3768 The first @code{find} command generates a list of files which are
3769 newer than the original timestamp file, and prints a list of them with
3770 their timestamps.  The @file{newest.pl} script simply filters out all
3771 the filenames which have timestamps which are older than whatever the
3772 newest file is:-
3774 @smallexample
3775 @verbatim
3776 #! /usr/bin/perl -0
3777 my @newest = ();
3778 my $latest_stamp = undef;
3779 while (<>) {
3780     my ($stamp, $name) = split(/:/);
3781     if (!defined($latest_stamp) || ($tstamp > $latest_stamp)) {
3782         $latest_stamp = $stamp;
3783         @newest = ();
3784     }
3785     if ($tstamp >= $latest_stamp) {
3786         push @newest, $name;
3787     }
3789 print join("\0", @newest);
3790 @end verbatim
3791 @end smallexample
3793 This prints a list of zero or more files, all of which are newer than
3794 the original timestamp file, and which have the same timestamp as each
3795 other, to the nearest second.  The second @code{find} command takes
3796 each resulting file one at a time, and if that is newer than the
3797 timestamp file, the timestamp is updated.
3799 @node Security Considerations, Error Messages, Worked Examples, Top
3800 @chapter Security Considerations
3802 Security considerations are important if you are using @code{find} or
3803 @code{xargs} to search for or process files that don't belong to you
3804 or which other people have control.  Security considerations
3805 relating to @code{locate} may also apply if you have files which you
3806 do not want others to see.
3808 The most severe forms of security problems affecting
3809 @code{find} and related programs are when third parties bring
3810 about a situation allowing them to do something
3811 they would normally not be able to accomplish.  This is called @emph{privilege
3812 elevation}.  This might include deleting files they would not normally
3813 be able to delete.  It is common for the operating system to periodically
3814 invoke @code{find} for self-maintenance purposes.  These invocations of
3815 @code{find} are particularly problematic from a security point of view
3816 as these are often invoked by the superuser and search the entire
3817 filesystem hierarchy.  Generally, the severity of any associated problem depends
3818 on what the system is going to do with the files found by @code{find}.
3820 @menu
3821 * Levels of Risk::      What is your level of exposure to security problems?
3822 * Security Considerations for find::  Security problems with find
3823 * Security Considerations for xargs:: Security problems with xargs
3824 * Security Considerations for locate:: Security problems with locate
3825 * Security Summary:: That was all very complex, what does it boil down to?
3826 @end menu
3829 @node Levels of Risk
3830 @section Levels of Risk
3832 There are some security risks inherent in the use of @code{find},
3833 @code{xargs} and (to a lesser extent) @code{locate}.  The severity of
3834 these risks depends on what sort of system you are using:
3836 @table @strong
3837 @item High risk
3838 Multi-user systems where you do not control (or trust) the other
3839 users, and on which you execute @code{find}, including areas where
3840 those other users can manipulate the filesystem (for example beneath
3841 @file{/home} or @file{/tmp}).
3843 @item Medium Risk
3844 Systems where the actions of other users can create file names chosen
3845 by them, but to which they don't have access while @code{find} is
3846 being run.  This access might include leaving programs running (shell
3847 background jobs, @code{at} or @code{cron} tasks, for example).  On
3848 these sorts of systems, carefully written commands (avoiding use of
3849 @samp{-print} for example) should not expose you to a high degree of
3850 risk.  Most systems fall into this category.
3852 @item Low Risk
3853 Systems to which untrusted parties do not have access, cannot create
3854 file names of their own choice (even remotely) and which contain no
3855 security flaws which might enable an untrusted third party to gain
3856 access.  Most systems do not fall into this category because there are
3857 many ways in which external parties can affect the names of files that
3858 are created on your system.  The system on which I am writing this for
3859 example automatically downloads software updates from the Internet;
3860 the names of the files in which these updates exist are chosen by
3861 third parties@footnote{Of course, I trust these parties to a large
3862 extent anyway, because I install software provided by them; I choose
3863 to trust them in this way, and that's a deliberate choice}.
3864 @end table
3866 In the discussion above, ``risk'' denotes the likelihood that someone
3867 can cause @code{find}, @code{xargs}, @code{locate} or some other
3868 program which is controlled by them to do something you did not
3869 intend.  The levels of risk suggested do not take any account of the
3870 consequences of this sort of event.  That is, if you operate a ``low
3871 risk'' type system, but the consequences of a security problem are
3872 disastrous, then you should still give serious thought to all the
3873 possible security problems, many of which of course will not be
3874 discussed here -- this section of the manual is intended to be
3875 informative but not comprehensive or exhaustive.
3877 If you are responsible for the operation of a system where the
3878 consequences of a security problem could be very important, you should
3879 do two things:-
3881 @enumerate
3882 @item Define a security policy which defines who is allowed to do what
3883 on your system.
3884 @item Seek competent advice on how to enforce your policy, detect
3885 breaches of that policy, and take account of any potential problems
3886 that might fall outside the scope of your policy.
3887 @end enumerate
3890 @node Security Considerations for find
3891 @section Security Considerations for @code{find}
3894 Some of the actions @code{find} might take have a direct effect;
3895 these include @code{-exec} and @code{-delete}.  However, it is also
3896 common to use @code{-print} explicitly or implicitly, and so if
3897 @code{find} produces the wrong list of file names, that can also be a
3898 security problem; consider the case for example where @code{find} is
3899 producing a list of files to be deleted.
3901 We normally assume that the @code{find} command line expresses the
3902 file selection criteria and actions that the user had in mind -- that
3903 is, the command line is ``trusted'' data.
3905 From a security analysis point of view, the output of @code{find}
3906 should be correct; that is, the output should contain only the names
3907 of those files which meet the user's criteria specified on the command
3908 line.  This applies for the @code{-exec} and @code{-delete} actions;
3909 one can consider these to be part of the output.
3911 On the other hand, the contents of the filesystem can be manipulated
3912 by other people, and hence we regard this as ``untrusted'' data.  This
3913 implies that the @code{find} command line is a filter which converts
3914 the untrusted contents of the filesystem into a correct list of output
3915 files.
3917 The filesystem will in general change while @code{find} is searching
3918 it; in fact, most of the potential security problems with @code{find}
3919 relate to this issue in some way.
3921 @dfn{Race conditions} are a general class of security problem where the
3922 relative ordering of actions taken by @code{find} (for example) and
3923 something else are critically important in getting the correct and expected result@footnote{This is more or less the
3924 definition of the term ``race condition''} .
3926 For @code{find}, an attacker might move or rename files or directories in
3927 the hope that an action might be taken against a file which was not
3928 normally intended to be affected.  Alternatively, this sort of attack
3929 might be intended to persuade @code{find} to search part of the
3930 filesystem which would not normally be included in the search
3931 (defeating the @code{-prune} action for example).
3933 @menu
3934 * Changing the Current Working Directory::
3935 * Race Conditions with -exec::
3936 * Race Conditions with -print and -print0::
3937 @end menu
3940 @node Changing the Current Working Directory
3941 @subsection Changing the Current Working Directory
3943 As @code{find} searches the filesystem, it finds subdirectories and
3944 then searches within them by changing its working directory.  First,
3945 @code{find} reaches and recognizes a subdirectory.  It then decides if that
3946 subdirectory meets the criteria for being searched; that is, any
3947 @samp{-xdev} or @samp{-prune} expressions are taken into account.  The
3948 @code{find} program will then change working directory and proceed to
3949 search the directory.
3951 A race condition attack might take the form that once the checks
3952 relevant to @samp{-xdev} and @samp{-prune} have been done, an attacker
3953 might rename the directory that was being considered, and put in its
3954 place a symbolic link that actually points somewhere else.
3956 The idea behind this attack is to fool @code{find} into going into the
3957 wrong directory.  This would leave @code{find} with a working
3958 directory chosen by an attacker, bypassing any protection apparently
3959 provided by @samp{-xdev} and @samp{-prune}, and any protection
3960 provided by being able to @emph{not} list particular directories on
3961 the @code{find} command line.  This form of attack is particularly
3962 problematic if the attacker can predict when the @code{find} command
3963 will be run, as is the case with @code{cron} tasks for example.
3965 GNU @code{find} has specific safeguards to prevent this general class
3966 of problem.  The exact form of these safeguards depends on the
3967 properties of your system.
3969 @menu
3970 * O_NOFOLLOW::                     Safely changing directory using fchdir().
3971 * Systems without O_NOFOLLOW::     Checking for symbolic links after chdir().
3972 @end menu
3974 @node O_NOFOLLOW
3975 @subsubsection O_NOFOLLOW
3977 If your system supports the O_NOFOLLOW flag @footnote{GNU/Linux
3978 (kernel version 2.1.126 and later) and FreeBSD (3.0-CURRENT and later)
3979 support this} to the @code{open(2)} system call, @code{find} uses it
3980 when safely changing directory.  The target directory is first opened
3981 and then @code{find} changes working directory with the
3982 @code{fchdir()} system call.  This ensures that symbolic links are not
3983 followed, preventing the sort of race condition attack in which use
3984 is made of symbolic links.
3986 If for any reason this approach does not work, @code{find} will fall
3987 back on the method which is normally used if O_NOFOLLOW is not
3988 supported.
3990 You can tell if your system supports O_NOFOLLOW by running
3992 @example
3993 find --version
3994 @end example
3996 This will tell you the version number and which features are enabled.
3997 For example, if I run this on my system now, this gives:
3998 @example
3999 GNU find version 4.2.18-CVS
4000 Features enabled: D_TYPE O_NOFOLLOW(enabled)
4001 @end example
4003 Here, you can see that I am running a version of @code{find} which was
4004 built from the development (CVS) code prior to the release of
4005 findutils-4.2.18, and that the D_TYPE and O_NOFOLLOW features are
4006 present.  O_NOFOLLOW is qualified with ``enabled''.  This simply means
4007 that the current system seems to support O_NOFOLLOW.  This check is
4008 needed because it is possible to build @code{find} on a system that
4009 defines O_NOFOLLOW and then run it on a system that ignores the
4010 O_NOFOLLOW flag.  We try to detect such cases at startup by checking
4011 the operating system and version number; when this happens you will
4012 see ``O_NOFOLLOW(disabled)'' instead.
4014 @node Systems without O_NOFOLLOW
4015 @subsubsection Systems without O_NOFOLLOW
4017 The strategy for preventing this type of problem on systems that lack
4018 support for the O_NOFOLLOW flag is more complex.  Each time
4019 @code{find} changes directory, it examines the directory it is about
4020 to move to, issues the @code{chdir()} system call, and then checks
4021 that it has ended up in the subdirectory it expected.  If all is as
4022 expected, processing continues as normal.  However, there are two main
4023 reasons why the directory might change: the use of an automounter and
4024 the someone removing the old directory and replacing it with something
4025 else while @code{find} is trying to descend into it.
4027 Where a filesystem ``automounter'' is in use it can be the case that
4028 the use of the @code{chdir()} system call can itself cause a new
4029 filesystem to be mounted at that point.  On systems that do not
4030 support O_NOFOLLOW, this will cause @code{find}'s security check to
4031 fail.
4033 However, this does not normally represent a security problem, since
4034 the automounter configuration is normally set up by the system
4035 administrator.  Therefore, if the @code{chdir()} sanity check fails,
4036 @code{find} will make one more attempt.  If that succeeds, execution
4037 carries on as normal.  This is the usual case for automounters.
4039 Where an attacker is trying to exploit a race condition, the problem
4040 may not have gone away on the second attampt.  If this is the case,
4041 @code{find} will issue a warning message and then ignore that
4042 subdirectory.  When this happens, actions such as @samp{-exec} or
4043 @samp{-print} may already have taken place for the problematic
4044 subdirectory.  This is because @code{find} applies tests and actions
4045 to directories before searching within them (unless @samp{-depth} was
4046 specified).
4048 Because of the nature of the directory-change operation and security
4049 check, in the worst case the only things that @code{find} would have
4050 done with the directory are to move into it and back out to the
4051 original parent.  No operations would have been performed within that
4052 directory.
4054 @node Race Conditions with -exec
4055 @subsection Race Conditions with -exec
4057 The @samp{-exec} action causes another program to be run.  It passes
4058 to the program the name of the file which is being considered at the
4059 time.  The invoked program will typically then perform some action
4060 on that file.  Once again, there is a race condition which can be
4061 exploited here.  We shall take as a specific example the command
4063 @example
4064 find /tmp -path /tmp/umsp/passwd -exec /bin/rm
4065 @end example
4067 In this simple example, we are identifying just one file to be deleted
4068 and invoking @code{/bin/rm} to delete it.  A problem exists because
4069 there is a time gap between the point where @code{find} decides that
4070 it needs to process the @samp{-exec} action and the point where the
4071 @code{/bin/rm} command actually issues the @code{unlink()} system
4072 call to delete the file from the filesystem.  Within this time period, an attacker can rename the
4073 @file{/tmp/umsp} directory, replacing it with a symbolic link to
4074 @file{/etc}.  There is no way for @code{/bin/rm} to determine that it
4075 is working on the same file that @code{find} had in mind.  Once the
4076 symbolic link is in place, the attacker has persuaded @code{find} to
4077 cause the deletion of the @file{/etc/passwd} file, which is not the
4078 effect intended by the command which was actually invoked.
4080 One possible defence against this type of attack is to modify the
4081 behaviour of @samp{-exec} so that the @code{/bin/rm} command is run
4082 with the argument @file{./passwd} and a suitable choice of working
4083 directory.  This would allow the normal sanity check that @code{find}
4084 performs to protect against this form of attack too.  Unfortunately,
4085 this strategy cannot be used as the POSIX standard specifies that the
4086 current working directory for commands invoked with @samp{-exec} must
4087 be the same as the current working directory from which @code{find}
4088 was invoked.  This means that the @samp{-exec} action is inherently
4089 insecure and can't be fixed.
4091 GNU @code{find} implements a more secure variant of the @samp{-exec}
4092 action, @samp{-execdir}.  The @samp{-execdir} action
4093 ensures that it is not necessary to dereference subdirectories to
4094 process target files.  The current directory used to invoke programs
4095 is the same as the directory in which the file to be processed exists
4096 (@file{/tmp/umsp} in our example, and only the basename of the file to
4097 be processed is passed to the invoked command, with a @samp{./}
4098 prepended (giving @file{./passwd} in our example).
4100 The @samp{-execdir} action refuses to do anything if the current
4101 directory is included in the @var{$PATH} environment variable.  This
4102 is necessary because @samp{-execdir} runs programs in the same
4103 directory in which it finds files -- in general, such a directory
4104 might be writable by untrusted users.  For similar reasons,
4105 @samp{-execdir} does not allow @samp{@{@}} to appear in the name of
4106 the command to be run.
4108 @node Race Conditions with -print and -print0
4109 @subsection Race Conditions with -print and -print0
4111 The @samp{-print} and @samp{-print0} actions can be used to produce a
4112 list of files matching some criteria, which can then be used with some
4113 other command, perhaps with @code{xargs}.  Unfortunately, this means
4114 that there is an unavoidable time gap between @code{find} deciding
4115 that one or more files meet its criteria and the relevant command
4116 being executed.  For this reason, the @samp{-print} and @samp{-print0}
4117 actions are just as insecure as @samp{-exec}.
4119 In fact, since the construction
4121 @example
4122 find @dots{}  -print | xargs @enddots{}
4123 @end example
4125 does not cope correctly with newlines or other ``white space'' in
4126 file names, and copes poorly with file names containing quotes, the
4127 @samp{-print} action is less secure even than @samp{-print0}.
4130 @comment  node-name,  next,  previous,  up
4131 @comment @node Security Considerations for xargs
4132 @node Security Considerations for xargs
4133 @section Security Considerations for @code{xargs}
4135 The description of the race conditions affecting the @samp{-print}
4136 action of @code{find} shows that @code{xargs} cannot be secure if it
4137 is possible for an attacker to modify a filesystem after @code{find}
4138 has started but before @code{xargs} has completed all its actions.
4140 However, there are other security issues that exist even if it is not
4141 possible for an attacker to have access to the filesystem in real
4142 time.  Firstly, if it is possible for an attacker to create files with
4143 names of their choice on the filesystem, then @code{xargs} is
4144 insecure unless the @samp{-0} option is used.  If a file with the name
4145 @file{/home/someuser/foo/bar\n/etc/passwd} exists (assume that
4146 @samp{\n} stands for a newline character), then @code{find @dots{} -print}
4147 can be persuaded to print three separate lines:
4149 @example
4150 /home/someuser/foo/bar
4152 /etc/passwd
4153 @end example
4155 If it finds a blank line in the input, @code{xargs} will ignore it.
4156 Therefore, if some action is to be taken on the basis of this list of
4157 files, the @file{/etc/passwd} file would be included even if this was
4158 not the intent of the person running find.  There are circumstances in
4159 which an attacker can use this to their advantage.  The same
4160 consideration applies to file names containing ordinary spaces rather
4161 than newlines, except that of course the list of file names will no
4162 longer contain an ``extra'' newline.
4164 This problem is an unavoidable consequence of the default behaviour of
4165 the @code{xargs} command, which is specified by the POSIX standard.
4166 The only ways to avoid this problem are either to avoid all use of
4167 @code{xargs} in favour for example of @samp{find -exec} or (where
4168 available) @samp{find -execdir}, or to use the @samp{-0} option, which
4169 ensures that @code{xargs} considers file names to be separated by
4170 ASCII NUL characters rather than whitespace.  However, useful as this
4171 option is, the POSIX standard does not make it mandatory.
4173 @comment  node-name,  next,  previous,  up
4174 @node Security Considerations for locate
4175 @section Security Considerations for @code{locate}
4177 It is fairly unusual for the output of @code{locate} to be fed into
4178 another command.  However, if this were to be done, this would raise
4179 the same set of security issues as the use of @samp{find @dots{} -print}.
4180 Although the problems relating to whitespace in file names can be
4181 resolved by using @code{locate}'s @samp{-0} option, this still leaves
4182 the race condition problems associated with @samp{find @dots{} -print0}.
4183 There is no way to avoid these problems in the case of @code{locate}.
4185 @node Security Summary
4186 @section Summary
4188 Where untrusted parties can create files on the system, or affect the
4189 names of files that are created, all uses for @code{find},
4190 @code{locate} and @code{xargs} have known security problems except the
4191 following:
4193 @table @asis
4194 @item Informational use only
4195 Uses where the programs are used to prepare lists of file names upon
4196 which no further action will ever be taken.
4198 @item @samp{-delete}
4199 Use of the @samp{-delete} action with @code{find} to delete files
4200 which meet specified criteria
4202 @item @samp{-execdir}
4203 Use of the @samp{-execdir} action with @code{find} where the
4204 @env{PATH} environment variable contains directories which contain
4205 only trusted programs.
4206 @end table
4208 @comment  node-name,  next,  previous,  up
4209 @node Error Messages, Primary Index, Security Considerations, Top
4210 @chapter Error Messages
4212 This section describes some of the error messages sometimes made by
4213 @code{find}, @code{xargs}, or @code{locate}, explains them and in some
4214 cases provides advice as to what you should do about this.
4216 This manual is written in English.  The GNU findutils software
4217 features translations of error messages for many languages.  For this
4218 reason the error messages produced by
4219 the programs are made to be as self-explanatory as possible.  This approach avoids leaving people to
4220 figure out which test an English-language error message
4221 corresponds to. Error messages which are self-explanatory
4222 will not normally be mentioned in this document.  For
4223 those messages mentioned in this document, only the
4224 English-language version of the message will be listed.
4226 @menu
4227 * Error Messages From find::
4228 * Error Messages From xargs::
4229 * Error Messages From locate::
4230 * Error Messages From updatedb::
4231 @end menu
4233 @node Error Messages From find, Error Messages From xargs, , Error Messages
4234 @section Error Messages From @code{find}
4236 @table @samp
4237 @item invalid predicate `-foo'
4238 This means that the @code{find} command line included something that
4239 started with a dash or other special character.  The @code{find}
4240 program tried to interpret this as a test, action or option, but
4241 didn't recognise it.  If it was intended to be a test, check what was
4242 specified against the documentation.  If, on the other hand, the
4243 string is the name of a file which has been expanded from a wildcard
4244 (for example because you have a @samp{*} on the command line),
4245 consider using @samp{./*} or just @samp{.} instead.
4247 @item unexpected extra predicate
4248 This usually happens if you have an extra bracket on the command line
4249 (for example @samp{find . -print \)}).
4251 @item Warning: filesystem /path/foo has recently been mounted
4252 @itemx Warning: filesystem /path/foo has recently been unmounted
4253 These messages might appear when @code{find} moves into a directory
4254 and finds that the device number and inode are different to what it
4255 expected them to be.  If the directory @code{find} has moved into is
4256 on an network filesystem (NFS), it will not issue this message, because
4257 @code{automount} frequently mounts new filesystems on directories as
4258 you move into them (that is how it knows you want to use the
4259 filesystem).  So, if you do see this message, be wary ---
4260 @code{automount} may not have been responsible.  Consider the
4261 possibility that someone else is manipulating the filesystem while
4262 @code{find} is running.  Some people might do this in order to mislead
4263 @code{find} or persuade it to look at one set of files when it thought
4264 it was looking at another set.
4266 @item /path/foo changed during execution of find (old device number 12345, new device number 6789, filesystem type is <whatever>) [ref XXX]
4267 This message is issued when @code{find} moves into a directory and ends up
4268 somewhere it didn't expect to be.  This happens in one of two
4269 circumstances.  Firstly, this happens when @code{automount} intervenes
4270 on a system where @code{find} doesn't know how to determine what
4271 the current set of mounted filesystems is.
4273 Secondly, this can happen when the device number of a directory
4274 appears to change during a change of current directory, but
4275 @code{find} is moving up the filesystem hierarchy rather than down into it.
4276 In order to prevent @code{find} wandering off into some unexpected
4277 part of the filesystem, we stop it at this point.
4279 @item Don't know how to use getmntent() to read `/etc/mtab'.  This is a bug.
4280 This message is issued when a problem similar to the above occurs on a
4281 system where @code{find} doesn't know how to figure out the current
4282 list of mount points.  Ask for help on @email{bug-findutils@@gnu.org}.
4284 @item /path/foo/bar changed during execution of find (old inode number 12345, new inode number 67893, filesystem type is <whatever>) [ref XXX]"),
4285 This message is issued when @code{find} moves into a directory and
4286 discovers that the inode number of that directory
4287 is different from the inode number that it obtained when it examined the
4288 directory previously.  This usually means that while
4289 @code{find} was deep in a directory hierarchy doing a
4290 time consuming operation, somebody has moved one of the parent directories to
4291 another location in the same filesystem.  This may or may not have been done
4292 maliciously.  In any case, @code{find} stops at this point
4293 to avoid traversing parts of the filesystem that it wasn't
4294 intended.  You can use @code{ls -li} or @code{find /path -inum
4295 12345 -o -inum 67893} to find out more about what has happened.
4297 @item sanity check of the fnmatch() library function failed.
4298 Please submit a bug report.  You may well be asked questions about
4299 your system, and if you compiled the @code{findutils} code yourself,
4300 you should keep your copy of the build tree around.  The likely
4301 explanation is that your system has a buggy implementation of
4302 @code{fnmatch} that looks enough like the GNU version to fool
4303 @code{configure}, but which doesn't work properly.
4305 @item cannot fork
4306 This normally happens if you use the @code{-exec} action or
4307 something similar (@code{-ok} and so forth) but the system has run out
4308 of free process slots.  This is either because the system is very busy
4309 and the system has reached its maximum process limit, or because you
4310 have a resource limit in place and you've reached it.  Check the
4311 system for runaway processes (with @code{ps}, if possible).  Some process
4312 slots are normally reserved for use by @samp{root}.
4314 @item some-program terminated by signal 99
4315 Some program which was launched with @code{-exec} or similar was killed
4316 with a fatal signal.  This is just an advisory message.
4317 @end table
4320 @node Error Messages From xargs, Error Messages From locate, Error Messages From find, Error Messages
4321 @section Error Messages From xargs
4323 @table @samp
4324 @item environment is too large for exec
4325 This message means that you have so many environment variables set (or
4326 such large values for them) that there is no room within the
4327 system-imposed limits on program command line argument length to
4328 invoke any program.  This is an unlikely situation and is more likely
4329 result of an attempt to test the limits of @code{xargs}, or break it.
4330 Please try unsetting some environment variables, or exiting the
4331 current shell.
4333 @item can not fit single argument within argument list size limit
4334 You are using the @samp{-I} option and @code{xargs} doesn't have
4335 enough space to build a command line because it has read a really
4336 large item and it doesn't fit.  You can probably work around this
4337 problem with the @samp{-s} option, but the default size is pretty
4338 large.  This is a rare situation and is more likely an attempt to test
4339 the limits of @code{xargs}, or break it.  Otherwise, you will need to
4340 try to shorten the problematic argument or not use @code{xargs}.
4342 @item cannot fork
4343 See the description of the similar message for @code{find}.
4345 @item <program>: exited with status 255; aborting
4346 When a command run by @code{xargs} exits with status 255, @code{xargs}
4347 is supposed to stop.  If this is not what you intended, wrap the
4348 program you are trying to invoke in a shell script which doesn't
4349 return status 255.
4351 @item <program>: terminated by signal 99
4352 See the description of the similar message for @code{find}.
4353 @end table
4355 @node Error Messages From locate, Error Messages From updatedb, Error Messages From xargs, Error Messages
4356 @section Error Messages From @code{locate}
4358 @table @samp
4359 @item warning: database `/usr/local/var/locatedb' is more than 8 days old
4360 The @code{locate} program relies on a database which is periodically
4361 built by the @code{updatedb} program.  That hasn't happened in a long
4362 time.  To fix this problem, run @code{updatedb} manually.  This can
4363 often happen on systems that are generally not left on, so the
4364 periodic ``cron'' task which normally does this doesn't get a chance
4365 to run.
4367 @item locate database `/usr/local/var/locatedb' is corrupt or invalid
4368 This should not happen.  Re-run @code{updatedb}.  If that works, but
4369 @code{locate} still produces this error, run @code{locate --version}
4370 and @code{updatedb --version}.  These should produce the same output.
4371 If not, you are using a mixed toolset; check your @samp{$PATH}
4372 environment variable and your shell aliases (if you have any).  If
4373 both programs claim to be GNU versions, this is a bug; all versions of
4374 these programs should interoperate without problem.  Ask for help on
4375 @email{bug-findutils@@gnu.org}.
4376 @end table
4379 @node Error Messages From updatedb, , Error Messages From locate, Error Messages
4380 @section Error Messages From updatedb
4382 The @code{updatedb} program (and the programs it invokes) do issue
4383 error messages, but none seem to be candidates for guidance.  If
4384 you are having a problem understanding one of these, ask for help on
4385 @email{bug-findutils@@gnu.org}.
4387 @node Primary Index, , Error Messages, Top
4388 @unnumbered @code{find} Primary Index
4390 This is a list of all of the primaries (tests, actions, and options)
4391 that make up @code{find} expressions for selecting files.  @xref{find
4392 Expressions}, for more information on expressions.
4394 @printindex fn
4396 @bye
4398 @comment texi related words used by Emacs' spell checker ispell.el
4400 @comment LocalWords: texinfo setfilename settitle setchapternewpage
4401 @comment LocalWords: iftex finalout ifinfo DIR titlepage vskip pt
4402 @comment LocalWords: filll dir samp dfn noindent xref pxref
4403 @comment LocalWords: var deffn texi deffnx itemx emph asis
4404 @comment LocalWords: findex smallexample subsubsection cindex
4405 @comment LocalWords: dircategory direntry itemize
4407 @comment other words used by Emacs' spell checker ispell.el
4408 @comment LocalWords: README fred updatedb xargs Plett Rendell akefile
4409 @comment LocalWords: args grep Filesystems fo foo fOo wildcards iname
4410 @comment LocalWords: ipath regex iregex expr fubar regexps
4411 @comment LocalWords: metacharacters macs sr sc inode lname ilname
4412 @comment LocalWords: sysdep noleaf ls inum xdev filesystems usr atime
4413 @comment LocalWords: ctime mtime amin cmin mmin al daystart Sladkey rm
4414 @comment LocalWords: anewer cnewer bckw rf xtype uname gname uid gid
4415 @comment LocalWords: nouser nogroup chown chgrp perm ch maxdepth
4416 @comment LocalWords: mindepth cpio src CD AFS statted stat fstype ufs
4417 @comment LocalWords: nfs tmp mfs printf fprint dils rw djm Nov lwall
4418 @comment LocalWords: POSIXLY fls fprintf strftime locale's EDT GMT AP
4419 @comment LocalWords: EST diff perl backquotes sprintf Falstad Oct cron
4420 @comment LocalWords: eg vmunix mkdir afs allexec allwrite ARG bigram
4421 @comment LocalWords: bigrams cd chmod comp crc CVS dbfile dum eof
4422 @comment LocalWords: fileserver filesystem fn frcode Ghazi Hnewc iXX
4423 @comment LocalWords: joeuser Kaveh localpaths localuser LOGNAME
4424 @comment LocalWords: Meyering mv netpaths netuser nonblank nonblanks
4425 @comment LocalWords: ois ok Pinard printindex proc procs prunefs
4426 @comment LocalWords: prunepaths pwd RFS rmadillo rmdir rsh sbins str
4427 @comment LocalWords: su Timar ubins ug unstripped vf VM Weitzel
4428 @comment LocalWords: wildcard zlogout basename execdir wholename iwholename
4429 @comment LocalWords: timestamp timestamps Solaris FreeBSD OpenBSD POSIX