Added a new "NON-BUGS" section pointing out things that look like bugs
[findutils.git] / doc / find.texi
blob6c971eeb8799caa26a5257ff6d48caeb8e29c525
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 @code{locate} searches special file name databases for file names that
218 match patterns.  The system administrator runs the @code{updatedb}
219 program to create the databases.  @code{locate} is run like this:
221 @example
222 locate @r{[}@var{option}@dots{}@r{]} @var{pattern}@dots{}
223 @end example
225 @noindent
226 This example prints the names of all files in the default file name
227 database whose name ends with @samp{Makefile} or @samp{makefile}.  Which
228 file names are stored in the database depends on how the system
229 administrator ran @code{updatedb}.
230 @example
231 locate '*[Mm]akefile'
232 @end example
234 The name @code{xargs}, pronounced EX-args, means ``combine arguments.''
235 @code{xargs} builds and executes command lines by gathering together
236 arguments it reads on the standard input.  Most often, these arguments
237 are lists of file names generated by @code{find}.  @code{xargs} is run
238 like this:
240 @example
241 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
242 @end example
244 @noindent
245 The following command searches the files listed in the file
246 @file{file-list} and prints all of the lines in them that contain the
247 word @samp{typedef}.
248 @example
249 xargs grep typedef < file-list
250 @end example
252 @node find Expressions
253 @section @code{find} Expressions
255 The expression that @code{find} uses to select files consists of one or
256 more @dfn{primaries}, each of which is a separate command line argument
257 to @code{find}.  @code{find} evaluates the expression each time it
258 processes a file.  An expression can contain any of the following types
259 of primaries:
261 @table @dfn
262 @item options
263 affect overall operation rather than the processing of a specific file;
264 @item tests
265 return a true or false value, depending on the file's attributes;
266 @item actions
267 have side effects and return a true or false value; and
268 @item operators
269 connect the other arguments and affect when and whether they are
270 evaluated.
271 @end table
273 You can omit the operator between two primaries; it defaults to
274 @samp{-and}.  @xref{Combining Primaries With Operators}, for ways to
275 connect primaries into more complex expressions.  If the expression
276 contains no actions other than @samp{-prune}, @samp{-print} is performed
277 on all files for which the entire expression is true (@pxref{Print File
278 Name}).
280 Options take effect immediately, rather than being evaluated for each
281 file when their place in the expression is reached.  Therefore, for
282 clarity, it is best to place them at the beginning of the expression.
284 Many of the primaries take arguments, which immediately follow them in
285 the next command line argument to @code{find}.  Some arguments are file
286 names, patterns, or other strings; others are numbers.  Numeric
287 arguments can be specified as
289 @table @code
290 @item +@var{n}
291 for greater than @var{n},
292 @item -@var{n}
293 for less than @var{n},
294 @item @var{n}
295 for exactly @var{n}.
296 @end table
298 @node Finding Files, Actions, Introduction, Top
299 @chapter Finding Files
301 By default, @code{find} prints to the standard output the names of the
302 files that match the given criteria.  @xref{Actions}, for how to get more
303 information about the matching files.
306 @menu
307 * Name::
308 * Links::
309 * Time::
310 * Size::
311 * Type::
312 * Owner::
313 * Permissions::
314 * Contents::
315 * Directories::
316 * Filesystems::
317 * Combining Primaries With Operators::
318 @end menu
320 @node Name
321 @section Name
323 Here are ways to search for files whose name matches a certain pattern.
324 @xref{Shell Pattern Matching}, for a description of the @var{pattern}
325 arguments to these tests.
327 Each of these tests has a case-sensitive version and a case-insensitive
328 version, whose name begins with @samp{i}.  In a case-insensitive
329 comparison, the patterns @samp{fo*} and @samp{F??} match the file names
330 @file{Foo}, @samp{FOO}, @samp{foo}, @samp{fOo}, etc.
332 @menu
333 * Base Name Patterns::
334 * Full Name Patterns::
335 * Fast Full Name Search::
336 * Shell Pattern Matching::      Wildcards used by these programs.
337 @end menu
339 @node Base Name Patterns
340 @subsection Base Name Patterns
342 @deffn Test -name pattern
343 @deffnx Test -iname pattern
344 True if the base of the file name (the path with the leading directories
345 removed) matches shell pattern @var{pattern}.  For @samp{-iname}, the
346 match is case-insensitive.  To ignore a whole directory tree, use
347 @samp{-prune} (@pxref{Directories}).  As an example, to find Texinfo
348 source files in @file{/usr/local/doc}:
350 @example
351 find /usr/local/doc -name '*.texi'
352 @end example
353 @end deffn
355 Patterns for @samp{-name} and @samp{-iname} will match a filename with
356 a leading @samp{.}.  For example the command @samp{find /tmp -name
357 \*bar} will match the file @file{/tmp/.foobar}.
360 @node Full Name Patterns
361 @subsection Full Name Patterns
363 @deffn Test -wholename pattern
364 @deffnx Test -iwholename pattern
365 True if the entire file name, starting with the command line argument
366 under which the file was found, matches shell pattern @var{pattern}.
367 For @samp{-iwholename}, the match is case-insensitive.  To ignore a
368 whole directory tree, use @samp{-prune} rather than checking every
369 file in the tree (@pxref{Directories}).  The ``entire file name'' as
370 used by find starts with the starting-point specified on the command
371 line, and is not converted to an absolute pathname, so for example
372 @code{cd /; find tmp -wholename /tmp} will never match anything.
373 @end deffn
375 @deffn Test -path pattern
376 @deffnx Test -ipath pattern
377 These tests are deprecated, but work as for @samp{-wholename} and @samp{-iwholename},
378 respectively.  The @samp{-ipath} test is a GNU extension, but @samp{-path} is also 
379 provided by HP-UX @code{find}.
380 @end deffn
382 @deffn Test -regex expr
383 @deffnx Test -iregex expr
384 True if the entire file name matches regular expression @var{expr}.
385 This is a match on the whole path, not a search.  For example, to match
386 a file named @file{./fubar3}, you can use the regular expression
387 @samp{.*bar.} or @samp{.*b.*3}, but not @samp{f.*r3}.  @xref{Regexps, ,
388 Syntax of Regular Expressions, emacs, The GNU Emacs Manual}, for a
389 description of the syntax of regular expressions.  For @samp{-iregex},
390 the match is case-insensitive.
391 @end deffn
393 @node Fast Full Name Search
394 @subsection Fast Full Name Search
396 To search for files by name without having to actually scan the
397 directories on the disk (which can be slow), you can use the
398 @code{locate} program.  For each shell pattern you give it,
399 @code{locate} searches one or more databases of file names and displays
400 the file names that contain the pattern.  @xref{Shell Pattern Matching},
401 for details about shell patterns.
403 If a pattern is a plain string---it contains no
404 metacharacters---@code{locate} displays all file names in the database
405 that contain that string.  If a pattern contains
406 metacharacters, @code{locate} only displays file names that match the
407 pattern exactly.  As a result, patterns that contain metacharacters
408 should usually begin with a @samp{*}, and will most often end with one
409 as well.  The exceptions are patterns that are intended to explicitly
410 match the beginning or end of a file name.   
412 If you only want @code{locate} to match against the last component of
413 the filenames (the ``base name'' of the files) you can use the
414 @samp{--basename} option.  The opposite behaviour is the default, but
415 can be selected explicitly by using the option @samp{--wholename}.
417 The command
418 @example
419 locate @var{pattern}
420 @end example
422 is almost equivalent to
423 @example
424 find @var{directories} -name @var{pattern}
425 @end example
427 where @var{directories} are the directories for which the file name
428 databases contain information.  The differences are that the
429 @code{locate} information might be out of date, and that @code{locate}
430 handles wildcards in the pattern slightly differently than @code{find}
431 (@pxref{Shell Pattern Matching}).
433 The file name databases contain lists of files that were on the system
434 when the databases were last updated.  The system administrator can
435 choose the file name of the default database, the frequency with which
436 the databases are updated, and the directories for which they contain
437 entries.
439 Here is how to select which file name databases @code{locate} searches.
440 The default is system-dependent.
442 @table @code
443 @item --database=@var{path}
444 @itemx -d @var{path}
445 Instead of searching the default file name database, search the file
446 name databases in @var{path}, which is a colon-separated list of
447 database file names.  You can also use the environment variable
448 @code{LOCATE_PATH} to set the list of database files to search.  The
449 option overrides the environment variable if both are used.
450 @end table
452 @node Shell Pattern Matching
453 @subsection Shell Pattern Matching
455 @code{find} and @code{locate} can compare file names, or parts of file
456 names, to shell patterns.  A @dfn{shell pattern} is a string that may
457 contain the following special characters, which are known as
458 @dfn{wildcards} or @dfn{metacharacters}.
460 You must quote patterns that contain metacharacters to prevent the shell
461 from expanding them itself.  Double and single quotes both work; so does
462 escaping with a backslash.
464 @table @code
465 @item *
466 Matches any zero or more characters.
468 @item ?
469 Matches any one character.
471 @item [@var{string}]
472 Matches exactly one character that is a member of the string
473 @var{string}.  This is called a @dfn{character class}.  As a shorthand,
474 @var{string} may contain ranges, which consist of two characters with a
475 dash between them.  For example, the class @samp{[a-z0-9_]} matches a
476 lowercase letter, a number, or an underscore.  You can negate a class by
477 placing a @samp{!} or @samp{^} immediately after the opening bracket.
478 Thus, @samp{[^A-Z@@]} matches any character except an uppercase letter
479 or an at sign.
481 @item \
482 Removes the special meaning of the character that follows it.  This
483 works even in character classes.
484 @end table
486 In the @code{find} tests that do shell pattern matching (@samp{-name},
487 @samp{-wholename}, etc.), wildcards in the pattern will match a @samp{.}
488 at the beginning of a file name.  This is also the case for
489 @code{locate}.  Thus, @samp{find -name '*macs'} will match a file
490 named @file{.emacs}, as will @samp{locate '*macs'}.
492 Slash characters have no special significance in the shell pattern
493 matching that @code{find} and @code{locate} do, unlike in the shell, in
494 which wildcards do not match them.  Therefore, a pattern @samp{foo*bar}
495 can match a file name @samp{foo3/bar}, and a pattern @samp{./sr*sc} can
496 match a file name @samp{./src/misc}.
498 If you want to locate some files with the @samp{locate} command but
499 don't need to see the full list you can use the @samp{--limit} option
500 to see just a small number of results, or the @samp{--count} option to
501 display only the total number of matches.
503 @node Links
504 @section Links
506 There are two ways that files can be linked together.  @dfn{Symbolic
507 links} are a special type of file whose contents are a portion of the
508 name of another file.  @dfn{Hard links} are multiple directory entries
509 for one file; the file names all have the same index node (@dfn{inode})
510 number on the disk.
512 @menu
513 * Symbolic Links::
514 * Hard Links::
515 @end menu
517 @node Symbolic Links
518 @subsection Symbolic Links
520 Symbolic links are names that reference other files.  GNU @code{find}
521 will handle symbolic links in one of two ways; firstly, it can
522 dereference the links for you - this means that if it comes across a
523 symbolic link, it examines the file that the link points to, in order
524 to see if it matches the criteria you have specified.  Secondly, it
525 can check the link itself in case you might be looking for the actual
526 link.  If the file that the symbolic link points to is also within the
527 directory hierarchy you are searching with the @code{find} command,
528 you may not see a great deal of difference between these two
529 alternatives.
531 By default, @code{find} examines symbolic links themselves when it
532 finds them (and, if it later comes across the linked-to file, it will
533 examine that, too).  If you would prefer @code{find} to dereference
534 the links and examine the file that each link points to, specify the
535 @samp{-L} option to @code{find}.  You can explicitly specify the
536 default behaviour by using the @samp{-P} option.    The @samp{-H}
537 option is a half-way-between option which ensures that any symbolic
538 links listed on the command line are dereferenced, but other symbolic
539 links are not.
541 Symbolic links are different to ``hard links'' in the sense that you
542 need permissions upon the linked-to file in order to be able to
543 dereference the link.  This can mean that even if you specify the
544 @samp{-L} option, find may not be able to determine the properties of
545 the file that the link points to (because you don't have sufficient
546 permissions).  In this situation, @samp{find} uses the properties of
547 the link itself.  This also occurs if a symbolic link exists but
548 points to a file that is missing.
550 The options controlling the behaviour of @code{find} with respect to
551 links are as follows :-
553 @table @samp
554 @item -P
555 @code{find} does not dereference symbolic links at all.  This is the
556 default behaviour.  This option must be specified before any of the
557 path names on the command line.
558 @item -H
559 @code{find} does not dereference symbolic links (except in the case of
560 file names on the command line, which are dereferenced).  If a
561 symbolic link cannot be dereferenced, the information for the symbolic
562 link itself is used.  This option must be specified before any of the
563 path names on the command line.
564 @item -L
565 @code{find} dereferences symbolic links where possible, and where this
566 is not possible it uses the properties of the symbolic link itself.
567 This option must be specified before any of the path names on the
568 command line.  Use of this option also implies the same behaviour as
569 the @samp{-noleaf} option.   If you later use the @samp{-H} or
570 @samp{-P} options, this does not turn off @samp{-noleaf}.
572 @item -follow
573 This option forms part of the ``expression'' and must be specified
574 after the path names, but it is otherwise equivalent to @samp{-L}.
575 @end table
577 The following differences in behavior occur when the @samp{-L} option
578 is used:
580 @itemize @bullet
581 @item
582 @code{find} follows symbolic links to directories when searching
583 directory trees.
584 @item
585 @samp{-lname} and @samp{-ilname} always return false (unless they
586 happen to match broken symbolic links).
587 @item
588 @samp{-type} reports the types of the files that symbolic links point
590 @item
591 Implies @samp{-noleaf} (@pxref{Directories}).
592 @end itemize
594 If the @samp{-L} option or the @samp{-H} option is used, 
595 the filenames used as arguments to @samp{-newer}, @samp{-anewer}, and
596 @samp{-cnewer} are dereferenced and the timestamp from the pointed-to
597 file is used instead (if possible -- otherwise the timestamp from the
598 symbolic link is used).
600 @deffn Test -lname pattern
601 @deffnx Test -ilname pattern
602 True if the file is a symbolic link whose contents match shell pattern
603 @var{pattern}.  For @samp{-ilname}, the match is case-insensitive.
604 @xref{Shell Pattern Matching}, for details about the @var{pattern}
605 argument.  If the @samp{-L} option is in effect, this test will always
606 fail for symbolic links unless they are broken.  So, to list any
607 symbolic links to @file{sysdep.c} in the current directory and its
608 subdirectories, you can do:
610 @example
611 find . -lname '*sysdep.c'
612 @end example
613 @end deffn
615 @node Hard Links
616 @subsection Hard Links
618 Hard links allow more than one name to refer to the same file.  To
619 find all the names which refer to the same file as NAME, use
620 @samp{-samefile NAME}.  If you are not using the @samp{-L} option, you
621 can confine your search to one filesystem using the @samp{-xdev}
622 option.  This is useful because hard links cannot point outside a
623 single filesystem, so this can cut down on needless searching.
625 If the @samp{-L} option is in effect, and NAME is in fact a symbolic
626 link, the symbolic link will be dereferenced.  Hence you are searching
627 for other links (hard or symbolic) to the file pointed to by NAME.  If
628 @samp{-L} is in effect but NAME is not itself a symbolic link, other
629 symbolic links to the file NAME will be matched.
631 You can also search for files by inode number.  This can occasionally
632 be useful in diagnosing problems with filesystems for example, because
633 @code{fsck} tends to print inode numbers.  Inode numbers also
634 occasionally turn up in log messages for some types of software, and
635 are used to support the @code{ftok()} library function.
637 You can learn a file's inode number and the number of links to it by
638 running @samp{ls -li} or @samp{find -ls}.  
640 You can search for hard links to inode number NUM by using @samp{-inum
641 NUM}. If there are any file system mount points below the directory
642 where you are starting the search, use the @samp{-xdev} option unless
643 you are also using the @samp{-L} option.  Using @samp{-xdev} this
644 saves needless searching, since hard links to a file must be on the
645 same filesystem.  @xref{Filesystems}.
647 @deffn Test -samefile NAME
648 File is a hard link to the same inode as NAME.  If the @samp{-L}
649 option is in effect, symbolic links to the same file as NAME points to
650 are also matched.
651 @end deffn
653 @deffn Test -inum n
654 File has inode number @var{n}.  The @samp{+} and @samp{-} qualifiers
655 also work, though these are rarely useful.
656 @end deffn
658 You can also search for files that have a certain number of links, with
659 @samp{-links}.  Directories normally have at least two hard links; their
660 @file{.} entry is the second one.  If they have subdirectories, each of
661 those also has a hard link called @file{..} to its parent directory.
662 The @file{.} and @file{..} directory entries are not normally searched
663 unless they are mentioned on the @code{find} command line.
665 @deffn Test -links n
666 File has @var{n} hard links.
667 @end deffn
669 @deffn Test -links +n
670 File has more than @var{n} hard links.
671 @end deffn
673 @deffn Test -links -n
674 File has fewer than @var{n} hard links.
675 @end deffn
677 @node Time
678 @section Time
680 Each file has three time stamps, which record the last time that certain
681 operations were performed on the file:
683 @enumerate
684 @item
685 access (read the file's contents)
686 @item
687 change the status (modify the file or its attributes)
688 @item
689 modify (change the file's contents)
690 @end enumerate
692 There is no timestamp that indicates when a file was @emph{created}.
694 You can search for files whose time stamps are within a certain age
695 range, or compare them to other time stamps.  
697 @menu
698 * Age Ranges::
699 * Comparing Timestamps::
700 @end menu
702 @node Age Ranges
703 @subsection Age Ranges
705 These tests are mainly useful with ranges (@samp{+@var{n}} and
706 @samp{-@var{n}}).
708 @deffn Test -atime n
709 @deffnx Test -ctime n
710 @deffnx Test -mtime n
711 True if the file was last accessed (or its status changed, or it was
712 modified) @var{n}*24 hours ago.  The number of 24-hour periods since
713 the file's timestamp is always rounded down; therefore 0 means ``less
714 than 24 hours ago'', 1 means ``between 24 and 48 hours ago'', and so
715 forth.
716 @end deffn
718 @deffn Test -amin n
719 @deffnx Test -cmin n
720 @deffnx Test -mmin n
721 True if the file was last accessed (or its status changed, or it was
722 modified) @var{n} minutes ago.  These tests provide finer granularity of
723 measurement than @samp{-atime} et al., but rounding is done in a 
724 similar way.  For example, to list files in
725 @file{/u/bill} that were last read from 2 to 6 minutes ago:
727 @example
728 find /u/bill -amin +2 -amin -6
729 @end example
730 @end deffn
732 @deffn Option -daystart
733 Measure times from the beginning of today rather than from 24 hours ago.
734 So, to list the regular files in your home directory that were modified
735 yesterday, do
737 @example
738 find ~ -daystart -type f -mtime 1
739 @end example
740 @end deffn
742 The @samp{-daystart} option is unlike most other options in that it
743 has an effect on the way that other tests are performed.  The affected
744 tests are @samp{-amin}, @samp{-cmin}, @samp{-mmin}, @samp{-atime},
745 @samp{-ctime} and @samp{-mtime}.
747 @node Comparing Timestamps
748 @subsection Comparing Timestamps
750 As an alternative to comparing timestamps to the current time, you can
751 compare them to another file's timestamp.  That file's timestamp could
752 be updated by another program when some event occurs.  Or you could set
753 it to a particular fixed date using the @code{touch} command.  For
754 example, to list files in @file{/usr} modified after February 1 of the
755 current year:
757 @c Idea from Rick Sladkey.
758 @example
759 touch -t 02010000 /tmp/stamp$$
760 find /usr -newer /tmp/stamp$$
761 rm -f /tmp/stamp$$
762 @end example
764 @deffn Test -anewer file
765 @deffnx Test -cnewer file
766 @deffnx Test -newer file
767 True if the file was last accessed (or its status changed, or it was
768 modified) more recently than @var{file} was modified.  These tests are
769 affected by @samp{-follow} only if @samp{-follow} comes before them on
770 the command line.  @xref{Symbolic Links}, for more information on
771 @samp{-follow}.  As an example, to list any files modified since
772 @file{/bin/sh} was last modified:
774 @example
775 find . -newer /bin/sh
776 @end example
777 @end deffn
779 @deffn Test -used n
780 True if the file was last accessed @var{n} days after its status was
781 last changed.  Useful for finding files that are not being used, and
782 could perhaps be archived or removed to save disk space.
783 @end deffn
785 @node Size
786 @section Size
788 @deffn Test -size n@r{[}bckwMG@r{]}
789 True if the file uses @var{n} units of space, rounding up.  The units
790 are 512-byte blocks by default, but they can be changed by adding a
791 one-character suffix to @var{n}:
793 @table @code
794 @item b
795 512-byte blocks (never 1024)
796 @item c
797 bytes
798 @item k
799 kilobytes (1024 bytes)
800 @item w
801 2-byte words
802 @item M
803 Megabytes
804 @item G
805 Gigabytes
806 @end table
808 The `b' suffix always considers blocks to be 512 bytes.  This is not
809 affected by the setting (or non-setting) of the POSIXLY_CORRECT
810 environment variable.  This behaviour is different to the behaviour of
811 the @samp{-ls} action).  If you want to use 1024-byte units, use the
812 `k' suffix instead.
814 The number can be prefixed with a `+' or a `-'.  A plus sign indicates
815 that the test should succeed if the file uses at least @var{n} units
816 of storage (this is the way I normally use this test) and a minus sign
817 indicates that the test should succeed if the file uses less than
818 @var{n} units of storage.   There is no `=' prefix, because that's the 
819 default anyway.  
821 The size does not count indirect blocks, but it does count blocks in
822 sparse files that are not actually allocated.  In other words, it's
823 consistent with the result you get for @samp{ls -l} or @samp{wc -c}.
824 This handling of sparse files differs from the output of the @samp{%k}
825 and @samp{%b} format specifiers for the @samp{-printf} predicate.
827 @end deffn
829 @deffn Test -empty
830 True if the file is empty and is either a regular file or a directory.
831 This might make it a good candidate for deletion.  This test is useful
832 with @samp{-depth} (@pxref{Directories}) and @samp{-delete}
833 (@pxref{Single File}).
834 @end deffn
836 @node Type
837 @section Type
839 @deffn Test -type c
840 True if the file is of type @var{c}:
842 @table @code
843 @item b
844 block (buffered) special
845 @item c
846 character (unbuffered) special
847 @item d
848 directory
849 @item p
850 named pipe (FIFO)
851 @item f
852 regular file
853 @item l
854 symbolic link
855 @item s
856 socket
857 @item D
858 door (Solaris)
859 @end table
860 @end deffn
862 @deffn Test -xtype c
863 The same as @samp{-type} unless the file is a symbolic link.  For
864 symbolic links: if @samp{-follow} has not been given, true if the file
865 is a link to a file of type @var{c}; if @samp{-follow} has been given,
866 true if @var{c} is @samp{l}.  In other words, for symbolic links,
867 @samp{-xtype} checks the type of the file that @samp{-type} does not
868 check.  @xref{Symbolic Links}, for more information on @samp{-follow}.
869 @end deffn
871 @node Owner
872 @section Owner
874 @deffn Test -user uname
875 @deffnx Test -group gname
876 True if the file is owned by user @var{uname} (belongs to group @var{gname}).
877 A numeric ID is allowed.
878 @end deffn
880 @deffn Test -uid n
881 @deffnx Test -gid n
882 True if the file's numeric user ID (group ID) is @var{n}.  These tests
883 support ranges (@samp{+@var{n}} and @samp{-@var{n}}), unlike
884 @samp{-user} and @samp{-group}.
885 @end deffn
887 @deffn Test -nouser
888 @deffnx Test -nogroup
889 True if no user corresponds to the file's numeric user ID (no group
890 corresponds to the numeric group ID).  These cases usually mean that the
891 files belonged to users who have since been removed from the system.
892 You probably should change the ownership of such files to an existing
893 user or group, using the @code{chown} or @code{chgrp} program.
894 @end deffn
896 @node Permissions
897 @section Permissions
899 @xref{File Permissions}, for information on how file permissions are
900 structured and how to specify them.
902 @deffn Test -perm mode
904 True if the file's permissions are exactly @var{mode} (which can be
905 numeric or symbolic).
907 If @var{mode} starts with @samp{-}, true if
908 @emph{all} of the permissions set in @var{mode} are set for the file;
909 permissions not set in @var{mode} are ignored.
910 If @var{mode} starts with @samp{/}, true if
911 @emph{any} of the permissions set in @var{mode} are set for the file;
912 permissions not set in @var{mode} are ignored.
913 This is a GNU extension.
915 If you don't use the @samp{/} or @samp{-} form with a symbolic mode
916 string, you may have to specify a rather complex mode string.  For
917 example @samp{-perm g=w} will only match files which have mode 0020
918 (that is, ones for which group write permission is the only permission
919 set).  It is more likely that you will want to use the @samp{/} or
920 @samp{-} forms, for example @samp{-perm -g=w}, which matches any file
921 with group write permission.
924 @table @samp
925 @item -perm 664
926 Match files which have read and write permission for their owner,
927 and group, but which the rest of the world can read but not write to.  
928 Files which meet these criteria but have other permissions bits set
929 (for example if someone can execute the file) will not be matched.
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 without regard to the presence of any extra permission bits (for
935 example the executable bit).  This will match a file which has mode
936 0777, for example.
938 @item -perm /222
939 Match files which are writeable by somebody (their owner, or
940 their group, or anybody else).
942 @item -perm /022
943 Match files which are writeable by either their owner or their
944 group.  The files don't have to be writeable by both the owner and
945 group to be matched; either will do.
947 @item -perm /g+w,o+w
948 As above.
950 @item -perm /g=w,o=w
951 As above
953 @item -perm -022
954 Search for files which are writeable by both their owner and their
955 group.
957 @item -perm -g+w,o+w
958 As above.
959 @end table
960 @end deffn
962 @node Contents
963 @section Contents
965 To search for files based on their contents, you can use the @code{grep}
966 program.  For example, to find out which C source files in the current
967 directory contain the string @samp{thing}, you can do:
969 @example
970 grep -l thing *.[ch]
971 @end example
973 If you also want to search for the string in files in subdirectories,
974 you can combine @code{grep} with @code{find} and @code{xargs}, like
975 this:
977 @example
978 find . -name '*.[ch]' | xargs grep -l thing
979 @end example
981 The @samp{-l} option causes @code{grep} to print only the names of files
982 that contain the string, rather than the lines that contain it.  The
983 string argument (@samp{thing}) is actually a regular expression, so it
984 can contain metacharacters.  This method can be refined a little by
985 using the @samp{-r} option to make @code{xargs} not run @code{grep} if
986 @code{find} produces no output, and using the @code{find} action
987 @samp{-print0} and the @code{xargs} option @samp{-0} to avoid
988 misinterpreting files whose names contain spaces:
990 @example
991 find . -name '*.[ch]' -print0 | xargs -r -0 grep -l thing
992 @end example
994 For a fuller treatment of finding files whose contents match a pattern,
995 see the manual page for @code{grep}.
997 @node Directories
998 @section Directories
1000 Here is how to control which directories @code{find} searches, and how
1001 it searches them.  These two options allow you to process a horizontal
1002 slice of a directory tree.
1004 @deffn Option -maxdepth levels
1005 Descend at most @var{levels} (a non-negative integer) levels of
1006 directories below the command line arguments.  @samp{-maxdepth 0} means
1007 only apply the tests and actions to the command line arguments.
1008 @end deffn
1010 @deffn Option -mindepth levels
1011 Do not apply any tests or actions at levels less than @var{levels} (a
1012 non-negative integer).  @samp{-mindepth 1} means process all files
1013 except the command line arguments.
1014 @end deffn
1016 @deffn Option -depth
1017 Process each directory's contents before the directory itself.  Doing
1018 this is a good idea when producing lists of files to archive with
1019 @code{cpio} or @code{tar}.  If a directory does not have write
1020 permission for its owner, its contents can still be restored from the
1021 archive since the directory's permissions are restored after its contents.
1022 @end deffn
1024 @deffn Option -d
1025 This is a deprecated synonym for @samp{-depth}, for compatibility with
1026 Mac OS X, FreeBSD and OpenBSD.   The @samp{-depth} option is a POSIX
1027 feature, so it is better to use that.
1028 @end deffn
1030 @deffn Action -prune
1031 If the file is a directory, do not descend into it.  The result is
1032 true.  For example, to skip the directory @file{src/emacs} and all
1033 files and directories under it, and print the names of the other files
1034 found:
1036 @example
1037 find . -wholename './src/emacs' -prune -o -print
1038 @end example
1040 The above command will not print @file{./src/emacs} among its list of
1041 results.  This however is not due to the effect of the @samp{-prune}
1042 action (which only prevents further descent, it doesn't make sure we
1043 ignore that item).  Instead, this effect is due to the use of
1044 @samp{-o}.  Since the left hand side of the ``or'' condition has
1045 succeeded for @file{./src/emacs}, it is not necessary to evaluate the
1046 right-hand-side (@samp{-print}) at all for this particular file.  If
1047 you wanted to print that directory name you could use either an extra
1048 @samp{-print} action:
1050 @example
1051 find . -wholename './src/emacs' -prune -print -o -print
1052 @end example
1054 or use the comma operator:
1056 @example
1057 find . -wholename './src/emacs' -prune , -print
1058 @end example
1060 If the @samp{-depth} option is in effect, the subdirectories will have
1061 already been visited in any case.  Hence @samp{-prune} has no effect
1062 and returns false.
1063 @end deffn
1066 @deffn Action -quit
1067 Exit immediately (with return value zero if no errors have occurred).
1068 No child processes will be left running, but no more paths specified
1069 on the command line will be processed.  For example, @code{find
1070 /tmp/foo /tmp/bar -print -quit} will print only @samp{/tmp/foo}.
1071 @end deffn 
1073 @deffn Option -noleaf
1074 Do not optimize by assuming that directories contain 2 fewer
1075 subdirectories than their hard link count.  This option is needed when
1076 searching filesystems that do not follow the Unix directory-link
1077 convention, such as CD-ROM or MS-DOS filesystems or AFS volume mount
1078 points.  Each directory on a normal Unix filesystem has at least 2 hard
1079 links: its name and its @file{.}  entry.  Additionally, its
1080 subdirectories (if any) each have a @file{..}  entry linked to that
1081 directory.  When @code{find} is examining a directory, after it has
1082 statted 2 fewer subdirectories than the directory's link count, it knows
1083 that the rest of the entries in the directory are non-directories
1084 (@dfn{leaf} files in the directory tree).  If only the files' names need
1085 to be examined, there is no need to stat them; this gives a significant
1086 increase in search speed.
1087 @end deffn
1089 @deffn Option -ignore_readdir_race
1090 If a file disappears after its name has been read from a directory but
1091 before @code{find} gets around to examining the file with @code{stat},
1092 don't issue an error message.  If you don't specify this option, an
1093 error message will be issued.  This option can be useful in system
1094 scripts (cron scripts, for example) that examine areas of the
1095 filesystem that change frequently (mail queues, temporary directories,
1096 and so forth), because this scenario is common for those sorts of
1097 directories.  Completely silencing error messages from @code{find} is
1098 undesirable, so this option neatly solves the problem.  There is no
1099 way to search one part of the filesystem with this option on and part
1100 of it with this option off, though.
1101 @end deffn
1103 @deffn Option -noignore_readdir_race
1104 This option reverses the effect of the @samp{-ignore_readdir_race} option.
1105 @end deffn
1108 @node Filesystems
1109 @section Filesystems
1111 A @dfn{filesystem} is a section of a disk, either on the local host or
1112 mounted from a remote host over a network.  Searching network
1113 filesystems can be slow, so it is common to make @code{find} avoid them.
1115 There are two ways to avoid searching certain filesystems.  One way is
1116 to tell @code{find} to only search one filesystem:
1118 @deffn Option -xdev
1119 @deffnx Option -mount
1120 Don't descend directories on other filesystems.  These options are synonyms.
1121 @end deffn
1123 The other way is to check the type of filesystem each file is on, and
1124 not descend directories that are on undesirable filesystem types:
1126 @deffn Test -fstype type
1127 True if the file is on a filesystem of type @var{type}.  The valid
1128 filesystem types vary among different versions of Unix; an incomplete
1129 list of filesystem types that are accepted on some version of Unix or
1130 another is:
1131 @example
1132 ext2 ext3 proc sysfs ufs 4.2 4.3 nfs tmp mfs S51K S52K
1133 @end example
1134 You can use @samp{-printf} with the @samp{%F} directive to see the types
1135 of your filesystems.  The @samp{%D} directive shows the device number.
1136 @xref{Print File Information}.  @samp{-fstype} is
1137 usually used with @samp{-prune} to avoid searching remote filesystems
1138 (@pxref{Directories}). 
1139 @end deffn
1141 @node Combining Primaries With Operators
1142 @section Combining Primaries With Operators
1144 Operators build a complex expression from tests and actions.
1145 The operators are, in order of decreasing precedence:
1147 @table @code
1148 @item @asis{( @var{expr} )}
1149 @findex ()
1150 Force precedence.  True if @var{expr} is true.
1152 @item @asis{! @var{expr}}
1153 @itemx @asis{-not @var{expr}}
1154 @findex !
1155 @findex -not
1156 True if @var{expr} is false.
1158 @item @asis{@var{expr1 expr2}}
1159 @itemx @asis{@var{expr1} -a @var{expr2}}
1160 @itemx @asis{@var{expr1} -and @var{expr2}}
1161 @findex -a
1162 @findex -and
1163 And; @var{expr2} is not evaluated if @var{expr1} is false.
1165 @item @asis{@var{expr1} -o @var{expr2}}
1166 @itemx @asis{@var{expr1} -or @var{expr2}}
1167 @findex -o
1168 @findex -or
1169 Or; @var{expr2} is not evaluated if @var{expr1} is true.
1171 @item @asis{@var{expr1} , @var{expr2}}
1172 @findex ,
1173 List; both @var{expr1} and @var{expr2} are always evaluated.  True if
1174 @var{expr2} is true.  The value of @var{expr1} is discarded.  This
1175 operator lets you do multiple independent operations on one traversal,
1176 without depending on whether other operations succeeded.  The two 
1177 operations @var{expr1} and @var{expr2} are not always fully
1178 independent, since @var{expr1} might have side effects like touching
1179 or deleting files, or it might use @samp{-prune} which would also
1180 affect @var{expr2}.
1181 @end table
1183 @code{find} searches the directory tree rooted at each file name by
1184 evaluating the expression from left to right, according to the rules of
1185 precedence, until the outcome is known (the left hand side is false for
1186 @samp{-and}, true for @samp{-or}), at which point @code{find} moves on
1187 to the next file name.
1189 There are two other tests that can be useful in complex expressions:
1191 @deffn Test -true
1192 Always true.
1193 @end deffn
1195 @deffn Test -false
1196 Always false.
1197 @end deffn
1199 @node Actions, Common Tasks, Finding Files, Top
1200 @chapter Actions
1202 There are several ways you can print information about the files that
1203 match the criteria you gave in the @code{find} expression.  You can
1204 print the information either to the standard output or to a file that
1205 you name.  You can also execute commands that have the file names as
1206 arguments.  You can use those commands as further filters to select files.
1208 @menu
1209 * Print File Name::
1210 * Print File Information::
1211 * Run Commands::
1212 * Delete Files::
1213 * Adding Tests::
1214 @end menu
1216 @node Print File Name
1217 @section Print File Name
1219 @deffn Action -print
1220 True; print the full file name on the standard output, followed by a
1221 newline.
1222 @end deffn
1224 @deffn Action -fprint file
1225 True; print the full file name into file @var{file}, followed by a
1226 newline.  If @var{file} does not exist when @code{find} is run, it is
1227 created; if it does exist, it is truncated to 0 bytes.  The file names
1228 @file{/dev/stdout} and @file{/dev/stderr} are handled specially; they
1229 refer to the standard output and standard error output, respectively.
1230 @end deffn
1232 @node Print File Information
1233 @section Print File Information
1235 @deffn Action -ls
1236 True; list the current file in @samp{ls -dils} format on the standard
1237 output.  The output looks like this:
1239 @smallexample
1240 204744   17 -rw-r--r--   1 djm      staff       17337 Nov  2  1992 ./lwall-quotes
1241 @end smallexample
1243 The fields are:
1245 @enumerate
1246 @item
1247 The inode number of the file.  @xref{Hard Links}, for how to find files
1248 based on their inode number.
1250 @item
1251 the number of blocks in the file.  The block counts are of 1K blocks,
1252 unless the environment variable @code{POSIXLY_CORRECT} is set, in which
1253 case 512-byte blocks are used.  @xref{Size}, for how to find files based
1254 on their size.
1256 @item
1257 The file's type and permissions.  The type is shown as a dash for a
1258 regular file; for other file types, a letter like for @samp{-type} is
1259 used (@pxref{Type}).  The permissions are read, write, and execute for
1260 the file's owner, its group, and other users, respectively; a dash means
1261 the permission is not granted.  @xref{File Permissions}, for more details
1262 about file permissions.  @xref{Permissions}, for how to find files based
1263 on their permissions.
1265 @item
1266 The number of hard links to the file.
1268 @item
1269 The user who owns the file.
1271 @item
1272 The file's group.
1274 @item
1275 The file's size in bytes.
1277 @item
1278 The date the file was last modified.
1280 @item
1281 The file's name.  @samp{-ls} quotes non-printable characters in the file
1282 names using C-like backslash escapes.  
1283 This may change soon, as the treatment of unprintable characters is
1284 harmonised for @samp{-ls}, @samp{-fls}, @samp{-print}, @samp{-fprint},
1285 @samp{-printf} and @samp{-fprintf}.
1286 @end enumerate
1287 @end deffn
1289 @deffn Action -fls file
1290 True; like @samp{-ls} but write to @var{file} like @samp{-fprint}
1291 (@pxref{Print File Name}).
1292 @end deffn
1294 @deffn Action -printf format
1295 True; print @var{format} on the standard output, interpreting @samp{\}
1296 escapes and @samp{%} directives.  Field widths and precisions can be
1297 specified as with the @code{printf} C function.  Format flags (like
1298 @samp{#} for example) may not work as you expect because many of the
1299 fields, even numeric ones, are printed with %s.  This means though
1300 that the format flag @samp{-} will work; it forces left-alignment of
1301 the field.  Unlike @samp{-print}, @samp{-printf} does not add a
1302 newline at the end of the string.  If you want a newline at the end of
1303 the string, add a @samp{\n}.
1304 @end deffn
1306 @deffn Action -fprintf file format
1307 True; like @samp{-printf} but write to @var{file} like @samp{-fprint}
1308 (@pxref{Print File Name}).
1309 @end deffn
1311 @menu
1312 * Escapes::
1313 * Format Directives::
1314 * Time Formats::
1315 @end menu
1317 @node Escapes
1318 @subsection Escapes
1320 The escapes that @samp{-printf} and @samp{-fprintf} recognize are:
1322 @table @code
1323 @item \a
1324 Alarm bell.
1325 @item \b
1326 Backspace.
1327 @item \c
1328 Stop printing from this format immediately and flush the output.
1329 @item \f
1330 Form feed.
1331 @item \n
1332 Newline.
1333 @item \r
1334 Carriage return.
1335 @item \t
1336 Horizontal tab.
1337 @item \v
1338 Vertical tab.
1339 @item \\
1340 A literal backslash (@samp{\}).
1341 @item \NNN
1342 The character whose ASCII code is NNN (octal).
1343 @end table
1345 A @samp{\} character followed by any other character is treated as an
1346 ordinary character, so they both are printed, and a warning message is
1347 printed to the standard error output (because it was probably a typo).
1349 @node Format Directives
1350 @subsection Format Directives
1352 @samp{-printf} and @samp{-fprintf} support the following format
1353 directives to print information about the file being processed.  The C
1354 @code{printf} function, field width and precision specifiers are
1355 supported, as applied to string (%s) types. That is, you can specify
1356 "minimum field width"."maximum field width" for each directive.
1357 Format flags (like @samp{#} for example) may not work as you expect
1358 because many of the fields, even numeric ones, are printed with %s.
1359 The format flag @samp{-} does work; it forces left-alignment of the
1360 field.
1362 @samp{%%} is a literal percent sign.  A @samp{%} character followed by
1363 an unrecognised character (i.e. not a known directive or printf field
1364 width and precision specifier), is discarded (but the unrecognised character
1365 is printed), and a warning message is printed to the standard error output
1366 (because it was probably a typo).
1368 @menu
1369 * Name Directives::
1370 * Ownership Directives::
1371 * Size Directives::
1372 * Location Directives::
1373 * Time Directives::
1374 * Formatting Flags::
1375 @end menu
1377 @node Name Directives
1378 @subsubsection Name Directives
1380 @table @code
1381 @item %p
1382 @c supports %-X.Yp
1383 File's name (not the absolute path name, but the name of the file as
1384 it was encountered by find - that is, as a relative path from one of
1385 the starting points).
1386 @item %f
1387 File's name with any leading directories removed (only the last element).
1388 @c supports %-X.Yf
1389 @item %h
1390 Leading directories of file's name (all but the last element and the
1391 slash before it).  If the file's name contains no slashes (for example
1392 because it was named on the command line and is in the current working
1393 directory), then ``%h'' expands to ``.''.  This prevents ``%h/%f''
1394 expanding to ``/foo'', which would be surprising and probably not
1395 desirable.
1396 @c supports %-X.Yh
1397 @item %P
1398 File's name with the name of the command line argument under which
1399 it was found removed from the beginning.
1400 @c supports %-X.YP
1401 @item %H
1402 Command line argument under which file was found.
1403 @c supports %-X.YH
1404 @end table
1406 @node Ownership Directives
1407 @subsubsection Ownership Directives
1409 @table @code
1410 @item %g
1411 @c supports %-X.Yg
1412 File's group name, or numeric group ID if the group has no name.
1413 @item %G
1414 @c supports %-X.Yg
1415 @c TODO: Needs to support # flag and 0 flag
1416 File's numeric group ID.
1417 @item %u
1418 @c supports %-X.Yu
1419 File's user name, or numeric user ID if the user has no name.
1420 @item %U
1421 @c supports %-X.Yu
1422 @c TODO: Needs to support # flag
1423 File's numeric user ID.
1424 @item %m
1425 @c full support, including # and 0.
1426 File's permissions (in octal).   If you always want to have a leading
1427 zero on the number, use the '#' format flag, for example '%#m'.
1428 @end table
1430 @node Size Directives
1431 @subsubsection Size Directives
1433 @table @code
1434 @item %k
1435 Amount of disk space occupied by the file, measured in 1K blocks
1436 (rounded up).  This can be less than the length of the file if 
1437 it is a sparse file (that is, it has ``holes'').  
1438 @item %b
1439 File's size in 512-byte blocks (rounded up).  This also can be less 
1440 than the length of the file, if the file is sparse.
1441 @item %s
1442 File's size in bytes.
1443 @end table
1445 @node Location Directives
1446 @subsubsection Location Directives
1448 @table @code
1449 @item %d
1450 File's depth in the directory tree (depth below a file named on the
1451 command line, not depth below the root directory).  Files named on the
1452 command line have a depth of 0.  Subdirectories immediately below them
1453 have a depth of 1, and so on.
1454 @item %D
1455 The device number on which the file exists (the @code{st_dev} field of
1456 @code{struct stat}), in decimal.
1457 @item %F
1458 Type of the filesystem the file is on; this value can be used for
1459 @samp{-fstype} (@pxref{Directories}).
1460 @item %l
1461 Object of symbolic link (empty string if file is not a symbolic link).
1462 @item %i
1463 File's inode number (in decimal).
1464 @item %n
1465 Number of hard links to file.
1466 @item %y 
1467 Type of the file as used with @samp{-type}.   If the file is a symbolic
1468 link, @samp{l} will be printed.
1469 @item %Y
1470 Type of the file as used with @samp{-type}.   If the file is a symbolic
1471 link, it is dereferenced.  If the file is a broken symbolic link,
1472 @samp{N} is printed.
1474 @end table
1476 @node Time Directives
1477 @subsubsection Time Directives
1479 Some of these directives use the C @code{ctime} function.  Its output
1480 depends on the current locale, but it typically looks like
1482 @example
1483 Wed Nov  2 00:42:36 1994
1484 @end example
1486 @table @code
1487 @item %a
1488 File's last access time in the format returned by the C @code{ctime} function.
1489 @item %A@var{k}
1490 File's last access time in the format specified by @var{k}
1491 (@pxref{Time Formats}). 
1492 @item %c
1493 File's last status change time in the format returned by the C @code{ctime}
1494 function.
1495 @item %C@var{k}
1496 File's last status change time in the format specified by @var{k}
1497 (@pxref{Time Formats}).
1498 @item %t
1499 File's last modification time in the format returned by the C @code{ctime}
1500 function.
1501 @item %T@var{k}
1502 File's last modification time in the format specified by @var{k} 
1503 (@pxref{Time Formats}). 
1504 @end table
1506 @node Time Formats
1507 @subsection Time Formats
1509 Below are the formats for the directives @samp{%A}, @samp{%C}, and
1510 @samp{%T}, which print the file's timestamps.  Some of these formats
1511 might not be available on all systems, due to differences in the C
1512 @code{strftime} function between systems.
1514 @menu
1515 * Time Components::
1516 * Date Components::
1517 * Combined Time Formats::
1518 @end menu
1520 @node Time Components
1521 @subsubsection Time Components
1523 The following format directives print single components of the time.
1525 @table @code
1526 @item H
1527 hour (00..23)
1528 @item I
1529 hour (01..12)
1530 @item k
1531 hour ( 0..23)
1532 @item l
1533 hour ( 1..12)
1534 @item p
1535 locale's AM or PM
1536 @item Z
1537 time zone (e.g., EDT), or nothing if no time zone is determinable
1538 @item M
1539 minute (00..59)
1540 @item S
1541 second (00..61)
1542 @item @@
1543 seconds since Jan. 1, 1970, 00:00 GMT.
1544 @end table
1546 @node Date Components
1547 @subsubsection Date Components
1549 The following format directives print single components of the date.
1551 @table @code
1552 @item a
1553 locale's abbreviated weekday name (Sun..Sat)
1554 @item A
1555 locale's full weekday name, variable length (Sunday..Saturday)
1556 @item b
1557 @itemx h
1558 locale's abbreviated month name (Jan..Dec)
1559 @item B
1560 locale's full month name, variable length (January..December)
1561 @item m
1562 month (01..12)
1563 @item d
1564 day of month (01..31)
1565 @item w
1566 day of week (0..6)
1567 @item j
1568 day of year (001..366)
1569 @item U
1570 week number of year with Sunday as first day of week (00..53)
1571 @item W
1572 week number of year with Monday as first day of week (00..53)
1573 @item Y
1574 year (1970@dots{})
1575 @item y
1576 last two digits of year (00..99)
1577 @end table
1579 @node Combined Time Formats
1580 @subsubsection Combined Time Formats
1582 The following format directives print combinations of time and date
1583 components. 
1585 @table @code
1586 @item r
1587 time, 12-hour (hh:mm:ss [AP]M)
1588 @item T
1589 time, 24-hour (hh:mm:ss)
1590 @item X
1591 locale's time representation (H:M:S)
1592 @item c
1593 locale's date and time (Sat Nov 04 12:02:33 EST 1989)
1594 @item D
1595 date (mm/dd/yy)
1596 @item x
1597 locale's date representation (mm/dd/yy)
1598 @item +
1599 Date and time, separated by '+', for example `2004-04-28+22:22:05'.
1600 The time is given in the current timezone (which may be affected by
1601 setting the TZ environment variable).  This is a GNU extension.
1602 @end table
1604 @node Formatting Flags
1605 @subsubsection Formatting Flags
1607 The @samp{%m} and @samp{%d} directives support the @samp{#}, @samp{0}
1608 and @samp{+} flags, but the other directives do not, even if they
1609 print numbers.  Numeric directives that do not support these flags
1610 include 
1612 @samp{G},
1613 @samp{U},
1614 @samp{b},
1615 @samp{D},
1616 @samp{k} and
1617 @samp{n}.
1619 All fields support the format flag @samp{-}, which makes fields
1620 left-aligned.  That is, if the field width is greater than the actual
1621 contents of the field, the requisite number of spaces are printed
1622 after the field content instead of before it.
1624 @node Run Commands
1625 @section Run Commands
1627 You can use the list of file names created by @code{find} or
1628 @code{locate} as arguments to other commands.  In this way you can
1629 perform arbitrary actions on the files.
1631 @menu
1632 * Single File::
1633 * Multiple Files::
1634 * Querying::
1635 @end menu
1637 @node Single File
1638 @subsection Single File
1640 Here is how to run a command on one file at a time.
1642 @deffn Action -execdir command ;
1643 Execute @var{command}; true if 0 status is returned.  @code{find} takes
1644 all arguments after @samp{-exec} to be part of the command until an
1645 argument consisting of @samp{;} is reached.  It replaces the string
1646 @samp{@{@}} by the current file name being processed everywhere it
1647 occurs in the command.  Both of these constructions need to be escaped
1648 (with a @samp{\}) or quoted to protect them from expansion by the shell.
1649 The command is executed in the directory in which @code{find} was run.
1651 For example, to compare each C header file in the current directory with
1652 the file @file{/tmp/master}:
1654 @example
1655 find . -name '*.h' -execdir diff -u '@{@}' /tmp/master ';'
1656 @end example
1657 @end deffn
1660 Another similar option, @samp{-exec} is supported, but is less secure.
1661 @xref{Security Considerations}, for a discussion of the security
1662 problems surrounding @samp{-exec}.
1665 @deffn Action -exec command ;
1666 This insecure variant of the @samp{-execdir} action is specified by
1667 POSIX.  The main difference is that the command is executed in the
1668 directory from which @code{find} was invoked, meaning that @samp{@{@}}
1669 is expanded to a relative path starting with the name of one of the
1670 starting directories, rather than just the basename of the matched
1671 file.
1672 @end deffn
1675 @node Multiple Files
1676 @subsection Multiple Files
1678 Sometimes you need to process files one of the time.  But usually this
1679 is not necessary, and, it is faster to run a command on as many files
1680 as possible at a time, rather than once per file.  Doing this saves on
1681 the time it takes to start up the command each time.
1683 The @samp{-execdir} and @samp{-exec} actions have variants that build
1684 command lines containing as many matched files as possible.  
1686 @deffn Action -execdir command @{@} +
1687 This works as for @samp{-execdir command ;}, except that the
1688 @samp{@{@}} at the end of the command is expanded to a list of names
1689 of matching files.  This expansion is done in such a way as to avoid
1690 exceeding the maximum command line length available on the system.
1691 Only one @samp{@{@}} is allowed within the command, and it must appear
1692 at the end, immediately before the @samp{+}.  A @samp{+} appearing in
1693 any position other than immediately after @samp{@{@}} is not
1694 considered to be special (that is, it does not terminate the command).
1695 @end deffn
1698 @deffn Action -exec command @{@} +
1699 This insecure variant of the @samp{-execdir} action is specified by
1700 POSIX.  The main difference is that the command is executed in the
1701 directory from which @code{find} was invoked, meaning that @samp{@{@}}
1702 is expanded to a relative path starting with the name of one of the
1703 starting directories, rather than just the basename of the matched
1704 file.
1705 @end deffn
1707 Before @code{find} exits, any partially-built command lines are
1708 executed.  This happens even if the exit was caused by the
1709 @samp{-quit} action.  However, some types of error (for example not
1710 being able to invoke @code{stat()} on the current directory) can cause
1711 an immediate fatal exit.  In this situation, any partially-built
1712 command lines will not be invoked (this prevents possible infinite
1713 loops).
1715 Another, but less secure, way to run a command on more than one file
1716 at once, is to use the @code{xargs} command, which is invoked like this:
1718 @example
1719 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
1720 @end example
1722 @code{xargs} normally reads arguments from the standard input.  These
1723 arguments are delimited by blanks (which can be protected with double
1724 or single quotes or a backslash) or newlines.  It executes the
1725 @var{command} (default is @file{/bin/echo}) one or more times with any
1726 @var{initial-arguments} followed by arguments read from standard
1727 input.  Blank lines on the standard input are ignored.
1729 Instead of blank-delimited names, it is safer to use @samp{find -print0}
1730 or @samp{find -fprint0} and process the output by giving the @samp{-0}
1731 or @samp{--null} option to GNU @code{xargs}, GNU @code{tar}, GNU
1732 @code{cpio}, or @code{perl}.  The @code{locate} command also has a
1733 @samp{-0} or @samp{--null} option which does the same thing.
1735 You can use shell command substitution (backquotes) to process a list of
1736 arguments, like this:
1738 @example
1739 grep -l sprintf `find $HOME -name '*.c' -print`
1740 @end example
1742 However, that method produces an error if the length of the @samp{.c}
1743 file names exceeds the operating system's command-line length limit.
1744 @code{xargs} avoids that problem by running the command as many times as
1745 necessary without exceeding the limit:
1747 @example
1748 find $HOME -name '*.c' -print | xargs grep -l sprintf
1749 @end example
1751 However, if the command needs to have its standard input be a terminal
1752 (@code{less}, for example), you have to use the shell command
1753 substitution method or use the @samp{--arg-file} option of
1754 @code{xargs}.
1756 The @code{xargs} command will process all its input, building command
1757 lines and executing them, unless one of the commands exits with a
1758 status of 255 (this will cause xargs to issue an error message and
1759 stop) or it reads a line contains the end of file string specified
1760 with the @samp{--eof} option. 
1762 @menu
1763 * Unsafe File Name Handling::
1764 * Safe File Name Handling::
1765 * Unusual Characters in File Names::
1766 * Limiting Command Size::
1767 * Interspersing File Names::
1768 @end menu
1770 @node Unsafe File Name Handling
1771 @subsubsection Unsafe File Name Handling
1773 Because file names can contain quotes, backslashes, blank characters,
1774 and even newlines, it is not safe to process them using @code{xargs} in its
1775 default mode of operation.  But since most files' names do not contain
1776 blanks, this problem occurs only infrequently.  If you are only
1777 searching through files that you know have safe names, then you need not
1778 be concerned about it.  
1780 @c This example is adapted from:
1781 @c From: pfalstad@stone.Princeton.EDU (Paul John Falstad)
1782 @c Newsgroups: comp.unix.shell
1783 @c Subject: Re: Beware xargs security holes
1784 @c Date: 16 Oct 90 19:12:06 GMT
1785 @c 
1786 In many applications, if @code{xargs} botches processing a file because
1787 its name contains special characters, some data might be lost.  The
1788 importance of this problem depends on the importance of the data and
1789 whether anyone notices the loss soon enough to correct it.  However,
1790 here is an extreme example of the problems that using blank-delimited
1791 names can cause.  If the following command is run daily from
1792 @code{cron}, then any user can remove any file on the system:
1794 @example
1795 find / -name '#*' -atime +7 -print | xargs rm
1796 @end example
1798 For example, you could do something like this:
1800 @example
1801 eg$ echo > '#
1802 vmunix'
1803 @end example
1805 @noindent
1806 and then @code{cron} would delete @file{/vmunix}, if it ran
1807 @code{xargs} with @file{/} as its current directory.
1809 To delete other files, for example @file{/u/joeuser/.plan}, you could do
1810 this:
1812 @example
1813 eg$ mkdir '#
1815 eg$ cd '#
1817 eg$ mkdir u u/joeuser u/joeuser/.plan'
1819 eg$ echo > u/joeuser/.plan'
1820 /#foo'
1821 eg$ cd ..
1822 eg$ find . -name '#*' -print | xargs echo
1823 ./# ./# /u/joeuser/.plan /#foo
1824 @end example
1826 @node Safe File Name Handling
1827 @subsubsection Safe File Name Handling
1829 Here is how to make @code{find} output file names so that they can be
1830 used by other programs without being mangled or misinterpreted.  You can
1831 process file names generated this way by giving the @samp{-0} or
1832 @samp{--null} option to GNU @code{xargs}, GNU @code{tar}, GNU
1833 @code{cpio}, or @code{perl}.
1835 @deffn Action -print0
1836 True; print the full file name on the standard output, followed by a
1837 null character.
1838 @end deffn
1840 @deffn Action -fprint0 file
1841 True; like @samp{-print0} but write to @var{file} like @samp{-fprint}
1842 (@pxref{Print File Name}).
1843 @end deffn
1845 As of findutils version 4.2.4, the @code{locate} program also has a
1846 @samp{--null} option which does the same thing.  For similarity with
1847 @code{xargs}, the short form of the option @samp{-0} can also be used.
1849 If you want to be able to handle file names safely but need to run
1850 commands which want to be connected to a terminal on their input, you
1851 can use the @samp{--arg-file} option to @code{xargs} like this:
1853 @example
1854 find / -name xyzzy -print0 > list
1855 xargs --null --arg-file=list munge
1856 @end example
1858 The example above runs the @code{munge} program on all the files named
1859 @file{xyzzy} that we can find, but @code{munge}'s input will still be
1860 the terminal (or whatever the shell was using as standard input).  If
1861 your shell has the ``process substitution'' feature @samp{<(...)}, you
1862 can do this in just one step:
1864 @example
1865 xargs --null --arg-file=<(find / -name xyzzy -print0) munge
1866 @end example
1868 @node Unusual Characters in File Names
1869 @subsubsection Unusual Characters in File Names
1870 As discussed above, you often need to be careful about how the names
1871 of files are handled by @code{find} and other programs.  If the output
1872 of @code{find} is not going to another program but instead is being
1873 shown on a terminal, this can still be a problem.  For example, some
1874 character sequences can reprogram the function keys on some terminals.
1875 @xref{Security Considerations}, for a discussion of other security
1876 problems relating to @code{find}.
1878 Unusual characters are handled differently by various
1879 actions, as described below.
1881 @table @samp
1882 @item -print0
1883 @itemx -fprint0
1884 Always print the exact filename, unchanged, even if the output is
1885 going to a terminal.
1886 @item -ok
1887 @itemx -okdir
1888 Always print the exact filename, unchanged.  This will probably change
1889 in a future release.
1890 @item -ls
1891 @itemx -fls
1892 Unusual characters are always escaped.  White space, backslash, and
1893 double quote characters are printed using C-style escaping (for
1894 example @samp{\f}, @samp{\"}).  Other unusual characters are printed
1895 using an octal escape.  Other Printable characters (for @samp{-ls} and
1896 @samp{-fls} these are the characters between octal 041 and 0176) are
1897 printed as-is.
1898 @item -printf
1899 @itemx -fprintf
1900 If the output is not going to a terminal, it is printed as-is.
1901 Otherwise, the result depends on which directive is in use:
1903 @table @asis
1904 @item %D, %F, %H, %Y, %y 
1905 These expand to values which are not under control of files' ownwers,
1906 and so are printed as-is.
1907 @item  %a, %b, %c, %d, %g, %G, %i, %k, %m, %M, %n, %s, %t, %u, %U
1908 These have values which are under the control of files' ownwers but which
1909 cannot be used to send arbitrary data to the terminal, and so these
1910 are printed as-is.  
1911 @item %f, %h, %l, %p, %P 
1912 The output of these directives is quoted if the output is going to a
1913 terminal.
1915 This quoting is performed in the same way as for GNU @code{ls}.   
1916 This is not the same quoting mechanism as the one used for  @samp{-ls} and
1917 @samp{fls}.   If you are able to decide what format to use for the output 
1918 of @code{find} then it is normally better to use @samp{\0} as a terminator
1919 than to use newline, as file names can contain white space and newline
1920 characters.
1921 @end table 
1922 @item -print
1923 @itemx -fprint
1924 Quoting is handled in the same way as for the @samp{%p} directive
1925 of @samp{-printf} and @samp{-fprintf}.  If you are using @code{find} in
1926 a script or in a situation where the matched files might have
1927 arbitrary names, you should consider using @samp{-print0} instead of
1928 @samp{-print}.
1929 @end table
1932 The @code{locate} program quotes and escapes unusual characters in
1933 file names in the same way as @code{find}'s @samp{-print} action.
1935 The behaviours described above may change soon, as the treatment of
1936 unprintable characters is harmonised for @samp{-ls}, @samp{-fls},
1937 @samp{-print}, @samp{-fprint}, @samp{-printf} and @samp{-fprintf}.
1939 @node Limiting Command Size
1940 @subsubsection Limiting Command Size
1942 @code{xargs} gives you control over how many arguments it passes to the
1943 command each time it executes it.  By default, it uses up to
1944 @code{ARG_MAX} - 2k, or 128k, whichever is smaller, characters per
1945 command.  It uses as many lines and arguments as fit within that limit.
1946 The following options modify those values.
1948 @table @code
1949 @item --no-run-if-empty
1950 @itemx -r
1951 If the standard input does not contain any nonblanks, do not run the
1952 command.  By default, the command is run once even if there is no input.
1954 @item --max-lines@r{[}=@var{max-lines}@r{]}
1955 @itemx -l@r{[}@var{max-lines}@r{]}
1956 Use at most @var{max-lines} nonblank input lines per command line;
1957 @var{max-lines} defaults to 1 if omitted.  Trailing blanks cause an
1958 input line to be logically continued on the next input line, for the
1959 purpose of counting the lines.  Implies @samp{-x}.
1961 @item --max-args=@var{max-args}
1962 @itemx -n @var{max-args}
1963 Use at most @var{max-args} arguments per command line.  Fewer than
1964 @var{max-args} arguments will be used if the size (see the @samp{-s}
1965 option) is exceeded, unless the @samp{-x} option is given, in which case
1966 @code{xargs} will exit.
1968 @item --max-chars=@var{max-chars}
1969 @itemx -s @var{max-chars}
1970 Use at most @var{max-chars} characters per command line, including the
1971 command and initial arguments and the terminating nulls at the ends of
1972 the argument strings.  If you specify a value for this option which is
1973 too large or small, a warning message is printed and the appropriate
1974 upper or lower limit is used instead.
1976 @item --max-procs=@var{max-procs}
1977 @itemx -P @var{max-procs}
1978 Run up to @var{max-procs} processes at a time; the default is 1.  If
1979 @var{max-procs} is 0, @code{xargs} will run as many processes as
1980 possible at a time.  Use the @samp{-n}, @samp{-s}, or @samp{-l} option
1981 with @samp{-P}; otherwise chances are that the command will be run only
1982 once.
1983 @end table
1985 @node Interspersing File Names
1986 @subsubsection Interspersing File Names
1988 @code{xargs} can insert the name of the file it is processing between
1989 arguments you give for the command.  Unless you also give options to
1990 limit the command size (@pxref{Limiting Command Size}), this mode of
1991 operation is equivalent to @samp{find -exec} (@pxref{Single File}).
1993 @table @code
1994 @item --replace@r{[}=@var{replace-str}@r{]}
1995 @itemx -i@r{[}@var{replace-str}@r{]}
1996 Replace occurrences of @var{replace-str} in the initial arguments with
1997 names read from the input.  Also, unquoted blanks do not
1998 terminate arguments; instead, the input is split at newlines only.  If
1999 @var{replace-str} is omitted, it defaults to @samp{@{@}} (like for
2000 @samp{find -exec}).  Implies @samp{-x} and @samp{-l 1}.  As an
2001 example, to sort each file the @file{bills} directory, leaving the
2002 output in that file name with @file{.sorted} appended, you could do:
2004 @example
2005 find bills -type f | xargs -iXX sort -o XX.sorted XX
2006 @end example
2008 @noindent
2009 The equivalent command using @samp{find -exec} is:
2011 @example
2012 find bills -type f -exec sort -o '@{@}.sorted' '@{@}' ';'
2013 @end example
2014 @end table
2016 @node Querying
2017 @subsection Querying
2019 To ask the user whether to execute a command on a single file, you can
2020 use the @code{find} primary @samp{-ok} instead of @samp{-exec}:
2022 @deffn Action -ok command ;
2023 Like @samp{-exec} (@pxref{Single File}), but ask the user first (on
2024 the standard input); if the response does not start with @samp{y} or
2025 @samp{Y}, do not run the command, and return false.
2026 @end deffn
2028 When processing multiple files with a single command, to query the user
2029 you give @code{xargs} the following option.  When using this option, you
2030 might find it useful to control the number of files processed per
2031 invocation of the command (@pxref{Limiting Command Size}).
2033 @table @code
2034 @item --interactive
2035 @itemx -p
2036 Prompt the user about whether to run each command line and read a line
2037 from the terminal.  Only run the command line if the response starts
2038 with @samp{y} or @samp{Y}.  Implies @samp{-t}.
2039 @end table
2041 @node Delete Files
2042 @section Delete Files
2044 @deffn Action -delete
2045 Delete files or directories; true if removal succeeded.  If the
2046 removal failed, an error message is issued.
2048 The use of the @samp{-delete} action on the command line automatically
2049 turns on the @samp{-depth} option (@pxref{find Expressions}).
2050 @end deffn
2052 @node Adding Tests
2053 @section Adding Tests
2055 You can test for file attributes that none of the @code{find} builtin
2056 tests check.  To do this, use @code{xargs} to run a program that filters
2057 a list of files printed by @code{find}.  If possible, use @code{find}
2058 builtin tests to pare down the list, so the program run by @code{xargs}
2059 has less work to do.  The tests builtin to @code{find} will likely run
2060 faster than tests that other programs perform.
2062 For reasons of efficiency it is often useful to limit the number of
2063 times an external program has to be run.  For this reason, it is often
2064 a good idea to implement ``extended'' tests by using @code{xargs}.
2066 For example, here is a way to print the names of all of the unstripped
2067 binaries in the @file{/usr/local} directory tree.  Builtin tests avoid
2068 running @code{file} on files that are not regular files or are not
2069 executable.
2071 @example
2072 find /usr/local -type f -perm /a=x | xargs file | 
2073   grep 'not stripped' | cut -d: -f1
2074 @end example
2076 @noindent
2077 The @code{cut} program removes everything after the file name from the
2078 output of @code{file}.
2080 However, using @code{xargs} can present important security problems
2081 (@pxref{Security Considerations}).  These can be avoided by using
2082 @samp{-execdir}.  The @samp{-execdir} action is also a useful way of
2083 putting your own test in the middle of a set of other tests or actions
2084 for @code{find} (for example, you might want to use @samp{-prune}).
2086 @c Idea from Martin Weitzel.
2087 To place a special test somewhere in the middle of a @code{find}
2088 expression, you can use @samp{-execdir} (or, less securely,
2089 @samp{-exec}) to run a program that performs the test.  Because
2090 @samp{-execdir} evaluates to the exit status of the executed program,
2091 you can use a program (which can be a shell script) that tests for a
2092 special attribute and make it exit with a true (zero) or false
2093 (non-zero) status.  It is a good idea to place such a special test
2094 @emph{after} the builtin tests, because it starts a new process which
2095 could be avoided if a builtin test evaluates to false.  
2097 Here is a shell script called @code{unstripped} that checks whether its
2098 argument is an unstripped binary file:
2100 @example
2101 #! /bin/sh
2102 file "$1" | grep -q "not stripped"
2103 @end example
2106 This script relies on the fact that the shell exits with the status of
2107 the last command in the pipeline, in this case @code{grep}.  The
2108 @code{grep} command exits with a true status if it found any matches,
2109 false if not.  Here is an example of using the script (assuming it is
2110 in your search path).  It lists the stripped executables (and shell
2111 scripts) in the file @file{sbins} and the unstripped ones in
2112 @file{ubins}.
2114 @example
2115 find /usr/local -type f -perm /a=x \
2116   \( -execdir unstripped '@{@}' \; -fprint ubins -o -fprint sbins \)
2117 @end example
2121 @node Common Tasks, Databases, Actions, Top
2122 @chapter Common Tasks
2124 The sections that follow contain some extended examples that both give a
2125 good idea of the power of these programs, and show you how to solve
2126 common real-world problems.
2128 @menu
2129 * Viewing And Editing::
2130 * Archiving::
2131 * Cleaning Up::
2132 * Strange File Names::
2133 * Fixing Permissions::
2134 * Classifying Files::
2135 @end menu
2137 @node Viewing And Editing
2138 @section Viewing And Editing
2140 To view a list of files that meet certain criteria, simply run your file
2141 viewing program with the file names as arguments.  Shells substitute a
2142 command enclosed in backquotes with its output, so the whole command
2143 looks like this:
2145 @example
2146 less `find /usr/include -name '*.h' | xargs grep -l mode_t`
2147 @end example
2149 @noindent
2150 You can edit those files by giving an editor name instead of a file
2151 viewing program:
2153 @example
2154 emacs `find /usr/include -name '*.h' | xargs grep -l mode_t`
2155 @end example
2157 Because there is a limit to the length of any individual command line,
2158 there is a limit to the number of files that can be handled in this
2159 way.  We can get around this difficulty by using xargs like this:
2161 @example
2162 find /usr/include -name '*.h' | xargs grep -l mode_t > todo
2163 xargs --arg-file=todo emacs
2164 @end example
2166 Here, @code{xargs} will run @code{emacs} as many times as necessary to
2167 visit all of the files listed in the file @file{todo}.
2169 @node Archiving
2170 @section Archiving
2172 You can pass a list of files produced by @code{find} to a file archiving
2173 program.  GNU @code{tar} and @code{cpio} can both read lists of file
2174 names from the standard input---either delimited by nulls (the safe way)
2175 or by blanks (the lazy, risky default way).  To use null-delimited
2176 names, give them the @samp{--null} option.  You can store a file archive
2177 in a file, write it on a tape, or send it over a network to extract on
2178 another machine.
2180 One common use of @code{find} to archive files is to send a list of the
2181 files in a directory tree to @code{cpio}.  Use @samp{-depth} so if a
2182 directory does not have write permission for its owner, its contents can
2183 still be restored from the archive since the directory's permissions are
2184 restored after its contents.  Here is an example of doing this using
2185 @code{cpio}; you could use a more complex @code{find} expression to
2186 archive only certain files.
2188 @example
2189 find . -depth -print0 |
2190   cpio --create --null --format=crc --file=/dev/nrst0
2191 @end example
2193 You could restore that archive using this command:
2195 @example
2196 cpio --extract --null --make-dir --unconditional \
2197   --preserve --file=/dev/nrst0
2198 @end example
2200 Here are the commands to do the same things using @code{tar}:
2202 @example
2203 find . -depth -print0 |
2204   tar --create --null --files-from=- --file=/dev/nrst0
2206 tar --extract --null --preserve-perm --same-owner \
2207   --file=/dev/nrst0
2208 @end example
2210 @c Idea from Rick Sladkey.
2211 Here is an example of copying a directory from one machine to another:
2213 @example
2214 find . -depth -print0 | cpio -0o -Hnewc |
2215   rsh @var{other-machine} "cd `pwd` && cpio -i0dum"
2216 @end example
2218 @node Cleaning Up
2219 @section Cleaning Up
2221 @c Idea from Jim Meyering.
2222 This section gives examples of removing unwanted files in various situations.
2223 Here is a command to remove the CVS backup files created when an update
2224 requires a merge:
2226 @example
2227 find . -name '.#*' -print0 | xargs -0r rm -f
2228 @end example
2230 The command above works, but the following is safer:
2232 @example
2233 find . -name '.#*' -depth -delete
2234 @end example
2236 @c Idea from Franc,ois Pinard.
2237 You can run this command to clean out your clutter in @file{/tmp}.  You
2238 might place it in the file your shell runs when you log out
2239 (@file{.bash_logout}, @file{.logout}, or @file{.zlogout}, depending on
2240 which shell you use).
2242 @example
2243 find /tmp -depth -user "$LOGNAME" -type f -delete 
2244 @end example
2246 If your @code{find} command removes directories, you may find that 
2247 you get a spurious error message when @code{find} tries to recurse
2248 into a directory that has now been removed.  Using the @samp{-depth}
2249 option will normally resolve this problem.
2251 @c Idea from Noah Friedman.
2252 To remove old Emacs backup and auto-save files, you can use a command
2253 like the following.  It is especially important in this case to use
2254 null-terminated file names because Emacs packages like the VM mailer
2255 often create temporary file names with spaces in them, like @file{#reply
2256 to David J. MacKenzie<1>#}.
2258 @example
2259 find ~ \( -name '*~' -o -name '#*#' \) -print0 |
2260   xargs --no-run-if-empty --null rm -vf
2261 @end example
2263 Removing old files from @file{/tmp} is commonly done from @code{cron}:
2265 @c Idea from Kaveh Ghazi.
2266 @example
2267 find /tmp /var/tmp -not -type d -mtime +3 -delete
2268 find /tmp /var/tmp -depth -mindepth 1 -type d -empty -delete
2269 @end example
2271 The second @code{find} command above uses @samp{-depth} so it cleans out
2272 empty directories depth-first, hoping that the parents become empty and
2273 can be removed too.  It uses @samp{-mindepth} to avoid removing
2274 @file{/tmp} itself if it becomes totally empty.
2276 @node Strange File Names
2277 @section Strange File Names
2279 @c Idea from:
2280 @c From: tmatimar@isgtec.com (Ted Timar)
2281 @c Newsgroups: comp.unix.questions,comp.unix.shell,comp.answers,news.answers
2282 @c Subject: Unix - Frequently Asked Questions (2/7) [Frequent posting]
2283 @c Subject: How do I remove a file with funny characters in the filename ?
2284 @c Date: Thu Mar 18 17:16:55 EST 1993
2285 @code{find} can help you remove or rename a file with strange characters
2286 in its name.  People are sometimes stymied by files whose names contain
2287 characters such as spaces, tabs, control characters, or characters with
2288 the high bit set.  The simplest way to remove such files is:
2290 @example
2291 rm -i @var{some*pattern*that*matches*the*problem*file}
2292 @end example
2294 @code{rm} asks you whether to remove each file matching the given
2295 pattern.  If you are using an old shell, this approach might not work if
2296 the file name contains a character with the high bit set; the shell may
2297 strip it off.  A more reliable way is:
2299 @example
2300 find . -maxdepth 1 @var{tests} -ok rm '@{@}' \;
2301 @end example
2303 @noindent
2304 where @var{tests} uniquely identify the file.  The @samp{-maxdepth 1}
2305 option prevents @code{find} from wasting time searching for the file in
2306 any subdirectories; if there are no subdirectories, you may omit it.  A
2307 good way to uniquely identify the problem file is to figure out its
2308 inode number; use
2310 @example
2311 ls -i
2312 @end example
2314 Suppose you have a file whose name contains control characters, and you
2315 have found that its inode number is 12345.  This command prompts you for
2316 whether to remove it:
2318 @example
2319 find . -maxdepth 1 -inum 12345 -ok rm -f '@{@}' \;
2320 @end example
2322 If you don't want to be asked, perhaps because the file name may contain
2323 a strange character sequence that will mess up your screen when printed,
2324 then use @samp{-exec} instead of @samp{-ok}.
2326 If you want to rename the file instead, you can use @code{mv} instead of
2327 @code{rm}:
2329 @example
2330 find . -maxdepth 1 -inum 12345 -ok mv '@{@}' @var{new-file-name} \;
2331 @end example
2333 @node Fixing Permissions
2334 @section Fixing Permissions
2336 Suppose you want to make sure that everyone can write to the directories in a
2337 certain directory tree.  Here is a way to find directories lacking either
2338 user or group write permission (or both), and fix their permissions:
2340 @example
2341 find . -type d -not -perm -ug=w | xargs chmod ug+w
2342 @end example
2344 @noindent
2345 You could also reverse the operations, if you want to make sure that
2346 directories do @emph{not} have world write permission.
2348 @node Classifying Files
2349 @section Classifying Files
2351 @c Idea from:
2352 @c From: martin@mwtech.UUCP (Martin Weitzel)
2353 @c Newsgroups: comp.unix.wizards,comp.unix.questions
2354 @c Subject: Advanced usage of 'find' (Re: Unix security automating script)
2355 @c Date: 22 Mar 90 15:05:19 GMT
2356 If you want to classify a set of files into several groups based on
2357 different criteria, you can use the comma operator to perform multiple
2358 independent tests on the files.  Here is an example:
2360 @example
2361 find / -type d \( -perm -o=w -fprint allwrite , \
2362   -perm -o=x -fprint allexec \)
2364 echo "Directories that can be written to by everyone:"
2365 cat allwrite
2366 echo ""
2367 echo "Directories with search permissions for everyone:"
2368 cat allexec
2369 @end example
2371 @code{find} has only to make one scan through the directory tree (which
2372 is one of the most time consuming parts of its work).
2374 @node Databases, File Permissions, Common Tasks, Top
2375 @chapter File Name Databases
2377 The file name databases used by @code{locate} contain lists of files
2378 that were in particular directory trees when the databases were last
2379 updated.  The file name of the default database is determined when
2380 @code{locate} and @code{updatedb} are configured and installed.  The
2381 frequency with which the databases are updated and the directories for
2382 which they contain entries depend on how often @code{updatedb} is run,
2383 and with which arguments.
2385 You can obtain some statistics about the databases by using
2386 @samp{locate --statistics}.
2388 @menu
2389 * Database Locations::
2390 * Database Formats::
2391 * Newline Handling::
2392 @end menu
2395 @node Database Locations
2396 @section Database Locations
2398 There can be multiple file name databases.  Users can select which
2399 databases @code{locate} searches using the @code{LOCATE_PATH}
2400 environment variable or a command line option.  The system
2401 administrator can choose the file name of the default database, the
2402 frequency with which the databases are updated, and the directories
2403 for which they contain entries.  File name databases are updated by
2404 running the @code{updatedb} program, typically nightly.
2406 In networked environments, it often makes sense to build a database at
2407 the root of each filesystem, containing the entries for that filesystem.
2408 @code{updatedb} is then run for each filesystem on the fileserver where
2409 that filesystem is on a local disk, to prevent thrashing the network.
2411 @xref{Invoking updatedb},
2412 for the description of the options to @code{updatedb}, which specify
2413 which directories would each database contain entries for.
2416 @node Database Formats
2417 @section Database Formats
2419 The file name databases contain lists of files that were in particular
2420 directory trees when the databases were last updated.  The file name
2421 database format changed starting with GNU @code{locate} version 4.0 to
2422 allow machines with different byte orderings to share the databases.  The
2423 new GNU @code{locate} can read both the old and new database formats.
2424 However, old versions of @code{locate} and @code{find} produce incorrect
2425 results if given a new-format database.
2427 If you run @samp{locate --statistics}, the resulting summary indicates
2428 the type of each locate database.  
2431 @menu
2432 * New Database Format::
2433 * Sample Database::
2434 * Old Database Format::
2435 @end menu
2437 @node New Database Format
2438 @subsection New Database Format
2440 @code{updatedb} runs a program called @code{frcode} to
2441 @dfn{front-compress} the list of file names, which reduces the database
2442 size by a factor of 4 to 5.  Front-compression (also known as
2443 incremental encoding) works as follows.
2445 The database entries are a sorted list (case-insensitively, for users'
2446 convenience).  Since the list is sorted, each entry is likely to share a
2447 prefix (initial string) with the previous entry.  Each database entry
2448 begins with an offset-differential count byte, which is the additional
2449 number of characters of prefix of the preceding entry to use beyond the
2450 number that the preceding entry is using of its predecessor.  (The
2451 counts can be negative.)  Following the count is a null-terminated ASCII
2452 remainder---the part of the name that follows the shared prefix.
2454 If the offset-differential count is larger than can be stored in a byte
2455 (+/-127), the byte has the value 0x80 and the count follows in a 2-byte
2456 word, with the high byte first (network byte order).
2458 Every database begins with a dummy entry for a file called
2459 @file{LOCATE02}, which @code{locate} checks for to ensure that the
2460 database file has the correct format; it ignores the entry in doing the
2461 search.
2463 Databases can not be concatenated together, even if the first (dummy)
2464 entry is trimmed from all but the first database.  This is because the
2465 offset-differential count in the first entry of the second and following
2466 databases will be wrong.
2468 In the output of @samp{locate --statistics}, the new database format
2469 is referred to as @samp{LOCATE02}.
2471 @node Sample Database
2472 @subsection Sample Database
2474 Sample input to @code{frcode}:
2475 @c with nulls changed to newlines:
2477 @example
2478 /usr/src
2479 /usr/src/cmd/aardvark.c
2480 /usr/src/cmd/armadillo.c
2481 /usr/tmp/zoo
2482 @end example
2484 Length of the longest prefix of the preceding entry to share:
2486 @example
2487 0 /usr/src
2488 8 /cmd/aardvark.c
2489 14 rmadillo.c
2490 5 tmp/zoo
2491 @end example
2493 Output from @code{frcode}, with trailing nulls changed to newlines
2494 and count bytes made printable:
2496 @example
2497 0 LOCATE02
2498 0 /usr/src
2499 8 /cmd/aardvark.c
2500 6 rmadillo.c
2501 -9 tmp/zoo
2502 @end example
2504 (6 = 14 - 8, and -9 = 5 - 14)
2506 @node Old Database Format
2507 @subsection Old Database Format
2509 The old database format is used by Unix @code{locate} and @code{find}
2510 programs and earlier releases of the GNU ones.  @code{updatedb} produces
2511 this format if given the @samp{--old-format} option.
2513 @code{updatedb} runs programs called @code{bigram} and @code{code} to
2514 produce old-format databases.  The old format differs from the new one
2515 in the following ways.  Instead of each entry starting with an
2516 offset-differential count byte and ending with a null, byte values from
2517 0 through 28 indicate offset-differential counts from -14 through 14.
2518 The byte value indicating that a long offset-differential count follows
2519 is 0x1e (30), not 0x80.  The long counts are stored in host byte order,
2520 which is not necessarily network byte order, and host integer word size,
2521 which is usually 4 bytes.  They also represent a count 14 less than
2522 their value.  The database lines have no termination byte; the start of
2523 the next line is indicated by its first byte having a value <= 30.
2525 In addition, instead of starting with a dummy entry, the old database
2526 format starts with a 256 byte table containing the 128 most common
2527 bigrams in the file list.  A bigram is a pair of adjacent bytes.  Bytes
2528 in the database that have the high bit set are indexes (with the high
2529 bit cleared) into the bigram table.  The bigram and offset-differential
2530 count coding makes these databases 20-25% smaller than the new format,
2531 but makes them not 8-bit clean.  Any byte in a file name that is in the
2532 ranges used for the special codes is replaced in the database by a
2533 question mark, which not coincidentally is the shell wildcard to match a
2534 single character.
2536 The old format therefore can not faithfully store entries with non-ASCII
2537 characters. It therefore should not be used in internationalized
2538 environments.
2540 The output of @samp{locate --statistics} will give an incorrect count
2541 of the number of filenames containing newlines or high-bit characters
2542 for old-format databases.
2544 @node Newline Handling
2545 @section Newline Handling
2547 Within the database, filenames are terminated with a null character.
2548 This is the case for both the old and the new format.  
2550 When the new database format is being used, the compression technique
2551 used to generate the database though relies on the ability to sort the
2552 list of files before they are presented to @code{frcode}.
2554 If the system's sort command allows its input list of files to be
2555 separated with null characters via the @samp{-z} option, this option
2556 is used and therefore @code{updatedb} and @code{locate} will both
2557 correctly handle filenames containing newlines.  If the @code{sort}
2558 command lacks support for this, the list of files is delimited with
2559 the newline character, meaning that parts of filenames containing
2560 newlines will be incorrectly sorted.  This can result in both
2561 incorrect matches and incorrect failures to match.
2563 On the other hand, if you are using the old database format, filenames
2564 with embedded newlines are not correctly handled.  There is no
2565 technical limitation which enforces this, it's just that the
2566 @code{bigram} program has no been updated to support lists of
2567 filenames separated by nulls.
2569 So, if you are using the new database format (this is the default) and
2570 your system uses GNU @code{find}, newlines will be correctly handled
2571 at all times.  Otherwise, newlines may not be correctly handled.
2573 @node File Permissions, Reference, Databases, Top
2574 @chapter File Permissions
2576 @include perm.texi
2578 @node Reference, Security Considerations, File Permissions, Top
2579 @chapter Reference
2581 Below are summaries of the command line syntax for the programs
2582 discussed in this manual.
2584 @menu
2585 * Invoking find::
2586 * Invoking locate::
2587 * Invoking updatedb::
2588 * Invoking xargs::
2589 @end menu
2591 @node Invoking find, Invoking locate, , Reference
2592 @section Invoking @code{find}
2594 @example
2595 find @r{[-H] [-L] [-P]} @r{[}@var{file}@dots{}@r{]} @r{[}@var{expression}@r{]}
2596 @end example
2598 @code{find} searches the directory tree rooted at each file name
2599 @var{file} by evaluating the @var{expression} on each file it finds in
2600 the tree.
2602 The options @samp{-H}, @samp{-L} or @samp{-P} may be specified at the
2603 start of the command line (if none of these is specified, @samp{-P} is
2604 assumed).  The arguments after these are a list of files or
2605 directories that should be searched.
2607 This list of files to search is followed by a list of expressions
2608 describing the files we wish to search for.  The first part of the
2609 expression is recognised by the fact that it begins with @samp{-},
2610 @samp{(}, @samp{)}, @samp{,}, or @samp{!}.  Any arguments after it are
2611 the rest of the expression.  If no paths are given, the current
2612 directory is used.  If no expression is given, the expression
2613 @samp{-print} is used.
2615 @code{find} exits with status 0 if all files are processed successfully,
2616 greater than 0 if errors occur.
2618 Three options can precede the list of path names.  They determine the
2619 way that symbolic links are handled.
2621 @table @code
2622 @item -P
2623 Never follow symbolic links (this is the default), except in the case
2624 of the @samp{-xtype} predicate.
2625 @item -L
2626 Always follow symbolic links, except in the case of the @samp{-xtype}
2627 predicate.
2628 @item -H
2629 Follow symbolic links specified in the list of paths to search, or
2630 which are otherwise specified on the command line.
2631 @end table
2633 If @code{find} would follow a symbolic link, but cannot for any reason
2634 (for example, because it has insufficient permissions or the link is
2635 broken), it falls back on using the properties of the symbolic link
2636 itself.  @ref{Symbolic Links} for a more complete description of how
2637 symbolic links are handled.
2639 @xref{Primary Index}, for a summary of all of the tests, actions, and
2640 options that the expression can contain.  If the expression is
2641 missing, @samp{-print} is assumed.
2645 @code{find} also recognizes two options for administrative use:
2647 @table @code
2648 @item --help
2649 Print a summary of the command-line argument format and exit.
2650 @item --version
2651 Print the version number of @code{find} and exit.
2652 @end table
2655 @menu
2656 * Warning Messages::
2657 @end menu
2660 @node Warning Messages,,, Invoking find
2661 @subsection Warning Messages
2663 If there is an error on the @code{find} command line, an error message
2664 is normally issued.  However, there are some usages that are
2665 inadvisable but which @code{find} should still accept.  Under these
2666 circumstances, @code{find} may issue a warning message.  By default,
2667 warnings are enabled only if @code{find} is being run interactively
2668 (specifically, if the standard input is a terminal).  Warning messages
2669 can be controlled explicitly by the use of options on the command
2670 line:
2672 @table @code
2673 @item -warn
2674 Issue warning messages where appropriate.
2675 @item -nowarn
2676 Do not issue warning messages.
2677 @end table
2679 These options take effect at the point on the command line where they
2680 are specified.  Therefore if you specify @samp{-nowarn} at the end of
2681 the command line, you will not see warning messages for any problems
2682 occurring before that.  The warning messages affected by the above
2683 options are triggered by:
2685 @itemize @minus
2686 @item
2687 Use of the @samp{-d} option which is deprecated; please use
2688 @samp{-depth} instead, since the latter is POSIX-compliant.
2689 @item
2690 Use of the @samp{-ipath} option which is deprecated; please use
2691 @samp{-iwholename} instead.
2692 @item 
2693 Specifying an option (for example @samp{-mindepth}) after a non-option
2694 (for example @samp{-type} or @samp{-print}) on the command line.
2695 @end itemize
2698 The default behaviour above is designed to work in that way so that
2699 existing shell scripts which use such constructs don't generate
2700 spurious errors, but people will be made aware of the problem.
2702 Some warning messages are issued for less common or more serious
2703 problems, and so cannot be turned off:
2705 @itemize @minus
2706 @item
2707 Use of an unrecognised backslash escape sequence with @samp{-fprintf}
2708 @item
2709 Use of an unrecognised formatting directive with @samp{-fprintf}
2710 @end itemize
2712 @node Invoking locate, Invoking updatedb, Invoking find, Reference
2713 @section Invoking @code{locate}
2715 @example
2716 locate @r{[}@var{option}@dots{}@r{]} @var{pattern}@dots{}
2717 @end example
2719 @table @code
2720 @item --basename
2721 @itemx -b
2722 The specified pattern is matched against just the last component of
2723 the name of the file in the locate database.  This last component is
2724 also called the ``base name''.  For example, the base name of
2725 @file{/tmp/mystuff/foo.old.c} is @file{foo.old.c}.  If the pattern
2726 contains metacharacters, it must match the base name exactly.  If not,
2727 it must match part of the base name.
2729 @item --count
2730 @itemx -c
2731 Instead of printing the matched filenames, just print the total 
2732 number of matches we found, unless @samp{--print} (@samp{-p}) is also
2733 present.
2736 @item --database=@var{path}
2737 @itemx -d @var{path}
2738 Instead of searching the default file name database, search the file
2739 name databases in @var{path}, which is a colon-separated list of
2740 database file names.  You can also use the environment variable
2741 @code{LOCATE_PATH} to set the list of database files to search.  The
2742 option overrides the environment variable if both are used.  Empty
2743 elements in @var{path} (that is, a leading or trailing colon, or two
2744 colons in a row) are taken to stand for the default database.
2745 A database can be supplied on stdin, using @samp{-} as an element
2746 of @samp{path}. If more than one element of @samp{path} is @samp{-},
2747 later instances are ignored (but a warning message is printed).
2749 @item --existing
2750 @itemx -e
2751 Only print out such names which currently exist (instead of such names
2752 which existed when the database was created).  Note that this may slow
2753 down the program a lot, if there are many matches in the database.
2754 The way in which broken symbolic links are treated is affected by the
2755 @samp{-L}, @samp{-P} and @samp{-H} options.
2757 @item --non-existing
2758 @itemx -E
2759 Only print out such names which currently do not exist (instead of
2760 such names which existed when the database was created).  Note that
2761 this may slow down the program a lot, if there are many matches in the
2762 database.  The way in which broken symbolic links are treated is
2763 affected by the @samp{-L}, @samp{-P} and @samp{-H} options.
2765 @item --follow
2766 @itemx -L
2767 If testing for the existence of files (with the @samp{-e} or @samp{-E}
2768 options), consider broken symbolic links to be non-existing.  This is
2769 the default.
2772 @item --nofollow
2773 @itemx -P
2774 @itemx -H
2775 If testing for the existence of files (with the @samp{-e} or @samp{-E}
2776 options), treat broken symbolic links as if they were existing files.
2777 The @samp{-H} form of this option is provided purely for similarity
2778 with @code{find}; the use of @samp{-P} is recommended over @samp{-H}.
2780 @item --ignore-case
2781 @itemx -i
2782 Ignore case distinctions in both the pattern and the file names.
2784 @item --limit=N
2785 @itemx -l N
2786 Limit the number of results printed to N.  If you use the
2787 @samp{--count} option, the value printed will never be larger than
2788 this limit.
2790 @item --mmap
2791 @itemx -m
2792 Accepted but does nothing.  The option is supported only to provide
2793 compatibility with BSD's @code{locate}.
2795 @item --null
2796 @itemx -0
2797 Results are separated with the ASCII NUL character rather than the
2798 newline character.  To get the full benefit of the use of this option,
2799 use the new locate database format (that is the default anyway).
2801 @item --print
2802 @itemx -p
2803 Print search results when they normally would not, because of the presence
2804 of @samp{--statistics} (@samp{-S}) or @samp{--count} (@samp{-c}).
2806 @item --wholename
2807 @itemx -w
2808 The specified pattern is matched against the whole name of the file in
2809 the locate database.  If the pattern contains metacharacters, it must
2810 match exactly.  If not, it must match part of the whole file name.
2811 This is the default behaviour.
2813 @item --regex
2814 @itemx -r
2815 Instead of using substring or shell glob matching, the pattern
2816 specified on the command line is understood to be a POSIX extended
2817 regular expression.  Filenames from the locate database which match
2818 the specified regular expression are printed (or counted).  If the
2819 @samp{-i} flag is also given, matching is case-insensitive.  Matches
2820 are performed against the whole path name, and so by default a
2821 pathname will be matched if any part of it matches the specified
2822 regular expression.  The regular expression may use @samp{^} or
2823 @samp{$} to anchor a match at the beginning or end of a pathname.
2825 @item --stdio
2826 @itemx -s
2827 Accepted but does nothing.  The option is supported only to provide
2828 compatibility with BSD's @code{locate}.
2830 @item --statistics
2831 @itemx -S
2832 Print some summary information for each locate database.  No search is
2833 performed unless non-option arguments are given.
2835 @item --help
2836 Print a summary of the options to @code{locate} and exit.
2838 @item --version
2839 Print the version number of @code{locate} and exit.
2840 @end table
2842 @node Invoking updatedb, Invoking xargs, Invoking locate, Reference
2843 @section Invoking @code{updatedb}
2845 @example
2846 updatedb @r{[}@var{option}@dots{}@r{]}
2847 @end example
2849 @table @code
2850 @item --findoptions='@var{OPTION}@dots{}'
2851 Global options to pass on to @code{find}.
2852 The environment variable @code{FINDOPTIONS} also sets this value.
2853 Default is none.
2855 @item --localpaths='@var{path}@dots{}'
2856 Non-network directories to put in the database.
2857 Default is @file{/}.
2859 @item --netpaths='@var{path}@dots{}'
2860 Network (NFS, AFS, RFS, etc.) directories to put in the database.
2861 The environment variable @code{NETPATHS} also sets this value.
2862 Default is none.
2864 @item --prunepaths='@var{path}@dots{}'
2865 Directories to omit from the database, which would otherwise be included.
2866 The environment variable @code{PRUNEPATHS} also sets this value.
2867 Default is @file{/tmp /usr/tmp /var/tmp /afs}.
2869 @item --prunefs='@var{path}@dots{}'
2870 File systems to omit from the database, which would otherwise be included.
2871 Note that files are pruned when a file system is reached;
2872 Any file system mounted under an undesired file system will be
2873 ignored.
2874 The environment variable @code{PRUNEFS} also sets this value.
2875 Default is @file{nfs NFS proc}.
2877 @item --output=@var{dbfile}
2878 The database file to build.
2879 Default is system-dependent, but typically @file{/usr/local/var/locatedb}.
2881 @item --localuser=@var{user}
2882 The user to search the non-network directories as, using @code{su}.
2883 Default is to search the non-network directories as the current user.
2884 You can also use the environment variable @code{LOCALUSER} to set this user.
2886 @item --netuser=@var{user}
2887 The user to search network directories as, using @code{su}.
2888 Default is @code{daemon}.
2889 You can also use the environment variable @code{NETUSER} to set this user.
2891 @item --old-format
2892 Generate a locate database in the old format, for compatibility with
2893 versions of @code{locate} other than GNU @code{locate}.  Using this
2894 option means that @code{locate} will not be able to properly handle
2895 non-ASCII characters in filenames (that is, filenames containing
2896 characters which have the eighth bit set, such as many of the
2897 characters from the ISO-8859-1 character set).
2898 @item --help
2899 Print a summary of the command-line argument format and exit.
2900 @item --version
2901 Print the version number of @code{updatedb} and exit.
2902 @end table
2904 @node Invoking xargs, ,  Invoking updatedb, Reference
2905 @section Invoking @code{xargs}
2907 @example
2908 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
2909 @end example
2911 @code{xargs} exits with the following status:
2913 @table @asis
2914 @item 0
2915 if it succeeds
2916 @item 123
2917 if any invocation of the command exited with status 1-125
2918 @item 124
2919 if the command exited with status 255
2920 @item 125
2921 if the command is killed by a signal
2922 @item 126
2923 if the command cannot be run
2924 @item 127
2925 if the command is not found
2926 @item 1
2927 if some other error occurred.
2928 @end table
2930 @table @code
2931 @item --arg-file@r{=@var{inputfile}}
2932 @itemx -a @r{=@var{inputfile}}
2933 Read names from the file @var{inputfile} instead of standard input.
2935 @item --null
2936 @itemx -0
2937 Input filenames are terminated by a null character instead of by
2938 whitespace, and the quotes and backslash are not special (every
2939 character is taken literally).  Disables the end of file string, which
2940 is treated like any other argument.
2942 @item --eof@r{[}=@var{eof-str}@r{]}
2943 @itemx -e@r{[}@var{eof-str}@r{]}
2944 Set the end of file string to @var{eof-str}.  If the end of file string
2945 occurs as a line of input, the rest of the input is ignored.  If
2946 @var{eof-str} is omitted, there is no end of file string.  If this
2947 option is not given, the end of file string defaults to @samp{_}.
2949 @item --help
2950 Print a summary of the options to @code{xargs} and exit.
2952 @item --replace@r{[}=@var{replace-str}@r{]}
2953 @itemx -i@r{[}@var{replace-str}@r{]}
2954 Replace occurrences of @var{replace-str} in the initial arguments with
2955 names read from standard input.  Also, unquoted blanks do not
2956 terminate arguments; instead, the input is split at newlines only.
2957 If @var{replace-str} is omitted, it defaults to @samp{@{@}}
2958 (like for @samp{find -exec}).  Implies @samp{-x} and @samp{-l 1}.
2960 @item --max-lines@r{[}=@var{max-lines}@r{]}
2961 @itemx -l@r{[}@var{max-lines}@r{]}
2962 Use at most @var{max-lines} nonblank input lines per command line;
2963 @var{max-lines} defaults to 1 if omitted.  Trailing blanks cause an
2964 input line to be logically continued on the next input line, for the
2965 purpose of counting the lines.  Implies @samp{-x}.
2967 @item --max-args=@var{max-args}
2968 @itemx -n @var{max-args}
2969 Use at most @var{max-args} arguments per command line.  Fewer than
2970 @var{max-args} arguments will be used if the size (see the @samp{-s}
2971 option) is exceeded, unless the @samp{-x} option is given, in which case
2972 @code{xargs} will exit.
2974 @item --interactive
2975 @itemx -p
2976 Prompt the user about whether to run each command line and read a line
2977 from the terminal.  Only run the command line if the response starts
2978 with @samp{y} or @samp{Y}.  Implies @samp{-t}.
2980 @item --no-run-if-empty
2981 @itemx -r
2982 If the standard input does not contain any nonblanks, do not run the
2983 command.  By default, the command is run once even if there is no input.
2985 @item --max-chars=@var{max-chars}
2986 @itemx -s @var{max-chars}
2987 Use at most @var{max-chars} characters per command line, including the
2988 command and initial arguments and the terminating nulls at the ends of
2989 the argument strings.
2991 @item --verbose
2992 @itemx -t
2993 Print the command line on the standard error output before executing
2996 @item --version
2997 Print the version number of @code{xargs} and exit.
2999 @item --exit
3000 @itemx -x
3001 Exit if the size (see the @samp{-s} option) is exceeded.
3004 @item --max-procs=@var{max-procs}
3005 @itemx -P @var{max-procs}
3006 Run up to @var{max-procs} processes at a time; the default is 1.  If
3007 @var{max-procs} is 0, @code{xargs} will run as many processes as
3008 possible at a time.
3009 @end table
3011 @node Security Considerations, Error Messages, Reference, Top
3012 @chapter Security Considerations
3014 Security considerations are important if you are using @code{find} or
3015 @code{xargs} to search for or process files that don't belong to you
3016 or over which other people have control.  Security considerations
3017 relating to @code{locate} may also apply if you have files which you
3018 may not want others to see.   
3020 In general, the most severe forms of security problems affecting
3021 @code{find} and related programs are where third parties can bring
3022 about a situation where those programs allow them to do something
3023 they would normally not be able to do.  This is called @emph{privilege
3024 elevation}.  This might include deleting files they would not normally
3025 be able to delete.  It is also common for the system to periodically
3026 invoke @code{find} for housekeeping purposes.  These invocations of
3027 @code{find} are particularly problematic from a security point of view
3028 as these are often invoked by the superuser and search the whole file
3029 system hierarchy.  The severity of any associated problem depends on
3030 what the system is going to do with the output of @code{find}.
3032 @menu
3033 * Levels of Risk::      What is your level of exposure to security problems?
3034 * Security Considerations for find::  Security problems with find
3035 * Security Considerations for xargs:: Security problems with xargs
3036 * Security Considerations for locate:: Security problems with locate
3037 * Security Summary:: That was all very complex, what does it boil down to?
3038 @end menu
3041 @node Levels of Risk
3042 @section Levels of Risk
3044 There are some security risks inherent in the use of @code{find},
3045 @code{xargs} and (to a lesser extent) @code{locate}.  The severity of
3046 these risks depends on what sort of system you are using:
3048 @table @strong
3049 @item High risk
3050 Multi-user systems where you do not control (or trust) the other
3051 users, and on which you execute @code{find}, including areas where
3052 those other users can manipulate the filesystem (for example beneath
3053 @file{/home} or @file{/tmp}).
3055 @item Medium Risk
3056 Systems where the actions of other users can create filenames chosen
3057 by them, but to which they don't have access while @code{find} is
3058 being run.  This access might include leaving programs running (shell
3059 background jobs, @code{at} or @code{cron} tasks, for example).  On
3060 these sorts of systems, carefully written commands (avoiding use of
3061 @samp{-print} for example) should not expose you to a high degree of
3062 risk.  Most systems fall into this category.
3064 @item Low Risk
3065 Systems to which untrusted parties do not have access, cannot create
3066 filenames of their own choice (even remotely) and which contain no
3067 security flaws which might enable an untrusted third party to gain
3068 access.  Most systems do not fall into this category because there are
3069 many ways in which external parties can affect the names of files that
3070 are created on your system.  The system on which I am writing this for
3071 example automatically downloads software updates from the Internet;
3072 the names of the files in which these updates exist are chosen by
3073 third parties@footnote{Of course, I trust these parties to a large
3074 extent anyway, because I install software provided by them; I choose
3075 to trust them in this way, and that's a deliberate choice}.
3076 @end table
3078 In the discussion above, ``risk'' denotes the likelihood that someone
3079 can cause @code{find}, @code{xargs}, @code{locate} or some other
3080 program which is controlled by them to do something you did not
3081 intend.  The levels of risk suggested do not take any account of the
3082 consequences of this sort of event.  That is, if you operate a ``low
3083 risk'' type system, but the consequences of a security problem are
3084 disastrous, then you should still give serious thought to all the
3085 possible security problems, many of which of course will not be
3086 discussed here -- this section of the manual is intended to be
3087 informative but not comprehensive or exhaustive.   
3089 If you are responsible for the operation of a system where the
3090 consequences of a security problem could be very important, you should
3091 do two things:-
3093 @enumerate
3094 @item Define a security policy which defines who is allowed to do what
3095 on your system
3096 @item Seek competent advice on how to enforce your policy, detect
3097 breaches of that policy, and take account of any potential problems
3098 that might fall outside the scope of your policy
3099 @end enumerate
3102 @node Security Considerations for find
3103 @section Security Considerations for find
3106 Some of the actions @code{find} might take have a direct effect;
3107 these include @code{-exec} and @code{-delete}.  However, it is also
3108 common to use @code{-print} explicitly or implicitly, and so if
3109 @code{find} produces the wrong list of filenames, that can also be a
3110 security problem; consider the case for example where @code{find} is
3111 producing a list of files to be deleted.
3113 We normally assume that the @code{find} command line expresses the
3114 file selection criteria and actions that the user had in mind -- that
3115 is, the command line is ``trusted'' data.  
3117 From a security analysis point of view, the output of @code{find}
3118 should be correct; that is, the output should contain only the names
3119 of those files which meet the user's criteria specified on the command
3120 line.  This applies for the @code{-exec} and @code{-delete} actions;
3121 one can consider these to be part of the output.
3123 On the other hand, the contents of the filesystem can be manipulated
3124 by other people, and hence we regard this as ``untrusted'' data.  This
3125 implies that the @code{find} command line is a filter which converts
3126 the untrusted contents of the filesystem into a correct list of output
3127 files.   
3129 The filesystem will in general change while @code{find} is searching
3130 it; in fact, most of the potential security problems with @code{find}
3131 relate to this issue in some way.
3133 Race conditions are a general class of security problem where the
3134 relative ordering of actions taken by @code{find} (for example) and
3135 something else are important@footnote{This is more or less the
3136 definition of the term ``race condition''} .  
3138 Typically, an attacker might move or rename files or directories in
3139 the hope that an action might be taken against a a file which was not
3140 normally intended to be affected.  Alternatively, this sort of attack
3141 might be intended to persuade @code{find} to search part of the
3142 filesystem which would not normally be included in the search
3143 (defeating the @code{-prune} action for example).  
3145 @menu
3146 * Changing the Current Working Directory::
3147 * Race Conditions with -exec::
3148 * Race Conditions with -print and -print0::
3149 @end menu
3152 @node Changing the Current Working Directory
3153 @subsection Changing the Current Working Directory
3155 As find searches the file system, it finds subdirectories and then
3156 searches within them by changing its working directory.  First,
3157 @code{find} notices a subdirectory.  It then decides if that
3158 subdirectory meets the criteria for being searched; that is, any
3159 @samp{-xdev} or @samp{-prune} expressions are taken into account.  The
3160 @code{find} program will then change working directory and proceed to
3161 search the directory.
3163 A race condition attack might take the form that once the checks
3164 relevant to @samp{-xdev} and @samp{-prune} have been done, an attacker
3165 might rename the directory that was being considered, and put in its
3166 place a symbolic link that actually points somewhere else.  
3168 The idea behind this attack is to fool @code{find} into going into the
3169 wrong directory.  This would leave @code{find} with a working
3170 directory chosen by an attacker, bypassing any protection apparently
3171 provided by @samp{-xdev} and @samp{-prune}, and any protection
3172 provided by being able to @emph{not} list particular directories on
3173 the @code{find} command line.  This form of attack is particularly
3174 problematic if the attacker can predict when the @code{find} command
3175 will be run, as is the case with @code{cron} tasks for example.
3177 GNU @code{find} has specific safeguards to prevent this general class
3178 of problem.  The exact form of these safeguards depends on the
3179 properties of your system.  
3181 @menu 
3182 * O_NOFOLLOW::                     Safely changing directory using fchdir().
3183 * Systems without O_NOFOLLOW::     Checking for symbolic links after chdir().
3184 * Working with automounters::      These can look like race condition exploits
3185 * Problems with dead NFS servers:: If you don't have O_NOFOLLOW, this is a problem.
3186 @end menu
3188 @node O_NOFOLLOW
3189 @subsubsection O_NOFOLLOW 
3191 If your system supports the O_NOFOLLOW flag @footnote{GNU/Linux
3192 (kernel version 2.1.126 and later) and FreeBSD (3.0-CURRENT and later)
3193 support this} to the @code{open(2)} system call, @code{find} uses it
3194 when safely changing directory.  The target directory is first opened
3195 and then @code{find} changes working directory with the
3196 @code{fchdir()} system call.  This ensures that symbolic links are not
3197 followed, preventing the sort of race condition attack in which use
3198 is made of symbolic links.
3200 If for any reason this approach does not work, @code{find} will fall
3201 back on the method which is normally used if O_NOFOLLOW is not
3202 supported.
3204 You can tell if your system supports O_NOFOLLOW by running 
3206 @example
3207 find --version
3208 @end example
3210 This will tell you the version number and which features are enabled.
3211 For example, if I run this on my system now, this gives:
3212 @example
3213 GNU find version 4.2.18-CVS
3214 Features enabled: D_TYPE O_NOFOLLOW(enabled)
3215 @end example
3217 Here, you can see that I am running a version of find which was built
3218 from the development (CVS) code prior to the release of
3219 findutils-4.2.18, and that the D_TYPE and O_NOFOLLOW features are
3220 present.  O_NOFOLLOW is qualified with ``enabled''.  This simply means
3221 that the current system seems to support O_NOFOLLOW.  This check is
3222 needed because it is possible to build find on a system that defines
3223 O_NOFOLLOW and then run it on a system that ignores the O_NOFOLLOW
3224 flag.  We try to detect such cases at startup by checking the
3225 operating system and version number; when this happens you will see
3226 ``O_NOFOLLOW(disabled)'' instead.
3228 @node Systems without O_NOFOLLOW
3229 @subsubsection Systems without O_NOFOLLOW
3231 The strategy for preventing this type of problem on systems that lack
3232 support for the O_NOFOLLOW flag is more complex.  Each time
3233 @code{find} changes directory, it examines the directory it is about
3234 to move to, issues the @code{chdir()} system call, and then checks
3235 that it has ended up in the subdirectory it expected.  If not, an
3236 error message is issued and @code{find} exits immediately.  This
3237 method prevents filesystem manipulation attacks from persuading
3238 @code{find} to search parts of the filesystem it did not intend.
3239 However, we heve to take special steps in order not to unneccessarily
3240 conclude that there is a problem with any ``automount'' mount points.
3242 @node Working with automounters
3243 @subsubsection Working with automounters
3245 Where an automounter is in use it can be the case that the use of the
3246 @code{chdir()} system call can itself cause a new filesystem to be
3247 mounted at that point.  On systems that do not support O_NOFOLLOW,
3248 this will cause @code{find}'s security check to fail.  
3250 However, this does not normally represent a security problem (since
3251 the automounter configuration is normally set up by the system
3252 administrator).  Therefore, if the @code{chdir()} sanity check fails,
3253 @code{find} will check to see if a new filesystem has been mounted at
3254 the current directory; if so, @code{find} will issue a warning message
3255 and continue.
3257 To make this solution work, @code{find} reads the list of mounted
3258 filesystems at startup, and again when the sanity check fails.  It
3259 compares the two lists to find out if the directory it has moved into
3260 has just been mounted.
3262 @node Problems with dead NFS servers
3263 @subsubsection Problems with dead NFS servers
3265 Examining every mount point on the system has a downside too.  In
3266 general, @code{find} will be used to search just part of the
3267 filesystem.  However, @code{find} examines every mount point.  If the
3268 system has a filesystem mounted on an unresponsive NFS server,
3269 @code{find} will hang, waiting for the NFS server to respond.  Worse,
3270 it does this even if the affected mount point is not within the
3271 directory tree that find would have searched anyway.
3273 This is very unfortunate.  However, this problem only affects systems
3274 that have no support for O_NOFOLLOW.  As far as I can tell, it is not
3275 possible on such systems to fix all three problems (the race
3276 condition, the false-alarm at automount mount points, and the hang at
3277 startup if there is a dead NFS server) at once.  If you have some
3278 ideas about how @code{find} could do this better, please send email to
3279 the @email{bug-findutils@@gnu.org} mailing list.
3281 @node Race Conditions with -exec
3282 @subsection Race Conditions with -exec
3284 The @samp{-exec} action causes another program to be run.  It is
3285 passed the name of the file which is being considered at the time.
3286 The invoked program will then - normally - perform some action on that
3287 file.  Once again, there is a race condition which can be exploited
3288 here.  We shall take as a specific example the command
3290 @example 
3291 find /tmp -path /tmp/umsp/passwd -exec /bin/rm
3292 @end example
3294 In this simple example, we are identifying just one file to be deleted
3295 and invoking @code{/bin/rm} to delete it.  A problem exists because
3296 there is a time gap between the point where @code{find} decides that
3297 it needs to process the @samp{-exec} action and the point where the
3298 @code{/bin/rm} command actually issues the @code{unlink()} system
3299 call.  Within this time period, an attacker can rename the
3300 @file{/tmp/umsp} directory, replacing it with a symbolic link to
3301 @file{/etc}.   There is no way for @code{/bin/rm} to determine that it
3302 is working on the same file that @code{find} had in mind.   Once the
3303 symbolic link is in place, the attacker has persuaded @code{find} to
3304 cause the deletion of the @file{/etc/passwd} file, which is not the
3305 effect intended by the command which was actually invoked.  
3307 One possible defence against this type of attack is to modify the
3308 behaviour of @samp{-exec} so that the @code{/bin/rm} command is run
3309 with the argument @file{./passwd} and a suitable choice of working
3310 directory.  This would allow the normal sanity check that @code{find}
3311 performs to protect against this form of attack too.  Unfortunately,
3312 this strategy cannot be used as the POSIX standard specifies that the
3313 current working directory for commands invoked via @samp{-exec} must
3314 be the same as the current working directory from which @code{find}
3315 was invoked.   This means that the @samp{-exec} action is inherently
3316 insecure and can't be fixed.   
3318 GNU @code{find} implements a more secure variant of the @samp{-exec}
3319 action, @samp{-execdir}.  The @samp{-execdir} action
3320 ensures that it is not necessary to dereference subdirectories to
3321 process target files.  The current directory used to invoke programs
3322 is the same as the directory in which the file to be processed exists
3323 (@file{/tmp/umsp} in our example, and only the basename of the file to
3324 be processed is passed to the invoked command, with a @samp{./}
3325 prepended (giving @file{./passwd} in our example).
3327 The @samp{-execdir} action refuses to do anything if the current
3328 directory is included in the @var{$PATH} environment variable.  This
3329 is necessary because @samp{-execdir} runs programs in the same
3330 directory in which it finds files -- in general, such a directory
3331 might be writable by untrusted users.  For similar reasons,
3332 @samp{-execdir} does not allow @samp{@{@}} to appear in the name of
3333 the command to be run.
3335 @node Race Conditions with -print and -print0
3336 @subsection Race Conditions with -print and -print0
3338 The @samp{-print} and @samp{-print0} actions can be used to produce a
3339 list of files matching some criteria, which can then be used with some
3340 other command, perhaps with @code{xargs}.   Unfortunately, this means
3341 that there is an unavoidable time gap between @code{find} deciding
3342 that one or more files meet its criteria and the relevant command
3343 being executed.  For this reason, the @samp{-print} and @samp{-print0}
3344 actions are just as insecure as @samp{-exec}.
3346 In fact, since the construction 
3348 @example
3349 find ....   -print | xargs ....
3350 @end example
3352 does not cope correctly with newlines or other ``white space'' in
3353 filenames, and copes poorly with filenames containing quotes, the
3354 @samp{-print} action is less secure even than @samp{-print0}.
3357 @comment  node-name,  next,  previous,  up
3358 @comment @node Security Considerations for xargs
3359 @node Security Considerations for xargs
3360 @section Security Considerations for @code{xargs}
3362 The description of the race conditions affecting the @samp{-print}
3363 action of @code{find} shows that @code{xargs} cannot be secure if it
3364 is possible for an attacker to modify a filesystem after @code{find}
3365 has started but before @code{xargs} has completed all its actions.
3367 However, there are other security issues that exist even if it is not
3368 possible for an attacker to have access to the filesystem in real
3369 time.  Firstly, if it is possible for an attacker to create files with
3370 names of their own choice on the filesystem, then @code{xargs} is
3371 insecure unless the @samp{-0} option is used.  If a file with the name
3372 @file{/home/someuser/foo/bar\n/etc/passwd} exists (assume that
3373 @samp{\n} stands for a newline character), then @code{find ... -print}
3374 can be persuaded to print three separate lines:
3376 @example
3377 /home/someuser/foo/bar
3379 /etc/passwd
3380 @end example
3382 If it finds a blank line in the input, @code{xargs} will ignore it.
3383 Therefore, if some action is to be taken on the basis of this list of
3384 files, the @file{/etc/passwd} file would be included even if this was
3385 not the intent of the person running find.  There are circumstances in
3386 which an attacker can use this to their advantage.  The same
3387 consideration applies to filenames containing ordinary spaces rather
3388 than newlines, except that of course the list of filenames will no
3389 longer contain an ``extra'' newline.
3391 This problem is an unavoidable consequence of the default behaviour of
3392 the @code{xargs} command, which is specified by the POSIX standard.
3393 The only ways to avoid this problem are either to avoid all use of
3394 @code{xargs} in favour for example of @samp{find -exec} or (where
3395 available) @samp{find -execdir}, or to use the @samp{-0} option, which
3396 ensures that @code{xargs} considers filenames to be separated by ASCII
3397 NUL characters rather than whitespace.   However, useful though this
3398 option is, the POSIX standard does not make it mandatory.
3400 @comment  node-name,  next,  previous,  up
3401 @node Security Considerations for locate
3402 @section Security Considerations for @code{locate}
3404 It is fairly unusual for the output of @code{locate} to be fed into
3405 another command.  However, if this were to be done, this would raise
3406 the same set of security issues as the use of @samp{find ... -print}.
3407 Although the problems relating to whitespace in filenames can be
3408 resolved by using @code{locate}'s @samp{-0} option, this still leaves
3409 the race condition problems associated with @samp{find ... -print0}.
3410 There is no way to avoid these problems in the case of @code{locate}.
3412 @node Security Summary
3413 @section Summary
3415 Where untrusted parties can create files on the system, or affect the
3416 names of files that are created, all uses for @code{find},
3417 @code{locate} and @code{xargs} have known security problems except the
3418 following:
3420 @table @asis
3421 @item Informational use only
3422 Uses where the programs are used to prepare lists of filenames upon which no further action will ever be taken.
3424 @item -delete 
3425 Use of the @samp{-delete} action to delete files which meet
3426 specified criteria
3428 @item -execdir 
3429 Use of the @samp{-execdir} action where the @env{PATH}
3430 environment variable contains directories which contain only trusted
3431 programs. 
3432 @end table
3434 @comment  node-name,  next,  previous,  up
3435 @node Error Messages, Primary Index, Security Considerations, Top
3436 @chapter Error Messages
3438 This section describes some of the error messages you might get from
3439 @code{find}, @code{xargs}, or @code{locate}, explains them and in some
3440 cases provides advice as to what you should do about this.
3442 This manual is written in English.  The GNU findutils software
3443 features translated error messages for many languages.  For this
3444 reason where possible we try to make the error messages produced by
3445 the programs self-explanatory.  This approach avoids asking people to
3446 figure out which English-language error message the test they actually
3447 saw might correspond to. Error messages which are self-explanatory
3448 will not normally be described or discussed in this document.  For
3449 those messages which are discussed in this document, only the
3450 English-language version of the message will be listed.
3452 @menu
3453 * Error Messages From find::
3454 * Error Messages From xargs::
3455 * Error Messages From locate::
3456 * Error Messages From updatedb::
3457 @end menu
3459 @node Error Messages From find, Error Messages From xargs, , Error Messages
3460 @section Error Messages From find
3462 @table @samp
3463 @item invalid predicate `-foo'
3464 This means that the @code{find} command line included something that
3465 started with a dash or other special character.  The @code{find}
3466 program tried to interpret this as a test, action or option, but
3467 didn't recognise it.  If you intended it to be a test, check what you
3468 specified against the documentation.  If, on the other hand, the
3469 string is the name of a file which has been expanded from a wildcard
3470 (for example because you have a @samp{*} on the command line),
3471 consider using @samp{./*} or just @samp{.} instead.
3473 @item unexpected extra predicate
3474 This usually happens if you have an extra bracket on the command line
3475 (for example @samp{find . -print \)}).
3477 @item Warning: filesystem /path/foo has recently been mounted
3478 @itemx Warning: filesystem /path/foo has recently been unmounted
3479 These messages might appear when @code{find} moves into a directory
3480 and finds that the device number and inode are different to what it
3481 expected them to be.  If the directory @code{find} has moved into is
3482 on an NFS filesystem, it will not issue this message, because
3483 @code{automount} frequently mounts new filesystems on directories as
3484 you move into them (that is how it knows you want to use the
3485 filesystem).  So, if you do see this message, be wary --
3486 @code{automount} may not have been responsible.  Consider the
3487 possibility that someone else is manipulating the filesystem while
3488 @code{find} is running.  Some people might do this in order to mislead
3489 @code{find} or persuade it to look at one set of files when it thought
3490 it was looking at another set.
3492 @item /path/foo changed during execution of find (old device number 12345, new device number 6789, filesystem type is <whatever>) [ref XXX]
3493 This message is issued when @code{find} changes directory and ends up
3494 somewhere it didn't expect to be.  This happens in one of two
3495 circumstances.  Firstly this happens when ``automount'' does its thing
3496 on a system where @code{find} doesn't know how to determine what the
3497 current set of mounted filesystems is
3499 Secondly, this can happen when the device number of a directory
3500 appears to change during a change of current directory, but
3501 @code{find} is moving up the filesystem hierarchy rather than down it.
3502 In order to prevent @code{find} wandering off into some unexpected
3503 part of the filesystem, we stop it at this point.
3505 @item Don't know how to use getmntent() to read `/etc/mtab'.  This is a bug.
3506 This message is issued when a problem similar to the above occurs on a
3507 system where @code{find} doesn't know how to figure out the current
3508 list of mount points.  Ask for help on @email{bug-findutils@@gnu.org}.
3510 @item /path/foo/bar changed during execution of find (old inode number 12345, new inode number 67893, filesystem type is <whatever>) [ref XXX]"),
3511 This message is issued when @code{find} changes directory and
3512 discovers that the inode number of that directory once it's got there
3513 is different to the inode number that it obtained when it examined the
3514 directory some time previously.  This normally means that while
3515 @code{find} has been deep in a directory hierarchy doing something
3516 time consuming, somebody has moved the one of the parent directories
3517 to another location in the same filesystem.  This may have been done
3518 maliciously, or may not.  In any case, @code{find} stops at this point
3519 in order to avoid traversing parts of the filesystem that it wasn't
3520 intended to.  You can use @code{ls -li} or @code{find /path -inum
3521 12345 -o -inum 67893} to find out more about what has happened.
3523 @item sanity check of the fnmatch() library function failed.
3524 Please submit a bug report.  You may well be asked questions about
3525 your system, and if you compiled the @code{findutils} code yourself,
3526 you should keep your copy of the build tree around.  The likely
3527 explanation is that your system has a buggy implementation of
3528 @code{fnmatch} that looks enough like the GNU version to fool
3529 @code{configure}, but which doesn't work properly.
3531 @item cannot fork
3532 This normally happens if you use the @code{-exec} action or a
3533 something similar (@code{-ok} and so forth) but the system has run out
3534 of free process slots.  This is either because the system is very busy
3535 and the system has reached its maximum process limit, or because you
3536 have a resource limit in place and you've reached it.  Check the
3537 system for runaway processes (if @code{ps} still works).  Some process
3538 slots are normally reserved for use by @samp{root}.
3540 @item some-program terminated by signal 99
3541 Some program which was launched via @code{-exec} or similar was killed
3542 with a fatal signal.  This is just an advisory message.
3543 @end table
3547 @node Error Messages From xargs, Error Messages From locate, Error Messages From find, Error Messages
3548 @section Error Messages From xargs
3550 @table @samp
3551 @item environment is too large for exec
3552 This message means that you have so many environment variables set
3553 (or such large values for them) that there is no room within the
3554 system-imposed limits on program command-line argument length to
3555 invoke any program.  I'm sure you did this deliberately.  Please try
3556 unsetting some environment variables, or exiting the current shell.
3558 @item can not fit single argument within argument list size limit
3559 You are using the @samp{-i} option and @code{xargs} doesn't have
3560 enough space to build a command line because it has read in a really
3561 large item and it doesn't fit.  You can probably work around this
3562 problem with the @samp{-s} option, but the default size is pretty
3563 large.  You must be trying pretty hard to break @code{xargs}.
3565 @item cannot fork
3566 See the description of the similar message for @code{find}.
3568 @item <program>: exited with status 255; aborting
3569 When a command run by @code{xargs} exits with status 255, @code{xargs}
3570 is supposed to stop.  If this is not what you intended, wrap the
3571 program you are trying to invoke in a shell script which doesn't
3572 return status 255.
3574 @item <program>: terminated by signal 99
3575 See the description of the similar message for @code{find}.
3576 @end table
3578 @node Error Messages From locate, Error Messages From updatedb, Error Messages From xargs, Error Messages
3579 @section Error Messages From locate
3581 @table @samp
3582 @item warning: database `/usr/local/var/locatedb' is more than 8 days old
3583 The @code{locate} program relies on a database which is periodically
3584 built by the @code{updatedb} program.  That hasn't happened in a long
3585 time.  To fix this problem, run @code{updatedb} manually.  This can
3586 often happen on systems that are generally not left on, so the periodic
3587 ``cron'' task which normally does this doesn't get a chance to run.
3589 @item locate database `/usr/local/var/locatedb' is corrupt or invalid
3590 This should not happen.  Re-run @code{updatedb}.  If that works, but
3591 @code{locate} still produces this error, run @code{locate --version}
3592 and @code{updatedb --version}.  These should produce the same output.
3593 If not, you are using a mixed toolset; check your @samp{$PATH}
3594 environment variable and your shell aliases (if you have any).  If
3595 both programs claim to be GNU versions, this is a bug; all versions of
3596 these programs should interoperate without problem.  Ask for help on
3597 @email{bug-findutils@@gnu.org}.
3598 @end table
3601 @node Error Messages From updatedb, , Error Messages From locate, Error Messages
3602 @section Error Messages From updatedb
3604 The @code{updatedb} program (and the programs it invokes) do issue
3605 error messages, but none of them seem to me to be candidates for
3606 guidance.  If you are having a problem understanding one of these, ask
3607 for help on @email{bug-findutils@@gnu.org}.
3609 @node Primary Index, , Error Messages, Top
3610 @unnumbered @code{find} Primary Index
3612 This is a list of all of the primaries (tests, actions, and options)
3613 that make up @code{find} expressions for selecting files.  @xref{find
3614 Expressions}, for more information on expressions.
3616 @printindex fn
3618 @bye
3620 @comment texi related words used by Emacs' spell checker ispell.el
3622 @comment LocalWords: texinfo setfilename settitle setchapternewpage
3623 @comment LocalWords: iftex finalout ifinfo DIR titlepage vskip pt
3624 @comment LocalWords: filll dir samp dfn noindent xref pxref
3625 @comment LocalWords: var deffn texi deffnx itemx emph asis
3626 @comment LocalWords: findex smallexample subsubsection cindex
3627 @comment LocalWords: dircategory direntry itemize
3629 @comment other words used by Emacs' spell checker ispell.el
3630 @comment LocalWords: README fred updatedb xargs Plett Rendell akefile
3631 @comment LocalWords: args grep Filesystems fo foo fOo wildcards iname
3632 @comment LocalWords: ipath regex iregex expr fubar regexps
3633 @comment LocalWords: metacharacters macs sr sc inode lname ilname
3634 @comment LocalWords: sysdep noleaf ls inum xdev filesystems usr atime
3635 @comment LocalWords: ctime mtime amin cmin mmin al daystart Sladkey rm
3636 @comment LocalWords: anewer cnewer bckw rf xtype uname gname uid gid
3637 @comment LocalWords: nouser nogroup chown chgrp perm ch maxdepth
3638 @comment LocalWords: mindepth cpio src CD AFS statted stat fstype ufs
3639 @comment LocalWords: nfs tmp mfs printf fprint dils rw djm Nov lwall
3640 @comment LocalWords: POSIXLY fls fprintf strftime locale's EDT GMT AP
3641 @comment LocalWords: EST diff perl backquotes sprintf Falstad Oct cron
3642 @comment LocalWords: eg vmunix mkdir afs allexec allwrite ARG bigram
3643 @comment LocalWords: bigrams cd chmod comp crc CVS dbfile dum eof
3644 @comment LocalWords: fileserver filesystem fn frcode Ghazi Hnewc iXX
3645 @comment LocalWords: joeuser Kaveh localpaths localuser LOGNAME
3646 @comment LocalWords: Meyering mv netpaths netuser nonblank nonblanks
3647 @comment LocalWords: ois ok Pinard printindex proc procs prunefs
3648 @comment LocalWords: prunepaths pwd RFS rmadillo rmdir rsh sbins str
3649 @comment LocalWords: su Timar ubins ug unstripped vf VM Weitzel
3650 @comment LocalWords: wildcard zlogout basename execdir wholename iwholename
3651 @comment LocalWords: timestamp timestamps Solaris FreeBSD OpenBSD POSIX