MMove the normalisation into a procedure.
[findutils.git] / doc / find.texi
blob9bd2b45be1c8d999c9aa08a29360a7e8996381c7
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 entire
49 resulting derived work is distributed under the terms of a permission
50 notice identical to this one.
52 Permission is granted to copy and distribute translations of this manual
53 into another language, under the above conditions for modified versions,
54 except that this permission notice may be stated in a translation approved
55 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.
77 This is edition @value{EDITION}, for @code{find} version @value{VERSION}.
78 @end ifnottex
80 @c The master menu, created with texinfo-master-menu, goes here.
82 @menu
83 * Introduction::                Summary of the tasks this manual describes.
84 * Finding Files::               Finding files that match certain criteria.
85 * Actions::                     Doing things to files you have found.
86 * Common Tasks::                Solutions to common real-world problems.
87 * Databases::                   Maintaining file name databases.
88 * File Permissions::            How to control access to files.
89 * Reference::                   Summary of how to invoke the programs.
90 * Security Considerations::     Security issues relating to findutils.
91 * Error Messages::              Explanations of some messages you might see.
92 * Primary Index::               The components of @code{find} expressions.
93 @end menu
95 @node Introduction, Finding Files, Top, Top
96 @chapter Introduction
98 This manual shows how to find files that meet criteria you specify, and
99 how to perform various actions on the files that you find.  The
100 principal programs that you use to perform these tasks are @code{find},
101 @code{locate}, and @code{xargs}.  Some of the examples in this manual
102 use capabilities specific to the GNU versions of those programs.
104 GNU @code{find} was originally written by Eric Decker, with enhancements
105 by David MacKenzie, Jay Plett, and Tim Wood.  GNU @code{xargs} was
106 originally written by Mike Rendell, with enhancements by David
107 MacKenzie.  GNU @code{locate} and its associated utilities were
108 originally written by James Woods, with enhancements by David MacKenzie.
109 The idea for @samp{find -print0} and @samp{xargs -0} came from Dan
110 Bernstein.  The current maintainer of GNU findutils (and this manual) is
111 James Youngman.  Many other people have contributed bug fixes, small
112 improvements, and helpful suggestions.  Thanks!
114 Mail suggestions and bug reports for these programs to
115 @code{bug-findutils@@gnu.org}.  Please include the version
116 number, which you can get by running @samp{find --version}.
118 @menu
119 * Scope::
120 * Overview::
121 * find Expressions::
122 @end menu
124 @node Scope
125 @section Scope
127 For brevity, the word @dfn{file} in this manual means a regular file, a
128 directory, a symbolic link, or any other kind of node that has a
129 directory entry.  A directory entry is also called a @dfn{file name}.  A
130 file name may contain some, all, or none of the directories in a path
131 that leads to the file.  These are all examples of what this manual
132 calls ``file names'':
134 @example
135 parser.c
136 README
137 ./budget/may-94.sc
138 fred/.cshrc
139 /usr/local/include/termcap.h
140 @end example
142 A @dfn{directory tree} is a directory and the files it contains, all of
143 its subdirectories and the files they contain, etc.  It can also be a
144 single non-directory file.
146 These programs enable you to find the files in one or more directory
147 trees that:
149 @itemize @bullet
150 @item
151 have names that contain certain text or match a certain pattern;
152 @item
153 are links to certain files;
154 @item
155 were last used during a certain period of time;
156 @item
157 are within a certain size range;
158 @item
159 are of a certain type (regular file, directory, symbolic link, etc.);
160 @item
161 are owned by a certain user or group;
162 @item
163 have certain access permissions;
164 @item
165 contain text that matches a certain pattern;
166 @item
167 are within a certain depth in the directory tree;
168 @item
169 or some combination of the above.
170 @end itemize
172 Once you have found the files you're looking for (or files that are
173 potentially the ones you're looking for), you can do more to them than
174 simply list their names.  You can get any combination of the files'
175 attributes, or process the files in many ways, either individually or in
176 groups of various sizes.  Actions that you might want to perform on the
177 files you have found include, but are not limited to:
179 @itemize @bullet
180 @item
181 view or edit
182 @item
183 store in an archive
184 @item
185 remove or rename
186 @item
187 change access permissions
188 @item
189 classify into groups
190 @end itemize
192 This manual describes how to perform each of those tasks, and more.
194 @node Overview
195 @section Overview
197 The principal programs used for making lists of files that match given
198 criteria and running commands on them are @code{find}, @code{locate},
199 and @code{xargs}.  An additional command, @code{updatedb}, is used by
200 system administrators to create databases for @code{locate} to use.
202 @code{find} searches for files in a directory hierarchy and prints
203 information about the files it found.  It is run like this:
205 @example
206 find @r{[}@var{file}@dots{}@r{]} @r{[}@var{expression}@r{]}
207 @end example
209 @noindent
210 Here is a typical use of @code{find}.  This example prints the names of
211 all files in the directory tree rooted in @file{/usr/src} whose name
212 ends with @samp{.c} and that are larger than 100 Kilobytes.
213 @example
214 find /usr/src -name '*.c' -size +100k -print
215 @end example
217 Notice that the wildcard must be enclosed in quotes in order to
218 protect it from expansion by the shell.
220 @code{locate} searches special file name databases for file names that
221 match patterns.  The system administrator runs the @code{updatedb}
222 program to create the databases.  @code{locate} is run like this:
224 @example
225 locate @r{[}@var{option}@dots{}@r{]} @var{pattern}@dots{}
226 @end example
228 @noindent
229 This example prints the names of all files in the default file name
230 database whose name ends with @samp{Makefile} or @samp{makefile}.  Which
231 file names are stored in the database depends on how the system
232 administrator ran @code{updatedb}.
233 @example
234 locate '*[Mm]akefile'
235 @end example
237 The name @code{xargs}, pronounced EX-args, means ``combine arguments.''
238 @code{xargs} builds and executes command lines by gathering together
239 arguments it reads on the standard input.  Most often, these arguments
240 are lists of file names generated by @code{find}.  @code{xargs} is run
241 like this:
243 @example
244 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
245 @end example
247 @noindent
248 The following command searches the files listed in the file
249 @file{file-list} and prints all of the lines in them that contain the
250 word @samp{typedef}.
251 @example
252 xargs grep typedef < file-list
253 @end example
255 @node find Expressions
256 @section @code{find} Expressions
258 The expression that @code{find} uses to select files consists of one or
259 more @dfn{primaries}, each of which is a separate command line argument
260 to @code{find}.  @code{find} evaluates the expression each time it
261 processes a file.  An expression can contain any of the following types
262 of primaries:
264 @table @dfn
265 @item options
266 affect overall operation rather than the processing of a specific file;
267 @item tests
268 return a true or false value, depending on the file's attributes;
269 @item actions
270 have side effects and return a true or false value; and
271 @item operators
272 connect the other arguments and affect when and whether they are
273 evaluated.
274 @end table
276 You can omit the operator between two primaries; it defaults to
277 @samp{-and}.  @xref{Combining Primaries With Operators}, for ways to
278 connect primaries into more complex expressions.  If the expression
279 contains no actions other than @samp{-prune}, @samp{-print} is performed
280 on all files for which the entire expression is true (@pxref{Print File
281 Name}).
283 Options take effect immediately, rather than being evaluated for each
284 file when their place in the expression is reached.  Therefore, for
285 clarity, it is best to place them at the beginning of the expression.
287 Many of the primaries take arguments, which immediately follow them in
288 the next command line argument to @code{find}.  Some arguments are file
289 names, patterns, or other strings; others are numbers.  Numeric
290 arguments can be specified as
292 @table @code
293 @item +@var{n}
294 for greater than @var{n},
295 @item -@var{n}
296 for less than @var{n},
297 @item @var{n}
298 for exactly @var{n}.
299 @end table
301 @node Finding Files, Actions, Introduction, Top
302 @chapter Finding Files
304 By default, @code{find} prints to the standard output the names of the
305 files that match the given criteria.  @xref{Actions}, for how to get more
306 information about the matching files.
309 @menu
310 * Name::
311 * Links::
312 * Time::
313 * Size::
314 * Type::
315 * Owner::
316 * Permissions::
317 * Contents::
318 * Directories::
319 * Filesystems::
320 * Combining Primaries With Operators::
321 @end menu
323 @node Name
324 @section Name
326 Here are ways to search for files whose name matches a certain pattern.
327 @xref{Shell Pattern Matching}, for a description of the @var{pattern}
328 arguments to these tests.
330 Each of these tests has a case-sensitive version and a case-insensitive
331 version, whose name begins with @samp{i}.  In a case-insensitive
332 comparison, the patterns @samp{fo*} and @samp{F??} match the file names
333 @file{Foo}, @samp{FOO}, @samp{foo}, @samp{fOo}, etc.
335 @menu
336 * Base Name Patterns::
337 * Full Name Patterns::
338 * Fast Full Name Search::
339 * Shell Pattern Matching::      Wildcards used by these programs.
340 @end menu
342 @node Base Name Patterns
343 @subsection Base Name Patterns
345 @deffn Test -name pattern
346 @deffnx Test -iname pattern
347 True if the base of the file name (the path with the leading directories
348 removed) matches shell pattern @var{pattern}.  For @samp{-iname}, the
349 match is case-insensitive.  To ignore a whole directory tree, use
350 @samp{-prune} (@pxref{Directories}).  As an example, to find Texinfo
351 source files in @file{/usr/local/doc}:
353 @example
354 find /usr/local/doc -name '*.texi'
355 @end example
356 @end deffn
358 Notice that the wildcard must be enclosed in quotes in order to
359 protect it from expansion by the shell.
361 Patterns for @samp{-name} and @samp{-iname} will match a filename with
362 a leading @samp{.}.  For example the command @samp{find /tmp -name
363 \*bar} will match the file @file{/tmp/.foobar}.
366 @node Full Name Patterns
367 @subsection Full Name Patterns
369 @deffn Test -wholename pattern
370 @deffnx Test -iwholename pattern
371 True if the entire file name, starting with the command line argument
372 under which the file was found, matches shell pattern @var{pattern}.
373 For @samp{-iwholename}, the match is case-insensitive.  To ignore a
374 whole directory tree, use @samp{-prune} rather than checking every
375 file in the tree (@pxref{Directories}).  The ``entire file name'' as
376 used by find starts with the starting-point specified on the command
377 line, and is not converted to an absolute pathname, so for example
378 @code{cd /; find tmp -wholename /tmp} will never match anything.
379 @end deffn
381 @deffn Test -path pattern
382 @deffnx Test -ipath pattern
383 These tests are deprecated, but work as for @samp{-wholename} and @samp{-iwholename},
384 respectively.  The @samp{-ipath} test is a GNU extension, but @samp{-path} is also 
385 provided by HP-UX @code{find}.
386 @end deffn
388 @deffn Test -regex expr
389 @deffnx Test -iregex expr
390 True if the entire file name matches regular expression @var{expr}.
391 This is a match on the whole path, not a search.  For example, to match
392 a file named @file{./fubar3}, you can use the regular expression
393 @samp{.*bar.} or @samp{.*b.*3}, but not @samp{f.*r3}.  @xref{Regexps, ,
394 Syntax of Regular Expressions, emacs, The GNU Emacs Manual}, for a
395 description of the syntax of regular expressions.  For @samp{-iregex},
396 the match is case-insensitive.
397 @end deffn
399 @node Fast Full Name Search
400 @subsection Fast Full Name Search
402 To search for files by name without having to actually scan the
403 directories on the disk (which can be slow), you can use the
404 @code{locate} program.  For each shell pattern you give it,
405 @code{locate} searches one or more databases of file names and displays
406 the file names that contain the pattern.  @xref{Shell Pattern Matching},
407 for details about shell patterns.
409 If a pattern is a plain string---it contains no
410 metacharacters---@code{locate} displays all file names in the database
411 that contain that string.  If a pattern contains
412 metacharacters, @code{locate} only displays file names that match the
413 pattern exactly.  As a result, patterns that contain metacharacters
414 should usually begin with a @samp{*}, and will most often end with one
415 as well.  The exceptions are patterns that are intended to explicitly
416 match the beginning or end of a file name.   
418 If you only want @code{locate} to match against the last component of
419 the filenames (the ``base name'' of the files) you can use the
420 @samp{--basename} option.  The opposite behaviour is the default, but
421 can be selected explicitly by using the option @samp{--wholename}.
423 The command
424 @example
425 locate @var{pattern}
426 @end example
428 is almost equivalent to
429 @example
430 find @var{directories} -name @var{pattern}
431 @end example
433 where @var{directories} are the directories for which the file name
434 databases contain information.  The differences are that the
435 @code{locate} information might be out of date, and that @code{locate}
436 handles wildcards in the pattern slightly differently than @code{find}
437 (@pxref{Shell Pattern Matching}).
439 The file name databases contain lists of files that were on the system
440 when the databases were last updated.  The system administrator can
441 choose the file name of the default database, the frequency with which
442 the databases are updated, and the directories for which they contain
443 entries.
445 Here is how to select which file name databases @code{locate} searches.
446 The default is system-dependent.
448 @table @code
449 @item --database=@var{path}
450 @itemx -d @var{path}
451 Instead of searching the default file name database, search the file
452 name databases in @var{path}, which is a colon-separated list of
453 database file names.  You can also use the environment variable
454 @code{LOCATE_PATH} to set the list of database files to search.  The
455 option overrides the environment variable if both are used.
456 @end table
458 @node Shell Pattern Matching
459 @subsection Shell Pattern Matching
461 @code{find} and @code{locate} can compare file names, or parts of file
462 names, to shell patterns.  A @dfn{shell pattern} is a string that may
463 contain the following special characters, which are known as
464 @dfn{wildcards} or @dfn{metacharacters}.
466 You must quote patterns that contain metacharacters to prevent the shell
467 from expanding them itself.  Double and single quotes both work; so does
468 escaping with a backslash.
470 @table @code
471 @item *
472 Matches any zero or more characters.
474 @item ?
475 Matches any one character.
477 @item [@var{string}]
478 Matches exactly one character that is a member of the string
479 @var{string}.  This is called a @dfn{character class}.  As a shorthand,
480 @var{string} may contain ranges, which consist of two characters with a
481 dash between them.  For example, the class @samp{[a-z0-9_]} matches a
482 lowercase letter, a number, or an underscore.  You can negate a class by
483 placing a @samp{!} or @samp{^} immediately after the opening bracket.
484 Thus, @samp{[^A-Z@@]} matches any character except an uppercase letter
485 or an at sign.
487 @item \
488 Removes the special meaning of the character that follows it.  This
489 works even in character classes.
490 @end table
492 In the @code{find} tests that do shell pattern matching (@samp{-name},
493 @samp{-wholename}, etc.), wildcards in the pattern will match a @samp{.}
494 at the beginning of a file name.  This is also the case for
495 @code{locate}.  Thus, @samp{find -name '*macs'} will match a file
496 named @file{.emacs}, as will @samp{locate '*macs'}.
498 Slash characters have no special significance in the shell pattern
499 matching that @code{find} and @code{locate} do, unlike in the shell, in
500 which wildcards do not match them.  Therefore, a pattern @samp{foo*bar}
501 can match a file name @samp{foo3/bar}, and a pattern @samp{./sr*sc} can
502 match a file name @samp{./src/misc}.
504 If you want to locate some files with the @samp{locate} command but
505 don't need to see the full list you can use the @samp{--limit} option
506 to see just a small number of results, or the @samp{--count} option to
507 display only the total number of matches.
509 @node Links
510 @section Links
512 There are two ways that files can be linked together.  @dfn{Symbolic
513 links} are a special type of file whose contents are a portion of the
514 name of another file.  @dfn{Hard links} are multiple directory entries
515 for one file; the file names all have the same index node (@dfn{inode})
516 number on the disk.
518 @menu
519 * Symbolic Links::
520 * Hard Links::
521 @end menu
523 @node Symbolic Links
524 @subsection Symbolic Links
526 Symbolic links are names that reference other files.  GNU @code{find}
527 will handle symbolic links in one of two ways; firstly, it can
528 dereference the links for you - this means that if it comes across a
529 symbolic link, it examines the file that the link points to, in order
530 to see if it matches the criteria you have specified.  Secondly, it
531 can check the link itself in case you might be looking for the actual
532 link.  If the file that the symbolic link points to is also within the
533 directory hierarchy you are searching with the @code{find} command,
534 you may not see a great deal of difference between these two
535 alternatives.
537 By default, @code{find} examines symbolic links themselves when it
538 finds them (and, if it later comes across the linked-to file, it will
539 examine that, too).  If you would prefer @code{find} to dereference
540 the links and examine the file that each link points to, specify the
541 @samp{-L} option to @code{find}.  You can explicitly specify the
542 default behaviour by using the @samp{-P} option.    The @samp{-H}
543 option is a half-way-between option which ensures that any symbolic
544 links listed on the command line are dereferenced, but other symbolic
545 links are not.
547 Symbolic links are different to ``hard links'' in the sense that you
548 need permissions upon the linked-to file in order to be able to
549 dereference the link.  This can mean that even if you specify the
550 @samp{-L} option, find may not be able to determine the properties of
551 the file that the link points to (because you don't have sufficient
552 permissions).  In this situation, @samp{find} uses the properties of
553 the link itself.  This also occurs if a symbolic link exists but
554 points to a file that is missing.
556 The options controlling the behaviour of @code{find} with respect to
557 links are as follows :-
559 @table @samp
560 @item -P
561 @code{find} does not dereference symbolic links at all.  This is the
562 default behaviour.  This option must be specified before any of the
563 path names on the command line.
564 @item -H
565 @code{find} does not dereference symbolic links (except in the case of
566 file names on the command line, which are dereferenced).  If a
567 symbolic link cannot be dereferenced, the information for the symbolic
568 link itself is used.  This option must be specified before any of the
569 path names on the command line.
570 @item -L
571 @code{find} dereferences symbolic links where possible, and where this
572 is not possible it uses the properties of the symbolic link itself.
573 This option must be specified before any of the path names on the
574 command line.  Use of this option also implies the same behaviour as
575 the @samp{-noleaf} option.   If you later use the @samp{-H} or
576 @samp{-P} options, this does not turn off @samp{-noleaf}.
578 @item -follow
579 This option forms part of the ``expression'' and must be specified
580 after the path names, but it is otherwise equivalent to @samp{-L}.
581 @end table
583 The following differences in behavior occur when the @samp{-L} option
584 is used:
586 @itemize @bullet
587 @item
588 @code{find} follows symbolic links to directories when searching
589 directory trees.
590 @item
591 @samp{-lname} and @samp{-ilname} always return false (unless they
592 happen to match broken symbolic links).
593 @item
594 @samp{-type} reports the types of the files that symbolic links point
596 @item
597 Implies @samp{-noleaf} (@pxref{Directories}).
598 @end itemize
600 If the @samp{-L} option or the @samp{-H} option is used, 
601 the filenames used as arguments to @samp{-newer}, @samp{-anewer}, and
602 @samp{-cnewer} are dereferenced and the timestamp from the pointed-to
603 file is used instead (if possible -- otherwise the timestamp from the
604 symbolic link is used).
606 @deffn Test -lname pattern
607 @deffnx Test -ilname pattern
608 True if the file is a symbolic link whose contents match shell pattern
609 @var{pattern}.  For @samp{-ilname}, the match is case-insensitive.
610 @xref{Shell Pattern Matching}, for details about the @var{pattern}
611 argument.  If the @samp{-L} option is in effect, this test will always
612 fail for symbolic links unless they are broken.  So, to list any
613 symbolic links to @file{sysdep.c} in the current directory and its
614 subdirectories, you can do:
616 @example
617 find . -lname '*sysdep.c'
618 @end example
619 @end deffn
621 @node Hard Links
622 @subsection Hard Links
624 Hard links allow more than one name to refer to the same file.  To
625 find all the names which refer to the same file as NAME, use
626 @samp{-samefile NAME}.  If you are not using the @samp{-L} option, you
627 can confine your search to one filesystem using the @samp{-xdev}
628 option.  This is useful because hard links cannot point outside a
629 single filesystem, so this can cut down on needless searching.
631 If the @samp{-L} option is in effect, and NAME is in fact a symbolic
632 link, the symbolic link will be dereferenced.  Hence you are searching
633 for other links (hard or symbolic) to the file pointed to by NAME.  If
634 @samp{-L} is in effect but NAME is not itself a symbolic link, other
635 symbolic links to the file NAME will be matched.
637 You can also search for files by inode number.  This can occasionally
638 be useful in diagnosing problems with filesystems for example, because
639 @code{fsck} tends to print inode numbers.  Inode numbers also
640 occasionally turn up in log messages for some types of software, and
641 are used to support the @code{ftok()} library function.
643 You can learn a file's inode number and the number of links to it by
644 running @samp{ls -li} or @samp{find -ls}.  
646 You can search for hard links to inode number NUM by using @samp{-inum
647 NUM}. If there are any file system mount points below the directory
648 where you are starting the search, use the @samp{-xdev} option unless
649 you are also using the @samp{-L} option.  Using @samp{-xdev} this
650 saves needless searching, since hard links to a file must be on the
651 same filesystem.  @xref{Filesystems}.
653 @deffn Test -samefile NAME
654 File is a hard link to the same inode as NAME.  If the @samp{-L}
655 option is in effect, symbolic links to the same file as NAME points to
656 are also matched.
657 @end deffn
659 @deffn Test -inum n
660 File has inode number @var{n}.  The @samp{+} and @samp{-} qualifiers
661 also work, though these are rarely useful.
662 @end deffn
664 You can also search for files that have a certain number of links, with
665 @samp{-links}.  Directories normally have at least two hard links; their
666 @file{.} entry is the second one.  If they have subdirectories, each of
667 those also has a hard link called @file{..} to its parent directory.
668 The @file{.} and @file{..} directory entries are not normally searched
669 unless they are mentioned on the @code{find} command line.
671 @deffn Test -links n
672 File has @var{n} hard links.
673 @end deffn
675 @deffn Test -links +n
676 File has more than @var{n} hard links.
677 @end deffn
679 @deffn Test -links -n
680 File has fewer than @var{n} hard links.
681 @end deffn
683 @node Time
684 @section Time
686 Each file has three time stamps, which record the last time that certain
687 operations were performed on the file:
689 @enumerate
690 @item
691 access (read the file's contents)
692 @item
693 change the status (modify the file or its attributes)
694 @item
695 modify (change the file's contents)
696 @end enumerate
698 There is no timestamp that indicates when a file was @emph{created}.
700 You can search for files whose time stamps are within a certain age
701 range, or compare them to other time stamps.  
703 @menu
704 * Age Ranges::
705 * Comparing Timestamps::
706 @end menu
708 @node Age Ranges
709 @subsection Age Ranges
711 These tests are mainly useful with ranges (@samp{+@var{n}} and
712 @samp{-@var{n}}).
714 @deffn Test -atime n
715 @deffnx Test -ctime n
716 @deffnx Test -mtime n
717 True if the file was last accessed (or its status changed, or it was
718 modified) @var{n}*24 hours ago.  The number of 24-hour periods since
719 the file's timestamp is always rounded down; therefore 0 means ``less
720 than 24 hours ago'', 1 means ``between 24 and 48 hours ago'', and so
721 forth.
722 @end deffn
724 @deffn Test -amin n
725 @deffnx Test -cmin n
726 @deffnx Test -mmin n
727 True if the file was last accessed (or its status changed, or it was
728 modified) @var{n} minutes ago.  These tests provide finer granularity of
729 measurement than @samp{-atime} et al., but rounding is done in a 
730 similar way.  For example, to list files in
731 @file{/u/bill} that were last read from 2 to 6 minutes ago:
733 @example
734 find /u/bill -amin +2 -amin -6
735 @end example
736 @end deffn
738 @deffn Option -daystart
739 Measure times from the beginning of today rather than from 24 hours ago.
740 So, to list the regular files in your home directory that were modified
741 yesterday, do
743 @example
744 find ~ -daystart -type f -mtime 1
745 @end example
746 @end deffn
748 The @samp{-daystart} option is unlike most other options in that it
749 has an effect on the way that other tests are performed.  The affected
750 tests are @samp{-amin}, @samp{-cmin}, @samp{-mmin}, @samp{-atime},
751 @samp{-ctime} and @samp{-mtime}.
753 @node Comparing Timestamps
754 @subsection Comparing Timestamps
756 As an alternative to comparing timestamps to the current time, you can
757 compare them to another file's timestamp.  That file's timestamp could
758 be updated by another program when some event occurs.  Or you could set
759 it to a particular fixed date using the @code{touch} command.  For
760 example, to list files in @file{/usr} modified after February 1 of the
761 current year:
763 @c Idea from Rick Sladkey.
764 @example
765 touch -t 02010000 /tmp/stamp$$
766 find /usr -newer /tmp/stamp$$
767 rm -f /tmp/stamp$$
768 @end example
770 @deffn Test -anewer file
771 @deffnx Test -cnewer file
772 @deffnx Test -newer file
773 True if the file was last accessed (or its status changed, or it was
774 modified) more recently than @var{file} was modified.  These tests are
775 affected by @samp{-follow} only if @samp{-follow} comes before them on
776 the command line.  @xref{Symbolic Links}, for more information on
777 @samp{-follow}.  As an example, to list any files modified since
778 @file{/bin/sh} was last modified:
780 @example
781 find . -newer /bin/sh
782 @end example
783 @end deffn
785 @deffn Test -used n
786 True if the file was last accessed @var{n} days after its status was
787 last changed.  Useful for finding files that are not being used, and
788 could perhaps be archived or removed to save disk space.
789 @end deffn
791 @node Size
792 @section Size
794 @deffn Test -size n@r{[}bckwMG@r{]}
795 True if the file uses @var{n} units of space, rounding up.  The units
796 are 512-byte blocks by default, but they can be changed by adding a
797 one-character suffix to @var{n}:
799 @table @code
800 @item b
801 512-byte blocks (never 1024)
802 @item c
803 bytes
804 @item k
805 kilobytes (1024 bytes)
806 @item w
807 2-byte words
808 @item M
809 Megabytes
810 @item G
811 Gigabytes
812 @end table
814 The `b' suffix always considers blocks to be 512 bytes.  This is not
815 affected by the setting (or non-setting) of the POSIXLY_CORRECT
816 environment variable.  This behaviour is different to the behaviour of
817 the @samp{-ls} action).  If you want to use 1024-byte units, use the
818 `k' suffix instead.
820 The number can be prefixed with a `+' or a `-'.  A plus sign indicates
821 that the test should succeed if the file uses at least @var{n} units
822 of storage (this is the way I normally use this test) and a minus sign
823 indicates that the test should succeed if the file uses less than
824 @var{n} units of storage.   There is no `=' prefix, because that's the 
825 default anyway.  
827 The size does not count indirect blocks, but it does count blocks in
828 sparse files that are not actually allocated.  In other words, it's
829 consistent with the result you get for @samp{ls -l} or @samp{wc -c}.
830 This handling of sparse files differs from the output of the @samp{%k}
831 and @samp{%b} format specifiers for the @samp{-printf} predicate.
833 @end deffn
835 @deffn Test -empty
836 True if the file is empty and is either a regular file or a directory.
837 This might make it a good candidate for deletion.  This test is useful
838 with @samp{-depth} (@pxref{Directories}) and @samp{-delete}
839 (@pxref{Single File}).
840 @end deffn
842 @node Type
843 @section Type
845 @deffn Test -type c
846 True if the file is of type @var{c}:
848 @table @code
849 @item b
850 block (buffered) special
851 @item c
852 character (unbuffered) special
853 @item d
854 directory
855 @item p
856 named pipe (FIFO)
857 @item f
858 regular file
859 @item l
860 symbolic link
861 @item s
862 socket
863 @item D
864 door (Solaris)
865 @end table
866 @end deffn
868 @deffn Test -xtype c
869 The same as @samp{-type} unless the file is a symbolic link.  For
870 symbolic links: if @samp{-follow} has not been given, true if the file
871 is a link to a file of type @var{c}; if @samp{-follow} has been given,
872 true if @var{c} is @samp{l}.  In other words, for symbolic links,
873 @samp{-xtype} checks the type of the file that @samp{-type} does not
874 check.  @xref{Symbolic Links}, for more information on @samp{-follow}.
875 @end deffn
877 @node Owner
878 @section Owner
880 @deffn Test -user uname
881 @deffnx Test -group gname
882 True if the file is owned by user @var{uname} (belongs to group @var{gname}).
883 A numeric ID is allowed.
884 @end deffn
886 @deffn Test -uid n
887 @deffnx Test -gid n
888 True if the file's numeric user ID (group ID) is @var{n}.  These tests
889 support ranges (@samp{+@var{n}} and @samp{-@var{n}}), unlike
890 @samp{-user} and @samp{-group}.
891 @end deffn
893 @deffn Test -nouser
894 @deffnx Test -nogroup
895 True if no user corresponds to the file's numeric user ID (no group
896 corresponds to the numeric group ID).  These cases usually mean that the
897 files belonged to users who have since been removed from the system.
898 You probably should change the ownership of such files to an existing
899 user or group, using the @code{chown} or @code{chgrp} program.
900 @end deffn
902 @node Permissions
903 @section Permissions
905 @xref{File Permissions}, for information on how file permissions are
906 structured and how to specify them.
908 @deffn Test -perm mode
910 True if the file's permissions are exactly @var{mode} (which can be
911 numeric or symbolic).
913 If @var{mode} starts with @samp{-}, true if
914 @emph{all} of the permissions set in @var{mode} are set for the file;
915 permissions not set in @var{mode} are ignored.
916 If @var{mode} starts with @samp{/}, true if
917 @emph{any} of the permissions set in @var{mode} are set for the file;
918 permissions not set in @var{mode} are ignored.
919 This is a GNU extension.
921 If you don't use the @samp{/} or @samp{-} form with a symbolic mode
922 string, you may have to specify a rather complex mode string.  For
923 example @samp{-perm g=w} will only match files which have mode 0020
924 (that is, ones for which group write permission is the only permission
925 set).  It is more likely that you will want to use the @samp{/} or
926 @samp{-} forms, for example @samp{-perm -g=w}, which matches any file
927 with group write permission.
930 @table @samp
931 @item -perm 664
932 Match files which have read and write permission for their owner,
933 and group, but which the rest of the world can read but not write to.  
934 Files which meet these criteria but have other permissions bits set
935 (for example if someone can execute the file) will not be matched.
937 @item -perm -664
938 Match files which have read and write permission for their owner,
939 and group, but which the rest of the world can read but not write to,
940 without regard to the presence of any extra permission bits (for
941 example the executable bit).  This will match a file which has mode
942 0777, for example.
944 @item -perm /222
945 Match files which are writeable by somebody (their owner, or
946 their group, or anybody else).
948 @item -perm /022
949 Match files which are writeable by either their owner or their
950 group.  The files don't have to be writeable by both the owner and
951 group to be matched; either will do.
953 @item -perm /g+w,o+w
954 As above.
956 @item -perm /g=w,o=w
957 As above
959 @item -perm -022
960 Search for files which are writeable by both their owner and their
961 group.
963 @item -perm -g+w,o+w
964 As above.
965 @end table
966 @end deffn
968 @node Contents
969 @section Contents
971 To search for files based on their contents, you can use the @code{grep}
972 program.  For example, to find out which C source files in the current
973 directory contain the string @samp{thing}, you can do:
975 @example
976 grep -l thing *.[ch]
977 @end example
979 If you also want to search for the string in files in subdirectories,
980 you can combine @code{grep} with @code{find} and @code{xargs}, like
981 this:
983 @example
984 find . -name '*.[ch]' | xargs grep -l thing
985 @end example
987 The @samp{-l} option causes @code{grep} to print only the names of files
988 that contain the string, rather than the lines that contain it.  The
989 string argument (@samp{thing}) is actually a regular expression, so it
990 can contain metacharacters.  This method can be refined a little by
991 using the @samp{-r} option to make @code{xargs} not run @code{grep} if
992 @code{find} produces no output, and using the @code{find} action
993 @samp{-print0} and the @code{xargs} option @samp{-0} to avoid
994 misinterpreting files whose names contain spaces:
996 @example
997 find . -name '*.[ch]' -print0 | xargs -r -0 grep -l thing
998 @end example
1000 For a fuller treatment of finding files whose contents match a pattern,
1001 see the manual page for @code{grep}.
1003 @node Directories
1004 @section Directories
1006 Here is how to control which directories @code{find} searches, and how
1007 it searches them.  These two options allow you to process a horizontal
1008 slice of a directory tree.
1010 @deffn Option -maxdepth levels
1011 Descend at most @var{levels} (a non-negative integer) levels of
1012 directories below the command line arguments.  @samp{-maxdepth 0} means
1013 only apply the tests and actions to the command line arguments.
1014 @end deffn
1016 @deffn Option -mindepth levels
1017 Do not apply any tests or actions at levels less than @var{levels} (a
1018 non-negative integer).  @samp{-mindepth 1} means process all files
1019 except the command line arguments.
1020 @end deffn
1022 @deffn Option -depth
1023 Process each directory's contents before the directory itself.  Doing
1024 this is a good idea when producing lists of files to archive with
1025 @code{cpio} or @code{tar}.  If a directory does not have write
1026 permission for its owner, its contents can still be restored from the
1027 archive since the directory's permissions are restored after its contents.
1028 @end deffn
1030 @deffn Option -d
1031 This is a deprecated synonym for @samp{-depth}, for compatibility with
1032 Mac OS X, FreeBSD and OpenBSD.   The @samp{-depth} option is a POSIX
1033 feature, so it is better to use that.
1034 @end deffn
1036 @deffn Action -prune
1037 If the file is a directory, do not descend into it.  The result is
1038 true.  For example, to skip the directory @file{src/emacs} and all
1039 files and directories under it, and print the names of the other files
1040 found:
1042 @example
1043 find . -wholename './src/emacs' -prune -o -print
1044 @end example
1046 The above command will not print @file{./src/emacs} among its list of
1047 results.  This however is not due to the effect of the @samp{-prune}
1048 action (which only prevents further descent, it doesn't make sure we
1049 ignore that item).  Instead, this effect is due to the use of
1050 @samp{-o}.  Since the left hand side of the ``or'' condition has
1051 succeeded for @file{./src/emacs}, it is not necessary to evaluate the
1052 right-hand-side (@samp{-print}) at all for this particular file.  If
1053 you wanted to print that directory name you could use either an extra
1054 @samp{-print} action:
1056 @example
1057 find . -wholename './src/emacs' -prune -print -o -print
1058 @end example
1060 or use the comma operator:
1062 @example
1063 find . -wholename './src/emacs' -prune , -print
1064 @end example
1066 If the @samp{-depth} option is in effect, the subdirectories will have
1067 already been visited in any case.  Hence @samp{-prune} has no effect
1068 and returns false.
1069 @end deffn
1072 @deffn Action -quit
1073 Exit immediately (with return value zero if no errors have occurred).
1074 No child processes will be left running, but no more paths specified
1075 on the command line will be processed.  For example, @code{find
1076 /tmp/foo /tmp/bar -print -quit} will print only @samp{/tmp/foo}.
1077 @end deffn 
1079 @deffn Option -noleaf
1080 Do not optimize by assuming that directories contain 2 fewer
1081 subdirectories than their hard link count.  This option is needed when
1082 searching filesystems that do not follow the Unix directory-link
1083 convention, such as CD-ROM or MS-DOS filesystems or AFS volume mount
1084 points.  Each directory on a normal Unix filesystem has at least 2 hard
1085 links: its name and its @file{.}  entry.  Additionally, its
1086 subdirectories (if any) each have a @file{..}  entry linked to that
1087 directory.  When @code{find} is examining a directory, after it has
1088 statted 2 fewer subdirectories than the directory's link count, it knows
1089 that the rest of the entries in the directory are non-directories
1090 (@dfn{leaf} files in the directory tree).  If only the files' names need
1091 to be examined, there is no need to stat them; this gives a significant
1092 increase in search speed.
1093 @end deffn
1095 @deffn Option -ignore_readdir_race
1096 If a file disappears after its name has been read from a directory but
1097 before @code{find} gets around to examining the file with @code{stat},
1098 don't issue an error message.  If you don't specify this option, an
1099 error message will be issued.  This option can be useful in system
1100 scripts (cron scripts, for example) that examine areas of the
1101 filesystem that change frequently (mail queues, temporary directories,
1102 and so forth), because this scenario is common for those sorts of
1103 directories.  Completely silencing error messages from @code{find} is
1104 undesirable, so this option neatly solves the problem.  There is no
1105 way to search one part of the filesystem with this option on and part
1106 of it with this option off, though.
1107 @end deffn
1109 @deffn Option -noignore_readdir_race
1110 This option reverses the effect of the @samp{-ignore_readdir_race} option.
1111 @end deffn
1114 @node Filesystems
1115 @section Filesystems
1117 A @dfn{filesystem} is a section of a disk, either on the local host or
1118 mounted from a remote host over a network.  Searching network
1119 filesystems can be slow, so it is common to make @code{find} avoid them.
1121 There are two ways to avoid searching certain filesystems.  One way is
1122 to tell @code{find} to only search one filesystem:
1124 @deffn Option -xdev
1125 @deffnx Option -mount
1126 Don't descend directories on other filesystems.  These options are synonyms.
1127 @end deffn
1129 The other way is to check the type of filesystem each file is on, and
1130 not descend directories that are on undesirable filesystem types:
1132 @deffn Test -fstype type
1133 True if the file is on a filesystem of type @var{type}.  The valid
1134 filesystem types vary among different versions of Unix; an incomplete
1135 list of filesystem types that are accepted on some version of Unix or
1136 another is:
1137 @example
1138 ext2 ext3 proc sysfs ufs 4.2 4.3 nfs tmp mfs S51K S52K
1139 @end example
1140 You can use @samp{-printf} with the @samp{%F} directive to see the types
1141 of your filesystems.  The @samp{%D} directive shows the device number.
1142 @xref{Print File Information}.  @samp{-fstype} is
1143 usually used with @samp{-prune} to avoid searching remote filesystems
1144 (@pxref{Directories}). 
1145 @end deffn
1147 @node Combining Primaries With Operators
1148 @section Combining Primaries With Operators
1150 Operators build a complex expression from tests and actions.
1151 The operators are, in order of decreasing precedence:
1153 @table @code
1154 @item @asis{( @var{expr} )}
1155 @findex ()
1156 Force precedence.  True if @var{expr} is true.
1158 @item @asis{! @var{expr}}
1159 @itemx @asis{-not @var{expr}}
1160 @findex !
1161 @findex -not
1162 True if @var{expr} is false.
1164 @item @asis{@var{expr1 expr2}}
1165 @itemx @asis{@var{expr1} -a @var{expr2}}
1166 @itemx @asis{@var{expr1} -and @var{expr2}}
1167 @findex -a
1168 @findex -and
1169 And; @var{expr2} is not evaluated if @var{expr1} is false.
1171 @item @asis{@var{expr1} -o @var{expr2}}
1172 @itemx @asis{@var{expr1} -or @var{expr2}}
1173 @findex -o
1174 @findex -or
1175 Or; @var{expr2} is not evaluated if @var{expr1} is true.
1177 @item @asis{@var{expr1} , @var{expr2}}
1178 @findex ,
1179 List; both @var{expr1} and @var{expr2} are always evaluated.  True if
1180 @var{expr2} is true.  The value of @var{expr1} is discarded.  This
1181 operator lets you do multiple independent operations on one traversal,
1182 without depending on whether other operations succeeded.  The two 
1183 operations @var{expr1} and @var{expr2} are not always fully
1184 independent, since @var{expr1} might have side effects like touching
1185 or deleting files, or it might use @samp{-prune} which would also
1186 affect @var{expr2}.
1187 @end table
1189 @code{find} searches the directory tree rooted at each file name by
1190 evaluating the expression from left to right, according to the rules of
1191 precedence, until the outcome is known (the left hand side is false for
1192 @samp{-and}, true for @samp{-or}), at which point @code{find} moves on
1193 to the next file name.
1195 There are two other tests that can be useful in complex expressions:
1197 @deffn Test -true
1198 Always true.
1199 @end deffn
1201 @deffn Test -false
1202 Always false.
1203 @end deffn
1205 @node Actions, Common Tasks, Finding Files, Top
1206 @chapter Actions
1208 There are several ways you can print information about the files that
1209 match the criteria you gave in the @code{find} expression.  You can
1210 print the information either to the standard output or to a file that
1211 you name.  You can also execute commands that have the file names as
1212 arguments.  You can use those commands as further filters to select files.
1214 @menu
1215 * Print File Name::
1216 * Print File Information::
1217 * Run Commands::
1218 * Delete Files::
1219 * Adding Tests::
1220 @end menu
1222 @node Print File Name
1223 @section Print File Name
1225 @deffn Action -print
1226 True; print the full file name on the standard output, followed by a
1227 newline.
1228 @end deffn
1230 @deffn Action -fprint file
1231 True; print the full file name into file @var{file}, followed by a
1232 newline.  If @var{file} does not exist when @code{find} is run, it is
1233 created; if it does exist, it is truncated to 0 bytes.  The file names
1234 @file{/dev/stdout} and @file{/dev/stderr} are handled specially; they
1235 refer to the standard output and standard error output, respectively.
1236 @end deffn
1238 @node Print File Information
1239 @section Print File Information
1241 @deffn Action -ls
1242 True; list the current file in @samp{ls -dils} format on the standard
1243 output.  The output looks like this:
1245 @smallexample
1246 204744   17 -rw-r--r--   1 djm      staff       17337 Nov  2  1992 ./lwall-quotes
1247 @end smallexample
1249 The fields are:
1251 @enumerate
1252 @item
1253 The inode number of the file.  @xref{Hard Links}, for how to find files
1254 based on their inode number.
1256 @item
1257 the number of blocks in the file.  The block counts are of 1K blocks,
1258 unless the environment variable @code{POSIXLY_CORRECT} is set, in which
1259 case 512-byte blocks are used.  @xref{Size}, for how to find files based
1260 on their size.
1262 @item
1263 The file's type and permissions.  The type is shown as a dash for a
1264 regular file; for other file types, a letter like for @samp{-type} is
1265 used (@pxref{Type}).  The permissions are read, write, and execute for
1266 the file's owner, its group, and other users, respectively; a dash means
1267 the permission is not granted.  @xref{File Permissions}, for more details
1268 about file permissions.  @xref{Permissions}, for how to find files based
1269 on their permissions.
1271 @item
1272 The number of hard links to the file.
1274 @item
1275 The user who owns the file.
1277 @item
1278 The file's group.
1280 @item
1281 The file's size in bytes.
1283 @item
1284 The date the file was last modified.
1286 @item
1287 The file's name.  @samp{-ls} quotes non-printable characters in the file
1288 names using C-like backslash escapes.  
1289 This may change soon, as the treatment of unprintable characters is
1290 harmonised for @samp{-ls}, @samp{-fls}, @samp{-print}, @samp{-fprint},
1291 @samp{-printf} and @samp{-fprintf}.
1292 @end enumerate
1293 @end deffn
1295 @deffn Action -fls file
1296 True; like @samp{-ls} but write to @var{file} like @samp{-fprint}
1297 (@pxref{Print File Name}).
1298 @end deffn
1300 @deffn Action -printf format
1301 True; print @var{format} on the standard output, interpreting @samp{\}
1302 escapes and @samp{%} directives.  Field widths and precisions can be
1303 specified as with the @code{printf} C function.  Format flags (like
1304 @samp{#} for example) may not work as you expect because many of the
1305 fields, even numeric ones, are printed with %s.  This means though
1306 that the format flag @samp{-} will work; it forces left-alignment of
1307 the field.  Unlike @samp{-print}, @samp{-printf} does not add a
1308 newline at the end of the string.  If you want a newline at the end of
1309 the string, add a @samp{\n}.
1310 @end deffn
1312 @deffn Action -fprintf file format
1313 True; like @samp{-printf} but write to @var{file} like @samp{-fprint}
1314 (@pxref{Print File Name}).
1315 @end deffn
1317 @menu
1318 * Escapes::
1319 * Format Directives::
1320 * Time Formats::
1321 @end menu
1323 @node Escapes
1324 @subsection Escapes
1326 The escapes that @samp{-printf} and @samp{-fprintf} recognize are:
1328 @table @code
1329 @item \a
1330 Alarm bell.
1331 @item \b
1332 Backspace.
1333 @item \c
1334 Stop printing from this format immediately and flush the output.
1335 @item \f
1336 Form feed.
1337 @item \n
1338 Newline.
1339 @item \r
1340 Carriage return.
1341 @item \t
1342 Horizontal tab.
1343 @item \v
1344 Vertical tab.
1345 @item \\
1346 A literal backslash (@samp{\}).
1347 @item \NNN
1348 The character whose ASCII code is NNN (octal).
1349 @end table
1351 A @samp{\} character followed by any other character is treated as an
1352 ordinary character, so they both are printed, and a warning message is
1353 printed to the standard error output (because it was probably a typo).
1355 @node Format Directives
1356 @subsection Format Directives
1358 @samp{-printf} and @samp{-fprintf} support the following format
1359 directives to print information about the file being processed.  The C
1360 @code{printf} function, field width and precision specifiers are
1361 supported, as applied to string (%s) types. That is, you can specify
1362 "minimum field width"."maximum field width" for each directive.
1363 Format flags (like @samp{#} for example) may not work as you expect
1364 because many of the fields, even numeric ones, are printed with %s.
1365 The format flag @samp{-} does work; it forces left-alignment of the
1366 field.
1368 @samp{%%} is a literal percent sign.  A @samp{%} character followed by
1369 an unrecognised character (i.e. not a known directive or printf field
1370 width and precision specifier), is discarded (but the unrecognised character
1371 is printed), and a warning message is printed to the standard error output
1372 (because it was probably a typo).
1374 @menu
1375 * Name Directives::
1376 * Ownership Directives::
1377 * Size Directives::
1378 * Location Directives::
1379 * Time Directives::
1380 * Formatting Flags::
1381 @end menu
1383 @node Name Directives
1384 @subsubsection Name Directives
1386 @table @code
1387 @item %p
1388 @c supports %-X.Yp
1389 File's name (not the absolute path name, but the name of the file as
1390 it was encountered by find - that is, as a relative path from one of
1391 the starting points).
1392 @item %f
1393 File's name with any leading directories removed (only the last element).
1394 @c supports %-X.Yf
1395 @item %h
1396 Leading directories of file's name (all but the last element and the
1397 slash before it).  If the file's name contains no slashes (for example
1398 because it was named on the command line and is in the current working
1399 directory), then ``%h'' expands to ``.''.  This prevents ``%h/%f''
1400 expanding to ``/foo'', which would be surprising and probably not
1401 desirable.
1402 @c supports %-X.Yh
1403 @item %P
1404 File's name with the name of the command line argument under which
1405 it was found removed from the beginning.
1406 @c supports %-X.YP
1407 @item %H
1408 Command line argument under which file was found.
1409 @c supports %-X.YH
1410 @end table
1412 @node Ownership Directives
1413 @subsubsection Ownership Directives
1415 @table @code
1416 @item %g
1417 @c supports %-X.Yg
1418 File's group name, or numeric group ID if the group has no name.
1419 @item %G
1420 @c supports %-X.Yg
1421 @c TODO: Needs to support # flag and 0 flag
1422 File's numeric group ID.
1423 @item %u
1424 @c supports %-X.Yu
1425 File's user name, or numeric user ID if the user has no name.
1426 @item %U
1427 @c supports %-X.Yu
1428 @c TODO: Needs to support # flag
1429 File's numeric user ID.
1430 @item %m
1431 @c full support, including # and 0.
1432 File's permissions (in octal).   If you always want to have a leading
1433 zero on the number, use the '#' format flag, for example '%#m'.
1434 @end table
1436 @node Size Directives
1437 @subsubsection Size Directives
1439 @table @code
1440 @item %k
1441 Amount of disk space occupied by the file, measured in 1K blocks
1442 (rounded up).  This can be less than the length of the file if 
1443 it is a sparse file (that is, it has ``holes'').  
1444 @item %b
1445 File's size in 512-byte blocks (rounded up).  This also can be less 
1446 than the length of the file, if the file is sparse.
1447 @item %s
1448 File's size in bytes.
1449 @end table
1451 @node Location Directives
1452 @subsubsection Location Directives
1454 @table @code
1455 @item %d
1456 File's depth in the directory tree (depth below a file named on the
1457 command line, not depth below the root directory).  Files named on the
1458 command line have a depth of 0.  Subdirectories immediately below them
1459 have a depth of 1, and so on.
1460 @item %D
1461 The device number on which the file exists (the @code{st_dev} field of
1462 @code{struct stat}), in decimal.
1463 @item %F
1464 Type of the filesystem the file is on; this value can be used for
1465 @samp{-fstype} (@pxref{Directories}).
1466 @item %l
1467 Object of symbolic link (empty string if file is not a symbolic link).
1468 @item %i
1469 File's inode number (in decimal).
1470 @item %n
1471 Number of hard links to file.
1472 @item %y 
1473 Type of the file as used with @samp{-type}.   If the file is a symbolic
1474 link, @samp{l} will be printed.
1475 @item %Y
1476 Type of the file as used with @samp{-type}.   If the file is a symbolic
1477 link, it is dereferenced.  If the file is a broken symbolic link,
1478 @samp{N} is printed.
1480 @end table
1482 @node Time Directives
1483 @subsubsection Time Directives
1485 Some of these directives use the C @code{ctime} function.  Its output
1486 depends on the current locale, but it typically looks like
1488 @example
1489 Wed Nov  2 00:42:36 1994
1490 @end example
1492 @table @code
1493 @item %a
1494 File's last access time in the format returned by the C @code{ctime} function.
1495 @item %A@var{k}
1496 File's last access time in the format specified by @var{k}
1497 (@pxref{Time Formats}). 
1498 @item %c
1499 File's last status change time in the format returned by the C @code{ctime}
1500 function.
1501 @item %C@var{k}
1502 File's last status change time in the format specified by @var{k}
1503 (@pxref{Time Formats}).
1504 @item %t
1505 File's last modification time in the format returned by the C @code{ctime}
1506 function.
1507 @item %T@var{k}
1508 File's last modification time in the format specified by @var{k} 
1509 (@pxref{Time Formats}). 
1510 @end table
1512 @node Time Formats
1513 @subsection Time Formats
1515 Below are the formats for the directives @samp{%A}, @samp{%C}, and
1516 @samp{%T}, which print the file's timestamps.  Some of these formats
1517 might not be available on all systems, due to differences in the C
1518 @code{strftime} function between systems.
1520 @menu
1521 * Time Components::
1522 * Date Components::
1523 * Combined Time Formats::
1524 @end menu
1526 @node Time Components
1527 @subsubsection Time Components
1529 The following format directives print single components of the time.
1531 @table @code
1532 @item H
1533 hour (00..23)
1534 @item I
1535 hour (01..12)
1536 @item k
1537 hour ( 0..23)
1538 @item l
1539 hour ( 1..12)
1540 @item p
1541 locale's AM or PM
1542 @item Z
1543 time zone (e.g., EDT), or nothing if no time zone is determinable
1544 @item M
1545 minute (00..59)
1546 @item S
1547 second (00..61)
1548 @item @@
1549 seconds since Jan. 1, 1970, 00:00 GMT.
1550 @end table
1552 @node Date Components
1553 @subsubsection Date Components
1555 The following format directives print single components of the date.
1557 @table @code
1558 @item a
1559 locale's abbreviated weekday name (Sun..Sat)
1560 @item A
1561 locale's full weekday name, variable length (Sunday..Saturday)
1562 @item b
1563 @itemx h
1564 locale's abbreviated month name (Jan..Dec)
1565 @item B
1566 locale's full month name, variable length (January..December)
1567 @item m
1568 month (01..12)
1569 @item d
1570 day of month (01..31)
1571 @item w
1572 day of week (0..6)
1573 @item j
1574 day of year (001..366)
1575 @item U
1576 week number of year with Sunday as first day of week (00..53)
1577 @item W
1578 week number of year with Monday as first day of week (00..53)
1579 @item Y
1580 year (1970@dots{})
1581 @item y
1582 last two digits of year (00..99)
1583 @end table
1585 @node Combined Time Formats
1586 @subsubsection Combined Time Formats
1588 The following format directives print combinations of time and date
1589 components. 
1591 @table @code
1592 @item r
1593 time, 12-hour (hh:mm:ss [AP]M)
1594 @item T
1595 time, 24-hour (hh:mm:ss)
1596 @item X
1597 locale's time representation (H:M:S)
1598 @item c
1599 locale's date and time (Sat Nov 04 12:02:33 EST 1989)
1600 @item D
1601 date (mm/dd/yy)
1602 @item x
1603 locale's date representation (mm/dd/yy)
1604 @item +
1605 Date and time, separated by '+', for example `2004-04-28+22:22:05'.
1606 The time is given in the current timezone (which may be affected by
1607 setting the TZ environment variable).  This is a GNU extension.
1608 @end table
1610 @node Formatting Flags
1611 @subsubsection Formatting Flags
1613 The @samp{%m} and @samp{%d} directives support the @samp{#}, @samp{0}
1614 and @samp{+} flags, but the other directives do not, even if they
1615 print numbers.  Numeric directives that do not support these flags
1616 include 
1618 @samp{G},
1619 @samp{U},
1620 @samp{b},
1621 @samp{D},
1622 @samp{k} and
1623 @samp{n}.
1625 All fields support the format flag @samp{-}, which makes fields
1626 left-aligned.  That is, if the field width is greater than the actual
1627 contents of the field, the requisite number of spaces are printed
1628 after the field content instead of before it.
1630 @node Run Commands
1631 @section Run Commands
1633 You can use the list of file names created by @code{find} or
1634 @code{locate} as arguments to other commands.  In this way you can
1635 perform arbitrary actions on the files.
1637 @menu
1638 * Single File::
1639 * Multiple Files::
1640 * Querying::
1641 @end menu
1643 @node Single File
1644 @subsection Single File
1646 Here is how to run a command on one file at a time.
1648 @deffn Action -execdir command ;
1649 Execute @var{command}; true if 0 status is returned.  @code{find} takes
1650 all arguments after @samp{-exec} to be part of the command until an
1651 argument consisting of @samp{;} is reached.  It replaces the string
1652 @samp{@{@}} by the current file name being processed everywhere it
1653 occurs in the command.  Both of these constructions need to be escaped
1654 (with a @samp{\}) or quoted to protect them from expansion by the shell.
1655 The command is executed in the directory in which @code{find} was run.
1657 For example, to compare each C header file in the current directory with
1658 the file @file{/tmp/master}:
1660 @example
1661 find . -name '*.h' -execdir diff -u '@{@}' /tmp/master ';'
1662 @end example
1663 @end deffn
1666 Another similar option, @samp{-exec} is supported, but is less secure.
1667 @xref{Security Considerations}, for a discussion of the security
1668 problems surrounding @samp{-exec}.
1671 @deffn Action -exec command ;
1672 This insecure variant of the @samp{-execdir} action is specified by
1673 POSIX.  The main difference is that the command is executed in the
1674 directory from which @code{find} was invoked, meaning that @samp{@{@}}
1675 is expanded to a relative path starting with the name of one of the
1676 starting directories, rather than just the basename of the matched
1677 file.
1678 @end deffn
1681 @node Multiple Files
1682 @subsection Multiple Files
1684 Sometimes you need to process files one of the time.  But usually this
1685 is not necessary, and, it is faster to run a command on as many files
1686 as possible at a time, rather than once per file.  Doing this saves on
1687 the time it takes to start up the command each time.
1689 The @samp{-execdir} and @samp{-exec} actions have variants that build
1690 command lines containing as many matched files as possible.  
1692 @deffn Action -execdir command @{@} +
1693 This works as for @samp{-execdir command ;}, except that the
1694 @samp{@{@}} at the end of the command is expanded to a list of names
1695 of matching files.  This expansion is done in such a way as to avoid
1696 exceeding the maximum command line length available on the system.
1697 Only one @samp{@{@}} is allowed within the command, and it must appear
1698 at the end, immediately before the @samp{+}.  A @samp{+} appearing in
1699 any position other than immediately after @samp{@{@}} is not
1700 considered to be special (that is, it does not terminate the command).
1701 @end deffn
1704 @deffn Action -exec command @{@} +
1705 This insecure variant of the @samp{-execdir} action is specified by
1706 POSIX.  The main difference is that the command is executed in the
1707 directory from which @code{find} was invoked, meaning that @samp{@{@}}
1708 is expanded to a relative path starting with the name of one of the
1709 starting directories, rather than just the basename of the matched
1710 file.
1711 @end deffn
1713 Before @code{find} exits, any partially-built command lines are
1714 executed.  This happens even if the exit was caused by the
1715 @samp{-quit} action.  However, some types of error (for example not
1716 being able to invoke @code{stat()} on the current directory) can cause
1717 an immediate fatal exit.  In this situation, any partially-built
1718 command lines will not be invoked (this prevents possible infinite
1719 loops).
1721 Another, but less secure, way to run a command on more than one file
1722 at once, is to use the @code{xargs} command, which is invoked like this:
1724 @example
1725 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
1726 @end example
1728 @code{xargs} normally reads arguments from the standard input.  These
1729 arguments are delimited by blanks (which can be protected with double
1730 or single quotes or a backslash) or newlines.  It executes the
1731 @var{command} (default is @file{/bin/echo}) one or more times with any
1732 @var{initial-arguments} followed by arguments read from standard
1733 input.  Blank lines on the standard input are ignored.
1735 Instead of blank-delimited names, it is safer to use @samp{find -print0}
1736 or @samp{find -fprint0} and process the output by giving the @samp{-0}
1737 or @samp{--null} option to GNU @code{xargs}, GNU @code{tar}, GNU
1738 @code{cpio}, or @code{perl}.  The @code{locate} command also has a
1739 @samp{-0} or @samp{--null} option which does the same thing.
1741 You can use shell command substitution (backquotes) to process a list of
1742 arguments, like this:
1744 @example
1745 grep -l sprintf `find $HOME -name '*.c' -print`
1746 @end example
1748 However, that method produces an error if the length of the @samp{.c}
1749 file names exceeds the operating system's command-line length limit.
1750 @code{xargs} avoids that problem by running the command as many times as
1751 necessary without exceeding the limit:
1753 @example
1754 find $HOME -name '*.c' -print | xargs grep -l sprintf
1755 @end example
1757 However, if the command needs to have its standard input be a terminal
1758 (@code{less}, for example), you have to use the shell command
1759 substitution method or use the @samp{--arg-file} option of
1760 @code{xargs}.
1762 The @code{xargs} command will process all its input, building command
1763 lines and executing them, unless one of the commands exits with a
1764 status of 255 (this will cause xargs to issue an error message and
1765 stop) or it reads a line contains the end of file string specified
1766 with the @samp{--eof} option. 
1768 @menu
1769 * Unsafe File Name Handling::
1770 * Safe File Name Handling::
1771 * Unusual Characters in File Names::
1772 * Limiting Command Size::
1773 * Interspersing File Names::
1774 @end menu
1776 @node Unsafe File Name Handling
1777 @subsubsection Unsafe File Name Handling
1779 Because file names can contain quotes, backslashes, blank characters,
1780 and even newlines, it is not safe to process them using @code{xargs} in its
1781 default mode of operation.  But since most files' names do not contain
1782 blanks, this problem occurs only infrequently.  If you are only
1783 searching through files that you know have safe names, then you need not
1784 be concerned about it.  
1786 @c This example is adapted from:
1787 @c From: pfalstad@stone.Princeton.EDU (Paul John Falstad)
1788 @c Newsgroups: comp.unix.shell
1789 @c Subject: Re: Beware xargs security holes
1790 @c Date: 16 Oct 90 19:12:06 GMT
1791 @c 
1792 In many applications, if @code{xargs} botches processing a file because
1793 its name contains special characters, some data might be lost.  The
1794 importance of this problem depends on the importance of the data and
1795 whether anyone notices the loss soon enough to correct it.  However,
1796 here is an extreme example of the problems that using blank-delimited
1797 names can cause.  If the following command is run daily from
1798 @code{cron}, then any user can remove any file on the system:
1800 @example
1801 find / -name '#*' -atime +7 -print | xargs rm
1802 @end example
1804 For example, you could do something like this:
1806 @example
1807 eg$ echo > '#
1808 vmunix'
1809 @end example
1811 @noindent
1812 and then @code{cron} would delete @file{/vmunix}, if it ran
1813 @code{xargs} with @file{/} as its current directory.
1815 To delete other files, for example @file{/u/joeuser/.plan}, you could do
1816 this:
1818 @example
1819 eg$ mkdir '#
1821 eg$ cd '#
1823 eg$ mkdir u u/joeuser u/joeuser/.plan'
1825 eg$ echo > u/joeuser/.plan'
1826 /#foo'
1827 eg$ cd ..
1828 eg$ find . -name '#*' -print | xargs echo
1829 ./# ./# /u/joeuser/.plan /#foo
1830 @end example
1832 @node Safe File Name Handling
1833 @subsubsection Safe File Name Handling
1835 Here is how to make @code{find} output file names so that they can be
1836 used by other programs without being mangled or misinterpreted.  You can
1837 process file names generated this way by giving the @samp{-0} or
1838 @samp{--null} option to GNU @code{xargs}, GNU @code{tar}, GNU
1839 @code{cpio}, or @code{perl}.
1841 @deffn Action -print0
1842 True; print the full file name on the standard output, followed by a
1843 null character.
1844 @end deffn
1846 @deffn Action -fprint0 file
1847 True; like @samp{-print0} but write to @var{file} like @samp{-fprint}
1848 (@pxref{Print File Name}).
1849 @end deffn
1851 As of findutils version 4.2.4, the @code{locate} program also has a
1852 @samp{--null} option which does the same thing.  For similarity with
1853 @code{xargs}, the short form of the option @samp{-0} can also be used.
1855 If you want to be able to handle file names safely but need to run
1856 commands which want to be connected to a terminal on their input, you
1857 can use the @samp{--arg-file} option to @code{xargs} like this:
1859 @example
1860 find / -name xyzzy -print0 > list
1861 xargs --null --arg-file=list munge
1862 @end example
1864 The example above runs the @code{munge} program on all the files named
1865 @file{xyzzy} that we can find, but @code{munge}'s input will still be
1866 the terminal (or whatever the shell was using as standard input).  If
1867 your shell has the ``process substitution'' feature @samp{<(...)}, you
1868 can do this in just one step:
1870 @example
1871 xargs --null --arg-file=<(find / -name xyzzy -print0) munge
1872 @end example
1874 @node Unusual Characters in File Names
1875 @subsubsection Unusual Characters in File Names
1876 As discussed above, you often need to be careful about how the names
1877 of files are handled by @code{find} and other programs.  If the output
1878 of @code{find} is not going to another program but instead is being
1879 shown on a terminal, this can still be a problem.  For example, some
1880 character sequences can reprogram the function keys on some terminals.
1881 @xref{Security Considerations}, for a discussion of other security
1882 problems relating to @code{find}.
1884 Unusual characters are handled differently by various
1885 actions, as described below.
1887 @table @samp
1888 @item -print0
1889 @itemx -fprint0
1890 Always print the exact filename, unchanged, even if the output is
1891 going to a terminal.
1892 @item -ok
1893 @itemx -okdir
1894 Always print the exact filename, unchanged.  This will probably change
1895 in a future release.
1896 @item -ls
1897 @itemx -fls
1898 Unusual characters are always escaped.  White space, backslash, and
1899 double quote characters are printed using C-style escaping (for
1900 example @samp{\f}, @samp{\"}).  Other unusual characters are printed
1901 using an octal escape.  Other Printable characters (for @samp{-ls} and
1902 @samp{-fls} these are the characters between octal 041 and 0176) are
1903 printed as-is.
1904 @item -printf
1905 @itemx -fprintf
1906 If the output is not going to a terminal, it is printed as-is.
1907 Otherwise, the result depends on which directive is in use:
1909 @table @asis
1910 @item %D, %F, %H, %Y, %y 
1911 These expand to values which are not under control of files' ownwers,
1912 and so are printed as-is.
1913 @item  %a, %b, %c, %d, %g, %G, %i, %k, %m, %M, %n, %s, %t, %u, %U
1914 These have values which are under the control of files' ownwers but which
1915 cannot be used to send arbitrary data to the terminal, and so these
1916 are printed as-is.  
1917 @item %f, %h, %l, %p, %P 
1918 The output of these directives is quoted if the output is going to a
1919 terminal.
1921 This quoting is performed in the same way as for GNU @code{ls}.   
1922 This is not the same quoting mechanism as the one used for  @samp{-ls} and
1923 @samp{fls}.   If you are able to decide what format to use for the output 
1924 of @code{find} then it is normally better to use @samp{\0} as a terminator
1925 than to use newline, as file names can contain white space and newline
1926 characters.
1927 @end table 
1928 @item -print
1929 @itemx -fprint
1930 Quoting is handled in the same way as for the @samp{%p} directive
1931 of @samp{-printf} and @samp{-fprintf}.  If you are using @code{find} in
1932 a script or in a situation where the matched files might have
1933 arbitrary names, you should consider using @samp{-print0} instead of
1934 @samp{-print}.
1935 @end table
1938 The @code{locate} program quotes and escapes unusual characters in
1939 file names in the same way as @code{find}'s @samp{-print} action.
1941 The behaviours described above may change soon, as the treatment of
1942 unprintable characters is harmonised for @samp{-ls}, @samp{-fls},
1943 @samp{-print}, @samp{-fprint}, @samp{-printf} and @samp{-fprintf}.
1945 @node Limiting Command Size
1946 @subsubsection Limiting Command Size
1948 @code{xargs} gives you control over how many arguments it passes to the
1949 command each time it executes it.  By default, it uses up to
1950 @code{ARG_MAX} - 2k, or 128k, whichever is smaller, characters per
1951 command.  It uses as many lines and arguments as fit within that limit.
1952 The following options modify those values.
1954 @table @code
1955 @item --no-run-if-empty
1956 @itemx -r
1957 If the standard input does not contain any nonblanks, do not run the
1958 command.  By default, the command is run once even if there is no input.
1960 @item --max-lines@r{[}=@var{max-lines}@r{]}
1961 @itemx -l@r{[}@var{max-lines}@r{]}
1962 Use at most @var{max-lines} nonblank input lines per command line;
1963 @var{max-lines} defaults to 1 if omitted.  Trailing blanks cause an
1964 input line to be logically continued on the next input line, for the
1965 purpose of counting the lines.  Implies @samp{-x}.
1967 @item --max-args=@var{max-args}
1968 @itemx -n @var{max-args}
1969 Use at most @var{max-args} arguments per command line.  Fewer than
1970 @var{max-args} arguments will be used if the size (see the @samp{-s}
1971 option) is exceeded, unless the @samp{-x} option is given, in which case
1972 @code{xargs} will exit.
1974 @item --max-chars=@var{max-chars}
1975 @itemx -s @var{max-chars}
1976 Use at most @var{max-chars} characters per command line, including the
1977 command and initial arguments and the terminating nulls at the ends of
1978 the argument strings.  If you specify a value for this option which is
1979 too large or small, a warning message is printed and the appropriate
1980 upper or lower limit is used instead.
1982 @item --max-procs=@var{max-procs}
1983 @itemx -P @var{max-procs}
1984 Run up to @var{max-procs} processes at a time; the default is 1.  If
1985 @var{max-procs} is 0, @code{xargs} will run as many processes as
1986 possible at a time.  Use the @samp{-n}, @samp{-s}, or @samp{-l} option
1987 with @samp{-P}; otherwise chances are that the command will be run only
1988 once.
1989 @end table
1991 @node Interspersing File Names
1992 @subsubsection Interspersing File Names
1994 @code{xargs} can insert the name of the file it is processing between
1995 arguments you give for the command.  Unless you also give options to
1996 limit the command size (@pxref{Limiting Command Size}), this mode of
1997 operation is equivalent to @samp{find -exec} (@pxref{Single File}).
1999 @table @code
2000 @item --replace@r{[}=@var{replace-str}@r{]}
2001 @itemx -i@r{[}@var{replace-str}@r{]}
2002 Replace occurrences of @var{replace-str} in the initial arguments with
2003 names read from the input.  Also, unquoted blanks do not
2004 terminate arguments; instead, the input is split at newlines only.  If
2005 @var{replace-str} is omitted, it defaults to @samp{@{@}} (like for
2006 @samp{find -exec}).  Implies @samp{-x} and @samp{-l 1}.  As an
2007 example, to sort each file the @file{bills} directory, leaving the
2008 output in that file name with @file{.sorted} appended, you could do:
2010 @example
2011 find bills -type f | xargs -iXX sort -o XX.sorted XX
2012 @end example
2014 @noindent
2015 The equivalent command using @samp{find -exec} is:
2017 @example
2018 find bills -type f -exec sort -o '@{@}.sorted' '@{@}' ';'
2019 @end example
2020 @end table
2022 @node Querying
2023 @subsection Querying
2025 To ask the user whether to execute a command on a single file, you can
2026 use the @code{find} primary @samp{-ok} instead of @samp{-exec}:
2028 @deffn Action -ok command ;
2029 Like @samp{-exec} (@pxref{Single File}), but ask the user first (on
2030 the standard input); if the response does not start with @samp{y} or
2031 @samp{Y}, do not run the command, and return false.
2032 @end deffn
2034 When processing multiple files with a single command, to query the user
2035 you give @code{xargs} the following option.  When using this option, you
2036 might find it useful to control the number of files processed per
2037 invocation of the command (@pxref{Limiting Command Size}).
2039 @table @code
2040 @item --interactive
2041 @itemx -p
2042 Prompt the user about whether to run each command line and read a line
2043 from the terminal.  Only run the command line if the response starts
2044 with @samp{y} or @samp{Y}.  Implies @samp{-t}.
2045 @end table
2047 @node Delete Files
2048 @section Delete Files
2050 @deffn Action -delete
2051 Delete files or directories; true if removal succeeded.  If the
2052 removal failed, an error message is issued.
2054 The use of the @samp{-delete} action on the command line automatically
2055 turns on the @samp{-depth} option (@pxref{find Expressions}).
2056 @end deffn
2058 @node Adding Tests
2059 @section Adding Tests
2061 You can test for file attributes that none of the @code{find} builtin
2062 tests check.  To do this, use @code{xargs} to run a program that filters
2063 a list of files printed by @code{find}.  If possible, use @code{find}
2064 builtin tests to pare down the list, so the program run by @code{xargs}
2065 has less work to do.  The tests builtin to @code{find} will likely run
2066 faster than tests that other programs perform.
2068 For reasons of efficiency it is often useful to limit the number of
2069 times an external program has to be run.  For this reason, it is often
2070 a good idea to implement ``extended'' tests by using @code{xargs}.
2072 For example, here is a way to print the names of all of the unstripped
2073 binaries in the @file{/usr/local} directory tree.  Builtin tests avoid
2074 running @code{file} on files that are not regular files or are not
2075 executable.
2077 @example
2078 find /usr/local -type f -perm /a=x | xargs file | 
2079   grep 'not stripped' | cut -d: -f1
2080 @end example
2082 @noindent
2083 The @code{cut} program removes everything after the file name from the
2084 output of @code{file}.
2086 However, using @code{xargs} can present important security problems
2087 (@pxref{Security Considerations}).  These can be avoided by using
2088 @samp{-execdir}.  The @samp{-execdir} action is also a useful way of
2089 putting your own test in the middle of a set of other tests or actions
2090 for @code{find} (for example, you might want to use @samp{-prune}).
2092 @c Idea from Martin Weitzel.
2093 To place a special test somewhere in the middle of a @code{find}
2094 expression, you can use @samp{-execdir} (or, less securely,
2095 @samp{-exec}) to run a program that performs the test.  Because
2096 @samp{-execdir} evaluates to the exit status of the executed program,
2097 you can use a program (which can be a shell script) that tests for a
2098 special attribute and make it exit with a true (zero) or false
2099 (non-zero) status.  It is a good idea to place such a special test
2100 @emph{after} the builtin tests, because it starts a new process which
2101 could be avoided if a builtin test evaluates to false.  
2103 Here is a shell script called @code{unstripped} that checks whether its
2104 argument is an unstripped binary file:
2106 @example
2107 #! /bin/sh
2108 file "$1" | grep -q "not stripped"
2109 @end example
2112 This script relies on the fact that the shell exits with the status of
2113 the last command in the pipeline, in this case @code{grep}.  The
2114 @code{grep} command exits with a true status if it found any matches,
2115 false if not.  Here is an example of using the script (assuming it is
2116 in your search path).  It lists the stripped executables (and shell
2117 scripts) in the file @file{sbins} and the unstripped ones in
2118 @file{ubins}.
2120 @example
2121 find /usr/local -type f -perm /a=x \
2122   \( -execdir unstripped '@{@}' \; -fprint ubins -o -fprint sbins \)
2123 @end example
2127 @node Common Tasks, Databases, Actions, Top
2128 @chapter Common Tasks
2130 The sections that follow contain some extended examples that both give a
2131 good idea of the power of these programs, and show you how to solve
2132 common real-world problems.
2134 @menu
2135 * Viewing And Editing::
2136 * Archiving::
2137 * Cleaning Up::
2138 * Strange File Names::
2139 * Fixing Permissions::
2140 * Classifying Files::
2141 @end menu
2143 @node Viewing And Editing
2144 @section Viewing And Editing
2146 To view a list of files that meet certain criteria, simply run your file
2147 viewing program with the file names as arguments.  Shells substitute a
2148 command enclosed in backquotes with its output, so the whole command
2149 looks like this:
2151 @example
2152 less `find /usr/include -name '*.h' | xargs grep -l mode_t`
2153 @end example
2155 @noindent
2156 You can edit those files by giving an editor name instead of a file
2157 viewing program:
2159 @example
2160 emacs `find /usr/include -name '*.h' | xargs grep -l mode_t`
2161 @end example
2163 Because there is a limit to the length of any individual command line,
2164 there is a limit to the number of files that can be handled in this
2165 way.  We can get around this difficulty by using xargs like this:
2167 @example
2168 find /usr/include -name '*.h' | xargs grep -l mode_t > todo
2169 xargs --arg-file=todo emacs
2170 @end example
2172 Here, @code{xargs} will run @code{emacs} as many times as necessary to
2173 visit all of the files listed in the file @file{todo}.
2175 @node Archiving
2176 @section Archiving
2178 You can pass a list of files produced by @code{find} to a file archiving
2179 program.  GNU @code{tar} and @code{cpio} can both read lists of file
2180 names from the standard input---either delimited by nulls (the safe way)
2181 or by blanks (the lazy, risky default way).  To use null-delimited
2182 names, give them the @samp{--null} option.  You can store a file archive
2183 in a file, write it on a tape, or send it over a network to extract on
2184 another machine.
2186 One common use of @code{find} to archive files is to send a list of the
2187 files in a directory tree to @code{cpio}.  Use @samp{-depth} so if a
2188 directory does not have write permission for its owner, its contents can
2189 still be restored from the archive since the directory's permissions are
2190 restored after its contents.  Here is an example of doing this using
2191 @code{cpio}; you could use a more complex @code{find} expression to
2192 archive only certain files.
2194 @example
2195 find . -depth -print0 |
2196   cpio --create --null --format=crc --file=/dev/nrst0
2197 @end example
2199 You could restore that archive using this command:
2201 @example
2202 cpio --extract --null --make-dir --unconditional \
2203   --preserve --file=/dev/nrst0
2204 @end example
2206 Here are the commands to do the same things using @code{tar}:
2208 @example
2209 find . -depth -print0 |
2210   tar --create --null --files-from=- --file=/dev/nrst0
2212 tar --extract --null --preserve-perm --same-owner \
2213   --file=/dev/nrst0
2214 @end example
2216 @c Idea from Rick Sladkey.
2217 Here is an example of copying a directory from one machine to another:
2219 @example
2220 find . -depth -print0 | cpio -0o -Hnewc |
2221   rsh @var{other-machine} "cd `pwd` && cpio -i0dum"
2222 @end example
2224 @node Cleaning Up
2225 @section Cleaning Up
2227 @c Idea from Jim Meyering.
2228 This section gives examples of removing unwanted files in various situations.
2229 Here is a command to remove the CVS backup files created when an update
2230 requires a merge:
2232 @example
2233 find . -name '.#*' -print0 | xargs -0r rm -f
2234 @end example
2236 The command above works, but the following is safer:
2238 @example
2239 find . -name '.#*' -depth -delete
2240 @end example
2242 @c Idea from Franc,ois Pinard.
2243 You can run this command to clean out your clutter in @file{/tmp}.  You
2244 might place it in the file your shell runs when you log out
2245 (@file{.bash_logout}, @file{.logout}, or @file{.zlogout}, depending on
2246 which shell you use).
2248 @example
2249 find /tmp -depth -user "$LOGNAME" -type f -delete 
2250 @end example
2252 If your @code{find} command removes directories, you may find that 
2253 you get a spurious error message when @code{find} tries to recurse
2254 into a directory that has now been removed.  Using the @samp{-depth}
2255 option will normally resolve this problem.
2257 @c Idea from Noah Friedman.
2258 To remove old Emacs backup and auto-save files, you can use a command
2259 like the following.  It is especially important in this case to use
2260 null-terminated file names because Emacs packages like the VM mailer
2261 often create temporary file names with spaces in them, like @file{#reply
2262 to David J. MacKenzie<1>#}.
2264 @example
2265 find ~ \( -name '*~' -o -name '#*#' \) -print0 |
2266   xargs --no-run-if-empty --null rm -vf
2267 @end example
2269 Removing old files from @file{/tmp} is commonly done from @code{cron}:
2271 @c Idea from Kaveh Ghazi.
2272 @example
2273 find /tmp /var/tmp -not -type d -mtime +3 -delete
2274 find /tmp /var/tmp -depth -mindepth 1 -type d -empty -delete
2275 @end example
2277 The second @code{find} command above uses @samp{-depth} so it cleans out
2278 empty directories depth-first, hoping that the parents become empty and
2279 can be removed too.  It uses @samp{-mindepth} to avoid removing
2280 @file{/tmp} itself if it becomes totally empty.
2282 @node Strange File Names
2283 @section Strange File Names
2285 @c Idea from:
2286 @c From: tmatimar@isgtec.com (Ted Timar)
2287 @c Newsgroups: comp.unix.questions,comp.unix.shell,comp.answers,news.answers
2288 @c Subject: Unix - Frequently Asked Questions (2/7) [Frequent posting]
2289 @c Subject: How do I remove a file with funny characters in the filename ?
2290 @c Date: Thu Mar 18 17:16:55 EST 1993
2291 @code{find} can help you remove or rename a file with strange characters
2292 in its name.  People are sometimes stymied by files whose names contain
2293 characters such as spaces, tabs, control characters, or characters with
2294 the high bit set.  The simplest way to remove such files is:
2296 @example
2297 rm -i @var{some*pattern*that*matches*the*problem*file}
2298 @end example
2300 @code{rm} asks you whether to remove each file matching the given
2301 pattern.  If you are using an old shell, this approach might not work if
2302 the file name contains a character with the high bit set; the shell may
2303 strip it off.  A more reliable way is:
2305 @example
2306 find . -maxdepth 1 @var{tests} -ok rm '@{@}' \;
2307 @end example
2309 @noindent
2310 where @var{tests} uniquely identify the file.  The @samp{-maxdepth 1}
2311 option prevents @code{find} from wasting time searching for the file in
2312 any subdirectories; if there are no subdirectories, you may omit it.  A
2313 good way to uniquely identify the problem file is to figure out its
2314 inode number; use
2316 @example
2317 ls -i
2318 @end example
2320 Suppose you have a file whose name contains control characters, and you
2321 have found that its inode number is 12345.  This command prompts you for
2322 whether to remove it:
2324 @example
2325 find . -maxdepth 1 -inum 12345 -ok rm -f '@{@}' \;
2326 @end example
2328 If you don't want to be asked, perhaps because the file name may contain
2329 a strange character sequence that will mess up your screen when printed,
2330 then use @samp{-exec} instead of @samp{-ok}.
2332 If you want to rename the file instead, you can use @code{mv} instead of
2333 @code{rm}:
2335 @example
2336 find . -maxdepth 1 -inum 12345 -ok mv '@{@}' @var{new-file-name} \;
2337 @end example
2339 @node Fixing Permissions
2340 @section Fixing Permissions
2342 Suppose you want to make sure that everyone can write to the directories in a
2343 certain directory tree.  Here is a way to find directories lacking either
2344 user or group write permission (or both), and fix their permissions:
2346 @example
2347 find . -type d -not -perm -ug=w | xargs chmod ug+w
2348 @end example
2350 @noindent
2351 You could also reverse the operations, if you want to make sure that
2352 directories do @emph{not} have world write permission.
2354 @node Classifying Files
2355 @section Classifying Files
2357 @c Idea from:
2358 @c From: martin@mwtech.UUCP (Martin Weitzel)
2359 @c Newsgroups: comp.unix.wizards,comp.unix.questions
2360 @c Subject: Advanced usage of 'find' (Re: Unix security automating script)
2361 @c Date: 22 Mar 90 15:05:19 GMT
2362 If you want to classify a set of files into several groups based on
2363 different criteria, you can use the comma operator to perform multiple
2364 independent tests on the files.  Here is an example:
2366 @example
2367 find / -type d \( -perm -o=w -fprint allwrite , \
2368   -perm -o=x -fprint allexec \)
2370 echo "Directories that can be written to by everyone:"
2371 cat allwrite
2372 echo ""
2373 echo "Directories with search permissions for everyone:"
2374 cat allexec
2375 @end example
2377 @code{find} has only to make one scan through the directory tree (which
2378 is one of the most time consuming parts of its work).
2380 @node Databases, File Permissions, Common Tasks, Top
2381 @chapter File Name Databases
2383 The file name databases used by @code{locate} contain lists of files
2384 that were in particular directory trees when the databases were last
2385 updated.  The file name of the default database is determined when
2386 @code{locate} and @code{updatedb} are configured and installed.  The
2387 frequency with which the databases are updated and the directories for
2388 which they contain entries depend on how often @code{updatedb} is run,
2389 and with which arguments.
2391 You can obtain some statistics about the databases by using
2392 @samp{locate --statistics}.
2394 @menu
2395 * Database Locations::
2396 * Database Formats::
2397 * Newline Handling::
2398 @end menu
2401 @node Database Locations
2402 @section Database Locations
2404 There can be multiple file name databases.  Users can select which
2405 databases @code{locate} searches using the @code{LOCATE_PATH}
2406 environment variable or a command line option.  The system
2407 administrator can choose the file name of the default database, the
2408 frequency with which the databases are updated, and the directories
2409 for which they contain entries.  File name databases are updated by
2410 running the @code{updatedb} program, typically nightly.
2412 In networked environments, it often makes sense to build a database at
2413 the root of each filesystem, containing the entries for that filesystem.
2414 @code{updatedb} is then run for each filesystem on the fileserver where
2415 that filesystem is on a local disk, to prevent thrashing the network.
2417 @xref{Invoking updatedb},
2418 for the description of the options to @code{updatedb}, which specify
2419 which directories would each database contain entries for.
2422 @node Database Formats
2423 @section Database Formats
2425 The file name databases contain lists of files that were in particular
2426 directory trees when the databases were last updated.  The file name
2427 database format changed starting with GNU @code{locate} version 4.0 to
2428 allow machines with different byte orderings to share the databases.  The
2429 new GNU @code{locate} can read both the old and new database formats.
2430 However, old versions of @code{locate} and @code{find} produce incorrect
2431 results if given a new-format database.
2433 If you run @samp{locate --statistics}, the resulting summary indicates
2434 the type of each locate database.  
2437 @menu
2438 * New Database Format::
2439 * Sample Database::
2440 * Old Database Format::
2441 @end menu
2443 @node New Database Format
2444 @subsection New Database Format
2446 @code{updatedb} runs a program called @code{frcode} to
2447 @dfn{front-compress} the list of file names, which reduces the database
2448 size by a factor of 4 to 5.  Front-compression (also known as
2449 incremental encoding) works as follows.
2451 The database entries are a sorted list (case-insensitively, for users'
2452 convenience).  Since the list is sorted, each entry is likely to share a
2453 prefix (initial string) with the previous entry.  Each database entry
2454 begins with an offset-differential count byte, which is the additional
2455 number of characters of prefix of the preceding entry to use beyond the
2456 number that the preceding entry is using of its predecessor.  (The
2457 counts can be negative.)  Following the count is a null-terminated ASCII
2458 remainder---the part of the name that follows the shared prefix.
2460 If the offset-differential count is larger than can be stored in a byte
2461 (+/-127), the byte has the value 0x80 and the count follows in a 2-byte
2462 word, with the high byte first (network byte order).
2464 Every database begins with a dummy entry for a file called
2465 @file{LOCATE02}, which @code{locate} checks for to ensure that the
2466 database file has the correct format; it ignores the entry in doing the
2467 search.
2469 Databases can not be concatenated together, even if the first (dummy)
2470 entry is trimmed from all but the first database.  This is because the
2471 offset-differential count in the first entry of the second and following
2472 databases will be wrong.
2474 In the output of @samp{locate --statistics}, the new database format
2475 is referred to as @samp{LOCATE02}.
2477 @node Sample Database
2478 @subsection Sample Database
2480 Sample input to @code{frcode}:
2481 @c with nulls changed to newlines:
2483 @example
2484 /usr/src
2485 /usr/src/cmd/aardvark.c
2486 /usr/src/cmd/armadillo.c
2487 /usr/tmp/zoo
2488 @end example
2490 Length of the longest prefix of the preceding entry to share:
2492 @example
2493 0 /usr/src
2494 8 /cmd/aardvark.c
2495 14 rmadillo.c
2496 5 tmp/zoo
2497 @end example
2499 Output from @code{frcode}, with trailing nulls changed to newlines
2500 and count bytes made printable:
2502 @example
2503 0 LOCATE02
2504 0 /usr/src
2505 8 /cmd/aardvark.c
2506 6 rmadillo.c
2507 -9 tmp/zoo
2508 @end example
2510 (6 = 14 - 8, and -9 = 5 - 14)
2512 @node Old Database Format
2513 @subsection Old Database Format
2515 The old database format is used by Unix @code{locate} and @code{find}
2516 programs and earlier releases of the GNU ones.  @code{updatedb} produces
2517 this format if given the @samp{--old-format} option.
2519 @code{updatedb} runs programs called @code{bigram} and @code{code} to
2520 produce old-format databases.  The old format differs from the new one
2521 in the following ways.  Instead of each entry starting with an
2522 offset-differential count byte and ending with a null, byte values from
2523 0 through 28 indicate offset-differential counts from -14 through 14.
2524 The byte value indicating that a long offset-differential count follows
2525 is 0x1e (30), not 0x80.  The long counts are stored in host byte order,
2526 which is not necessarily network byte order, and host integer word size,
2527 which is usually 4 bytes.  They also represent a count 14 less than
2528 their value.  The database lines have no termination byte; the start of
2529 the next line is indicated by its first byte having a value <= 30.
2531 In addition, instead of starting with a dummy entry, the old database
2532 format starts with a 256 byte table containing the 128 most common
2533 bigrams in the file list.  A bigram is a pair of adjacent bytes.  Bytes
2534 in the database that have the high bit set are indexes (with the high
2535 bit cleared) into the bigram table.  The bigram and offset-differential
2536 count coding makes these databases 20-25% smaller than the new format,
2537 but makes them not 8-bit clean.  Any byte in a file name that is in the
2538 ranges used for the special codes is replaced in the database by a
2539 question mark, which not coincidentally is the shell wildcard to match a
2540 single character.
2542 The old format therefore can not faithfully store entries with non-ASCII
2543 characters. It therefore should not be used in internationalized
2544 environments.
2546 The output of @samp{locate --statistics} will give an incorrect count
2547 of the number of filenames containing newlines or high-bit characters
2548 for old-format databases.
2550 @node Newline Handling
2551 @section Newline Handling
2553 Within the database, filenames are terminated with a null character.
2554 This is the case for both the old and the new format.  
2556 When the new database format is being used, the compression technique
2557 used to generate the database though relies on the ability to sort the
2558 list of files before they are presented to @code{frcode}.
2560 If the system's sort command allows its input list of files to be
2561 separated with null characters via the @samp{-z} option, this option
2562 is used and therefore @code{updatedb} and @code{locate} will both
2563 correctly handle filenames containing newlines.  If the @code{sort}
2564 command lacks support for this, the list of files is delimited with
2565 the newline character, meaning that parts of filenames containing
2566 newlines will be incorrectly sorted.  This can result in both
2567 incorrect matches and incorrect failures to match.
2569 On the other hand, if you are using the old database format, filenames
2570 with embedded newlines are not correctly handled.  There is no
2571 technical limitation which enforces this, it's just that the
2572 @code{bigram} program has no been updated to support lists of
2573 filenames separated by nulls.
2575 So, if you are using the new database format (this is the default) and
2576 your system uses GNU @code{find}, newlines will be correctly handled
2577 at all times.  Otherwise, newlines may not be correctly handled.
2579 @node File Permissions, Reference, Databases, Top
2580 @chapter File Permissions
2582 @include perm.texi
2584 @node Reference, Security Considerations, File Permissions, Top
2585 @chapter Reference
2587 Below are summaries of the command line syntax for the programs
2588 discussed in this manual.
2590 @menu
2591 * Invoking find::
2592 * Invoking locate::
2593 * Invoking updatedb::
2594 * Invoking xargs::
2595 @end menu
2597 @node Invoking find, Invoking locate, , Reference
2598 @section Invoking @code{find}
2600 @example
2601 find @r{[-H] [-L] [-P]} @r{[}@var{file}@dots{}@r{]} @r{[}@var{expression}@r{]}
2602 @end example
2604 @code{find} searches the directory tree rooted at each file name
2605 @var{file} by evaluating the @var{expression} on each file it finds in
2606 the tree.
2608 The options @samp{-H}, @samp{-L} or @samp{-P} may be specified at the
2609 start of the command line (if none of these is specified, @samp{-P} is
2610 assumed).  The arguments after these are a list of files or
2611 directories that should be searched.
2613 This list of files to search is followed by a list of expressions
2614 describing the files we wish to search for.  The first part of the
2615 expression is recognised by the fact that it begins with @samp{-},
2616 @samp{(}, @samp{)}, @samp{,}, or @samp{!}.  Any arguments after it are
2617 the rest of the expression.  If no paths are given, the current
2618 directory is used.  If no expression is given, the expression
2619 @samp{-print} is used.
2621 @code{find} exits with status 0 if all files are processed successfully,
2622 greater than 0 if errors occur.
2624 Three options can precede the list of path names.  They determine the
2625 way that symbolic links are handled.
2627 @table @code
2628 @item -P
2629 Never follow symbolic links (this is the default), except in the case
2630 of the @samp{-xtype} predicate.
2631 @item -L
2632 Always follow symbolic links, except in the case of the @samp{-xtype}
2633 predicate.
2634 @item -H
2635 Follow symbolic links specified in the list of paths to search, or
2636 which are otherwise specified on the command line.
2637 @end table
2639 If @code{find} would follow a symbolic link, but cannot for any reason
2640 (for example, because it has insufficient permissions or the link is
2641 broken), it falls back on using the properties of the symbolic link
2642 itself.  @ref{Symbolic Links} for a more complete description of how
2643 symbolic links are handled.
2645 @xref{Primary Index}, for a summary of all of the tests, actions, and
2646 options that the expression can contain.  If the expression is
2647 missing, @samp{-print} is assumed.
2651 @code{find} also recognizes two options for administrative use:
2653 @table @code
2654 @item --help
2655 Print a summary of the command-line argument format and exit.
2656 @item --version
2657 Print the version number of @code{find} and exit.
2658 @end table
2661 @menu
2662 * Warning Messages::
2663 @end menu
2666 @node Warning Messages,,, Invoking find
2667 @subsection Warning Messages
2669 If there is an error on the @code{find} command line, an error message
2670 is normally issued.  However, there are some usages that are
2671 inadvisable but which @code{find} should still accept.  Under these
2672 circumstances, @code{find} may issue a warning message.  By default,
2673 warnings are enabled only if @code{find} is being run interactively
2674 (specifically, if the standard input is a terminal).  Warning messages
2675 can be controlled explicitly by the use of options on the command
2676 line:
2678 @table @code
2679 @item -warn
2680 Issue warning messages where appropriate.
2681 @item -nowarn
2682 Do not issue warning messages.
2683 @end table
2685 These options take effect at the point on the command line where they
2686 are specified.  Therefore if you specify @samp{-nowarn} at the end of
2687 the command line, you will not see warning messages for any problems
2688 occurring before that.  The warning messages affected by the above
2689 options are triggered by:
2691 @itemize @minus
2692 @item
2693 Use of the @samp{-d} option which is deprecated; please use
2694 @samp{-depth} instead, since the latter is POSIX-compliant.
2695 @item
2696 Use of the @samp{-ipath} option which is deprecated; please use
2697 @samp{-iwholename} instead.
2698 @item 
2699 Specifying an option (for example @samp{-mindepth}) after a non-option
2700 (for example @samp{-type} or @samp{-print}) on the command line.
2701 @end itemize
2704 The default behaviour above is designed to work in that way so that
2705 existing shell scripts which use such constructs don't generate
2706 spurious errors, but people will be made aware of the problem.
2708 Some warning messages are issued for less common or more serious
2709 problems, and so cannot be turned off:
2711 @itemize @minus
2712 @item
2713 Use of an unrecognised backslash escape sequence with @samp{-fprintf}
2714 @item
2715 Use of an unrecognised formatting directive with @samp{-fprintf}
2716 @end itemize
2718 @node Invoking locate, Invoking updatedb, Invoking find, Reference
2719 @section Invoking @code{locate}
2721 @example
2722 locate @r{[}@var{option}@dots{}@r{]} @var{pattern}@dots{}
2723 @end example
2725 @table @code
2726 @item --basename
2727 @itemx -b
2728 The specified pattern is matched against just the last component of
2729 the name of the file in the locate database.  This last component is
2730 also called the ``base name''.  For example, the base name of
2731 @file{/tmp/mystuff/foo.old.c} is @file{foo.old.c}.  If the pattern
2732 contains metacharacters, it must match the base name exactly.  If not,
2733 it must match part of the base name.
2735 @item --count
2736 @itemx -c
2737 Instead of printing the matched filenames, just print the total 
2738 number of matches we found, unless @samp{--print} (@samp{-p}) is also
2739 present.
2742 @item --database=@var{path}
2743 @itemx -d @var{path}
2744 Instead of searching the default file name database, search the file
2745 name databases in @var{path}, which is a colon-separated list of
2746 database file names.  You can also use the environment variable
2747 @code{LOCATE_PATH} to set the list of database files to search.  The
2748 option overrides the environment variable if both are used.  Empty
2749 elements in @var{path} (that is, a leading or trailing colon, or two
2750 colons in a row) are taken to stand for the default database.
2751 A database can be supplied on stdin, using @samp{-} as an element
2752 of @samp{path}. If more than one element of @samp{path} is @samp{-},
2753 later instances are ignored (but a warning message is printed).
2755 @item --existing
2756 @itemx -e
2757 Only print out such names which currently exist (instead of such names
2758 which existed when the database was created).  Note that this may slow
2759 down the program a lot, if there are many matches in the database.
2760 The way in which broken symbolic links are treated is affected by the
2761 @samp{-L}, @samp{-P} and @samp{-H} options.
2763 @item --non-existing
2764 @itemx -E
2765 Only print out such names which currently do not exist (instead of
2766 such names which existed when the database was created).  Note that
2767 this may slow down the program a lot, if there are many matches in the
2768 database.  The way in which broken symbolic links are treated is
2769 affected by the @samp{-L}, @samp{-P} and @samp{-H} options.
2771 @item --follow
2772 @itemx -L
2773 If testing for the existence of files (with the @samp{-e} or @samp{-E}
2774 options), consider broken symbolic links to be non-existing.  This is
2775 the default.
2778 @item --nofollow
2779 @itemx -P
2780 @itemx -H
2781 If testing for the existence of files (with the @samp{-e} or @samp{-E}
2782 options), treat broken symbolic links as if they were existing files.
2783 The @samp{-H} form of this option is provided purely for similarity
2784 with @code{find}; the use of @samp{-P} is recommended over @samp{-H}.
2786 @item --ignore-case
2787 @itemx -i
2788 Ignore case distinctions in both the pattern and the file names.
2790 @item --limit=N
2791 @itemx -l N
2792 Limit the number of results printed to N.  If you use the
2793 @samp{--count} option, the value printed will never be larger than
2794 this limit.
2796 @item --mmap
2797 @itemx -m
2798 Accepted but does nothing.  The option is supported only to provide
2799 compatibility with BSD's @code{locate}.
2801 @item --null
2802 @itemx -0
2803 Results are separated with the ASCII NUL character rather than the
2804 newline character.  To get the full benefit of the use of this option,
2805 use the new locate database format (that is the default anyway).
2807 @item --print
2808 @itemx -p
2809 Print search results when they normally would not, because of the presence
2810 of @samp{--statistics} (@samp{-S}) or @samp{--count} (@samp{-c}).
2812 @item --wholename
2813 @itemx -w
2814 The specified pattern is matched against the whole name of the file in
2815 the locate database.  If the pattern contains metacharacters, it must
2816 match exactly.  If not, it must match part of the whole file name.
2817 This is the default behaviour.
2819 @item --regex
2820 @itemx -r
2821 Instead of using substring or shell glob matching, the pattern
2822 specified on the command line is understood to be a POSIX extended
2823 regular expression.  Filenames from the locate database which match
2824 the specified regular expression are printed (or counted).  If the
2825 @samp{-i} flag is also given, matching is case-insensitive.  Matches
2826 are performed against the whole path name, and so by default a
2827 pathname will be matched if any part of it matches the specified
2828 regular expression.  The regular expression may use @samp{^} or
2829 @samp{$} to anchor a match at the beginning or end of a pathname.
2831 @item --stdio
2832 @itemx -s
2833 Accepted but does nothing.  The option is supported only to provide
2834 compatibility with BSD's @code{locate}.
2836 @item --statistics
2837 @itemx -S
2838 Print some summary information for each locate database.  No search is
2839 performed unless non-option arguments are given.
2841 @item --help
2842 Print a summary of the options to @code{locate} and exit.
2844 @item --version
2845 Print the version number of @code{locate} and exit.
2846 @end table
2848 @node Invoking updatedb, Invoking xargs, Invoking locate, Reference
2849 @section Invoking @code{updatedb}
2851 @example
2852 updatedb @r{[}@var{option}@dots{}@r{]}
2853 @end example
2855 @table @code
2856 @item --findoptions='@var{OPTION}@dots{}'
2857 Global options to pass on to @code{find}.
2858 The environment variable @code{FINDOPTIONS} also sets this value.
2859 Default is none.
2861 @item --localpaths='@var{path}@dots{}'
2862 Non-network directories to put in the database.
2863 Default is @file{/}.
2865 @item --netpaths='@var{path}@dots{}'
2866 Network (NFS, AFS, RFS, etc.) directories to put in the database.
2867 The environment variable @code{NETPATHS} also sets this value.
2868 Default is none.
2870 @item --prunepaths='@var{path}@dots{}'
2871 Directories to omit from the database, which would otherwise be included.
2872 The environment variable @code{PRUNEPATHS} also sets this value.
2873 Default is @file{/tmp /usr/tmp /var/tmp /afs}.
2875 @item --prunefs='@var{path}@dots{}'
2876 File systems to omit from the database, which would otherwise be included.
2877 Note that files are pruned when a file system is reached;
2878 Any file system mounted under an undesired file system will be
2879 ignored.
2880 The environment variable @code{PRUNEFS} also sets this value.
2881 Default is @file{nfs NFS proc}.
2883 @item --output=@var{dbfile}
2884 The database file to build.
2885 Default is system-dependent, but typically @file{/usr/local/var/locatedb}.
2887 @item --localuser=@var{user}
2888 The user to search the non-network directories as, using @code{su}.
2889 Default is to search the non-network directories as the current user.
2890 You can also use the environment variable @code{LOCALUSER} to set this user.
2892 @item --netuser=@var{user}
2893 The user to search network directories as, using @code{su}.
2894 Default is @code{daemon}.
2895 You can also use the environment variable @code{NETUSER} to set this user.
2897 @item --old-format
2898 Generate a locate database in the old format, for compatibility with
2899 versions of @code{locate} other than GNU @code{locate}.  Using this
2900 option means that @code{locate} will not be able to properly handle
2901 non-ASCII characters in filenames (that is, filenames containing
2902 characters which have the eighth bit set, such as many of the
2903 characters from the ISO-8859-1 character set).
2904 @item --help
2905 Print a summary of the command-line argument format and exit.
2906 @item --version
2907 Print the version number of @code{updatedb} and exit.
2908 @end table
2910 @node Invoking xargs, ,  Invoking updatedb, Reference
2911 @section Invoking @code{xargs}
2913 @example
2914 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
2915 @end example
2917 @code{xargs} exits with the following status:
2919 @table @asis
2920 @item 0
2921 if it succeeds
2922 @item 123
2923 if any invocation of the command exited with status 1-125
2924 @item 124
2925 if the command exited with status 255
2926 @item 125
2927 if the command is killed by a signal
2928 @item 126
2929 if the command cannot be run
2930 @item 127
2931 if the command is not found
2932 @item 1
2933 if some other error occurred.
2934 @end table
2936 @table @code
2937 @item --arg-file@r{=@var{inputfile}}
2938 @itemx -a @r{=@var{inputfile}}
2939 Read names from the file @var{inputfile} instead of standard input.
2941 @item --null
2942 @itemx -0
2943 Input filenames are terminated by a null character instead of by
2944 whitespace, and the quotes and backslash are not special (every
2945 character is taken literally).  Disables the end of file string, which
2946 is treated like any other argument.
2948 @item --eof@r{[}=@var{eof-str}@r{]}
2949 @itemx -e@r{[}@var{eof-str}@r{]}
2950 Set the end of file string to @var{eof-str}.  If the end of file string
2951 occurs as a line of input, the rest of the input is ignored.  If
2952 @var{eof-str} is omitted, there is no end of file string.  If this
2953 option is not given, the end of file string defaults to @samp{_}.
2955 @item --help
2956 Print a summary of the options to @code{xargs} and exit.
2958 @item --replace@r{[}=@var{replace-str}@r{]}
2959 @itemx -i@r{[}@var{replace-str}@r{]}
2960 Replace occurrences of @var{replace-str} in the initial arguments with
2961 names read from standard input.  Also, unquoted blanks do not
2962 terminate arguments; instead, the input is split at newlines only.
2963 If @var{replace-str} is omitted, it defaults to @samp{@{@}}
2964 (like for @samp{find -exec}).  Implies @samp{-x} and @samp{-l 1}.
2966 @item --max-lines@r{[}=@var{max-lines}@r{]}
2967 @itemx -l@r{[}@var{max-lines}@r{]}
2968 Use at most @var{max-lines} nonblank input lines per command line;
2969 @var{max-lines} defaults to 1 if omitted.  Trailing blanks cause an
2970 input line to be logically continued on the next input line, for the
2971 purpose of counting the lines.  Implies @samp{-x}.
2973 @item --max-args=@var{max-args}
2974 @itemx -n @var{max-args}
2975 Use at most @var{max-args} arguments per command line.  Fewer than
2976 @var{max-args} arguments will be used if the size (see the @samp{-s}
2977 option) is exceeded, unless the @samp{-x} option is given, in which case
2978 @code{xargs} will exit.
2980 @item --interactive
2981 @itemx -p
2982 Prompt the user about whether to run each command line and read a line
2983 from the terminal.  Only run the command line if the response starts
2984 with @samp{y} or @samp{Y}.  Implies @samp{-t}.
2986 @item --no-run-if-empty
2987 @itemx -r
2988 If the standard input does not contain any nonblanks, do not run the
2989 command.  By default, the command is run once even if there is no input.
2991 @item --max-chars=@var{max-chars}
2992 @itemx -s @var{max-chars}
2993 Use at most @var{max-chars} characters per command line, including the
2994 command and initial arguments and the terminating nulls at the ends of
2995 the argument strings.
2997 @item --verbose
2998 @itemx -t
2999 Print the command line on the standard error output before executing
3002 @item --version
3003 Print the version number of @code{xargs} and exit.
3005 @item --exit
3006 @itemx -x
3007 Exit if the size (see the @samp{-s} option) is exceeded.
3010 @item --max-procs=@var{max-procs}
3011 @itemx -P @var{max-procs}
3012 Run up to @var{max-procs} processes at a time; the default is 1.  If
3013 @var{max-procs} is 0, @code{xargs} will run as many processes as
3014 possible at a time.
3015 @end table
3017 @node Security Considerations, Error Messages, Reference, Top
3018 @chapter Security Considerations
3020 Security considerations are important if you are using @code{find} or
3021 @code{xargs} to search for or process files that don't belong to you
3022 or over which other people have control.  Security considerations
3023 relating to @code{locate} may also apply if you have files which you
3024 may not want others to see.   
3026 In general, the most severe forms of security problems affecting
3027 @code{find} and related programs are where third parties can bring
3028 about a situation where those programs allow them to do something
3029 they would normally not be able to do.  This is called @emph{privilege
3030 elevation}.  This might include deleting files they would not normally
3031 be able to delete.  It is also common for the system to periodically
3032 invoke @code{find} for housekeeping purposes.  These invocations of
3033 @code{find} are particularly problematic from a security point of view
3034 as these are often invoked by the superuser and search the whole file
3035 system hierarchy.  The severity of any associated problem depends on
3036 what the system is going to do with the output of @code{find}.
3038 @menu
3039 * Levels of Risk::      What is your level of exposure to security problems?
3040 * Security Considerations for find::  Security problems with find
3041 * Security Considerations for xargs:: Security problems with xargs
3042 * Security Considerations for locate:: Security problems with locate
3043 * Security Summary:: That was all very complex, what does it boil down to?
3044 @end menu
3047 @node Levels of Risk
3048 @section Levels of Risk
3050 There are some security risks inherent in the use of @code{find},
3051 @code{xargs} and (to a lesser extent) @code{locate}.  The severity of
3052 these risks depends on what sort of system you are using:
3054 @table @strong
3055 @item High risk
3056 Multi-user systems where you do not control (or trust) the other
3057 users, and on which you execute @code{find}, including areas where
3058 those other users can manipulate the filesystem (for example beneath
3059 @file{/home} or @file{/tmp}).
3061 @item Medium Risk
3062 Systems where the actions of other users can create filenames chosen
3063 by them, but to which they don't have access while @code{find} is
3064 being run.  This access might include leaving programs running (shell
3065 background jobs, @code{at} or @code{cron} tasks, for example).  On
3066 these sorts of systems, carefully written commands (avoiding use of
3067 @samp{-print} for example) should not expose you to a high degree of
3068 risk.  Most systems fall into this category.
3070 @item Low Risk
3071 Systems to which untrusted parties do not have access, cannot create
3072 filenames of their own choice (even remotely) and which contain no
3073 security flaws which might enable an untrusted third party to gain
3074 access.  Most systems do not fall into this category because there are
3075 many ways in which external parties can affect the names of files that
3076 are created on your system.  The system on which I am writing this for
3077 example automatically downloads software updates from the Internet;
3078 the names of the files in which these updates exist are chosen by
3079 third parties@footnote{Of course, I trust these parties to a large
3080 extent anyway, because I install software provided by them; I choose
3081 to trust them in this way, and that's a deliberate choice}.
3082 @end table
3084 In the discussion above, ``risk'' denotes the likelihood that someone
3085 can cause @code{find}, @code{xargs}, @code{locate} or some other
3086 program which is controlled by them to do something you did not
3087 intend.  The levels of risk suggested do not take any account of the
3088 consequences of this sort of event.  That is, if you operate a ``low
3089 risk'' type system, but the consequences of a security problem are
3090 disastrous, then you should still give serious thought to all the
3091 possible security problems, many of which of course will not be
3092 discussed here -- this section of the manual is intended to be
3093 informative but not comprehensive or exhaustive.   
3095 If you are responsible for the operation of a system where the
3096 consequences of a security problem could be very important, you should
3097 do two things:-
3099 @enumerate
3100 @item Define a security policy which defines who is allowed to do what
3101 on your system
3102 @item Seek competent advice on how to enforce your policy, detect
3103 breaches of that policy, and take account of any potential problems
3104 that might fall outside the scope of your policy
3105 @end enumerate
3108 @node Security Considerations for find
3109 @section Security Considerations for find
3112 Some of the actions @code{find} might take have a direct effect;
3113 these include @code{-exec} and @code{-delete}.  However, it is also
3114 common to use @code{-print} explicitly or implicitly, and so if
3115 @code{find} produces the wrong list of filenames, that can also be a
3116 security problem; consider the case for example where @code{find} is
3117 producing a list of files to be deleted.
3119 We normally assume that the @code{find} command line expresses the
3120 file selection criteria and actions that the user had in mind -- that
3121 is, the command line is ``trusted'' data.  
3123 From a security analysis point of view, the output of @code{find}
3124 should be correct; that is, the output should contain only the names
3125 of those files which meet the user's criteria specified on the command
3126 line.  This applies for the @code{-exec} and @code{-delete} actions;
3127 one can consider these to be part of the output.
3129 On the other hand, the contents of the filesystem can be manipulated
3130 by other people, and hence we regard this as ``untrusted'' data.  This
3131 implies that the @code{find} command line is a filter which converts
3132 the untrusted contents of the filesystem into a correct list of output
3133 files.   
3135 The filesystem will in general change while @code{find} is searching
3136 it; in fact, most of the potential security problems with @code{find}
3137 relate to this issue in some way.
3139 Race conditions are a general class of security problem where the
3140 relative ordering of actions taken by @code{find} (for example) and
3141 something else are important@footnote{This is more or less the
3142 definition of the term ``race condition''} .  
3144 Typically, an attacker might move or rename files or directories in
3145 the hope that an action might be taken against a a file which was not
3146 normally intended to be affected.  Alternatively, this sort of attack
3147 might be intended to persuade @code{find} to search part of the
3148 filesystem which would not normally be included in the search
3149 (defeating the @code{-prune} action for example).  
3151 @menu
3152 * Changing the Current Working Directory::
3153 * Race Conditions with -exec::
3154 * Race Conditions with -print and -print0::
3155 @end menu
3158 @node Changing the Current Working Directory
3159 @subsection Changing the Current Working Directory
3161 As find searches the file system, it finds subdirectories and then
3162 searches within them by changing its working directory.  First,
3163 @code{find} notices a subdirectory.  It then decides if that
3164 subdirectory meets the criteria for being searched; that is, any
3165 @samp{-xdev} or @samp{-prune} expressions are taken into account.  The
3166 @code{find} program will then change working directory and proceed to
3167 search the directory.
3169 A race condition attack might take the form that once the checks
3170 relevant to @samp{-xdev} and @samp{-prune} have been done, an attacker
3171 might rename the directory that was being considered, and put in its
3172 place a symbolic link that actually points somewhere else.  
3174 The idea behind this attack is to fool @code{find} into going into the
3175 wrong directory.  This would leave @code{find} with a working
3176 directory chosen by an attacker, bypassing any protection apparently
3177 provided by @samp{-xdev} and @samp{-prune}, and any protection
3178 provided by being able to @emph{not} list particular directories on
3179 the @code{find} command line.  This form of attack is particularly
3180 problematic if the attacker can predict when the @code{find} command
3181 will be run, as is the case with @code{cron} tasks for example.
3183 GNU @code{find} has specific safeguards to prevent this general class
3184 of problem.  The exact form of these safeguards depends on the
3185 properties of your system.  
3187 @menu 
3188 * O_NOFOLLOW::                     Safely changing directory using fchdir().
3189 * Systems without O_NOFOLLOW::     Checking for symbolic links after chdir().
3190 * Working with automounters::      These can look like race condition exploits
3191 * Problems with dead NFS servers:: If you don't have O_NOFOLLOW, this is a problem.
3192 @end menu
3194 @node O_NOFOLLOW
3195 @subsubsection O_NOFOLLOW 
3197 If your system supports the O_NOFOLLOW flag @footnote{GNU/Linux
3198 (kernel version 2.1.126 and later) and FreeBSD (3.0-CURRENT and later)
3199 support this} to the @code{open(2)} system call, @code{find} uses it
3200 when safely changing directory.  The target directory is first opened
3201 and then @code{find} changes working directory with the
3202 @code{fchdir()} system call.  This ensures that symbolic links are not
3203 followed, preventing the sort of race condition attack in which use
3204 is made of symbolic links.
3206 If for any reason this approach does not work, @code{find} will fall
3207 back on the method which is normally used if O_NOFOLLOW is not
3208 supported.
3210 You can tell if your system supports O_NOFOLLOW by running 
3212 @example
3213 find --version
3214 @end example
3216 This will tell you the version number and which features are enabled.
3217 For example, if I run this on my system now, this gives:
3218 @example
3219 GNU find version 4.2.18-CVS
3220 Features enabled: D_TYPE O_NOFOLLOW(enabled)
3221 @end example
3223 Here, you can see that I am running a version of find which was built
3224 from the development (CVS) code prior to the release of
3225 findutils-4.2.18, and that the D_TYPE and O_NOFOLLOW features are
3226 present.  O_NOFOLLOW is qualified with ``enabled''.  This simply means
3227 that the current system seems to support O_NOFOLLOW.  This check is
3228 needed because it is possible to build find on a system that defines
3229 O_NOFOLLOW and then run it on a system that ignores the O_NOFOLLOW
3230 flag.  We try to detect such cases at startup by checking the
3231 operating system and version number; when this happens you will see
3232 ``O_NOFOLLOW(disabled)'' instead.
3234 @node Systems without O_NOFOLLOW
3235 @subsubsection Systems without O_NOFOLLOW
3237 The strategy for preventing this type of problem on systems that lack
3238 support for the O_NOFOLLOW flag is more complex.  Each time
3239 @code{find} changes directory, it examines the directory it is about
3240 to move to, issues the @code{chdir()} system call, and then checks
3241 that it has ended up in the subdirectory it expected.  If not, an
3242 error message is issued and @code{find} exits immediately.  This
3243 method prevents filesystem manipulation attacks from persuading
3244 @code{find} to search parts of the filesystem it did not intend.
3245 However, we heve to take special steps in order not to unneccessarily
3246 conclude that there is a problem with any ``automount'' mount points.
3248 @node Working with automounters
3249 @subsubsection Working with automounters
3251 Where an automounter is in use it can be the case that the use of the
3252 @code{chdir()} system call can itself cause a new filesystem to be
3253 mounted at that point.  On systems that do not support O_NOFOLLOW,
3254 this will cause @code{find}'s security check to fail.  
3256 However, this does not normally represent a security problem (since
3257 the automounter configuration is normally set up by the system
3258 administrator).  Therefore, if the @code{chdir()} sanity check fails,
3259 @code{find} will check to see if a new filesystem has been mounted at
3260 the current directory; if so, @code{find} will issue a warning message
3261 and continue.
3263 To make this solution work, @code{find} reads the list of mounted
3264 filesystems at startup, and again when the sanity check fails.  It
3265 compares the two lists to find out if the directory it has moved into
3266 has just been mounted.
3268 @node Problems with dead NFS servers
3269 @subsubsection Problems with dead NFS servers
3271 Examining every mount point on the system has a downside too.  In
3272 general, @code{find} will be used to search just part of the
3273 filesystem.  However, @code{find} examines every mount point.  If the
3274 system has a filesystem mounted on an unresponsive NFS server,
3275 @code{find} will hang, waiting for the NFS server to respond.  Worse,
3276 it does this even if the affected mount point is not within the
3277 directory tree that find would have searched anyway.
3279 This is very unfortunate.  However, this problem only affects systems
3280 that have no support for O_NOFOLLOW.  As far as I can tell, it is not
3281 possible on such systems to fix all three problems (the race
3282 condition, the false-alarm at automount mount points, and the hang at
3283 startup if there is a dead NFS server) at once.  If you have some
3284 ideas about how @code{find} could do this better, please send email to
3285 the @email{bug-findutils@@gnu.org} mailing list.
3287 @node Race Conditions with -exec
3288 @subsection Race Conditions with -exec
3290 The @samp{-exec} action causes another program to be run.  It is
3291 passed the name of the file which is being considered at the time.
3292 The invoked program will then - normally - perform some action on that
3293 file.  Once again, there is a race condition which can be exploited
3294 here.  We shall take as a specific example the command
3296 @example 
3297 find /tmp -path /tmp/umsp/passwd -exec /bin/rm
3298 @end example
3300 In this simple example, we are identifying just one file to be deleted
3301 and invoking @code{/bin/rm} to delete it.  A problem exists because
3302 there is a time gap between the point where @code{find} decides that
3303 it needs to process the @samp{-exec} action and the point where the
3304 @code{/bin/rm} command actually issues the @code{unlink()} system
3305 call.  Within this time period, an attacker can rename the
3306 @file{/tmp/umsp} directory, replacing it with a symbolic link to
3307 @file{/etc}.   There is no way for @code{/bin/rm} to determine that it
3308 is working on the same file that @code{find} had in mind.   Once the
3309 symbolic link is in place, the attacker has persuaded @code{find} to
3310 cause the deletion of the @file{/etc/passwd} file, which is not the
3311 effect intended by the command which was actually invoked.  
3313 One possible defence against this type of attack is to modify the
3314 behaviour of @samp{-exec} so that the @code{/bin/rm} command is run
3315 with the argument @file{./passwd} and a suitable choice of working
3316 directory.  This would allow the normal sanity check that @code{find}
3317 performs to protect against this form of attack too.  Unfortunately,
3318 this strategy cannot be used as the POSIX standard specifies that the
3319 current working directory for commands invoked via @samp{-exec} must
3320 be the same as the current working directory from which @code{find}
3321 was invoked.   This means that the @samp{-exec} action is inherently
3322 insecure and can't be fixed.   
3324 GNU @code{find} implements a more secure variant of the @samp{-exec}
3325 action, @samp{-execdir}.  The @samp{-execdir} action
3326 ensures that it is not necessary to dereference subdirectories to
3327 process target files.  The current directory used to invoke programs
3328 is the same as the directory in which the file to be processed exists
3329 (@file{/tmp/umsp} in our example, and only the basename of the file to
3330 be processed is passed to the invoked command, with a @samp{./}
3331 prepended (giving @file{./passwd} in our example).
3333 The @samp{-execdir} action refuses to do anything if the current
3334 directory is included in the @var{$PATH} environment variable.  This
3335 is necessary because @samp{-execdir} runs programs in the same
3336 directory in which it finds files -- in general, such a directory
3337 might be writable by untrusted users.  For similar reasons,
3338 @samp{-execdir} does not allow @samp{@{@}} to appear in the name of
3339 the command to be run.
3341 @node Race Conditions with -print and -print0
3342 @subsection Race Conditions with -print and -print0
3344 The @samp{-print} and @samp{-print0} actions can be used to produce a
3345 list of files matching some criteria, which can then be used with some
3346 other command, perhaps with @code{xargs}.   Unfortunately, this means
3347 that there is an unavoidable time gap between @code{find} deciding
3348 that one or more files meet its criteria and the relevant command
3349 being executed.  For this reason, the @samp{-print} and @samp{-print0}
3350 actions are just as insecure as @samp{-exec}.
3352 In fact, since the construction 
3354 @example
3355 find ....   -print | xargs ....
3356 @end example
3358 does not cope correctly with newlines or other ``white space'' in
3359 filenames, and copes poorly with filenames containing quotes, the
3360 @samp{-print} action is less secure even than @samp{-print0}.
3363 @comment  node-name,  next,  previous,  up
3364 @comment @node Security Considerations for xargs
3365 @node Security Considerations for xargs
3366 @section Security Considerations for @code{xargs}
3368 The description of the race conditions affecting the @samp{-print}
3369 action of @code{find} shows that @code{xargs} cannot be secure if it
3370 is possible for an attacker to modify a filesystem after @code{find}
3371 has started but before @code{xargs} has completed all its actions.
3373 However, there are other security issues that exist even if it is not
3374 possible for an attacker to have access to the filesystem in real
3375 time.  Firstly, if it is possible for an attacker to create files with
3376 names of their own choice on the filesystem, then @code{xargs} is
3377 insecure unless the @samp{-0} option is used.  If a file with the name
3378 @file{/home/someuser/foo/bar\n/etc/passwd} exists (assume that
3379 @samp{\n} stands for a newline character), then @code{find ... -print}
3380 can be persuaded to print three separate lines:
3382 @example
3383 /home/someuser/foo/bar
3385 /etc/passwd
3386 @end example
3388 If it finds a blank line in the input, @code{xargs} will ignore it.
3389 Therefore, if some action is to be taken on the basis of this list of
3390 files, the @file{/etc/passwd} file would be included even if this was
3391 not the intent of the person running find.  There are circumstances in
3392 which an attacker can use this to their advantage.  The same
3393 consideration applies to filenames containing ordinary spaces rather
3394 than newlines, except that of course the list of filenames will no
3395 longer contain an ``extra'' newline.
3397 This problem is an unavoidable consequence of the default behaviour of
3398 the @code{xargs} command, which is specified by the POSIX standard.
3399 The only ways to avoid this problem are either to avoid all use of
3400 @code{xargs} in favour for example of @samp{find -exec} or (where
3401 available) @samp{find -execdir}, or to use the @samp{-0} option, which
3402 ensures that @code{xargs} considers filenames to be separated by ASCII
3403 NUL characters rather than whitespace.   However, useful though this
3404 option is, the POSIX standard does not make it mandatory.
3406 @comment  node-name,  next,  previous,  up
3407 @node Security Considerations for locate
3408 @section Security Considerations for @code{locate}
3410 It is fairly unusual for the output of @code{locate} to be fed into
3411 another command.  However, if this were to be done, this would raise
3412 the same set of security issues as the use of @samp{find ... -print}.
3413 Although the problems relating to whitespace in filenames can be
3414 resolved by using @code{locate}'s @samp{-0} option, this still leaves
3415 the race condition problems associated with @samp{find ... -print0}.
3416 There is no way to avoid these problems in the case of @code{locate}.
3418 @node Security Summary
3419 @section Summary
3421 Where untrusted parties can create files on the system, or affect the
3422 names of files that are created, all uses for @code{find},
3423 @code{locate} and @code{xargs} have known security problems except the
3424 following:
3426 @table @asis
3427 @item Informational use only
3428 Uses where the programs are used to prepare lists of filenames upon which no further action will ever be taken.
3430 @item -delete 
3431 Use of the @samp{-delete} action to delete files which meet
3432 specified criteria
3434 @item -execdir 
3435 Use of the @samp{-execdir} action where the @env{PATH}
3436 environment variable contains directories which contain only trusted
3437 programs. 
3438 @end table
3440 @comment  node-name,  next,  previous,  up
3441 @node Error Messages, Primary Index, Security Considerations, Top
3442 @chapter Error Messages
3444 This section describes some of the error messages you might get from
3445 @code{find}, @code{xargs}, or @code{locate}, explains them and in some
3446 cases provides advice as to what you should do about this.
3448 This manual is written in English.  The GNU findutils software
3449 features translated error messages for many languages.  For this
3450 reason where possible we try to make the error messages produced by
3451 the programs self-explanatory.  This approach avoids asking people to
3452 figure out which English-language error message the test they actually
3453 saw might correspond to. Error messages which are self-explanatory
3454 will not normally be described or discussed in this document.  For
3455 those messages which are discussed in this document, only the
3456 English-language version of the message will be listed.
3458 @menu
3459 * Error Messages From find::
3460 * Error Messages From xargs::
3461 * Error Messages From locate::
3462 * Error Messages From updatedb::
3463 @end menu
3465 @node Error Messages From find, Error Messages From xargs, , Error Messages
3466 @section Error Messages From find
3468 @table @samp
3469 @item invalid predicate `-foo'
3470 This means that the @code{find} command line included something that
3471 started with a dash or other special character.  The @code{find}
3472 program tried to interpret this as a test, action or option, but
3473 didn't recognise it.  If you intended it to be a test, check what you
3474 specified against the documentation.  If, on the other hand, the
3475 string is the name of a file which has been expanded from a wildcard
3476 (for example because you have a @samp{*} on the command line),
3477 consider using @samp{./*} or just @samp{.} instead.
3479 @item unexpected extra predicate
3480 This usually happens if you have an extra bracket on the command line
3481 (for example @samp{find . -print \)}).
3483 @item Warning: filesystem /path/foo has recently been mounted
3484 @itemx Warning: filesystem /path/foo has recently been unmounted
3485 These messages might appear when @code{find} moves into a directory
3486 and finds that the device number and inode are different to what it
3487 expected them to be.  If the directory @code{find} has moved into is
3488 on an NFS filesystem, it will not issue this message, because
3489 @code{automount} frequently mounts new filesystems on directories as
3490 you move into them (that is how it knows you want to use the
3491 filesystem).  So, if you do see this message, be wary --
3492 @code{automount} may not have been responsible.  Consider the
3493 possibility that someone else is manipulating the filesystem while
3494 @code{find} is running.  Some people might do this in order to mislead
3495 @code{find} or persuade it to look at one set of files when it thought
3496 it was looking at another set.
3498 @item /path/foo changed during execution of find (old device number 12345, new device number 6789, filesystem type is <whatever>) [ref XXX]
3499 This message is issued when @code{find} changes directory and ends up
3500 somewhere it didn't expect to be.  This happens in one of two
3501 circumstances.  Firstly this happens when ``automount'' does its thing
3502 on a system where @code{find} doesn't know how to determine what the
3503 current set of mounted filesystems is
3505 Secondly, this can happen when the device number of a directory
3506 appears to change during a change of current directory, but
3507 @code{find} is moving up the filesystem hierarchy rather than down it.
3508 In order to prevent @code{find} wandering off into some unexpected
3509 part of the filesystem, we stop it at this point.
3511 @item Don't know how to use getmntent() to read `/etc/mtab'.  This is a bug.
3512 This message is issued when a problem similar to the above occurs on a
3513 system where @code{find} doesn't know how to figure out the current
3514 list of mount points.  Ask for help on @email{bug-findutils@@gnu.org}.
3516 @item /path/foo/bar changed during execution of find (old inode number 12345, new inode number 67893, filesystem type is <whatever>) [ref XXX]"),
3517 This message is issued when @code{find} changes directory and
3518 discovers that the inode number of that directory once it's got there
3519 is different to the inode number that it obtained when it examined the
3520 directory some time previously.  This normally means that while
3521 @code{find} has been deep in a directory hierarchy doing something
3522 time consuming, somebody has moved the one of the parent directories
3523 to another location in the same filesystem.  This may have been done
3524 maliciously, or may not.  In any case, @code{find} stops at this point
3525 in order to avoid traversing parts of the filesystem that it wasn't
3526 intended to.  You can use @code{ls -li} or @code{find /path -inum
3527 12345 -o -inum 67893} to find out more about what has happened.
3529 @item sanity check of the fnmatch() library function failed.
3530 Please submit a bug report.  You may well be asked questions about
3531 your system, and if you compiled the @code{findutils} code yourself,
3532 you should keep your copy of the build tree around.  The likely
3533 explanation is that your system has a buggy implementation of
3534 @code{fnmatch} that looks enough like the GNU version to fool
3535 @code{configure}, but which doesn't work properly.
3537 @item cannot fork
3538 This normally happens if you use the @code{-exec} action or a
3539 something similar (@code{-ok} and so forth) but the system has run out
3540 of free process slots.  This is either because the system is very busy
3541 and the system has reached its maximum process limit, or because you
3542 have a resource limit in place and you've reached it.  Check the
3543 system for runaway processes (if @code{ps} still works).  Some process
3544 slots are normally reserved for use by @samp{root}.
3546 @item some-program terminated by signal 99
3547 Some program which was launched via @code{-exec} or similar was killed
3548 with a fatal signal.  This is just an advisory message.
3549 @end table
3553 @node Error Messages From xargs, Error Messages From locate, Error Messages From find, Error Messages
3554 @section Error Messages From xargs
3556 @table @samp
3557 @item environment is too large for exec
3558 This message means that you have so many environment variables set
3559 (or such large values for them) that there is no room within the
3560 system-imposed limits on program command-line argument length to
3561 invoke any program.  I'm sure you did this deliberately.  Please try
3562 unsetting some environment variables, or exiting the current shell.
3564 @item can not fit single argument within argument list size limit
3565 You are using the @samp{-i} option and @code{xargs} doesn't have
3566 enough space to build a command line because it has read in a really
3567 large item and it doesn't fit.  You can probably work around this
3568 problem with the @samp{-s} option, but the default size is pretty
3569 large.  You must be trying pretty hard to break @code{xargs}.
3571 @item cannot fork
3572 See the description of the similar message for @code{find}.
3574 @item <program>: exited with status 255; aborting
3575 When a command run by @code{xargs} exits with status 255, @code{xargs}
3576 is supposed to stop.  If this is not what you intended, wrap the
3577 program you are trying to invoke in a shell script which doesn't
3578 return status 255.
3580 @item <program>: terminated by signal 99
3581 See the description of the similar message for @code{find}.
3582 @end table
3584 @node Error Messages From locate, Error Messages From updatedb, Error Messages From xargs, Error Messages
3585 @section Error Messages From locate
3587 @table @samp
3588 @item warning: database `/usr/local/var/locatedb' is more than 8 days old
3589 The @code{locate} program relies on a database which is periodically
3590 built by the @code{updatedb} program.  That hasn't happened in a long
3591 time.  To fix this problem, run @code{updatedb} manually.  This can
3592 often happen on systems that are generally not left on, so the periodic
3593 ``cron'' task which normally does this doesn't get a chance to run.
3595 @item locate database `/usr/local/var/locatedb' is corrupt or invalid
3596 This should not happen.  Re-run @code{updatedb}.  If that works, but
3597 @code{locate} still produces this error, run @code{locate --version}
3598 and @code{updatedb --version}.  These should produce the same output.
3599 If not, you are using a mixed toolset; check your @samp{$PATH}
3600 environment variable and your shell aliases (if you have any).  If
3601 both programs claim to be GNU versions, this is a bug; all versions of
3602 these programs should interoperate without problem.  Ask for help on
3603 @email{bug-findutils@@gnu.org}.
3604 @end table
3607 @node Error Messages From updatedb, , Error Messages From locate, Error Messages
3608 @section Error Messages From updatedb
3610 The @code{updatedb} program (and the programs it invokes) do issue
3611 error messages, but none of them seem to me to be candidates for
3612 guidance.  If you are having a problem understanding one of these, ask
3613 for help on @email{bug-findutils@@gnu.org}.
3615 @node Primary Index, , Error Messages, Top
3616 @unnumbered @code{find} Primary Index
3618 This is a list of all of the primaries (tests, actions, and options)
3619 that make up @code{find} expressions for selecting files.  @xref{find
3620 Expressions}, for more information on expressions.
3622 @printindex fn
3624 @bye
3626 @comment texi related words used by Emacs' spell checker ispell.el
3628 @comment LocalWords: texinfo setfilename settitle setchapternewpage
3629 @comment LocalWords: iftex finalout ifinfo DIR titlepage vskip pt
3630 @comment LocalWords: filll dir samp dfn noindent xref pxref
3631 @comment LocalWords: var deffn texi deffnx itemx emph asis
3632 @comment LocalWords: findex smallexample subsubsection cindex
3633 @comment LocalWords: dircategory direntry itemize
3635 @comment other words used by Emacs' spell checker ispell.el
3636 @comment LocalWords: README fred updatedb xargs Plett Rendell akefile
3637 @comment LocalWords: args grep Filesystems fo foo fOo wildcards iname
3638 @comment LocalWords: ipath regex iregex expr fubar regexps
3639 @comment LocalWords: metacharacters macs sr sc inode lname ilname
3640 @comment LocalWords: sysdep noleaf ls inum xdev filesystems usr atime
3641 @comment LocalWords: ctime mtime amin cmin mmin al daystart Sladkey rm
3642 @comment LocalWords: anewer cnewer bckw rf xtype uname gname uid gid
3643 @comment LocalWords: nouser nogroup chown chgrp perm ch maxdepth
3644 @comment LocalWords: mindepth cpio src CD AFS statted stat fstype ufs
3645 @comment LocalWords: nfs tmp mfs printf fprint dils rw djm Nov lwall
3646 @comment LocalWords: POSIXLY fls fprintf strftime locale's EDT GMT AP
3647 @comment LocalWords: EST diff perl backquotes sprintf Falstad Oct cron
3648 @comment LocalWords: eg vmunix mkdir afs allexec allwrite ARG bigram
3649 @comment LocalWords: bigrams cd chmod comp crc CVS dbfile dum eof
3650 @comment LocalWords: fileserver filesystem fn frcode Ghazi Hnewc iXX
3651 @comment LocalWords: joeuser Kaveh localpaths localuser LOGNAME
3652 @comment LocalWords: Meyering mv netpaths netuser nonblank nonblanks
3653 @comment LocalWords: ois ok Pinard printindex proc procs prunefs
3654 @comment LocalWords: prunepaths pwd RFS rmadillo rmdir rsh sbins str
3655 @comment LocalWords: su Timar ubins ug unstripped vf VM Weitzel
3656 @comment LocalWords: wildcard zlogout basename execdir wholename iwholename
3657 @comment LocalWords: timestamp timestamps Solaris FreeBSD OpenBSD POSIX