We use GNU Emacs regexps by default now.
[findutils.git] / doc / find.texi
blob01d1686d1f3deffba20818520f2472a863f98593
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 * Common Tasks::                Solutions to common real-world problems.
88 * Databases::                   Maintaining file name databases.
89 * File Permissions::            How to control access to files.
90 * Reference::                   Summary of how to invoke the programs.
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
416 @table @samp
417 @item emacs
418 Regular expressions compatible with GNU Emacs; this is also the
419 default behaviour if this option is not used.
420 @item posix-awk
421 Regular expressions compatible with the POSIX awk command (not GNU awk)
422 @item posix-basic
423 POSIX Basic Regular Expressions.
424 @item posix-egrep
425 Regular expressions compatible with the POSIX egrep command
426 @item posix-extended
427 POSIX Extended Regular Expressions
428 @end table
430 @end deffn
432 @node Fast Full Name Search
433 @subsection Fast Full Name Search
435 To search for files by name without having to actually scan the
436 directories on the disk (which can be slow), you can use the
437 @code{locate} program.  For each shell pattern you give it,
438 @code{locate} searches one or more databases of file names and
439 displays the file names that contain the pattern.  @xref{Shell Pattern
440 Matching}, for details about shell patterns.
442 If a pattern is a plain string---it contains no
443 metacharacters---@code{locate} displays all file names in the database
444 that contain that string.  If a pattern contains
445 metacharacters, @code{locate} only displays file names that match the
446 pattern exactly.  As a result, patterns that contain metacharacters
447 should usually begin with a @samp{*}, and will most often end with one
448 as well.  The exceptions are patterns that are intended to explicitly
449 match the beginning or end of a file name.
451 If you only want @code{locate} to match against the last component of
452 the file names (the ``base name'' of the files) you can use the
453 @samp{--basename} option.  The opposite behaviour is the default, but
454 can be selected explicitly by using the option @samp{--wholename}.
456 The command
457 @example
458 locate @var{pattern}
459 @end example
461 is almost equivalent to
462 @example
463 find @var{directories} -name @var{pattern}
464 @end example
466 where @var{directories} are the directories for which the file name
467 databases contain information.  The differences are that the
468 @code{locate} information might be out of date, and that @code{locate}
469 handles wildcards in the pattern slightly differently than @code{find}
470 (@pxref{Shell Pattern Matching}).
472 The file name databases contain lists of files that were on the system
473 when the databases were last updated.  The system administrator can
474 choose the file name of the default database, the frequency with which
475 the databases are updated, and the directories for which they contain
476 entries.
478 Here is how to select which file name databases @code{locate}
479 searches.  The default is system-dependent.
481 @table @code
482 @item --database=@var{path}
483 @itemx -d @var{path}
484 Instead of searching the default file name database, search the file
485 name databases in @var{path}, which is a colon-separated list of
486 database file names.  You can also use the environment variable
487 @code{LOCATE_PATH} to set the list of database files to search.  The
488 option overrides the environment variable if both are used.
489 @end table
491 @node Shell Pattern Matching
492 @subsection Shell Pattern Matching
494 @code{find} and @code{locate} can compare file names, or parts of file
495 names, to shell patterns.  A @dfn{shell pattern} is a string that may
496 contain the following special characters, which are known as
497 @dfn{wildcards} or @dfn{metacharacters}.
499 You must quote patterns that contain metacharacters to prevent the
500 shell from expanding them itself.  Double and single quotes both work;
501 so does escaping with a backslash.
503 @table @code
504 @item *
505 Matches any zero or more characters.
507 @item ?
508 Matches any one character.
510 @item [@var{string}]
511 Matches exactly one character that is a member of the string
512 @var{string}.  This is called a @dfn{character class}.  As a
513 shorthand, @var{string} may contain ranges, which consist of two
514 characters with a dash between them.  For example, the class
515 @samp{[a-z0-9_]} matches a lowercase letter, a number, or an
516 underscore.  You can negate a class by placing a @samp{!} or @samp{^}
517 immediately after the opening bracket.  Thus, @samp{[^A-Z@@]} matches
518 any character except an uppercase letter or an at sign.
520 @item \
521 Removes the special meaning of the character that follows it.  This
522 works even in character classes.
523 @end table
525 In the @code{find} tests that do shell pattern matching (@samp{-name},
526 @samp{-wholename}, etc.), wildcards in the pattern will match a
527 @samp{.}  at the beginning of a file name.  This is also the case for
528 @code{locate}.  Thus, @samp{find -name '*macs'} will match a file
529 named @file{.emacs}, as will @samp{locate '*macs'}.
531 Slash characters have no special significance in the shell pattern
532 matching that @code{find} and @code{locate} do, unlike in the shell,
533 in which wildcards do not match them.  Therefore, a pattern
534 @samp{foo*bar} can match a file name @samp{foo3/bar}, and a pattern
535 @samp{./sr*sc} can match a file name @samp{./src/misc}.
537 If you want to locate some files with the @samp{locate} command but
538 don't need to see the full list you can use the @samp{--limit} option
539 to see just a small number of results, or the @samp{--count} option to
540 display only the total number of matches.
542 @node Links
543 @section Links
545 There are two ways that files can be linked together.  @dfn{Symbolic
546 links} are a special type of file whose contents are a portion of the
547 name of another file.  @dfn{Hard links} are multiple directory entries
548 for one file; the file names all have the same index node
549 (@dfn{inode}) number on the disk.
551 @menu
552 * Symbolic Links::
553 * Hard Links::
554 @end menu
556 @node Symbolic Links
557 @subsection Symbolic Links
559 Symbolic links are names that reference other files.  GNU @code{find}
560 will handle symbolic links in one of two ways; firstly, it can
561 dereference the links for you - this means that if it comes across a
562 symbolic link, it examines the file that the link points to, in order
563 to see if it matches the criteria you have specified.  Secondly, it
564 can check the link itself in case you might be looking for the actual
565 link.  If the file that the symbolic link points to is also within the
566 directory hierarchy you are searching with the @code{find} command,
567 you may not see a great deal of difference between these two
568 alternatives.
570 By default, @code{find} examines symbolic links themselves when it
571 finds them (and, if it later comes across the linked-to file, it will
572 examine that, too).  If you would prefer @code{find} to dereference
573 the links and examine the file that each link points to, specify the
574 @samp{-L} option to @code{find}.  You can explicitly specify the
575 default behaviour by using the @samp{-P} option.  The @samp{-H}
576 option is a half-way-between option which ensures that any symbolic
577 links listed on the command line are dereferenced, but other symbolic
578 links are not.
580 Symbolic links are different to ``hard links'' in the sense that you
581 need permissions upon the linked-to file in order to be able to
582 dereference the link.  This can mean that even if you specify the
583 @samp{-L} option, @code{find} may not be able to determine the
584 properties of the file that the link points to (because you don't have
585 sufficient permissions).  In this situation, @code{find} uses the
586 properties of the link itself.  This also occurs if a symbolic link
587 exists but points to a file that is missing.
589 The options controlling the behaviour of @code{find} with respect to
590 links are as follows :-
592 @table @samp
593 @item -P
594 @code{find} does not dereference symbolic links at all.  This is the
595 default behaviour.  This option must be specified before any of the
596 file names on the command line.
597 @item -H
598 @code{find} does not dereference symbolic links (except in the case of
599 file names on the command line, which are dereferenced).  If a
600 symbolic link cannot be dereferenced, the information for the symbolic
601 link itself is used.  This option must be specified before any of the
602 file names on the command line.
603 @item -L
604 @code{find} dereferences symbolic links where possible, and where this
605 is not possible it uses the properties of the symbolic link itself.
606 This option must be specified before any of the file names on the
607 command line.  Use of this option also implies the same behaviour as
608 the @samp{-noleaf} option.  If you later use the @samp{-H} or
609 @samp{-P} options, this does not turn off @samp{-noleaf}.
611 @item -follow
612 This option forms part of the ``expression'' and must be specified
613 after the file names, but it is otherwise equivalent to @samp{-L}.
614 @end table
616 The following differences in behavior occur when the @samp{-L} option
617 is used:
619 @itemize @bullet
620 @item
621 @code{find} follows symbolic links to directories when searching
622 directory trees.
623 @item
624 @samp{-lname} and @samp{-ilname} always return false (unless they
625 happen to match broken symbolic links).
626 @item
627 @samp{-type} reports the types of the files that symbolic links point
629 @item
630 Implies @samp{-noleaf} (@pxref{Directories}).
631 @end itemize
633 If the @samp{-L} option or the @samp{-H} option is used,
634 the file names used as arguments to @samp{-newer}, @samp{-anewer}, and
635 @samp{-cnewer} are dereferenced and the timestamp from the pointed-to
636 file is used instead (if possible -- otherwise the timestamp from the
637 symbolic link is used).
639 @deffn Test -lname pattern
640 @deffnx Test -ilname pattern
641 True if the file is a symbolic link whose contents match shell pattern
642 @var{pattern}.  For @samp{-ilname}, the match is case-insensitive.
643 @xref{Shell Pattern Matching}, for details about the @var{pattern}
644 argument.  If the @samp{-L} option is in effect, this test will always
645 return false for symbolic links unless they are broken.  So, to list
646 any symbolic links to @file{sysdep.c} in the current directory and its
647 subdirectories, you can do:
649 @example
650 find . -lname '*sysdep.c'
651 @end example
652 @end deffn
654 @node Hard Links
655 @subsection Hard Links
657 Hard links allow more than one name to refer to the same file.  To
658 find all the names which refer to the same file as NAME, use
659 @samp{-samefile NAME}.  If you are not using the @samp{-L} option, you
660 can confine your search to one filesystem using the @samp{-xdev}
661 option.  This is useful because hard links cannot point outside a
662 single filesystem, so this can cut down on needless searching.
664 If the @samp{-L} option is in effect, and NAME is in fact a symbolic
665 link, the symbolic link will be dereferenced.  Hence you are searching
666 for other links (hard or symbolic) to the file pointed to by NAME.  If
667 @samp{-L} is in effect but NAME is not itself a symbolic link, other
668 symbolic links to the file NAME will be matched.
670 You can also search for files by inode number.  This can occasionally
671 be useful in diagnosing problems with filesystems for example, because
672 @code{fsck} tends to print inode numbers.  Inode numbers also
673 occasionally turn up in log messages for some types of software, and
674 are used to support the @code{ftok()} library function.
676 You can learn a file's inode number and the number of links to it by
677 running @samp{ls -li} or @samp{find -ls}.
679 You can search for hard links to inode number NUM by using @samp{-inum
680 NUM}. If there are any filesystem mount points below the directory
681 where you are starting the search, use the @samp{-xdev} option unless
682 you are also using the @samp{-L} option.  Using @samp{-xdev} this
683 saves needless searching, since hard links to a file must be on the
684 same filesystem.  @xref{Filesystems}.
686 @deffn Test -samefile NAME
687 File is a hard link to the same inode as NAME.  If the @samp{-L}
688 option is in effect, symbolic links to the same file as NAME points to
689 are also matched.
690 @end deffn
692 @deffn Test -inum n
693 File has inode number @var{n}.  The @samp{+} and @samp{-} qualifiers
694 also work, though these are rarely useful.
695 @end deffn
697 You can also search for files that have a certain number of links,
698 with @samp{-links}.  Directories normally have at least two hard
699 links; their @file{.} entry is the second one.  If they have
700 subdirectories, each of those also has a hard link called @file{..} to
701 its parent directory.  The @file{.} and @file{..} directory entries
702 are not normally searched unless they are mentioned on the @code{find}
703 command line.
705 @deffn Test -links n
706 File has @var{n} hard links.
707 @end deffn
709 @deffn Test -links +n
710 File has more than @var{n} hard links.
711 @end deffn
713 @deffn Test -links -n
714 File has fewer than @var{n} hard links.
715 @end deffn
717 @node Time
718 @section Time
720 Each file has three time stamps, which record the last time that
721 certain operations were performed on the file:
723 @enumerate
724 @item
725 access (read the file's contents)
726 @item
727 change the status (modify the file or its attributes)
728 @item
729 modify (change the file's contents)
730 @end enumerate
732 There is no timestamp that indicates when a file was @emph{created}.
734 You can search for files whose time stamps are within a certain age
735 range, or compare them to other time stamps.
737 @menu
738 * Age Ranges::
739 * Comparing Timestamps::
740 @end menu
742 @node Age Ranges
743 @subsection Age Ranges
745 These tests are mainly useful with ranges (@samp{+@var{n}} and
746 @samp{-@var{n}}).
748 @deffn Test -atime n
749 @deffnx Test -ctime n
750 @deffnx Test -mtime n
751 True if the file was last accessed (or its status changed, or it was
752 modified) @var{n}*24 hours ago.  The number of 24-hour periods since
753 the file's timestamp is always rounded down; therefore 0 means ``less
754 than 24 hours ago'', 1 means ``between 24 and 48 hours ago'', and so
755 forth.
756 @end deffn
758 @deffn Test -amin n
759 @deffnx Test -cmin n
760 @deffnx Test -mmin n
761 True if the file was last accessed (or its status changed, or it was
762 modified) @var{n} minutes ago.  These tests provide finer granularity
763 of measurement than @samp{-atime} et al., but rounding is done in a
764 similar way.  For example, to list files in @file{/u/bill} that were
765 last read from 2 to 6 minutes ago:
767 @example
768 find /u/bill -amin +2 -amin -6
769 @end example
770 @end deffn
772 @deffn Option -daystart
773 Measure times from the beginning of today rather than from 24 hours
774 ago.  So, to list the regular files in your home directory that were
775 modified yesterday, do
777 @example
778 find ~ -daystart -type f -mtime 1
779 @end example
780 @end deffn
782 The @samp{-daystart} option is unlike most other options in that it
783 has an effect on the way that other tests are performed.  The affected
784 tests are @samp{-amin}, @samp{-cmin}, @samp{-mmin}, @samp{-atime},
785 @samp{-ctime} and @samp{-mtime}.
787 @node Comparing Timestamps
788 @subsection Comparing Timestamps
790 As an alternative to comparing timestamps to the current time, you can
791 compare them to another file's timestamp.  That file's timestamp could
792 be updated by another program when some event occurs.  Or you could
793 set it to a particular fixed date using the @code{touch} command.  For
794 example, to list files in @file{/usr} modified after February 1 of the
795 current year:
797 @c Idea from Rick Sladkey.
798 @example
799 touch -t 02010000 /tmp/stamp$$
800 find /usr -newer /tmp/stamp$$
801 rm -f /tmp/stamp$$
802 @end example
804 @deffn Test -anewer file
805 @deffnx Test -cnewer file
806 @deffnx Test -newer file
807 True if the file was last accessed (or its status changed, or it was
808 modified) more recently than @var{file} was modified.  These tests are
809 affected by @samp{-follow} only if @samp{-follow} comes before them on
810 the command line.  @xref{Symbolic Links}, for more information on
811 @samp{-follow}.  As an example, to list any files modified since
812 @file{/bin/sh} was last modified:
814 @example
815 find . -newer /bin/sh
816 @end example
817 @end deffn
819 @deffn Test -used n
820 True if the file was last accessed @var{n} days after its status was
821 last changed.  Useful for finding files that are not being used, and
822 could perhaps be archived or removed to save disk space.
823 @end deffn
825 @node Size
826 @section Size
828 @deffn Test -size n@r{[}bckwMG@r{]}
829 True if the file uses @var{n} units of space, rounding up.  The units
830 are 512-byte blocks by default, but they can be changed by adding a
831 one-character suffix to @var{n}:
833 @table @code
834 @item b
835 512-byte blocks (never 1024)
836 @item c
837 bytes
838 @item k
839 kilobytes (1024 bytes)
840 @item w
841 2-byte words
842 @item M
843 Megabytes
844 @item G
845 Gigabytes
846 @end table
848 The `b' suffix always considers blocks to be 512 bytes.  This is not
849 affected by the setting (or non-setting) of the POSIXLY_CORRECT
850 environment variable.  This behaviour is different to the behaviour of
851 the @samp{-ls} action).  If you want to use 1024-byte units, use the
852 `k' suffix instead.
854 The number can be prefixed with a `+' or a `-'.  A plus sign indicates
855 that the test should succeed if the file uses at least @var{n} units
856 of storage (a common use of this test) and a minus sign
857 indicates that the test should succeed if the file uses less than
858 @var{n} units of storage.  There is no `=' prefix, because that's the
859 default anyway.
861 The size does not count indirect blocks, but it does count blocks in
862 sparse files that are not actually allocated.  In other words, it's
863 consistent with the result you get for @samp{ls -l} or @samp{wc -c}.
864 This handling of sparse files differs from the output of the @samp{%k}
865 and @samp{%b} format specifiers for the @samp{-printf} predicate.
867 @end deffn
869 @deffn Test -empty
870 True if the file is empty and is either a regular file or a directory.
871 This might help determine good candidates for deletion.  This test is
872 useful with @samp{-depth} (@pxref{Directories}) and @samp{-delete}
873 (@pxref{Single File}).
874 @end deffn
876 @node Type
877 @section Type
879 @deffn Test -type c
880 True if the file is of type @var{c}:
882 @table @code
883 @item b
884 block (buffered) special
885 @item c
886 character (unbuffered) special
887 @item d
888 directory
889 @item p
890 named pipe (FIFO)
891 @item f
892 regular file
893 @item l
894 symbolic link
895 @item s
896 socket
897 @item D
898 door (Solaris)
899 @end table
900 @end deffn
902 @deffn Test -xtype c
903 The same as @samp{-type} unless the file is a symbolic link.  For
904 symbolic links: if @samp{-follow} has not been given, true if the file
905 is a link to a file of type @var{c}; if @samp{-follow} has been given,
906 true if @var{c} is @samp{l}.  In other words, for symbolic links,
907 @samp{-xtype} checks the type of the file that @samp{-type} does not
908 check.  @xref{Symbolic Links}, for more information on @samp{-follow}.
909 @end deffn
911 @node Owner
912 @section Owner
914 @deffn Test -user uname
915 @deffnx Test -group gname
916 True if the file is owned by user @var{uname} (belongs to group
917 @var{gname}).  A numeric ID is allowed.
918 @end deffn
920 @deffn Test -uid n
921 @deffnx Test -gid n
922 True if the file's numeric user ID (group ID) is @var{n}.  These tests
923 support ranges (@samp{+@var{n}} and @samp{-@var{n}}), unlike
924 @samp{-user} and @samp{-group}.
925 @end deffn
927 @deffn Test -nouser
928 @deffnx Test -nogroup
929 True if no user corresponds to the file's numeric user ID (no group
930 corresponds to the numeric group ID).  These cases usually mean that
931 the files belonged to users who have since been removed from the
932 system.  You probably should change the ownership of such files to an
933 existing user or group, using the @code{chown} or @code{chgrp}
934 program.
935 @end deffn
937 @node Permissions
938 @section Permissions
940 @xref{File Permissions}, for information on how file permissions are
941 structured and how to specify them.
943 @deffn Test -perm mode
945 True if the file's permissions are exactly @var{mode}, which can be
946 numeric or symbolic.
948 If @var{mode} starts with @samp{-}, true if
949 @emph{all} of the permissions set in @var{mode} are set for the file;
950 permissions not set in @var{mode} are ignored.
951 If @var{mode} starts with @samp{/}, true if
952 @emph{any} of the permissions set in @var{mode} are set for the file;
953 permissions not set in @var{mode} are ignored.
954 This is a GNU extension.
956 If you don't use the @samp{/} or @samp{-} form with a symbolic mode
957 string, you may have to specify a rather complex mode string.  For
958 example @samp{-perm g=w} will only match files which have mode 0020
959 (that is, ones for which group write permission is the only permission
960 set).  It is more likely that you will want to use the @samp{/} or
961 @samp{-} forms, for example @samp{-perm -g=w}, which matches any file
962 with group write permission.
965 @table @samp
966 @item -perm 664
967 Match files which have read and write permission for their owner,
968 and group, but which the rest of the world can read but not write to.
969 Files which meet these criteria but have other permissions bits set
970 (for example if someone can execute the file) will not be matched.
972 @item -perm -664
973 Match files which have read and write permission for their owner,
974 and group, but which the rest of the world can read but not write to,
975 without regard to the presence of any extra permission bits (for
976 example the executable bit).  This will match a file which has mode
977 0777, for example.
979 @item -perm /222
980 Match files which are writeable by somebody (their owner, or
981 their group, or anybody else).
983 @item -perm /022
984 Match files which are writeable by either their owner or their
985 group.  The files don't have to be writeable by both the owner and
986 group to be matched; either will do.
988 @item -perm /g+w,o+w
989 As above.
991 @item -perm /g=w,o=w
992 As above
994 @item -perm -022
995 Search for files which are writeable by both their owner and their
996 group.
998 @item -perm -g+w,o+w
999 As above.
1000 @end table
1001 @end deffn
1003 @node Contents
1004 @section Contents
1006 To search for files based on their contents, you can use the
1007 @code{grep} program.  For example, to find out which C source files in
1008 the current directory contain the string @samp{thing}, you can do:
1010 @example
1011 grep -l thing *.[ch]
1012 @end example
1014 If you also want to search for the string in files in subdirectories,
1015 you can combine @code{grep} with @code{find} and @code{xargs}, like
1016 this:
1018 @example
1019 find . -name '*.[ch]' | xargs grep -l thing
1020 @end example
1022 The @samp{-l} option causes @code{grep} to print only the names of
1023 files that contain the string, rather than the lines that contain it.
1024 The string argument (@samp{thing}) is actually a regular expression,
1025 so it can contain metacharacters.  This method can be refined a little
1026 by using the @samp{-r} option to make @code{xargs} not run @code{grep}
1027 if @code{find} produces no output, and using the @code{find} action
1028 @samp{-print0} and the @code{xargs} option @samp{-0} to avoid
1029 misinterpreting files whose names contain spaces:
1031 @example
1032 find . -name '*.[ch]' -print0 | xargs -r -0 grep -l thing
1033 @end example
1035 For a fuller treatment of finding files whose contents match a
1036 pattern, see the manual page for @code{grep}.
1038 @node Directories
1039 @section Directories
1041 Here is how to control which directories @code{find} searches, and how
1042 it searches them.  These two options allow you to process a horizontal
1043 slice of a directory tree.
1045 @deffn Option -maxdepth levels
1046 Descend at most @var{levels} (a non-negative integer) levels of
1047 directories below the command line arguments.  @samp{-maxdepth 0}
1048 means only apply the tests and actions to the command line arguments.
1049 @end deffn
1051 @deffn Option -mindepth levels
1052 Do not apply any tests or actions at levels less than @var{levels} (a
1053 non-negative integer).  @samp{-mindepth 1} means process all files
1054 except the command line arguments.
1055 @end deffn
1057 @deffn Option -depth
1058 Process each directory's contents before the directory itself.  Doing
1059 this is a good idea when producing lists of files to archive with
1060 @code{cpio} or @code{tar}.  If a directory does not have write
1061 permission for its owner, its contents can still be restored from the
1062 archive since the directory's permissions are restored after its
1063 contents.
1064 @end deffn
1066 @deffn Option -d
1067 This is a deprecated synonym for @samp{-depth}, for compatibility with
1068 Mac OS X, FreeBSD and OpenBSD.  The @samp{-depth} option is a POSIX
1069 feature, so it is better to use that.
1070 @end deffn
1072 @deffn Action -prune
1073 If the file is a directory, do not descend into it.  The result is
1074 true.  For example, to skip the directory @file{src/emacs} and all
1075 files and directories under it, and print the names of the other files
1076 found:
1078 @example
1079 find . -wholename './src/emacs' -prune -o -print
1080 @end example
1082 The above command will not print @file{./src/emacs} among its list of
1083 results.  This however is not due to the effect of the @samp{-prune}
1084 action (which only prevents further descent, it doesn't make sure we
1085 ignore that item).  Instead, this effect is due to the use of
1086 @samp{-o}.  Since the left hand side of the ``or'' condition has
1087 succeeded for @file{./src/emacs}, it is not necessary to evaluate the
1088 right-hand-side (@samp{-print}) at all for this particular file.  If
1089 you wanted to print that directory name you could use either an extra
1090 @samp{-print} action:
1092 @example
1093 find . -wholename './src/emacs' -prune -print -o -print
1094 @end example
1096 or use the comma operator:
1098 @example
1099 find . -wholename './src/emacs' -prune , -print
1100 @end example
1102 If the @samp{-depth} option is in effect, the subdirectories will have
1103 already been visited in any case.  Hence @samp{-prune} has no effect
1104 and returns false.
1105 @end deffn
1108 @deffn Action -quit
1109 Exit immediately (with return value zero if no errors have occurred).
1110 No child processes will be left running, but no more files specified
1111 on the command line will be processed.  For example, @code{find
1112 /tmp/foo /tmp/bar -print -quit} will print only @samp{/tmp/foo}.
1113 @end deffn
1115 @deffn Option -noleaf
1116 Do not optimize by assuming that directories contain 2 fewer
1117 subdirectories than their hard link count.  This option is needed when
1118 searching filesystems that do not follow the Unix directory-link
1119 convention, such as CD-ROM or MS-DOS filesystems or AFS volume mount
1120 points.  Each directory on a normal Unix filesystem has at least 2
1121 hard links: its name and its @file{.}  entry.  Additionally, its
1122 subdirectories (if any) each have a @file{..}  entry linked to that
1123 directory.  When @code{find} is examining a directory, after it has
1124 statted 2 fewer subdirectories than the directory's link count, it
1125 knows that the rest of the entries in the directory are
1126 non-directories (@dfn{leaf} files in the directory tree).  If only the
1127 files' names need to be examined, there is no need to stat them; this
1128 gives a significant increase in search speed.
1129 @end deffn
1131 @deffn Option -ignore_readdir_race
1132 If a file disappears after its name has been read from a directory but
1133 before @code{find} gets around to examining the file with @code{stat},
1134 don't issue an error message.  If you don't specify this option, an
1135 error message will be issued.  This option can be useful in system
1136 scripts (cron scripts, for example) that examine areas of the
1137 filesystem that change frequently (mail queues, temporary directories,
1138 and so forth), because this scenario is common for those sorts of
1139 directories.  Completely silencing error messages from @code{find} is
1140 undesirable, so this option neatly solves the problem.  There is no
1141 way to search one part of the filesystem with this option on and part
1142 of it with this option off, though.
1143 @end deffn
1145 @deffn Option -noignore_readdir_race
1146 This option reverses the effect of the @samp{-ignore_readdir_race}
1147 option.
1148 @end deffn
1151 @node Filesystems
1152 @section Filesystems
1154 A @dfn{filesystem} is a section of a disk, either on the local host or
1155 mounted from a remote host over a network.  Searching network
1156 filesystems can be slow, so it is common to make @code{find} avoid
1157 them.
1159 There are two ways to avoid searching certain filesystems.  One way is
1160 to tell @code{find} to only search one filesystem:
1162 @deffn Option -xdev
1163 @deffnx Option -mount
1164 Don't descend directories on other filesystems.  These options are
1165 synonyms.
1166 @end deffn
1168 The other way is to check the type of filesystem each file is on, and
1169 not descend directories that are on undesirable filesystem types:
1171 @deffn Test -fstype type
1172 True if the file is on a filesystem of type @var{type}.  The valid
1173 filesystem types vary among different versions of Unix; an incomplete
1174 list of filesystem types that are accepted on some version of Unix or
1175 another is:
1176 @example
1177 ext2 ext3 proc sysfs ufs 4.2 4.3 nfs tmp mfs S51K S52K
1178 @end example
1179 You can use @samp{-printf} with the @samp{%F} directive to see the
1180 types of your filesystems.  The @samp{%D} directive shows the device
1181 number.  @xref{Print File Information}.  @samp{-fstype} is usually
1182 used with @samp{-prune} to avoid searching remote filesystems
1183 (@pxref{Directories}).
1184 @end deffn
1186 @node Combining Primaries With Operators
1187 @section Combining Primaries With Operators
1189 Operators build a complex expression from tests and actions.
1190 The operators are, in order of decreasing precedence:
1192 @table @code
1193 @item @asis{( @var{expr} )}
1194 @findex ()
1195 Force precedence.  True if @var{expr} is true.
1197 @item @asis{! @var{expr}}
1198 @itemx @asis{-not @var{expr}}
1199 @findex !
1200 @findex -not
1201 True if @var{expr} is false.
1203 @item @asis{@var{expr1 expr2}}
1204 @itemx @asis{@var{expr1} -a @var{expr2}}
1205 @itemx @asis{@var{expr1} -and @var{expr2}}
1206 @findex -a
1207 @findex -and
1208 And; @var{expr2} is not evaluated if @var{expr1} is false.
1210 @item @asis{@var{expr1} -o @var{expr2}}
1211 @itemx @asis{@var{expr1} -or @var{expr2}}
1212 @findex -o
1213 @findex -or
1214 Or; @var{expr2} is not evaluated if @var{expr1} is true.
1216 @item @asis{@var{expr1} , @var{expr2}}
1217 @findex ,
1218 List; both @var{expr1} and @var{expr2} are always evaluated.  True if
1219 @var{expr2} is true.  The value of @var{expr1} is discarded.  This
1220 operator lets you do multiple independent operations on one traversal,
1221 without depending on whether other operations succeeded.  The two
1222 operations @var{expr1} and @var{expr2} are not always fully
1223 independent, since @var{expr1} might have side effects like touching
1224 or deleting files, or it might use @samp{-prune} which would also
1225 affect @var{expr2}.
1226 @end table
1228 @code{find} searches the directory tree rooted at each file name by
1229 evaluating the expression from left to right, according to the rules
1230 of precedence, until the outcome is known (the left hand side is false
1231 for @samp{-and}, true for @samp{-or}), at which point @code{find}
1232 moves on to the next file name.
1234 There are two other tests that can be useful in complex expressions:
1236 @deffn Test -true
1237 Always true.
1238 @end deffn
1240 @deffn Test -false
1241 Always false.
1242 @end deffn
1244 @node Actions, Common Tasks, Finding Files, Top
1245 @chapter Actions
1247 There are several ways you can print information about the files that
1248 match the criteria you gave in the @code{find} expression.  You can
1249 print the information either to the standard output or to a file that
1250 you name.  You can also execute commands that have the file names as
1251 arguments.  You can use those commands as further filters to select
1252 files.
1254 @menu
1255 * Print File Name::
1256 * Print File Information::
1257 * Run Commands::
1258 * Delete Files::
1259 * Adding Tests::
1260 @end menu
1262 @node Print File Name
1263 @section Print File Name
1265 @deffn Action -print
1266 True; print the entire file name on the standard output, followed by a
1267 newline.
1268 @end deffn
1270 @deffn Action -fprint file
1271 True; print the entire file name into file @var{file}, followed by a
1272 newline.  If @var{file} does not exist when @code{find} is run, it is
1273 created; if it does exist, it is truncated to 0 bytes.  The file names
1274 @file{/dev/stdout} and @file{/dev/stderr} are handled specially; they
1275 refer to the standard output and standard error output, respectively.
1276 @end deffn
1278 @node Print File Information
1279 @section Print File Information
1281 @deffn Action -ls
1282 True; list the current file in @samp{ls -dils} format on the standard
1283 output.  The output looks like this:
1285 @smallexample
1286 204744   17 -rw-r--r--   1 djm      staff       17337 Nov  2  1992 ./lwall-quotes
1287 @end smallexample
1289 The fields are:
1291 @enumerate
1292 @item
1293 The inode number of the file.  @xref{Hard Links}, for how to find
1294 files based on their inode number.
1296 @item
1297 the number of blocks in the file.  The block counts are of 1K blocks,
1298 unless the environment variable @code{POSIXLY_CORRECT} is set, in
1299 which case 512-byte blocks are used.  @xref{Size}, for how to find
1300 files based on their size.
1302 @item
1303 The file's type and permissions.  The type is shown as a dash for a
1304 regular file; for other file types, a letter like for @samp{-type} is
1305 used (@pxref{Type}).  The permissions are read, write, and execute for
1306 the file's owner, its group, and other users, respectively; a dash
1307 means the permission is not granted.  @xref{File Permissions}, for
1308 more details about file permissions.  @xref{Permissions}, for how to
1309 find files based on their permissions.
1311 @item
1312 The number of hard links to the file.
1314 @item
1315 The user who owns the file.
1317 @item
1318 The file's group.
1320 @item
1321 The file's size in bytes.
1323 @item
1324 The date the file was last modified.
1326 @item
1327 The file's name.  @samp{-ls} quotes non-printable characters in the
1328 file names using C-like backslash escapes.  This may change soon, as
1329 the treatment of unprintable characters is harmonised for @samp{-ls},
1330 @samp{-fls}, @samp{-print}, @samp{-fprint}, @samp{-printf} and
1331 @samp{-fprintf}.
1332 @end enumerate
1333 @end deffn
1335 @deffn Action -fls file
1336 True; like @samp{-ls} but write to @var{file} like @samp{-fprint}
1337 (@pxref{Print File Name}).
1338 @end deffn
1340 @deffn Action -printf format
1341 True; print @var{format} on the standard output, interpreting @samp{\}
1342 escapes and @samp{%} directives.  Field widths and precisions can be
1343 specified as with the @code{printf} C function.  Format flags (like
1344 @samp{#} for example) may not work as you expect because many of the
1345 fields, even numeric ones, are printed with %s.  This means though
1346 that the format flag @samp{-} will work; it forces left-alignment of
1347 the field.  Unlike @samp{-print}, @samp{-printf} does not add a
1348 newline at the end of the string.  If you want a newline at the end of
1349 the string, add a @samp{\n}.
1350 @end deffn
1352 @deffn Action -fprintf file format
1353 True; like @samp{-printf} but write to @var{file} like @samp{-fprint}
1354 (@pxref{Print File Name}).
1355 @end deffn
1357 @menu
1358 * Escapes::
1359 * Format Directives::
1360 * Time Formats::
1361 @end menu
1363 @node Escapes
1364 @subsection Escapes
1366 The escapes that @samp{-printf} and @samp{-fprintf} recognize are:
1368 @table @code
1369 @item \a
1370 Alarm bell.
1371 @item \b
1372 Backspace.
1373 @item \c
1374 Stop printing from this format immediately and flush the output.
1375 @item \f
1376 Form feed.
1377 @item \n
1378 Newline.
1379 @item \r
1380 Carriage return.
1381 @item \t
1382 Horizontal tab.
1383 @item \v
1384 Vertical tab.
1385 @item \\
1386 A literal backslash (@samp{\}).
1387 @item \NNN
1388 The character whose ASCII code is NNN (octal).
1389 @end table
1391 A @samp{\} character followed by any other character is treated as an
1392 ordinary character, so they both are printed, and a warning message is
1393 printed to the standard error output (because it was probably a typo).
1395 @node Format Directives
1396 @subsection Format Directives
1398 @samp{-printf} and @samp{-fprintf} support the following format
1399 directives to print information about the file being processed.  The C
1400 @code{printf} function, field width and precision specifiers are
1401 supported, as applied to string (%s) types. That is, you can specify
1402 "minimum field width"."maximum field width" for each directive.
1403 Format flags (like @samp{#} for example) may not work as you expect
1404 because many of the fields, even numeric ones, are printed with %s.
1405 The format flag @samp{-} does work; it forces left-alignment of the
1406 field.
1408 @samp{%%} is a literal percent sign.  A @samp{%} character followed by
1409 an unrecognised character (i.e. not a known directive or printf field
1410 width and precision specifier), is discarded (but the unrecognised
1411 character is printed), and a warning message is printed to the
1412 standard error output (because it was probably a typo).
1414 @menu
1415 * Name Directives::
1416 * Ownership Directives::
1417 * Size Directives::
1418 * Location Directives::
1419 * Time Directives::
1420 * Formatting Flags::
1421 @end menu
1423 @node Name Directives
1424 @subsubsection Name Directives
1426 @table @code
1427 @item %p
1428 @c supports %-X.Yp
1429 File's name (not the absolute path name, but the name of the file as
1430 it was encountered by @code{find} - that is, as a relative path from
1431 one of the starting points).
1432 @item %f
1433 File's name with any leading directories removed (only the last
1434 element).
1435 @c supports %-X.Yf
1436 @item %h
1437 Leading directories of file's name (all but the last element and the
1438 slash before it).  If the file's name contains no slashes (for example
1439 because it was named on the command line and is in the current working
1440 directory), then ``%h'' expands to ``.''.  This prevents ``%h/%f''
1441 expanding to ``/foo'', which would be surprising and probably not
1442 desirable.
1443 @c supports %-X.Yh
1444 @item %P
1445 File's name with the name of the command line argument under which
1446 it was found removed from the beginning.
1447 @c supports %-X.YP
1448 @item %H
1449 Command line argument under which file was found.
1450 @c supports %-X.YH
1451 @end table
1453 @node Ownership Directives
1454 @subsubsection Ownership Directives
1456 @table @code
1457 @item %g
1458 @c supports %-X.Yg
1459 File's group name, or numeric group ID if the group has no name.
1460 @item %G
1461 @c supports %-X.Yg
1462 @c TODO: Needs to support # flag and 0 flag
1463 File's numeric group ID.
1464 @item %u
1465 @c supports %-X.Yu
1466 File's user name, or numeric user ID if the user has no name.
1467 @item %U
1468 @c supports %-X.Yu
1469 @c TODO: Needs to support # flag
1470 File's numeric user ID.
1471 @item %m
1472 @c full support, including # and 0.
1473 File's permissions (in octal).  If you always want to have a leading
1474 zero on the number, use the '#' format flag, for example '%#m'.
1475 @end table
1477 @node Size Directives
1478 @subsubsection Size Directives
1480 @table @code
1481 @item %k
1482 The amount of disk space used for this file in 1K blocks. Since disk
1483 space is allocated in multiples of the filesystem block size this is
1484 usually greater than %s/1024, but it can also be smaller if the file
1485 is a sparse file (that is, it has ``holes'').
1486 @item %b
1487 The amount of disk space used for this file in 512-byte blocks. Since
1488 disk space is allocated in multiples of the filesystem block size this
1489 is usually greater than %s/1024, but it can also be smaller if the
1490 file is a sparse file (that is, it has ``holes'').
1491 @item %s
1492 File's size in bytes.
1493 @end table
1495 @node Location Directives
1496 @subsubsection Location Directives
1498 @table @code
1499 @item %d
1500 File's depth in the directory tree (depth below a file named on the
1501 command line, not depth below the root directory).  Files named on the
1502 command line have a depth of 0.  Subdirectories immediately below them
1503 have a depth of 1, and so on.
1504 @item %D
1505 The device number on which the file exists (the @code{st_dev} field of
1506 @code{struct stat}), in decimal.
1507 @item %F
1508 Type of the filesystem the file is on; this value can be used for
1509 @samp{-fstype} (@pxref{Directories}).
1510 @item %l
1511 Object of symbolic link (empty string if file is not a symbolic link).
1512 @item %i
1513 File's inode number (in decimal).
1514 @item %n
1515 Number of hard links to file.
1516 @item %y
1517 Type of the file as used with @samp{-type}.  If the file is a symbolic
1518 link, @samp{l} will be printed.
1519 @item %Y
1520 Type of the file as used with @samp{-type}.  If the file is a symbolic
1521 link, it is dereferenced.  If the file is a broken symbolic link,
1522 @samp{N} is printed.
1524 @end table
1526 @node Time Directives
1527 @subsubsection Time Directives
1529 Some of these directives use the C @code{ctime} function.  Its output
1530 depends on the current locale, but it typically looks like
1532 @example
1533 Wed Nov  2 00:42:36 1994
1534 @end example
1536 @table @code
1537 @item %a
1538 File's last access time in the format returned by the C @code{ctime}
1539 function.
1540 @item %A@var{k}
1541 File's last access time in the format specified by @var{k}
1542 (@pxref{Time Formats}).
1543 @item %c
1544 File's last status change time in the format returned by the C
1545 @code{ctime} function.
1546 @item %C@var{k}
1547 File's last status change time in the format specified by @var{k}
1548 (@pxref{Time Formats}).
1549 @item %t
1550 File's last modification time in the format returned by the C
1551 @code{ctime} function.
1552 @item %T@var{k}
1553 File's last modification time in the format specified by @var{k}
1554 (@pxref{Time Formats}).
1555 @end table
1557 @node Time Formats
1558 @subsection Time Formats
1560 Below are the formats for the directives @samp{%A}, @samp{%C}, and
1561 @samp{%T}, which print the file's timestamps.  Some of these formats
1562 might not be available on all systems, due to differences in the C
1563 @code{strftime} function between systems.
1565 @menu
1566 * Time Components::
1567 * Date Components::
1568 * Combined Time Formats::
1569 @end menu
1571 @node Time Components
1572 @subsubsection Time Components
1574 The following format directives print single components of the time.
1576 @table @code
1577 @item H
1578 hour (00..23)
1579 @item I
1580 hour (01..12)
1581 @item k
1582 hour ( 0..23)
1583 @item l
1584 hour ( 1..12)
1585 @item p
1586 locale's AM or PM
1587 @item Z
1588 time zone (e.g., EDT), or nothing if no time zone is determinable
1589 @item M
1590 minute (00..59)
1591 @item S
1592 second (00..61)
1593 @item @@
1594 seconds since Jan. 1, 1970, 00:00 GMT.
1595 @end table
1597 @node Date Components
1598 @subsubsection Date Components
1600 The following format directives print single components of the date.
1602 @table @code
1603 @item a
1604 locale's abbreviated weekday name (Sun..Sat)
1605 @item A
1606 locale's full weekday name, variable length (Sunday..Saturday)
1607 @item b
1608 @itemx h
1609 locale's abbreviated month name (Jan..Dec)
1610 @item B
1611 locale's full month name, variable length (January..December)
1612 @item m
1613 month (01..12)
1614 @item d
1615 day of month (01..31)
1616 @item w
1617 day of week (0..6)
1618 @item j
1619 day of year (001..366)
1620 @item U
1621 week number of year with Sunday as first day of week (00..53)
1622 @item W
1623 week number of year with Monday as first day of week (00..53)
1624 @item Y
1625 year (1970@dots{})
1626 @item y
1627 last two digits of year (00..99)
1628 @end table
1630 @node Combined Time Formats
1631 @subsubsection Combined Time Formats
1633 The following format directives print combinations of time and date
1634 components.
1636 @table @code
1637 @item r
1638 time, 12-hour (hh:mm:ss [AP]M)
1639 @item T
1640 time, 24-hour (hh:mm:ss)
1641 @item X
1642 locale's time representation (H:M:S)
1643 @item c
1644 locale's date and time (Sat Nov 04 12:02:33 EST 1989)
1645 @item D
1646 date (mm/dd/yy)
1647 @item x
1648 locale's date representation (mm/dd/yy)
1649 @item +
1650 Date and time, separated by '+', for example `2004-04-28+22:22:05'.
1651 The time is given in the current timezone (which may be affected by
1652 setting the TZ environment variable).  This is a GNU extension.
1653 @end table
1655 @node Formatting Flags
1656 @subsubsection Formatting Flags
1658 The @samp{%m} and @samp{%d} directives support the @samp{#}, @samp{0}
1659 and @samp{+} flags, but the other directives do not, even if they
1660 print numbers.  Numeric directives that do not support these flags
1661 include
1663 @samp{G},
1664 @samp{U},
1665 @samp{b},
1666 @samp{D},
1667 @samp{k} and
1668 @samp{n}.
1670 All fields support the format flag @samp{-}, which makes fields
1671 left-aligned.  That is, if the field width is greater than the actual
1672 contents of the field, the requisite number of spaces are printed
1673 after the field content instead of before it.
1675 @node Run Commands
1676 @section Run Commands
1678 You can use the list of file names created by @code{find} or
1679 @code{locate} as arguments to other commands.  In this way you can
1680 perform arbitrary actions on the files.
1682 @menu
1683 * Single File::
1684 * Multiple Files::
1685 * Querying::
1686 @end menu
1688 @node Single File
1689 @subsection Single File
1691 Here is how to run a command on one file at a time.
1693 @deffn Action -execdir command ;
1694 Execute @var{command}; true if zero status is returned.  @code{find}
1695 takes all arguments after @samp{-exec} to be part of the command until
1696 an argument consisting of @samp{;} is reached.  It replaces the string
1697 @samp{@{@}} by the current file name being processed everywhere it
1698 occurs in the command.  Both of these constructions need to be escaped
1699 (with a @samp{\}) or quoted to protect them from expansion by the
1700 shell.  The command is executed in the directory in which @code{find}
1701 was run.
1703 For example, to compare each C header file in the current directory
1704 with the file @file{/tmp/master}:
1706 @example
1707 find . -name '*.h' -execdir diff -u '@{@}' /tmp/master ';'
1708 @end example
1709 @end deffn
1712 Another similar option, @samp{-exec} is supported, but is less secure.
1713 @xref{Security Considerations}, for a discussion of the security
1714 problems surrounding @samp{-exec}.
1717 @deffn Action -exec command ;
1718 This insecure variant of the @samp{-execdir} action is specified by
1719 POSIX.  The main difference is that the command is executed in the
1720 directory from which @code{find} was invoked, meaning that @samp{@{@}}
1721 is expanded to a relative path starting with the name of one of the
1722 starting directories, rather than just the basename of the matched
1723 file.
1724 @end deffn
1727 @node Multiple Files
1728 @subsection Multiple Files
1730 Sometimes you need to process files one of the time.  But usually this
1731 is not necessary, and, it is faster to run a command on as many files
1732 as possible at a time, rather than once per file.  Doing this saves on
1733 the time it takes to start up the command each time.
1735 The @samp{-execdir} and @samp{-exec} actions have variants that build
1736 command lines containing as many matched files as possible.
1738 @deffn Action -execdir command @{@} +
1739 This works as for @samp{-execdir command ;}, except that the
1740 @samp{@{@}} at the end of the command is expanded to a list of names
1741 of matching files.  This expansion is done in such a way as to avoid
1742 exceeding the maximum command line length available on the system.
1743 Only one @samp{@{@}} is allowed within the command, and it must appear
1744 at the end, immediately before the @samp{+}.  A @samp{+} appearing in
1745 any position other than immediately after @samp{@{@}} is not
1746 considered to be special (that is, it does not terminate the command).
1747 @end deffn
1750 @deffn Action -exec command @{@} +
1751 This insecure variant of the @samp{-execdir} action is specified by
1752 POSIX.  The main difference is that the command is executed in the
1753 directory from which @code{find} was invoked, meaning that @samp{@{@}}
1754 is expanded to a relative path starting with the name of one of the
1755 starting directories, rather than just the basename of the matched
1756 file.
1757 @end deffn
1759 Before @code{find} exits, any partially-built command lines are
1760 executed.  This happens even if the exit was caused by the
1761 @samp{-quit} action.  However, some types of error (for example not
1762 being able to invoke @code{stat()} on the current directory) can cause
1763 an immediate fatal exit.  In this situation, any partially-built
1764 command lines will not be invoked (this prevents possible infinite
1765 loops).
1767 Another, but less secure, way to run a command on more than one file
1768 at once, is to use the @code{xargs} command, which is invoked like
1769 this:
1771 @example
1772 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
1773 @end example
1775 @code{xargs} normally reads arguments from the standard input.  These
1776 arguments are delimited by blanks (which can be protected with double
1777 or single quotes or a backslash) or newlines.  It executes the
1778 @var{command} (default is @file{/bin/echo}) one or more times with any
1779 @var{initial-arguments} followed by arguments read from standard
1780 input.  Blank lines on the standard input are ignored.
1782 Instead of blank-delimited names, it is safer to use @samp{find
1783 -print0} or @samp{find -fprint0} and process the output by giving the
1784 @samp{-0} or @samp{--null} option to GNU @code{xargs}, GNU @code{tar},
1785 GNU @code{cpio}, or @code{perl}.  The @code{locate} command also has a
1786 @samp{-0} or @samp{--null} option which does the same thing.
1788 You can use shell command substitution (backquotes) to process a list
1789 of arguments, like this:
1791 @example
1792 grep -l sprintf `find $HOME -name '*.c' -print`
1793 @end example
1795 However, that method produces an error if the length of the @samp{.c}
1796 file names exceeds the operating system's command line length limit.
1797 @code{xargs} avoids that problem by running the command as many times
1798 as necessary without exceeding the limit:
1800 @example
1801 find $HOME -name '*.c' -print | xargs grep -l sprintf
1802 @end example
1804 However, if the command needs to have its standard input be a terminal
1805 (@code{less}, for example), you have to use the shell command
1806 substitution method or use the @samp{--arg-file} option of
1807 @code{xargs}.
1809 The @code{xargs} command will process all its input, building command
1810 lines and executing them, unless one of the commands exits with a
1811 status of 255 (this will cause xargs to issue an error message and
1812 stop) or it reads a line contains the end of file string specified
1813 with the @samp{--eof} option.
1815 @menu
1816 * Unsafe File Name Handling::
1817 * Safe File Name Handling::
1818 * Unusual Characters in File Names::
1819 * Limiting Command Size::
1820 * Interspersing File Names::
1821 @end menu
1823 @node Unsafe File Name Handling
1824 @subsubsection Unsafe File Name Handling
1826 Because file names can contain quotes, backslashes, blank characters,
1827 and even newlines, it is not safe to process them using @code{xargs}
1828 in its default mode of operation.  But since most files' names do not
1829 contain blanks, this problem occurs only infrequently.  If you are
1830 only searching through files that you know have safe names, then you
1831 need not be concerned about it.
1833 @c This example is adapted from:
1834 @c From: pfalstad@stone.Princeton.EDU (Paul John Falstad)
1835 @c Newsgroups: comp.unix.shell
1836 @c Subject: Re: Beware xargs security holes
1837 @c Date: 16 Oct 90 19:12:06 GMT
1839 In many applications, if @code{xargs} botches processing a file
1840 because its name contains special characters, some data might be lost.
1841 The importance of this problem depends on the importance of the data
1842 and whether anyone notices the loss soon enough to correct it.
1843 However, here is an extreme example of the problems that using
1844 blank-delimited names can cause.  If the following command is run
1845 daily from @code{cron}, then any user can remove any file on the
1846 system:
1848 @example
1849 find / -name '#*' -atime +7 -print | xargs rm
1850 @end example
1852 For example, you could do something like this:
1854 @example
1855 eg$ echo > '#
1856 vmunix'
1857 @end example
1859 @noindent
1860 and then @code{cron} would delete @file{/vmunix}, if it ran
1861 @code{xargs} with @file{/} as its current directory.
1863 To delete other files, for example @file{/u/joeuser/.plan}, you could
1864 do this:
1866 @example
1867 eg$ mkdir '#
1869 eg$ cd '#
1871 eg$ mkdir u u/joeuser u/joeuser/.plan'
1873 eg$ echo > u/joeuser/.plan'
1874 /#foo'
1875 eg$ cd ..
1876 eg$ find . -name '#*' -print | xargs echo
1877 ./# ./# /u/joeuser/.plan /#foo
1878 @end example
1880 @node Safe File Name Handling
1881 @subsubsection Safe File Name Handling
1883 Here is how to make @code{find} output file names so that they can be
1884 used by other programs without being mangled or misinterpreted.  You
1885 can process file names generated this way by giving the @samp{-0} or
1886 @samp{--null} option to GNU @code{xargs}, GNU @code{tar}, GNU
1887 @code{cpio}, or @code{perl}.
1889 @deffn Action -print0
1890 True; print the entire file name on the standard output, followed by a
1891 null character.
1892 @end deffn
1894 @deffn Action -fprint0 file
1895 True; like @samp{-print0} but write to @var{file} like @samp{-fprint}
1896 (@pxref{Print File Name}).
1897 @end deffn
1899 As of findutils version 4.2.4, the @code{locate} program also has a
1900 @samp{--null} option which does the same thing.  For similarity with
1901 @code{xargs}, the short form of the option @samp{-0} can also be used.
1903 If you want to be able to handle file names safely but need to run
1904 commands which want to be connected to a terminal on their input, you
1905 can use the @samp{--arg-file} option to @code{xargs} like this:
1907 @example
1908 find / -name xyzzy -print0 > list
1909 xargs --null --arg-file=list munge
1910 @end example
1912 The example above runs the @code{munge} program on all the files named
1913 @file{xyzzy} that we can find, but @code{munge}'s input will still be
1914 the terminal (or whatever the shell was using as standard input).  If
1915 your shell has the ``process substitution'' feature @samp{<(...)}, you
1916 can do this in just one step:
1918 @example
1919 xargs --null --arg-file=<(find / -name xyzzy -print0) munge
1920 @end example
1922 @node Unusual Characters in File Names
1923 @subsubsection Unusual Characters in File Names
1924 As discussed above, you often need to be careful about how the names
1925 of files are handled by @code{find} and other programs.  If the output
1926 of @code{find} is not going to another program but instead is being
1927 shown on a terminal, this can still be a problem.  For example, some
1928 character sequences can reprogram the function keys on some terminals.
1929 @xref{Security Considerations}, for a discussion of other security
1930 problems relating to @code{find}.
1932 Unusual characters are handled differently by various
1933 actions, as described below.
1935 @table @samp
1936 @item -print0
1937 @itemx -fprint0
1938 Always print the exact file name, unchanged, even if the output is
1939 going to a terminal.
1940 @item -ok
1941 @itemx -okdir
1942 Always print the exact file name, unchanged.  This will probably
1943 change in a future release.
1944 @item -ls
1945 @itemx -fls
1946 Unusual characters are always escaped.  White space, backslash, and
1947 double quote characters are printed using C-style escaping (for
1948 example @samp{\f}, @samp{\"}).  Other unusual characters are printed
1949 using an octal escape.  Other Printable characters (for @samp{-ls} and
1950 @samp{-fls} these are the characters between octal 041 and 0176) are
1951 printed as-is.
1952 @item -printf
1953 @itemx -fprintf
1954 If the output is not going to a terminal, it is printed as-is.
1955 Otherwise, the result depends on which directive is in use:
1957 @table @asis
1958 @item %D, %F, %H, %Y, %y
1959 These expand to values which are not under control of files' ownwers,
1960 and so are printed as-is.
1961 @item  %a, %b, %c, %d, %g, %G, %i, %k, %m, %M, %n, %s, %t, %u, %U
1962 These have values which are under the control of files' ownwers but
1963 which cannot be used to send arbitrary data to the terminal, and so
1964 these are printed as-is.
1965 @item %f, %h, %l, %p, %P
1966 The output of these directives is quoted if the output is going to a
1967 terminal.
1969 This quoting is performed in the same way as for GNU @code{ls}.  This
1970 is not the same quoting mechanism as the one used for @samp{-ls} and
1971 @samp{fls}.  If you are able to decide what format to use for the
1972 output of @code{find} then it is normally better to use @samp{\0} as a
1973 terminator than to use newline, as file names can contain white space
1974 and newline characters.
1975 @end table
1976 @item -print
1977 @itemx -fprint
1978 Quoting is handled in the same way as for the @samp{%p} directive of
1979 @samp{-printf} and @samp{-fprintf}.  If you are using @code{find} in a
1980 script or in a situation where the matched files might have arbitrary
1981 names, you should consider using @samp{-print0} instead of
1982 @samp{-print}.
1983 @end table
1986 The @code{locate} program quotes and escapes unusual characters in
1987 file names in the same way as @code{find}'s @samp{-print} action.
1989 The behaviours described above may change soon, as the treatment of
1990 unprintable characters is harmonised for @samp{-ls}, @samp{-fls},
1991 @samp{-print}, @samp{-fprint}, @samp{-printf} and @samp{-fprintf}.
1993 @node Limiting Command Size
1994 @subsubsection Limiting Command Size
1996 @code{xargs} gives you control over how many arguments it passes to
1997 the command each time it executes it.  By default, it uses up to
1998 @code{ARG_MAX} - 2k, or 128k, whichever is smaller, characters per
1999 command.  It uses as many lines and arguments as fit within that
2000 limit.  The following options modify those values.
2002 @table @code
2003 @item --no-run-if-empty
2004 @itemx -r
2005 If the standard input does not contain any nonblanks, do not run the
2006 command.  By default, the command is run once even if there is no
2007 input.
2009 @item --max-lines@r{[}=@var{max-lines}@r{]}
2010 @itemx -L@r{[}@var{max-lines}@r{]}
2011 @itemx -l@r{[}@var{max-lines}@r{]}
2012 Use at most @var{max-lines} nonblank input lines per command line;
2013 @var{max-lines} defaults to 1 if omitted.  Trailing blanks cause an
2014 input line to be logically continued on the next input line, for the
2015 purpose of counting the lines.  Implies @samp{-x}.  The preferred
2016 name for this option is @samp{-L} as this is specified by POSIX.  This
2017 option should be compatible with @samp{-I} but currently it is not.
2019 @item --max-args=@var{max-args}
2020 @itemx -n @var{max-args}
2021 Use at most @var{max-args} arguments per command line.  Fewer than
2022 @var{max-args} arguments will be used if the size (see the @samp{-s}
2023 option) is exceeded, unless the @samp{-x} option is given, in which
2024 case @code{xargs} will exit.
2026 @item --max-chars=@var{max-chars}
2027 @itemx -s @var{max-chars}
2028 Use at most @var{max-chars} characters per command line, including the
2029 command initial arguments and the terminating nulls at the ends of
2030 the argument strings.  If you specify a value for this option which is
2031 too large or small, a warning message is printed and the appropriate
2032 upper or lower limit is used instead.
2034 @item --max-procs=@var{max-procs}
2035 @itemx -P @var{max-procs}
2036 Run up to @var{max-procs} processes at a time; the default is 1.  If
2037 @var{max-procs} is 0, @code{xargs} will run as many processes as
2038 possible at a time.  Use the @samp{-n}, @samp{-s}, or @samp{-L} option
2039 with @samp{-P}; otherwise chances are that the command will be run
2040 only once.
2041 @end table
2043 @node Interspersing File Names
2044 @subsubsection Interspersing File Names
2046 @code{xargs} can insert the name of the file it is processing between
2047 arguments you give for the command.  Unless you also give options to
2048 limit the command size (@pxref{Limiting Command Size}), this mode of
2049 operation is equivalent to @samp{find -exec} (@pxref{Single File}).
2051 @table @code
2052 @item --replace@r{[}=@var{replace-str}@r{]}
2053 @itemx -I@r{[}@var{replace-str}@r{]}
2054 @itemx -i@r{[}@var{replace-str}@r{]}
2055 Replace occurrences of @var{replace-str} in the initial arguments with
2056 names read from the input.  Also, unquoted blanks do not
2057 terminate arguments; instead, the input is split at newlines only.  If
2058 @var{replace-str} is omitted, it defaults to @samp{@{@}} (like for
2059 @samp{find -exec}).  Implies @samp{-x} and @samp{-l 1}.  @samp{-i} is
2060 depreceated in favour of @samp{-I}. As an
2061 example, to sort each file in the @file{bills} directory, leaving the
2062 output in that file name with @file{.sorted} appended, you could do:
2064 @example
2065 find bills -type f | xargs -IXX sort -o XX.sorted XX
2066 @end example
2068 @noindent
2069 The equivalent command using @samp{find -execdir} is:
2071 @example
2072 find bills -type f -execdir sort -o '@{@}.sorted' '@{@}' ';'
2073 @end example
2074 @end table
2076 @node Querying
2077 @subsection Querying
2079 To ask the user whether to execute a command on a single file, you can
2080 use the @code{find} primary @samp{-okdir} instead of @samp{-execdir},
2081 and the @code{find} primary @samp{-ok} instead of @samp{-exec}:
2083 @deffn Action -okdir command ;
2084 Like @samp{-execdir} (@pxref{Single File}), but ask the user first (on
2085 the standard input); if the response does not start with @samp{y} or
2086 @samp{Y}, do not run the command, and return false.
2087 @end deffn
2089 @deffn Action -ok command ;
2090 This insecure variant of the @samp{-okdir} action is specified by
2091 POSIX.  The main difference is that the command is executed in the
2092 directory from which @code{find} was invoked, meaning that @samp{@{@}}
2093 is expanded to a relative path starting with the name of one of the
2094 starting directories, rather than just the basename of the matched
2095 file.
2096 @end deffn
2098 When processing multiple files with a single command, to query the
2099 user you give @code{xargs} the following option.  When using this
2100 option, you might find it useful to control the number of files
2101 processed per invocation of the command (@pxref{Limiting Command
2102 Size}).
2104 @table @code
2105 @item --interactive
2106 @itemx -p
2107 Prompt the user about whether to run each command line and read a line
2108 from the terminal.  Only run the command line if the response starts
2109 with @samp{y} or @samp{Y}.  Implies @samp{-t}.
2110 @end table
2112 @node Delete Files
2113 @section Delete Files
2115 @deffn Action -delete
2116 Delete files or directories; true if removal succeeded.  If the
2117 removal failed, an error message is issued.
2119 The use of the @samp{-delete} action on the command line automatically
2120 turns on the @samp{-depth} option (@pxref{find Expressions}).
2121 @end deffn
2123 @node Adding Tests
2124 @section Adding Tests
2126 You can test for file attributes that none of the @code{find} builtin
2127 tests check.  To do this, use @code{xargs} to run a program that
2128 filters a list of files printed by @code{find}.  If possible, use
2129 @code{find} builtin tests to pare down the list, so the program run by
2130 @code{xargs} has less work to do.  The tests builtin to @code{find}
2131 will likely run faster than tests that other programs perform.
2133 For reasons of efficiency it is often useful to limit the number of
2134 times an external program has to be run.  For this reason, it is often
2135 a good idea to implement ``extended'' tests by using @code{xargs}.
2137 For example, here is a way to print the names of all of the unstripped
2138 binaries in the @file{/usr/local} directory tree.  Builtin tests avoid
2139 running @code{file} on files that are not regular files or are not
2140 executable.
2142 @example
2143 find /usr/local -type f -perm /a=x | xargs file |
2144   grep 'not stripped' | cut -d: -f1
2145 @end example
2147 @noindent
2148 The @code{cut} program removes everything after the file name from the
2149 output of @code{file}.
2151 However, using @code{xargs} can present important security problems
2152 (@pxref{Security Considerations}).  These can be avoided by using
2153 @samp{-execdir}.  The @samp{-execdir} action is also a useful way of
2154 putting your own test in the middle of a set of other tests or actions
2155 for @code{find} (for example, you might want to use @samp{-prune}).
2157 @c Idea from Martin Weitzel.
2158 To place a special test somewhere in the middle of a @code{find}
2159 expression, you can use @samp{-execdir} (or, less securely,
2160 @samp{-exec}) to run a program that performs the test.  Because
2161 @samp{-execdir} evaluates to the exit status of the executed program,
2162 you can use a program (which can be a shell script) that tests for a
2163 special attribute and make it exit with a true (zero) or false
2164 (non-zero) status.  It is a good idea to place such a special test
2165 @emph{after} the builtin tests, because it starts a new process which
2166 could be avoided if a builtin test evaluates to false.
2168 Here is a shell script called @code{unstripped} that checks whether
2169 its argument is an unstripped binary file:
2171 @example
2172 #! /bin/sh
2173 file "$1" | grep -q "not stripped"
2174 @end example
2177 This script relies on the shell exiting with the status of
2178 the last command in the pipeline, in this case @code{grep}.  The
2179 @code{grep} command exits with a true status if it found any matches,
2180 false if not.  Here is an example of using the script (assuming it is
2181 in your search path).  It lists the stripped executables (and shell
2182 scripts) in the file @file{sbins} and the unstripped ones in
2183 @file{ubins}.
2185 @example
2186 find /usr/local -type f -perm /a=x \
2187   \( -execdir unstripped '@{@}' \; -fprint ubins -o -fprint sbins \)
2188 @end example
2191 @node Common Tasks, Databases, Actions, Top
2192 @chapter Common Tasks
2194 The sections that follow contain some extended examples that both give
2195 a good idea of the power of these programs, and show you how to solve
2196 common real-world problems.
2198 @menu
2199 * Viewing And Editing::
2200 * Archiving::
2201 * Cleaning Up::
2202 * Strange File Names::
2203 * Fixing Permissions::
2204 * Classifying Files::
2205 @end menu
2207 @node Viewing And Editing
2208 @section Viewing And Editing
2210 To view a list of files that meet certain criteria, simply run your
2211 file viewing program with the file names as arguments.  Shells
2212 substitute a command enclosed in backquotes with its output, so the
2213 whole command looks like this:
2215 @example
2216 less `find /usr/include -name '*.h' | xargs grep -l mode_t`
2217 @end example
2219 @noindent
2220 You can edit those files by giving an editor name instead of a file
2221 viewing program:
2223 @example
2224 emacs `find /usr/include -name '*.h' | xargs grep -l mode_t`
2225 @end example
2227 Because there is a limit to the length of any individual command line,
2228 there is a limit to the number of files that can be handled in this
2229 way.  We can get around this difficulty by using xargs like this:
2231 @example
2232 find /usr/include -name '*.h' | xargs grep -l mode_t > todo
2233 xargs --arg-file=todo emacs
2234 @end example
2236 Here, @code{xargs} will run @code{emacs} as many times as necessary to
2237 visit all of the files listed in the file @file{todo}.
2239 @node Archiving
2240 @section Archiving
2242 You can pass a list of files produced by @code{find} to a file
2243 archiving program.  GNU @code{tar} and @code{cpio} can both read lists
2244 of file names from the standard input---either delimited by nulls (the
2245 safe way) or by blanks (the lazy, risky default way).  To use
2246 null-delimited names, give them the @samp{--null} option.  You can
2247 store a file archive in a file, write it on a tape, or send it over a
2248 network to extract on another machine.
2250 One common use of @code{find} to archive files is to send a list of
2251 the files in a directory tree to @code{cpio}.  Use @samp{-depth} so if
2252 a directory does not have write permission for its owner, its contents
2253 can still be restored from the archive since the directory's
2254 permissions are restored after its contents.  Here is an example of
2255 doing this using @code{cpio}; you could use a more complex @code{find}
2256 expression to archive only certain files.
2258 @example
2259 find . -depth -print0 |
2260   cpio --create --null --format=crc --file=/dev/nrst0
2261 @end example
2263 You could restore that archive using this command:
2265 @example
2266 cpio --extract --null --make-dir --unconditional \
2267   --preserve --file=/dev/nrst0
2268 @end example
2270 Here are the commands to do the same things using @code{tar}:
2272 @example
2273 find . -depth -print0 |
2274   tar --create --null --files-from=- --file=/dev/nrst0
2276 tar --extract --null --preserve-perm --same-owner \
2277   --file=/dev/nrst0
2278 @end example
2280 @c Idea from Rick Sladkey.
2281 Here is an example of copying a directory from one machine to another:
2283 @example
2284 find . -depth -print0 | cpio -0o -Hnewc |
2285   rsh @var{other-machine} "cd `pwd` && cpio -i0dum"
2286 @end example
2288 @node Cleaning Up
2289 @section Cleaning Up
2291 @c Idea from Jim Meyering.
2292 This section gives examples of removing unwanted files in various
2293 situations.  Here is a command to remove the CVS backup files created
2294 when an update requires a merge:
2296 @example
2297 find . -name '.#*' -print0 | xargs -0r rm -f
2298 @end example
2300 The command above works, but the following is safer:
2302 @example
2303 find . -name '.#*' -depth -delete
2304 @end example
2306 @c Idea from Franc,ois Pinard.
2307 You can run this command to clean out your clutter in @file{/tmp}.
2308 You might place it in the file your shell runs when you log out
2309 (@file{.bash_logout}, @file{.logout}, or @file{.zlogout}, depending on
2310 which shell you use).
2312 @example
2313 find /tmp -depth -user "$LOGNAME" -type f -delete
2314 @end example
2316 If your @code{find} command removes directories, you may find that
2317 you get a spurious error message when @code{find} tries to recurse
2318 into a directory that has now been removed.  Using the @samp{-depth}
2319 option will normally resolve this problem.
2321 @c Idea from Noah Friedman.
2322 To remove old Emacs backup and auto-save files, you can use a command
2323 like the following.  It is especially important in this case to use
2324 null-terminated file names because Emacs packages like the VM mailer
2325 often create temporary file names with spaces in them, like
2326 @file{#reply to David J. MacKenzie<1>#}.
2328 @example
2329 find ~ \( -name '*~' -o -name '#*#' \) -print0 |
2330   xargs --no-run-if-empty --null rm -vf
2331 @end example
2333 Removing old files from @file{/tmp} is commonly done from @code{cron}:
2335 @c Idea from Kaveh Ghazi.
2336 @example
2337 find /tmp /var/tmp -not -type d -mtime +3 -delete
2338 find /tmp /var/tmp -depth -mindepth 1 -type d -empty -delete
2339 @end example
2341 The second @code{find} command above uses @samp{-depth} so it cleans
2342 out empty directories depth-first, hoping that the parents become
2343 empty and can be removed too.  It uses @samp{-mindepth} to avoid
2344 removing @file{/tmp} itself if it becomes totally empty.
2346 @node Strange File Names
2347 @section Strange File Names
2349 @c Idea from:
2350 @c From: tmatimar@isgtec.com (Ted Timar)
2351 @c Newsgroups: comp.unix.questions,comp.unix.shell,comp.answers,news.answers
2352 @c Subject: Unix - Frequently Asked Questions (2/7) [Frequent posting]
2353 @c Subject: How do I remove a file with funny characters in the filename ?
2354 @c Date: Thu Mar 18 17:16:55 EST 1993
2355 @code{find} can help you remove or rename a file with strange
2356 characters in its name.  People are sometimes stymied by files whose
2357 names contain characters such as spaces, tabs, control characters, or
2358 characters with the high bit set.  The simplest way to remove such
2359 files is:
2361 @example
2362 rm -i @var{some*pattern*that*matches*the*problem*file}
2363 @end example
2365 @code{rm} asks you whether to remove each file matching the given
2366 pattern.  If you are using an old shell, this approach might not work
2367 if the file name contains a character with the high bit set; the shell
2368 may strip it off.  A more reliable way is:
2370 @example
2371 find . -maxdepth 1 @var{tests} -okdir rm '@{@}' \;
2372 @end example
2374 @noindent
2375 where @var{tests} uniquely identify the file.  The @samp{-maxdepth 1}
2376 option prevents @code{find} from wasting time searching for the file
2377 in any subdirectories; if there are no subdirectories, you may omit
2378 it.  A good way to uniquely identify the problem file is to figure out
2379 its inode number; use
2381 @example
2382 ls -i
2383 @end example
2385 Suppose you have a file whose name contains control characters, and
2386 you have found that its inode number is 12345.  This command prompts
2387 you for whether to remove it:
2389 @example
2390 find . -maxdepth 1 -inum 12345 -okdir rm -f '@{@}' \;
2391 @end example
2393 If you don't want to be asked, perhaps because the file name may
2394 contain a strange character sequence that will mess up your screen
2395 when printed, then use @samp{-execdir} instead of @samp{-okdir}.
2397 If you want to rename the file instead, you can use @code{mv} instead
2398 of @code{rm}:
2400 @example
2401 find . -maxdepth 1 -inum 12345 -okdir mv '@{@}' @var{new-file-name} \;
2402 @end example
2404 @node Fixing Permissions
2405 @section Fixing Permissions
2407 Suppose you want to make sure that everyone can write to the
2408 directories in a certain directory tree.  Here is a way to find
2409 directories lacking either user or group write permission (or both),
2410 and fix their permissions:
2412 @example
2413 find . -type d -not -perm -ug=w | xargs chmod ug+w
2414 @end example
2416 @noindent
2417 You could also reverse the operations, if you want to make sure that
2418 directories do @emph{not} have world write permission.
2420 @node Classifying Files
2421 @section Classifying Files
2423 @c Idea from:
2424 @c From: martin@mwtech.UUCP (Martin Weitzel)
2425 @c Newsgroups: comp.unix.wizards,comp.unix.questions
2426 @c Subject: Advanced usage of 'find' (Re: Unix security automating script)
2427 @c Date: 22 Mar 90 15:05:19 GMT
2428 If you want to classify a set of files into several groups based on
2429 different criteria, you can use the comma operator to perform multiple
2430 independent tests on the files.  Here is an example:
2432 @example
2433 find / -type d \( -perm -o=w -fprint allwrite , \
2434   -perm -o=x -fprint allexec \)
2436 echo "Directories that can be written to by everyone:"
2437 cat allwrite
2438 echo ""
2439 echo "Directories with search permissions for everyone:"
2440 cat allexec
2441 @end example
2443 @code{find} has only to make one scan through the directory tree
2444 (which is one of the most time consuming parts of its work).
2446 @node Databases, File Permissions, Common Tasks, Top
2447 @chapter File Name Databases
2449 The file name databases used by @code{locate} contain lists of files
2450 that were in particular directory trees when the databases were last
2451 updated.  The file name of the default database is determined when
2452 @code{locate} and @code{updatedb} are configured and installed.  The
2453 frequency with which the databases are updated and the directories for
2454 which they contain entries depend on how often @code{updatedb} is run,
2455 and with which arguments.
2457 You can obtain some statistics about the databases by using
2458 @samp{locate --statistics}.
2460 @menu
2461 * Database Locations::
2462 * Database Formats::
2463 * Newline Handling::
2464 @end menu
2467 @node Database Locations
2468 @section Database Locations
2470 There can be multiple file name databases.  Users can select which
2471 databases @code{locate} searches using the @code{LOCATE_PATH}
2472 environment variable or a command line option.  The system
2473 administrator can choose the file name of the default database, the
2474 frequency with which the databases are updated, and the directories
2475 for which they contain entries.  File name databases are updated by
2476 running the @code{updatedb} program, typically nightly.
2478 In networked environments, it often makes sense to build a database at
2479 the root of each filesystem, containing the entries for that
2480 filesystem.  @code{updatedb} is then run for each filesystem on the
2481 fileserver where that filesystem is on a local disk, to prevent
2482 thrashing the network.
2484 @xref{Invoking updatedb},
2485 for the description of the options to @code{updatedb}, which specify
2486 which directories would each database contain entries for.
2489 @node Database Formats
2490 @section Database Formats
2492 The file name databases contain lists of files that were in particular
2493 directory trees when the databases were last updated.  The file name
2494 database format changed starting with GNU @code{locate} version 4.0 to
2495 allow machines with different byte orderings to share the databases.
2496 The new GNU @code{locate} can read both the old and new database
2497 formats.  However, old versions of @code{locate} and @code{find}
2498 produce incorrect results if given a new-format database.
2500 If you run @samp{locate --statistics}, the resulting summary indicates
2501 the type of each @code{locate} database.
2504 @menu
2505 * New Database Format::
2506 * Sample Database::
2507 * Old Database Format::
2508 @end menu
2510 @node New Database Format
2511 @subsection New Database Format
2513 @code{updatedb} runs a program called @code{frcode} to
2514 @dfn{front-compress} the list of file names, which reduces the
2515 database size by a factor of 4 to 5.  Front-compression (also known as
2516 incremental encoding) works as follows.
2518 The database entries are a sorted list (case-insensitively, for users'
2519 convenience).  Since the list is sorted, each entry is likely to share
2520 a prefix (initial string) with the previous entry.  Each database
2521 entry begins with an offset-differential count byte, which is the
2522 additional number of characters of prefix of the preceding entry to
2523 use beyond the number that the preceding entry is using of its
2524 predecessor.  (The counts can be negative.)  Following the count is a
2525 null-terminated ASCII remainder---the part of the name that follows
2526 the shared prefix.
2528 If the offset-differential count is larger than can be stored in a
2529 byte (+/-127), the byte has the value 0x80 and the count follows in a
2530 2-byte word, with the high byte first (network byte order).
2532 Every database begins with a dummy entry for a file called
2533 @file{LOCATE02}, which @code{locate} checks for to ensure that the
2534 database file has the correct format; it ignores the entry in doing
2535 the search.
2537 Databases cannot be concatenated together, even if the first (dummy)
2538 entry is trimmed from all but the first database.  This is because the
2539 offset-differential count in the first entry of the second and
2540 following databases will be wrong.
2542 In the output of @samp{locate --statistics}, the new database format
2543 is referred to as @samp{LOCATE02}.
2545 @node Sample Database
2546 @subsection Sample Database
2548 Sample input to @code{frcode}:
2549 @c with nulls changed to newlines:
2551 @example
2552 /usr/src
2553 /usr/src/cmd/aardvark.c
2554 /usr/src/cmd/armadillo.c
2555 /usr/tmp/zoo
2556 @end example
2558 Length of the longest prefix of the preceding entry to share:
2560 @example
2561 0 /usr/src
2562 8 /cmd/aardvark.c
2563 14 rmadillo.c
2564 5 tmp/zoo
2565 @end example
2567 Output from @code{frcode}, with trailing nulls changed to newlines
2568 and count bytes made printable:
2570 @example
2571 0 LOCATE02
2572 0 /usr/src
2573 8 /cmd/aardvark.c
2574 6 rmadillo.c
2575 -9 tmp/zoo
2576 @end example
2578 (6 = 14 - 8, and -9 = 5 - 14)
2580 @node Old Database Format
2581 @subsection Old Database Format
2583 The old database format is used by Unix @code{locate} and @code{find}
2584 programs and earlier releases of the GNU ones.  @code{updatedb}
2585 produces this format if given the @samp{--old-format} option.
2587 @code{updatedb} runs programs called @code{bigram} and @code{code} to
2588 produce old-format databases.  The old format differs from the new one
2589 in the following ways.  Instead of each entry starting with an
2590 offset-differential count byte and ending with a null, byte values
2591 from 0 through 28 indicate offset-differential counts from -14 through
2592 14.  The byte value indicating that a long offset-differential count
2593 follows is 0x1e (30), not 0x80.  The long counts are stored in host
2594 byte order, which is not necessarily network byte order, and host
2595 integer word size, which is usually 4 bytes.  They also represent a
2596 count 14 less than their value.  The database lines have no
2597 termination byte; the start of the next line is indicated by its first
2598 byte having a value <= 30.
2600 In addition, instead of starting with a dummy entry, the old database
2601 format starts with a 256 byte table containing the 128 most common
2602 bigrams in the file list.  A bigram is a pair of adjacent bytes.
2603 Bytes in the database that have the high bit set are indexes (with the
2604 high bit cleared) into the bigram table.  The bigram and
2605 offset-differential count coding makes these databases 20-25% smaller
2606 than the new format, but makes them not 8-bit clean.  Any byte in a
2607 file name that is in the ranges used for the special codes is replaced
2608 in the database by a question mark, which not coincidentally is the
2609 shell wildcard to match a single character.
2611 The old format therefore cannot faithfully store entries with
2612 non-ASCII characters. It therefore should not be used in
2613 internationalized environments.
2615 The output of @samp{locate --statistics} will give an incorrect count
2616 of the number of file names containing newlines or high-bit characters
2617 for old-format databases.
2619 @node Newline Handling
2620 @section Newline Handling
2622 Within the database, file names are terminated with a null character.
2623 This is the case for both the old and the new format.
2625 When the new database format is being used, the compression technique
2626 used to generate the database though relies on the ability to sort the
2627 list of files before they are presented to @code{frcode}.
2629 If the system's sort command allows its input list of files to be
2630 separated with null characters via the @samp{-z} option, this option
2631 is used and therefore @code{updatedb} and @code{locate} will both
2632 correctly handle file names containing newlines.  If the @code{sort}
2633 command lacks support for this, the list of files is delimited with
2634 the newline character, meaning that parts of file names containing
2635 newlines will be incorrectly sorted.  This can result in both
2636 incorrect matches and incorrect failures to match.
2638 On the other hand, if you are using the old database format, file
2639 names with embedded newlines are not correctly handled.  There is no
2640 technical limitation which enforces this, it's just that the
2641 @code{bigram} program has not been updated to support lists of file
2642 names separated by nulls.
2644 So, if you are using the new database format (this is the default) and
2645 your system uses GNU @code{sort}, newlines will be correctly handled
2646 at all times.  Otherwise, newlines may not be correctly handled.
2648 @node File Permissions, Reference, Databases, Top
2649 @chapter File Permissions
2651 @include perm.texi
2653 @node Reference, Worked Examples, File Permissions, Top
2654 @chapter Reference
2656 Below are summaries of the command line syntax for the programs
2657 discussed in this manual.
2659 @menu
2660 * Invoking find::
2661 * Invoking locate::
2662 * Invoking updatedb::
2663 * Invoking xargs::
2664 @end menu
2666 @node Invoking find, Invoking locate, , Reference
2667 @section Invoking @code{find}
2669 @example
2670 find @r{[-H] [-L] [-P]} @r{[}@var{file}@dots{}@r{]} @r{[}@var{expression}@r{]}
2671 @end example
2673 @code{find} searches the directory tree rooted at each file name
2674 @var{file} by evaluating the @var{expression} on each file it finds in
2675 the tree.
2677 The options @samp{-H}, @samp{-L} or @samp{-P} may be specified at the
2678 start of the command line (if none of these is specified, @samp{-P} is
2679 assumed).  The arguments after these are a list of files or
2680 directories that should be searched.
2682 This list of files to search is followed by a list of expressions
2683 describing the files we wish to search for.  The first part of the
2684 expression is recognised by the fact that it begins with @samp{-},
2685 @samp{(}, @samp{)}, @samp{,}, or @samp{!}.  Any arguments after it are
2686 the rest of the expression.  If no files are given, the current
2687 directory is used.  If no expression is given, the expression
2688 @samp{-print} is used.
2690 @code{find} exits with status zero if all files matched are processed
2691 successfully, greater than 0 if errors occur.
2693 Three options can precede the list of files.  They determine the
2694 way that symbolic links are handled.
2696 @table @code
2697 @item -P
2698 Never follow symbolic links (this is the default), except in the case
2699 of the @samp{-xtype} predicate.
2700 @item -L
2701 Always follow symbolic links, except in the case of the @samp{-xtype}
2702 predicate.
2703 @item -H
2704 Follow symbolic links specified in the list of files to search, or
2705 which are otherwise specified on the command line.
2706 @end table
2708 If @code{find} would follow a symbolic link, but cannot for any reason
2709 (for example, because it has insufficient permissions or the link is
2710 broken), it falls back on using the properties of the symbolic link
2711 itself.  @ref{Symbolic Links} for a more complete description of how
2712 symbolic links are handled.
2714 @xref{Primary Index}, for a summary of all of the tests, actions, and
2715 options that the expression can contain.  If the expression is
2716 missing, @samp{-print} is assumed.
2719 @code{find} also recognizes two options for administrative use:
2721 @table @code
2722 @item --help
2723 Print a summary of the command line usage and exit.
2724 @item --version
2725 Print the version number of @code{find} and exit.
2726 @end table
2729 @menu
2730 * Warning Messages::
2731 @end menu
2734 @node Warning Messages,,, Invoking find
2735 @subsection Warning Messages
2737 If there is an error on the @code{find} command line, an error message
2738 is normally issued.  However, there are some usages that are
2739 inadvisable but which @code{find} should still accept.  Under these
2740 circumstances, @code{find} may issue a warning message.  By default,
2741 warnings are enabled only if @code{find} is being run interactively
2742 (specifically, if the standard input is a terminal).  Warning messages
2743 can be controlled explicitly by the use of options on the command
2744 line:
2746 @table @code
2747 @item -warn
2748 Issue warning messages where appropriate.
2749 @item -nowarn
2750 Do not issue warning messages.
2751 @end table
2753 These options take effect at the point on the command line where they
2754 are specified.  Therefore if you specify @samp{-nowarn} at the end of
2755 the command line, you will not see warning messages for any problems
2756 occurring before that.  The warning messages affected by the above
2757 options are triggered by:
2759 @itemize @minus
2760 @item
2761 Use of the @samp{-d} option which is deprecated; please use
2762 @samp{-depth} instead, since the latter is POSIX-compliant.
2763 @item
2764 Use of the @samp{-ipath} option which is deprecated; please use
2765 @samp{-iwholename} instead.
2766 @item
2767 Specifying an option (for example @samp{-mindepth}) after a non-option
2768 (for example @samp{-type} or @samp{-print}) on the command line.
2769 @end itemize
2772 The default behaviour above is designed to work in that way so that
2773 existing shell scripts which use such constructs don't generate
2774 spurious errors, but people will be made aware of the problem.
2776 Some warning messages are issued for less common or more serious
2777 problems, and consequently cannot be turned off:
2779 @itemize @minus
2780 @item
2781 Use of an unrecognised backslash escape sequence with @samp{-fprintf}
2782 @item
2783 Use of an unrecognised formatting directive with @samp{-fprintf}
2784 @end itemize
2786 @node Invoking locate, Invoking updatedb, Invoking find, Reference
2787 @section Invoking @code{locate}
2789 @example
2790 locate @r{[}@var{option}@dots{}@r{]} @var{pattern}@dots{}
2791 @end example
2793 For each @var{pattern} given @code{locate} searches one or more file
2794 name databases returning each match of @var{pattern}.
2796 For each @var{pattern} given @code{locate} searches one or more file
2797 name databases returning each match of @var{pattern}.
2799 @table @code
2800 @item --all
2801 @itemx -A
2802 Print only names which match all non-option arguments, not those
2803 matching one or more non-option arguments.
2805 @item --basename
2806 @itemx -b
2807 The specified pattern is matched against just the last component of
2808 the name of a file in the @code{locate} database.  This last
2809 component is also called the ``base name''.  For example, the base
2810 name of @file{/tmp/mystuff/foo.old.c} is @file{foo.old.c}.  If the
2811 pattern contains metacharacters, it must match the base name exactly.
2812 If not, it must match part of the base name.
2814 @item --count
2815 @itemx -c
2816 Instead of printing the matched file names, just print the total
2817 number of matches found, unless @samp{--print} (@samp{-p}) is also
2818 present.
2821 @item --database=@var{path}
2822 @itemx -d @var{path}
2823 Instead of searching the default @code{locate} database, @code{locate} search the file
2824 name databases in @var{path}, which is a colon-separated list of
2825 database file names.  You can also use the environment variable
2826 @code{LOCATE_PATH} to set the list of database files to search.  The
2827 option overrides the environment variable if both are used.  Empty
2828 elements in @var{path} (that is, a leading or trailing colon, or two
2829 colons in a row) are taken to stand for the default database.
2830 A database can be supplied on stdin, using @samp{-} as an element
2831 of @samp{path}. If more than one element of @samp{path} is @samp{-},
2832 later instances are ignored (but a warning message is printed).
2834 @item --existing
2835 @itemx -e
2836 Only print out such names which currently exist (instead of such names
2837 which existed when the database was created).  Note that this may slow
2838 down the program a lot, if there are many matches in the database.
2839 The way in which broken symbolic links are treated is affected by the
2840 @samp{-L}, @samp{-P} and @samp{-H} options.
2842 @item --non-existing
2843 @itemx -E
2844 Only print out such names which currently do not exist (instead of
2845 such names which existed when the database was created).  Note that
2846 this may slow down the program a lot, if there are many matches in the
2847 database.  The way in which broken symbolic links are treated is
2848 affected by the @samp{-L}, @samp{-P} and @samp{-H} options.
2850 @item --follow
2851 @itemx -L
2852 If testing for the existence of files (with the @samp{-e} or @samp{-E}
2853 options), consider broken symbolic links to be non-existing.  This is
2854 the default behaviour.
2857 @item --nofollow
2858 @itemx -P
2859 @itemx -H
2860 If testing for the existence of files (with the @samp{-e} or @samp{-E}
2861 options), treat broken symbolic links as if they were existing files.
2862 The @samp{-H} form of this option is provided purely for similarity
2863 with @code{find}; the use of @samp{-P} is recommended over @samp{-H}.
2865 @item --ignore-case
2866 @itemx -i
2867 Ignore case distinctions in both the pattern and the file names.
2869 @item --limit=N
2870 @itemx -l N
2871 Limit the number of results printed to N.  When used with the
2872 @samp{--count} option, the value printed will never be larger than
2873 this limit.
2875 @item --mmap
2876 @itemx -m
2877 Accepted but does nothing.  The option is supported only to provide
2878 compatibility with BSD's @code{locate}.
2880 @item --null
2881 @itemx -0
2882 Results are separated with the ASCII NUL character rather than the
2883 newline character.  To get the full benefit of the use of this option,
2884 use the new @code{locate} database format (that is the default
2885 anyway).
2887 @item --print
2888 @itemx -p
2889 Print search results when they normally would not, because of the
2890 presence of @samp{--statistics} (@samp{-S}) or @samp{--count}
2891 (@samp{-c}).
2893 @item --wholename
2894 @itemx -w
2895 The specified pattern is matched against the whole name of the file in
2896 the @code{locate} database.  If the pattern contains metacharacters,
2897 it must match exactly.  If not, it must match part of the whole file
2898 name.  This is the default behaviour.
2900 @item --regex
2901 @itemx -r
2902 Instead of using substring or shell glob matching, the pattern
2903 specified on the command line is understood to be a regular
2904 expression.  GNU Emacs-style regular expressions are assumed unless
2905 the @samp{--regextype} option is also given.  File names from the
2906 @code{locate} database are matched using the specified regular
2907 expression.  If the @samp{-i} flag is also given, matching is
2908 case-insensitive.  Matches are performed against the whole path name,
2909 and so by default a pathname will be matched if any part of it matches
2910 the specified regular expression.  The regular expression may use
2911 @samp{^} or @samp{$} to anchor a match at the beginning or end of a
2912 pathname.
2914 @item --regextype
2915 This option changes the regular expression dialect used.  Dialects
2916 understood are described in @ref{Regular Expressions}.
2919 @item --stdio
2920 @itemx -s
2921 Accepted but does nothing.  The option is supported only to provide
2922 compatibility with BSD's @code{locate}.
2924 @item --statistics
2925 @itemx -S
2926 Print some summary information for each @code{locate} database.  No
2927 search is performed unless non-option arguments are given.
2929 @item --help
2930 Print a summary of the command line usage for @code{locate} and exit.
2932 @item --version
2933 Print the version number of @code{locate} and exit.
2934 @end table
2936 @node Invoking updatedb, Invoking xargs, Invoking locate, Reference
2937 @section Invoking @code{updatedb}
2939 @example
2940 updatedb @r{[}@var{option}@dots{}@r{]}
2941 @end example
2943 @code{updatedb} creates and updates the database of file names used by
2944 @code{locate}.  @code{updatedb} generates a list of files similar to
2945 the output of @code{find} and then uses utilities for optimizing the
2946 database for performance.  @code{updatedb} is often run periodically
2947 as a @code{cron} job and configured with environment variables or
2948 command options.  Typically, operating systems have a shell script
2949 that ``exports'' configurations for variable definitions and uses
2950 another schell script that ``sources'' the configuration file into the
2951 environment and then executes @code{updatedb} in the environment.
2953 @code{updatedb} creates and updates the database of file names used by
2954 @code{locate}.  @code{updatedb} generates a list of files similar to
2955 the output of @code{find} and then uses utilities for optimizing the
2956 database for performance.  @code{updatedb} is often run periodically
2957 as a @code{cron} job and configured with environment variables or
2958 command options.  Typically, operating systems have a shell script
2959 that ``exports'' configurations for variable definitions and uses
2960 another schell script that ``sources'' the configuration file into the
2961 environment and then executes @code{updatedb} in the environment.
2963 @table @code
2964 @item --findoptions='@var{OPTION}@dots{}'
2965 Global options to pass on to @code{find}.
2966 The environment variable @code{FINDOPTIONS} also sets this value.
2967 Default is none.
2969 @item --localpaths='@var{path}@dots{}'
2970 Non-network directories to put in the database.
2971 Default is @file{/}.
2973 @item --netpaths='@var{path}@dots{}'
2974 Network (NFS, AFS, RFS, etc.) directories to put in the database.
2975 The environment variable @code{NETPATHS} also sets this value.
2976 Default is none.
2978 @item --prunepaths='@var{path}@dots{}'
2979 Directories to omit from the database, which would otherwise be
2980 included.  The environment variable @code{PRUNEPATHS} also sets this
2981 value.  Default is @file{/tmp /usr/tmp /var/tmp /afs}.  The paths are
2982 used as regular expressions (with @code{find ... -regex}, so you need
2983 to specify these paths in the same way that @code{find} will encounter
2984 them.  This means for example that the paths must not include trailing
2985 slashes.
2987 @item --prunefs='@var{path}@dots{}'
2988 Filesystems to omit from the database, which would otherwise be
2989 included.  Note that files are pruned when a filesystem is reached;
2990 Any filesystem mounted under an undesired filesystem will be ignored.
2991 The environment variable @code{PRUNEFS} also sets this value.  Default
2992 is @file{nfs NFS proc}.
2994 @item --output=@var{dbfile}
2995 The database file to build.  Default is system-dependent, but
2996 typically @file{/usr/local/var/locatedb}.
2998 @item --localuser=@var{user}
2999 The user to search the non-network directories as, using @code{su}.
3000 Default is to search the non-network directories as the current user.
3001 You can also use the environment variable @code{LOCALUSER} to set this user.
3003 @item --netuser=@var{user}
3004 The user to search network directories as, using @code{su}.  Default
3005 @code{user} is @code{daemon}.  You can also use the environment variable
3006 @code{NETUSER} to set this user.
3008 @item --old-format
3009 Generate a @code{locate} database in the old format, for compatibility
3010 with versions of @code{locate} other than GNU @code{locate}.  Using
3011 this option means that @code{locate} will not be able to properly
3012 handle non-ASCII characters in file names (that is, file names
3013 containing characters which have the eighth bit set, such as many of
3014 the characters from the ISO-8859-1 character set).
3015 @item --help
3016 Print a summary of the command line usage and exit.
3017 @item --version
3018 Print the version number of @code{updatedb} and exit.
3019 @end table
3021 @node Invoking xargs, ,  Invoking updatedb, Reference
3022 @section Invoking @code{xargs}
3024 @example
3025 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
3026 @end example
3028 @code{xargs} exits with the following status:
3030 @table @asis
3031 @item 0
3032 if it succeeds
3033 @item 123
3034 if any invocation of the command exited with status 1-125
3035 @item 124
3036 if the command exited with status 255
3037 @item 125
3038 if the command is killed by a signal
3039 @item 126
3040 if the command cannot be run
3041 @item 127
3042 if the command is not found
3043 @item 1
3044 if some other error occurred.
3045 @end table
3047 @table @code
3048 @item --arg-file@r{=@var{inputfile}}
3049 @itemx -a o@r{@var{inputfile}}
3050 Read names from the file @var{inputfile} instead of standard input.
3052 @item --null
3053 @itemx -0
3054 Input file names are terminated by a null character instead of by
3055 whitespace, and any quotes and backslash characters are not considered special (every
3056 character is taken literally).  Disables the end of file string, which
3057 is treated like any other argument.
3059 @item --eof@r{[}=@var{eof-str}@r{]}
3060 @itemx -e@r{[}@var{eof-str}@r{]}
3061 Set the end of file string to @var{eof-str}.  If the end of file
3062 string occurs as a line of input, the rest of the input is ignored.
3063 If @var{eof-str} is omitted, there is no end of file string.  If this
3064 option is not given, the end of file string defaults to @samp{_}.
3066 @item --help
3067 Print a summary of the options to @code{xargs} and exit.
3069 @item --replace@r{[}=@var{replace-str}@r{]}
3070 @itemx -I@r{[}@var{replace-str}@r{]}
3071 @itemx -i@r{[}@var{replace-str}@r{]}
3072 Replace occurrences of @var{replace-str} in the initial arguments with
3073 names read from standard input.  Also, unquoted blanks do not
3074 terminate arguments; instead, the input is split at newlines only.
3075 If @var{replace-str} is omitted, it defaults to @samp{@{@}}
3076 (like for @samp{find -exec}).  Implies @samp{-x} and @samp{-l 1}.  @samp{-i} is
3077 depreceated in favour of @samp{-I}.
3079 @item --max-lines@r{[}=@var{max-lines}@r{]}
3080 @itemx -l@r{[}@var{max-lines}@r{]}
3081 Use at most @var{max-lines} non-blank input lines per command line;
3082 @var{max-lines} defaults to 1 if omitted.  Trailing blanks cause an
3083 input line to be logically continued on the next input line, for the
3084 purpose of counting the lines.  Implies @samp{-x}.
3086 @item --max-args=@var{max-args}
3087 @itemx -n @var{max-args}
3088 Use at most @var{max-args} arguments per command line.  Fewer than
3089 @var{max-args} arguments will be used if the size (see the @samp{-s}
3090 option) is exceeded, unless the @samp{-x} option is given, in which
3091 case @code{xargs} will exit.
3093 @item --interactive
3094 @itemx -p
3095 Prompt the user about whether to run each command line and read a line
3096 from the terminal.  Only run the command line if the response starts
3097 with @samp{y} or @samp{Y}.  Implies @samp{-t}.
3099 @item --no-run-if-empty
3100 @itemx -r
3101 If the standard input is completely empty, do not run the
3102 command.  By default, the command is run once even if there is no
3103 input.
3105 @item --max-chars=@var{max-chars}
3106 @itemx -s @var{max-chars}
3107 Use at most @var{max-chars} characters per command line, including the
3108 command, initial arguments and any terminating nulls at the ends of
3109 the argument strings.
3111 @item --verbose
3112 @itemx -t
3113 Print the command line on the standard error output before executing
3116 @item --version
3117 Print the version number of @code{xargs} and exit.
3119 @item --exit
3120 @itemx -x
3121 Exit if the size (see the @samp{-s} option) is exceeded.
3124 @item --max-procs=@var{max-procs}
3125 @itemx -P @var{max-procs}
3126 Run simultaneously up to @var{max-procs} processes at once; the default is 1.  If
3127 @var{max-procs} is 0, @code{xargs} will run as many processes as
3128 possible simultaneously.
3129 @end table
3131 @node Worked Examples, Security Considerations, Reference, Top
3132 @chapter Worked Examples
3134 The tools in the findutils package, and in particular @code{find},
3135 have a large number of options.  This means that quite often,
3136 there is more than one way to do things.  Some of the options
3137 and facilities only exist for compatibility with other tools, and
3138 findutils provides improved ways of doing things.
3140 This chapter describes a number of useful tasks that are
3141 commonly performed, and compares the different ways of achieving them.
3143 @section Deleting Files
3145 One of the most common tasks that @code{find} is used for is locating
3146 files that can be deleted.  This might include:
3148 @itemize
3149 @item 
3150 Files last modified more than 3 years ago which haven't been accessed
3151 for at least 2 years
3152 @item
3153 Files belonging to a certain user
3154 @item
3155 Temporary files which are no longer required
3156 @end itemize
3158 This example concentrates on the actual deletion task rather than on
3159 sophisticated ways of locatng the files that need to be deleted.
3160 We'll assume that the files we want to delete are old files underneath
3161 @file{/var/tmp/stuff}.
3163 @subsection The Traditional Way
3165 The traditional way to delete files in @file{var/tmp/stuff} that have
3166 not been modified in over 90 days would have been:
3168 @smallexample
3169 find /var/tmp/stuff -mtime +90 -exec /bin/rm @{@} \;
3170 @end smallexample
3172 The above command uses @samp{-exec} to run the @code{/bin/rm} command
3173 to remove each file.  This approach works and in fact would have
3174 worked in Version 7 Unix in 1979.  However, there are a number of
3175 problems with this approach.
3177 @subsection Deleting more than one file at a time
3179 The most obvious problem with the approach above is that it causes
3180 @code{find} to fork every time it finds a file that needs to delete,
3181 and the child process then has to use the @code{exec} system call to
3182 launch @code{/bin/rm}.   All this is quite inefficient.  If we are
3183 going to use @code{/bin/rm} to do this job, it is better to make it
3184 delete more than one file at a time.  
3186 The most obvious way of doing this is to use the shell's command
3187 expansion feature:
3189 @smallexample
3190 /bin/rm `find /var/tmp/stuff -mtime +90 -print`
3191 @end smallexample
3193 The above example uses the shell's back-tick operator to build a
3194 command line which deletes all the files at once.   There is also a
3195 more modern way of doing this:
3197 @smallexample
3198 /bin/rm $(find /var/tmp/stuff -mtime +90 -print)
3199 @end smallexample
3201 The advantage of @code{$(@dots{})} over @code{`@dots{}`} is that the first
3202 form nests without needing lots of backslash quoting.  So, the
3203 commands above are much more efficient than the first attempt.
3204 However, the first version is actually better than the second. 
3206 The reason for this is that the shell has a maximum command length
3207 which is imposed by the operating system (the actual limit varies
3208 between systems).  This means that while the second command will
3209 usually work, it will suddenly fail when there are lots of files to
3210 delete.  Since the task is to delete unwanted files, this is precisely
3211 the time we don't want things to go wrong.
3213 @subsection Making Use of xargs
3215 So, is there a way to be more efficient in the use of @code{fork()}
3216 and @code{exec()} without running up against this limit?
3217 Yes, we can be almost optimally efficient by making use
3218 of the @code{xargs} command.  The @code{xargs} command reads arguments
3219 from its standard input and builds them into command lines.  We can
3220 use it like this:
3222 @smallexample
3223 find /var/tmp/stuff -mtime +90 -print | xargs /bin/rm 
3224 @end smallexample
3226 For example if the files found by @code{find} are
3227 @file{/var/tmp/stuff/A}, 
3228 @file{/var/tmp/stuff/B} and 
3229 @file{/var/tmp/stuff/C} then @code{xargs} might issue the commands 
3231 @smallexample
3232 /bin/rm /var/tmp/stuff/A /var/tmp/stuff/B
3233 /bin/rm /var/tmp/stuff/C
3234 @end smallexample
3236 The above assumes that @code{xargs} has a very small maximum command
3237 line length.  The real limit is much larger but the idea is that
3238 @code{xargs} will run @code{/bin/rm} as many times as necessary to get
3239 the job done, given the limits on command line length.
3241 This usage of @code{xargs} is pretty efficient, and the @code{xargs}
3242 command is widely implemented (all modern versions of Unix offer it).
3243 So far then, the news is all good.  However, there is bad news too.
3245 @subsection Unusual characters in filenames
3247 Unix-like systems allow any characters to appear in file names with
3248 the exception of the ASCII NUL character and the backslash.
3249 Backslashes can occur in path names (as the directory separator) but
3250 not in the names of actual directory entries.  This means that the
3251 list of files that @code{xargs} reads could in fact contain white space
3252 characters --- spaces, tabs and newline characters.  Since by default,
3253 @code{xargs} assumes that the list of files it is reading uses white
3254 space as an argument separator, it cannot correctly handle the case
3255 where a filename actually includes white space.  This makes the
3256 default behaviour of @code{xargs} almost useless for handling
3257 arbitrary data.
3259 To solve this problem, GNU findutils introduced the @samp{-print0}
3260 action for @code{find}.  This uses the ASCII NUL character to separate
3261 the entries in the file list that it produces.  This is the ideal
3262 choice of separator since it is the only character that cannot appear
3263 within a path name.  The @samp{-0} option to @code{xargs} makes it
3264 assume that arguments are separated with ASCII NUL instead of white
3265 space.  It also turns off another misfeature in the default behaviour
3266 of @code{xargs}, which is that it pays attention to quote characters
3267 in its input.  Some versions of @code{xargs} also terminate when they
3268 see a lone @samp{_} in the input, but GNU @code{find} no longer does
3269 that (since it has become an optional behaviour in the Unix standard).
3271 So, putting @code{find -print0} together with @code{xargs -0} we get
3272 this command:
3274 @smallexample
3275 find /var/tmp/stuff -mtime +90 -print0 | xargs -0 /bin/rm 
3276 @end smallexample
3278 The result is an efficient way of proceeding that
3279 correctly handles all the possible characters that could appear in the
3280 list of files to delete.  This is good news.  However, there is, as
3281 I'm sure you're expecting, also more bad news.  The problem is that
3282 this is not a portable construct; although other versions of Unix
3283 (notable BSD-derived ones) support @samp{-print0}, it's not
3284 universal.  So, is there a more universal mechanism?
3286 @subsection Going back to -exec
3288 There is indeed a more universal mechanism, which is a slight
3289 modification to the @samp{-exec} action.  The normal @samp{-exec}
3290 action assumes that the command to run is terminated with a semicolon
3291 (the semicolon normally has to be quoted in order to protect it from
3292 interpretation as the shell command separator).  The SVR4 edition of
3293 Unix introduced a slight variation, which involves terminating the
3294 command with @samp{+} instead:
3296 @smallexample
3297 find /var/tmp/stuff -mtime +90 -exec /bin/rm @{@} \+
3298 @end smallexample
3300 The above use of @samp{-exec} causes @code{find} to build up a long
3301 command line and then issue it.  This can be less efficient than some
3302 uses of @code{xargs}; for example @code{xargs} allows new command
3303 lines to be built up while the previous command is still executing, and
3304 allows you to specify a number of commands to run in parallel.
3305 However, the @code{find @dots{} -exec @dots{} +} construct has the advantage
3306 of wide portability.  GNU findutils did not support @samp{-exec @dots{} +}
3307 until version 4.2.12; one of the reasons for this is that it already
3308 had the @samp{-print0} action in any case.
3311 @subsection A more secure version of -exec
3313 The command above seems to be efficient and portable.  However,
3314 within it lurks a security problem.  The problem is shared with
3315 all the commands we've tried in this worked example so far, too.  The
3316 security problem is a race condition; that is, if it is possible for
3317 somebody to manipulate the filesystem that you are searching while you
3318 are searching it, it is possible for them to persuade your @code{find}
3319 command to cause the deletion of a file that you can delete but they
3320 normally cannot.  
3322 The problem occurs because the @samp{-exec} action is defined by the
3323 @acronym{POSIX} standard to invoke its command with the same working directory
3324 as @code{find} had when it was started.  This means that the arguments
3325 which replace the @{@} include a relative path from @code{find}'s
3326 starting point down the file that needs to be deleted.  For example,
3328 @smallexample
3329 find /var/tmp/stuff -mtime +90 -exec /bin/rm @{@} \+
3330 @end smallexample
3332 might actually issue the command:
3334 @smallexample
3335 /bin/rm /var/tmp/stuff/A /var/tmp/stuff/B /var/tmp/stuff/passwd
3336 @end smallexample
3338 Notice the file @file{/var/tmp/stuff/passwd}.  Likewise, the command:
3340 @smallexample
3341 cd /var/tmp && find stuff -mtime +90 -exec /bin/rm @{@} \+
3342 @end smallexample
3344 might actually issue the command:
3346 @smallexample
3347 /bin/rm stuff/A stuff/B stuff/passwd
3348 @end smallexample
3350 If an attacker can rename @file{stuff} to something else (making use
3351 of their write permissions in @file{/var/tmp}) they can replace it
3352 with a symbolic link to @file{/etc}.  That means that the
3353 @code{/bin/rm} command will be invoked on @file{/etc/passwd}.  If you
3354 are running your @code{find} command as root, the attacker has just managed
3355 to delete a vital file.  All they needed to do to achieve this was
3356 replace a subdirectory with a symbolic link at the vital moment.
3358 There is however, a simple solution to the problem.  This is an action
3359 which works a lot like @code{-exec} but doesn't need to traverse a
3360 chain of directories to reach the file that it needs to work on.  This
3361 is the @samp{-execdir} action, which was introduced by the BSD family
3362 of operating systems.   The command,
3364 @smallexample
3365 find /var/tmp/stuff -mtime +90 -execdir /bin/rm @{@} \+
3366 @end smallexample
3368 might delete a set of files by performing these actions:
3370 @enumerate
3371 @item 
3372 Change directory to /var/tmp/stuff/foo
3373 @item 
3374 Invoke @code{/bin/rm ./file1 ./file2 ./file3}
3375 @item
3376 Change directory to /var/tmp/stuff/bar
3377 @item 
3378 Invoke @code{/bin/rm ./file99 ./file100 ./file101}
3379 @end enumerate
3381 This is a much more secure method.  We are no longer exposed to a race
3382 condition.  For many typical uses of @code{find}, this is the best
3383 strategy.   It's reasonably efficient, but the length of the command
3384 line is limited not just by the operating system limits, but also by
3385 how many files we actually need to delete from each directory.
3387 Is it possible to do any better?   In the case of general file
3388 processing, no.  However, in the specific case of deleting files it is
3389 indeed possible to do better.  
3391 @subsection Using the -delete action
3393 The most efficient and secure method of solving this problem is to use
3394 the @samp{-delete} action:
3396 @smallexample
3397 find /var/tmp/stuff -mtime +90 -delete
3398 @end smallexample
3400 This alternative is more efficient than any of the @samp{-exec} or
3401 @samp{-execdir} actions, since it entirely avoids the overhead of
3402 forking a new process and using @code{exec} to run @code{/bin/rm}.  It
3403 is also normally more efficient than @code{xargs} for the same
3404 reason.   The file deletion is performed from the directory containing
3405 the entry to be deleted, so the @samp{-delete} action has the same
3406 security advantages as the @samp{-execdir} action has.  
3408 The @samp{-delete} action was introduced by the BSD family of
3409 operating systems.
3411 @subsection Improving things still further
3413 Is it possible to improve things still further?  Not without either
3414 modifying the system library to the operating system or having more specific
3415 knowledge of the layout of the filesystem and disk I/O subsystem, or
3416 both.
3418 The @code{find} command traverses the filesystem, reading
3419 directories.  It then issues a separate system call for each file to
3420 be deleted.  If we could modify the operating system, there are
3421 potential gains that could be made:
3423 @itemize
3424 @item
3425 We could have a system call to which we pass more than one filename
3426 for deletion
3427 @item
3428 Alternatively, we could pass in a list of inode numbers (on GNU/Linux
3429 systems, @code{readdir()} also returns the inode number of each
3430 directory entry) to be deleted.
3431 @end itemize
3433 The above possibilities sound interesting, but from the kernel's point
3434 of view it is difficult to enforce standard Unix access controls for
3435 such processing by inode number.  Such a facility would probably
3436 need to be restricted to the superuser.
3438 Another way of improving performance would be to increase the
3439 parallelism of the process.  For example if the directory hierarchy we
3440 are searching is actually spread actross a number of disks, we might
3441 somehow be able to arrange for @code{find} to process each disk in
3442 parallel.  In practice GNU @code{find} doesn't have such an intimate
3443 understanding of the system's filesystem layout and disk I/O
3444 subsystem.
3446 However, since the system administrator can have such an understanding
3447 they can take advantage of it like so:
3449 @smallexample
3450 find /var/tmp/stuff1 -mtime +90 -delete &
3451 find /var/tmp/stuff2 -mtime +90 -delete &
3452 find /var/tmp/stuff3 -mtime +90 -delete &
3453 find /var/tmp/stuff4 -mtime +90 -delete &
3454 wait
3455 @end smallexample
3457 In the example above, four spearate instances of @code{find} are used
3458 to search four subdirectories in parallel.  The @code{wait} command
3459 simply waits for all of these to complete.  Whether this approach is
3460 more or less efficient than a single instance of @code{find} depends
3461 on a number of things:
3463 @itemize
3464 @item
3465 Are the directories being searched in parallel actually on separate
3466 disks?  If not, this parallel search might just result in a lot of
3467 disk head movement and so the speed might even be slower.
3468 @item
3469 Other activity - are other programs also doing things on those disks?
3470 @end itemize
3473 @subsection Conclusion
3475 The fastest and most secure way to delete files with the help of
3476 @code{find} is to use @samp{-delete}.
3478 In the case where we're doing things other than deleting files, the
3479 most secure alternative is @samp{-execdir @dots{} +}, but this is not as
3480 portable as the insecure action @samp{-exec @dots{} +}.
3482 The @samp{-delete} action is not completely portable, but the only
3483 other possiblility which is as secure (@samp{-execdir}) is no more
3484 portable.  The most efficient portable alternative is @samp{-exec
3485 @dots{}+}, but this is insecure and isn't supported by versions of GNU
3486 findutils prior to 4.2.12.
3488 @node Security Considerations, Error Messages, Worked Examples, Top
3489 @chapter Security Considerations
3491 Security considerations are important if you are using @code{find} or
3492 @code{xargs} to search for or process files that don't belong to you
3493 or which other people have control.  Security considerations
3494 relating to @code{locate} may also apply if you have files which you
3495 do not want others to see.
3497 The most severe forms of security problems affecting
3498 @code{find} and related programs are when third parties bring
3499 about a situation allowing them to do something
3500 they would normally not be able to accomplish.  This is called @emph{privilege
3501 elevation}.  This might include deleting files they would not normally
3502 be able to delete.  It is common for the operating system to periodically
3503 invoke @code{find} for self-maintenance purposes.  These invocations of
3504 @code{find} are particularly problematic from a security point of view
3505 as these are often invoked by the superuser and search the entire
3506 filesystem hierarchy.  Generally, the severity of any associated problem depends
3507 on what the system is going to do with the files found by @code{find}.
3509 @menu
3510 * Levels of Risk::      What is your level of exposure to security problems?
3511 * Security Considerations for find::  Security problems with find
3512 * Security Considerations for xargs:: Security problems with xargs
3513 * Security Considerations for locate:: Security problems with locate
3514 * Security Summary:: That was all very complex, what does it boil down to?
3515 @end menu
3518 @node Levels of Risk
3519 @section Levels of Risk
3521 There are some security risks inherent in the use of @code{find},
3522 @code{xargs} and (to a lesser extent) @code{locate}.  The severity of
3523 these risks depends on what sort of system you are using:
3525 @table @strong
3526 @item High risk
3527 Multi-user systems where you do not control (or trust) the other
3528 users, and on which you execute @code{find}, including areas where
3529 those other users can manipulate the filesystem (for example beneath
3530 @file{/home} or @file{/tmp}).
3532 @item Medium Risk
3533 Systems where the actions of other users can create file names chosen
3534 by them, but to which they don't have access while @code{find} is
3535 being run.  This access might include leaving programs running (shell
3536 background jobs, @code{at} or @code{cron} tasks, for example).  On
3537 these sorts of systems, carefully written commands (avoiding use of
3538 @samp{-print} for example) should not expose you to a high degree of
3539 risk.  Most systems fall into this category.
3541 @item Low Risk
3542 Systems to which untrusted parties do not have access, cannot create
3543 file names of their own choice (even remotely) and which contain no
3544 security flaws which might enable an untrusted third party to gain
3545 access.  Most systems do not fall into this category because there are
3546 many ways in which external parties can affect the names of files that
3547 are created on your system.  The system on which I am writing this for
3548 example automatically downloads software updates from the Internet;
3549 the names of the files in which these updates exist are chosen by
3550 third parties@footnote{Of course, I trust these parties to a large
3551 extent anyway, because I install software provided by them; I choose
3552 to trust them in this way, and that's a deliberate choice}.
3553 @end table
3555 In the discussion above, ``risk'' denotes the likelihood that someone
3556 can cause @code{find}, @code{xargs}, @code{locate} or some other
3557 program which is controlled by them to do something you did not
3558 intend.  The levels of risk suggested do not take any account of the
3559 consequences of this sort of event.  That is, if you operate a ``low
3560 risk'' type system, but the consequences of a security problem are
3561 disastrous, then you should still give serious thought to all the
3562 possible security problems, many of which of course will not be
3563 discussed here -- this section of the manual is intended to be
3564 informative but not comprehensive or exhaustive.
3566 If you are responsible for the operation of a system where the
3567 consequences of a security problem could be very important, you should
3568 do two things:-
3570 @enumerate
3571 @item Define a security policy which defines who is allowed to do what
3572 on your system.
3573 @item Seek competent advice on how to enforce your policy, detect
3574 breaches of that policy, and take account of any potential problems
3575 that might fall outside the scope of your policy.
3576 @end enumerate
3579 @node Security Considerations for find
3580 @section Security Considerations for @code{find}
3583 Some of the actions @code{find} might take have a direct effect;
3584 these include @code{-exec} and @code{-delete}.  However, it is also
3585 common to use @code{-print} explicitly or implicitly, and so if
3586 @code{find} produces the wrong list of file names, that can also be a
3587 security problem; consider the case for example where @code{find} is
3588 producing a list of files to be deleted.
3590 We normally assume that the @code{find} command line expresses the
3591 file selection criteria and actions that the user had in mind -- that
3592 is, the command line is ``trusted'' data.
3594 From a security analysis point of view, the output of @code{find}
3595 should be correct; that is, the output should contain only the names
3596 of those files which meet the user's criteria specified on the command
3597 line.  This applies for the @code{-exec} and @code{-delete} actions;
3598 one can consider these to be part of the output.
3600 On the other hand, the contents of the filesystem can be manipulated
3601 by other people, and hence we regard this as ``untrusted'' data.  This
3602 implies that the @code{find} command line is a filter which converts
3603 the untrusted contents of the filesystem into a correct list of output
3604 files.
3606 The filesystem will in general change while @code{find} is searching
3607 it; in fact, most of the potential security problems with @code{find}
3608 relate to this issue in some way.
3610 @dfn{Race conditions} are a general class of security problem where the
3611 relative ordering of actions taken by @code{find} (for example) and
3612 something else are critically important in getting the correct and expected result@footnote{This is more or less the
3613 definition of the term ``race condition''} .
3615 For @code{find}, an attacker might move or rename files or directories in
3616 the hope that an action might be taken against a file which was not
3617 normally intended to be affected.  Alternatively, this sort of attack
3618 might be intended to persuade @code{find} to search part of the
3619 filesystem which would not normally be included in the search
3620 (defeating the @code{-prune} action for example).
3622 @menu
3623 * Changing the Current Working Directory::
3624 * Race Conditions with -exec::
3625 * Race Conditions with -print and -print0::
3626 @end menu
3629 @node Changing the Current Working Directory
3630 @subsection Changing the Current Working Directory
3632 As @code{find} searches the filesystem, it finds subdirectories and
3633 then searches within them by changing its working directory.  First,
3634 @code{find} reaches and recognizes a subdirectory.  It then decides if that
3635 subdirectory meets the criteria for being searched; that is, any
3636 @samp{-xdev} or @samp{-prune} expressions are taken into account.  The
3637 @code{find} program will then change working directory and proceed to
3638 search the directory.
3640 A race condition attack might take the form that once the checks
3641 relevant to @samp{-xdev} and @samp{-prune} have been done, an attacker
3642 might rename the directory that was being considered, and put in its
3643 place a symbolic link that actually points somewhere else.
3645 The idea behind this attack is to fool @code{find} into going into the
3646 wrong directory.  This would leave @code{find} with a working
3647 directory chosen by an attacker, bypassing any protection apparently
3648 provided by @samp{-xdev} and @samp{-prune}, and any protection
3649 provided by being able to @emph{not} list particular directories on
3650 the @code{find} command line.  This form of attack is particularly
3651 problematic if the attacker can predict when the @code{find} command
3652 will be run, as is the case with @code{cron} tasks for example.
3654 GNU @code{find} has specific safeguards to prevent this general class
3655 of problem.  The exact form of these safeguards depends on the
3656 properties of your system.
3658 @menu
3659 * O_NOFOLLOW::                     Safely changing directory using fchdir().
3660 * Systems without O_NOFOLLOW::     Checking for symbolic links after chdir().
3661 * Working with automounters::      These can look like race condition exploits
3662 * Problems with dead NFS servers:: If you don't have O_NOFOLLOW, this is a problem.
3663 @end menu
3665 @node O_NOFOLLOW
3666 @subsubsection O_NOFOLLOW
3668 If your system supports the O_NOFOLLOW flag @footnote{GNU/Linux
3669 (kernel version 2.1.126 and later) and FreeBSD (3.0-CURRENT and later)
3670 support this} to the @code{open(2)} system call, @code{find} uses it
3671 when safely changing directory.  The target directory is first opened
3672 and then @code{find} changes working directory with the
3673 @code{fchdir()} system call.  This ensures that symbolic links are not
3674 followed, preventing the sort of race condition attack in which use
3675 is made of symbolic links.
3677 If for any reason this approach does not work, @code{find} will fall
3678 back on the method which is normally used if O_NOFOLLOW is not
3679 supported.
3681 You can tell if your system supports O_NOFOLLOW by running
3683 @example
3684 find --version
3685 @end example
3687 This will tell you the version number and which features are enabled.
3688 For example, if I run this on my system now, this gives:
3689 @example
3690 GNU find version 4.2.18-CVS
3691 Features enabled: D_TYPE O_NOFOLLOW(enabled)
3692 @end example
3694 Here, you can see that I am running a version of @code{find} which was
3695 built from the development (CVS) code prior to the release of
3696 findutils-4.2.18, and that the D_TYPE and O_NOFOLLOW features are
3697 present.  O_NOFOLLOW is qualified with ``enabled''.  This simply means
3698 that the current system seems to support O_NOFOLLOW.  This check is
3699 needed because it is possible to build @code{find} on a system that
3700 defines O_NOFOLLOW and then run it on a system that ignores the
3701 O_NOFOLLOW flag.  We try to detect such cases at startup by checking
3702 the operating system and version number; when this happens you will
3703 see ``O_NOFOLLOW(disabled)'' instead.
3705 @node Systems without O_NOFOLLOW
3706 @subsubsection Systems without O_NOFOLLOW
3708 The strategy for preventing this type of problem on systems that lack
3709 support for the O_NOFOLLOW flag is more complex.  Each time
3710 @code{find} changes directory, it examines the directory it is about
3711 to move to, issues the @code{chdir()} system call, and then checks
3712 that it has ended up in the subdirectory it expected.  If not, an
3713 error message is issued and @code{find} exits immediately.  This
3714 method prevents filesystem manipulation attacks from persuading
3715 @code{find} to search parts of the filesystem it did not intend.
3716 However, we have to take special steps in order to avoid mistakenly
3717 concluding that there is a problem with any ``automount'' mount
3718 points.
3720 @node Working with automounters
3721 @subsubsection Working with automounters
3723 Where a filesystem ``automounter'' is in use it can be the case that the use of the
3724 @code{chdir()} system call can itself cause a new filesystem to be
3725 mounted at that point.  On systems that do not support O_NOFOLLOW,
3726 this will cause @code{find}'s security check to fail.
3728 However, this does not normally represent a security problem, since
3729 the automounter configuration is normally set up by the system
3730 administrator.  Therefore, if the @code{chdir()} sanity check fails,
3731 @code{find} will check to see if a new filesystem has been mounted at
3732 the current directory; if so, @code{find} will issue a warning message
3733 and continue.
3735 To make this solution work, @code{find} reads the list of mounted
3736 filesystems at startup, and again when the sanity check fails.  It
3737 compares the two lists to find out if the directory it has moved into
3738 has just been mounted.
3740 @node Problems with dead NFS servers
3741 @subsubsection Problems with dead NFS servers
3743 Examining every mount point on the system has a downside too.  In
3744 general, @code{find} will be used to search just part of the
3745 filesystem.  However, @code{find} examines every mount point.  If the
3746 system has a filesystem mounted on an unresponsive NFS server,
3747 @code{find} will hang, waiting for the NFS server to respond.  Worse,
3748 it does this even if the affected mount point is not within the
3749 directory tree that @code{find} would have searched anyway.
3751 This is very unfortunate.  However, this problem only affects systems
3752 that have no support for O_NOFOLLOW.  As far as I can tell, it is not
3753 possible on such systems to fix all three problems (the race
3754 condition, the false-alarm at automount mount points, and the hang at
3755 startup if there is a dead NFS server) at once.  If you have some
3756 ideas about how @code{find} could do this better, please send email to
3757 the @email{bug-findutils@@gnu.org} mailing list.
3759 @node Race Conditions with -exec
3760 @subsection Race Conditions with -exec
3762 The @samp{-exec} action causes another program to be run.  It passes
3763 to the program the name of the file which is being considered at the
3764 time.  The invoked program will typically then perform some action
3765 on that file.  Once again, there is a race condition which can be
3766 exploited here.  We shall take as a specific example the command
3768 @example
3769 find /tmp -path /tmp/umsp/passwd -exec /bin/rm
3770 @end example
3772 In this simple example, we are identifying just one file to be deleted
3773 and invoking @code{/bin/rm} to delete it.  A problem exists because
3774 there is a time gap between the point where @code{find} decides that
3775 it needs to process the @samp{-exec} action and the point where the
3776 @code{/bin/rm} command actually issues the @code{unlink()} system
3777 call to delete the file from the filesystem.  Within this time period, an attacker can rename the
3778 @file{/tmp/umsp} directory, replacing it with a symbolic link to
3779 @file{/etc}.  There is no way for @code{/bin/rm} to determine that it
3780 is working on the same file that @code{find} had in mind.  Once the
3781 symbolic link is in place, the attacker has persuaded @code{find} to
3782 cause the deletion of the @file{/etc/passwd} file, which is not the
3783 effect intended by the command which was actually invoked.
3785 One possible defence against this type of attack is to modify the
3786 behaviour of @samp{-exec} so that the @code{/bin/rm} command is run
3787 with the argument @file{./passwd} and a suitable choice of working
3788 directory.  This would allow the normal sanity check that @code{find}
3789 performs to protect against this form of attack too.  Unfortunately,
3790 this strategy cannot be used as the POSIX standard specifies that the
3791 current working directory for commands invoked with @samp{-exec} must
3792 be the same as the current working directory from which @code{find}
3793 was invoked.  This means that the @samp{-exec} action is inherently
3794 insecure and can't be fixed.
3796 GNU @code{find} implements a more secure variant of the @samp{-exec}
3797 action, @samp{-execdir}.  The @samp{-execdir} action
3798 ensures that it is not necessary to dereference subdirectories to
3799 process target files.  The current directory used to invoke programs
3800 is the same as the directory in which the file to be processed exists
3801 (@file{/tmp/umsp} in our example, and only the basename of the file to
3802 be processed is passed to the invoked command, with a @samp{./}
3803 prepended (giving @file{./passwd} in our example).
3805 The @samp{-execdir} action refuses to do anything if the current
3806 directory is included in the @var{$PATH} environment variable.  This
3807 is necessary because @samp{-execdir} runs programs in the same
3808 directory in which it finds files -- in general, such a directory
3809 might be writable by untrusted users.  For similar reasons,
3810 @samp{-execdir} does not allow @samp{@{@}} to appear in the name of
3811 the command to be run.
3813 @node Race Conditions with -print and -print0
3814 @subsection Race Conditions with -print and -print0
3816 The @samp{-print} and @samp{-print0} actions can be used to produce a
3817 list of files matching some criteria, which can then be used with some
3818 other command, perhaps with @code{xargs}.  Unfortunately, this means
3819 that there is an unavoidable time gap between @code{find} deciding
3820 that one or more files meet its criteria and the relevant command
3821 being executed.  For this reason, the @samp{-print} and @samp{-print0}
3822 actions are just as insecure as @samp{-exec}.
3824 In fact, since the construction
3826 @example
3827 find @dots{}  -print | xargs @enddots{}
3828 @end example
3830 does not cope correctly with newlines or other ``white space'' in
3831 file names, and copes poorly with file names containing quotes, the
3832 @samp{-print} action is less secure even than @samp{-print0}.
3835 @comment  node-name,  next,  previous,  up
3836 @comment @node Security Considerations for xargs
3837 @node Security Considerations for xargs
3838 @section Security Considerations for @code{xargs}
3840 The description of the race conditions affecting the @samp{-print}
3841 action of @code{find} shows that @code{xargs} cannot be secure if it
3842 is possible for an attacker to modify a filesystem after @code{find}
3843 has started but before @code{xargs} has completed all its actions.
3845 However, there are other security issues that exist even if it is not
3846 possible for an attacker to have access to the filesystem in real
3847 time.  Firstly, if it is possible for an attacker to create files with
3848 names of their choice on the filesystem, then @code{xargs} is
3849 insecure unless the @samp{-0} option is used.  If a file with the name
3850 @file{/home/someuser/foo/bar\n/etc/passwd} exists (assume that
3851 @samp{\n} stands for a newline character), then @code{find @dots{} -print}
3852 can be persuaded to print three separate lines:
3854 @example
3855 /home/someuser/foo/bar
3857 /etc/passwd
3858 @end example
3860 If it finds a blank line in the input, @code{xargs} will ignore it.
3861 Therefore, if some action is to be taken on the basis of this list of
3862 files, the @file{/etc/passwd} file would be included even if this was
3863 not the intent of the person running find.  There are circumstances in
3864 which an attacker can use this to their advantage.  The same
3865 consideration applies to file names containing ordinary spaces rather
3866 than newlines, except that of course the list of file names will no
3867 longer contain an ``extra'' newline.
3869 This problem is an unavoidable consequence of the default behaviour of
3870 the @code{xargs} command, which is specified by the POSIX standard.
3871 The only ways to avoid this problem are either to avoid all use of
3872 @code{xargs} in favour for example of @samp{find -exec} or (where
3873 available) @samp{find -execdir}, or to use the @samp{-0} option, which
3874 ensures that @code{xargs} considers file names to be separated by
3875 ASCII NUL characters rather than whitespace.  However, useful as this
3876 option is, the POSIX standard does not make it mandatory.
3878 @comment  node-name,  next,  previous,  up
3879 @node Security Considerations for locate
3880 @section Security Considerations for @code{locate}
3882 It is fairly unusual for the output of @code{locate} to be fed into
3883 another command.  However, if this were to be done, this would raise
3884 the same set of security issues as the use of @samp{find @dots{} -print}.
3885 Although the problems relating to whitespace in file names can be
3886 resolved by using @code{locate}'s @samp{-0} option, this still leaves
3887 the race condition problems associated with @samp{find @dots{} -print0}.
3888 There is no way to avoid these problems in the case of @code{locate}.
3890 @node Security Summary
3891 @section Summary
3893 Where untrusted parties can create files on the system, or affect the
3894 names of files that are created, all uses for @code{find},
3895 @code{locate} and @code{xargs} have known security problems except the
3896 following:
3898 @table @asis
3899 @item Informational use only
3900 Uses where the programs are used to prepare lists of file names upon
3901 which no further action will ever be taken.
3903 @item @samp{-delete}
3904 Use of the @samp{-delete} action with @code{find} to delete files
3905 which meet specified criteria
3907 @item @samp{-execdir}
3908 Use of the @samp{-execdir} action with @code{find} where the
3909 @env{PATH} environment variable contains directories which contain
3910 only trusted programs.
3911 @end table
3913 @comment  node-name,  next,  previous,  up
3914 @node Error Messages, Primary Index, Security Considerations, Top
3915 @chapter Error Messages
3917 This section describes some of the error messages sometimes made by
3918 @code{find}, @code{xargs}, or @code{locate}, explains them and in some
3919 cases provides advice as to what you should do about this.
3921 This manual is written in English.  The GNU findutils software
3922 features translations of error messages for many languages.  For this
3923 reason the error messages produced by
3924 the programs are made to be as self-explanatory as possible.  This approach avoids leaving people to
3925 figure out which test an English-language error message
3926 corresponds to. Error messages which are self-explanatory
3927 will not normally be mentioned in this document.  For
3928 those messages mentioned in this document, only the
3929 English-language version of the message will be listed.
3931 @menu
3932 * Error Messages From find::
3933 * Error Messages From xargs::
3934 * Error Messages From locate::
3935 * Error Messages From updatedb::
3936 @end menu
3938 @node Error Messages From find, Error Messages From xargs, , Error Messages
3939 @section Error Messages From @code{find}
3941 @table @samp
3942 @item invalid predicate `-foo'
3943 This means that the @code{find} command line included something that
3944 started with a dash or other special character.  The @code{find}
3945 program tried to interpret this as a test, action or option, but
3946 didn't recognise it.  If it was intended to be a test, check what was
3947 specified against the documentation.  If, on the other hand, the
3948 string is the name of a file which has been expanded from a wildcard
3949 (for example because you have a @samp{*} on the command line),
3950 consider using @samp{./*} or just @samp{.} instead.
3952 @item unexpected extra predicate
3953 This usually happens if you have an extra bracket on the command line
3954 (for example @samp{find . -print \)}).
3956 @item Warning: filesystem /path/foo has recently been mounted
3957 @itemx Warning: filesystem /path/foo has recently been unmounted
3958 These messages might appear when @code{find} moves into a directory
3959 and finds that the device number and inode are different to what it
3960 expected them to be.  If the directory @code{find} has moved into is
3961 on an network filesystem (NFS), it will not issue this message, because
3962 @code{automount} frequently mounts new filesystems on directories as
3963 you move into them (that is how it knows you want to use the
3964 filesystem).  So, if you do see this message, be wary ---
3965 @code{automount} may not have been responsible.  Consider the
3966 possibility that someone else is manipulating the filesystem while
3967 @code{find} is running.  Some people might do this in order to mislead
3968 @code{find} or persuade it to look at one set of files when it thought
3969 it was looking at another set.
3971 @item /path/foo changed during execution of find (old device number 12345, new device number 6789, filesystem type is <whatever>) [ref XXX]
3972 This message is issued when @code{find} moves into a directory and ends up
3973 somewhere it didn't expect to be.  This happens in one of two
3974 circumstances.  Firstly, this happens when @code{automount} intervenes
3975 on a system where @code{find} doesn't know how to determine what
3976 the current set of mounted filesystems is.
3978 Secondly, this can happen when the device number of a directory
3979 appears to change during a change of current directory, but
3980 @code{find} is moving up the filesystem hierarchy rather than down into it.
3981 In order to prevent @code{find} wandering off into some unexpected
3982 part of the filesystem, we stop it at this point.
3984 @item Don't know how to use getmntent() to read `/etc/mtab'.  This is a bug.
3985 This message is issued when a problem similar to the above occurs on a
3986 system where @code{find} doesn't know how to figure out the current
3987 list of mount points.  Ask for help on @email{bug-findutils@@gnu.org}.
3989 @item /path/foo/bar changed during execution of find (old inode number 12345, new inode number 67893, filesystem type is <whatever>) [ref XXX]"),
3990 This message is issued when @code{find} moves into a directory and
3991 discovers that the inode number of that directory
3992 is different from the inode number that it obtained when it examined the
3993 directory previously.  This usually means that while
3994 @code{find} was deep in a directory hierarchy doing a
3995 time consuming operation, somebody has moved one of the parent directories to
3996 another location in the same filesystem.  This may or may not have been done
3997 maliciously.  In any case, @code{find} stops at this point
3998 to avoid traversing parts of the filesystem that it wasn't
3999 intended.  You can use @code{ls -li} or @code{find /path -inum
4000 12345 -o -inum 67893} to find out more about what has happened.
4002 @item sanity check of the fnmatch() library function failed.
4003 Please submit a bug report.  You may well be asked questions about
4004 your system, and if you compiled the @code{findutils} code yourself,
4005 you should keep your copy of the build tree around.  The likely
4006 explanation is that your system has a buggy implementation of
4007 @code{fnmatch} that looks enough like the GNU version to fool
4008 @code{configure}, but which doesn't work properly.
4010 @item cannot fork
4011 This normally happens if you use the @code{-exec} action or
4012 something similar (@code{-ok} and so forth) but the system has run out
4013 of free process slots.  This is either because the system is very busy
4014 and the system has reached its maximum process limit, or because you
4015 have a resource limit in place and you've reached it.  Check the
4016 system for runaway processes (with @code{ps}, if possible).  Some process
4017 slots are normally reserved for use by @samp{root}.
4019 @item some-program terminated by signal 99
4020 Some program which was launched with @code{-exec} or similar was killed
4021 with a fatal signal.  This is just an advisory message.
4022 @end table
4025 @node Error Messages From xargs, Error Messages From locate, Error Messages From find, Error Messages
4026 @section Error Messages From xargs
4028 @table @samp
4029 @item environment is too large for exec
4030 This message means that you have so many environment variables set (or
4031 such large values for them) that there is no room within the
4032 system-imposed limits on program command line argument length to
4033 invoke any program.  This is an unlikely situation and is more likely
4034 result of an attempt to test the limits of @code{xargs}, or break it.
4035 Please try unsetting some environment variables, or exiting the
4036 current shell.
4038 @item can not fit single argument within argument list size limit
4039 You are using the @samp{-I} option and @code{xargs} doesn't have
4040 enough space to build a command line because it has read a really
4041 large item and it doesn't fit.  You can probably work around this
4042 problem with the @samp{-s} option, but the default size is pretty
4043 large.  This is a rare situation and is more likely an attempt to test
4044 the limits of @code{xargs}, or break it.  Otherwise, you will need to
4045 try to shorten the problematic argument or not use @code{xargs}.
4047 @item cannot fork
4048 See the description of the similar message for @code{find}.
4050 @item <program>: exited with status 255; aborting
4051 When a command run by @code{xargs} exits with status 255, @code{xargs}
4052 is supposed to stop.  If this is not what you intended, wrap the
4053 program you are trying to invoke in a shell script which doesn't
4054 return status 255.
4056 @item <program>: terminated by signal 99
4057 See the description of the similar message for @code{find}.
4058 @end table
4060 @node Error Messages From locate, Error Messages From updatedb, Error Messages From xargs, Error Messages
4061 @section Error Messages From @code{locate}
4063 @table @samp
4064 @item warning: database `/usr/local/var/locatedb' is more than 8 days old
4065 The @code{locate} program relies on a database which is periodically
4066 built by the @code{updatedb} program.  That hasn't happened in a long
4067 time.  To fix this problem, run @code{updatedb} manually.  This can
4068 often happen on systems that are generally not left on, so the
4069 periodic ``cron'' task which normally does this doesn't get a chance
4070 to run.
4072 @item locate database `/usr/local/var/locatedb' is corrupt or invalid
4073 This should not happen.  Re-run @code{updatedb}.  If that works, but
4074 @code{locate} still produces this error, run @code{locate --version}
4075 and @code{updatedb --version}.  These should produce the same output.
4076 If not, you are using a mixed toolset; check your @samp{$PATH}
4077 environment variable and your shell aliases (if you have any).  If
4078 both programs claim to be GNU versions, this is a bug; all versions of
4079 these programs should interoperate without problem.  Ask for help on
4080 @email{bug-findutils@@gnu.org}.
4081 @end table
4084 @node Error Messages From updatedb, , Error Messages From locate, Error Messages
4085 @section Error Messages From updatedb
4087 The @code{updatedb} program (and the programs it invokes) do issue
4088 error messages, but none seem to be candidates for guidance.  If
4089 you are having a problem understanding one of these, ask for help on
4090 @email{bug-findutils@@gnu.org}.
4092 @node Primary Index, , Error Messages, Top
4093 @unnumbered @code{find} Primary Index
4095 This is a list of all of the primaries (tests, actions, and options)
4096 that make up @code{find} expressions for selecting files.  @xref{find
4097 Expressions}, for more information on expressions.
4099 @printindex fn
4101 @bye
4103 @comment texi related words used by Emacs' spell checker ispell.el
4105 @comment LocalWords: texinfo setfilename settitle setchapternewpage
4106 @comment LocalWords: iftex finalout ifinfo DIR titlepage vskip pt
4107 @comment LocalWords: filll dir samp dfn noindent xref pxref
4108 @comment LocalWords: var deffn texi deffnx itemx emph asis
4109 @comment LocalWords: findex smallexample subsubsection cindex
4110 @comment LocalWords: dircategory direntry itemize
4112 @comment other words used by Emacs' spell checker ispell.el
4113 @comment LocalWords: README fred updatedb xargs Plett Rendell akefile
4114 @comment LocalWords: args grep Filesystems fo foo fOo wildcards iname
4115 @comment LocalWords: ipath regex iregex expr fubar regexps
4116 @comment LocalWords: metacharacters macs sr sc inode lname ilname
4117 @comment LocalWords: sysdep noleaf ls inum xdev filesystems usr atime
4118 @comment LocalWords: ctime mtime amin cmin mmin al daystart Sladkey rm
4119 @comment LocalWords: anewer cnewer bckw rf xtype uname gname uid gid
4120 @comment LocalWords: nouser nogroup chown chgrp perm ch maxdepth
4121 @comment LocalWords: mindepth cpio src CD AFS statted stat fstype ufs
4122 @comment LocalWords: nfs tmp mfs printf fprint dils rw djm Nov lwall
4123 @comment LocalWords: POSIXLY fls fprintf strftime locale's EDT GMT AP
4124 @comment LocalWords: EST diff perl backquotes sprintf Falstad Oct cron
4125 @comment LocalWords: eg vmunix mkdir afs allexec allwrite ARG bigram
4126 @comment LocalWords: bigrams cd chmod comp crc CVS dbfile dum eof
4127 @comment LocalWords: fileserver filesystem fn frcode Ghazi Hnewc iXX
4128 @comment LocalWords: joeuser Kaveh localpaths localuser LOGNAME
4129 @comment LocalWords: Meyering mv netpaths netuser nonblank nonblanks
4130 @comment LocalWords: ois ok Pinard printindex proc procs prunefs
4131 @comment LocalWords: prunepaths pwd RFS rmadillo rmdir rsh sbins str
4132 @comment LocalWords: su Timar ubins ug unstripped vf VM Weitzel
4133 @comment LocalWords: wildcard zlogout basename execdir wholename iwholename
4134 @comment LocalWords: timestamp timestamps Solaris FreeBSD OpenBSD POSIX