Added in the -H and -L options on the usage message.
[findutils.git] / doc / find.texi
blob72bd4345273e786470be1463488d21ff710872cc
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 @ifinfo
22 This file documents the GNU utilities for finding files that match
23 certain criteria and performing various operations on them.
25 Copyright (C) 1994,1996,1998,2000,2001,2003,2004 Free Software Foundation, Inc.
27 Permission is granted to make and distribute verbatim copies of
28 this manual provided the copyright notice and this permission notice
29 are preserved on all copies.
31 @ignore
32 Permission is granted to process this file through TeX and print the
33 results, provided the printed document carries copying permission
34 notice identical to this one except for the removal of this paragraph
35 (this paragraph not being relevant to the printed manual).
37 @end ignore
38 Permission is granted to copy and distribute modified versions of this
39 manual under the conditions for verbatim copying, provided that the entire
40 resulting derived work is distributed under the terms of a permission
41 notice identical to this one.
43 Permission is granted to copy and distribute translations of this manual
44 into another language, under the above conditions for modified versions,
45 except that this permission notice may be stated in a translation approved
46 by the Foundation.
47 @end ifinfo
49 @titlepage
50 @title Finding Files
51 @subtitle Edition @value{EDITION}, for GNU @code{find} version @value{VERSION}
52 @subtitle @value{UPDATED}
53 @author by David MacKenzie
55 @page
56 @vskip 0pt plus 1filll
57 Copyright @copyright{} 1994,1996,1998,2000,2001,2003,2004 Free Software Foundation, Inc.
59 Permission is granted to make and distribute verbatim copies of
60 this manual provided the copyright notice and this permission notice
61 are preserved on all copies.
63 Permission is granted to copy and distribute modified versions of this
64 manual under the conditions for verbatim copying, provided that the entire
65 resulting derived work is distributed under the terms of a permission
66 notice identical to this one.
68 Permission is granted to copy and distribute translations of this manual
69 into another language, under the above conditions for modified versions,
70 except that this permission notice may be stated in a translation approved
71 by the Foundation.
72 @end titlepage
74 @node Top, Introduction, , (dir)
75 @comment  node-name,  next,  previous,  up
77 @ifinfo
78 This file documents the GNU utilities for finding files that match
79 certain criteria and performing various actions on them.
80 This is edition @value{EDITION}, for @code{find} version @value{VERSION}.
81 @end ifinfo
83 @c The master menu, created with texinfo-master-menu, goes here.
85 @menu
86 * Introduction::                Summary of the tasks this manual describes.
87 * Finding Files::               Finding files that match certain criteria.
88 * Actions::                     Doing things to files you have found.
89 * Common Tasks::                Solutions to common real-world problems.
90 * Databases::                   Maintaining file name databases.
91 * File Permissions::            How to control access to files.
92 * Reference::                   Summary of how to invoke the programs.
93 * Primary Index::               The components of @code{find} expressions.
94 @end menu
96 @node Introduction, Finding Files, Top, Top
97 @chapter Introduction
99 This manual shows how to find files that meet criteria you specify, and
100 how to perform various actions on the files that you find.  The
101 principal programs that you use to perform these tasks are @code{find},
102 @code{locate}, and @code{xargs}.  Some of the examples in this manual
103 use capabilities specific to the GNU versions of those programs.
105 GNU @code{find} was originally written by Eric Decker, with enhancements
106 by David MacKenzie, Jay Plett, and Tim Wood.  GNU @code{xargs} was
107 originally written by Mike Rendell, with enhancements by David
108 MacKenzie.  GNU @code{locate} and its associated utilities were
109 originally written by James Woods, with enhancements by David MacKenzie.
110 The idea for @samp{find -print0} and @samp{xargs -0} came from Dan
111 Bernstein.  The current maintainer of GNU findutils (and this manual) is
112 James Youngman.  Many other people have contributed bug fixes, small
113 improvements, and helpful suggestions.  Thanks!
115 Mail suggestions and bug reports for these programs to
116 @code{bug-findutils@@gnu.org}.  Please include the version
117 number, which you can get by running @samp{find --version}.
119 @menu
120 * Scope::
121 * Overview::
122 * find Expressions::
123 @end menu
125 @node Scope
126 @section Scope
128 For brevity, the word @dfn{file} in this manual means a regular file, a
129 directory, a symbolic link, or any other kind of node that has a
130 directory entry.  A directory entry is also called a @dfn{file name}.  A
131 file name may contain some, all, or none of the directories in a path
132 that leads to the file.  These are all examples of what this manual
133 calls ``file names'':
135 @example
136 parser.c
137 README
138 ./budget/may-94.sc
139 fred/.cshrc
140 /usr/local/include/termcap.h
141 @end example
143 A @dfn{directory tree} is a directory and the files it contains, all of
144 its subdirectories and the files they contain, etc.  It can also be a
145 single non-directory file.
147 These programs enable you to find the files in one or more directory
148 trees that:
150 @itemize @bullet
151 @item
152 have names that contain certain text or match a certain pattern;
153 @item
154 are links to certain files;
155 @item
156 were last used during a certain period of time;
157 @item
158 are within a certain size range;
159 @item
160 are of a certain type (regular file, directory, symbolic link, etc.);
161 @item
162 are owned by a certain user or group;
163 @item
164 have certain access permissions;
165 @item
166 contain text that matches a certain pattern;
167 @item
168 are within a certain depth in the directory tree;
169 @item
170 or some combination of the above.
171 @end itemize
173 Once you have found the files you're looking for (or files that are
174 potentially the ones you're looking for), you can do more to them than
175 simply list their names.  You can get any combination of the files'
176 attributes, or process the files in many ways, either individually or in
177 groups of various sizes.  Actions that you might want to perform on the
178 files you have found include, but are not limited to:
180 @itemize @bullet
181 @item
182 view or edit
183 @item
184 store in an archive
185 @item
186 remove or rename
187 @item
188 change access permissions
189 @item
190 classify into groups
191 @end itemize
193 This manual describes how to perform each of those tasks, and more.
195 @node Overview
196 @section Overview
198 The principal programs used for making lists of files that match given
199 criteria and running commands on them are @code{find}, @code{locate},
200 and @code{xargs}.  An additional command, @code{updatedb}, is used by
201 system administrators to create databases for @code{locate} to use.
203 @code{find} searches for files in a directory hierarchy and prints
204 information about the files it found.  It is run like this:
206 @example
207 find @r{[}@var{file}@dots{}@r{]} @r{[}@var{expression}@r{]}
208 @end example
210 @noindent
211 Here is a typical use of @code{find}.  This example prints the names of
212 all files in the directory tree rooted in @file{/usr/src} whose name
213 ends with @samp{.c} and that are larger than 100 Kilobytes.
214 @example
215 find /usr/src -name '*.c' -size +100k -print
216 @end example
218 @code{locate} searches special file name databases for file names that
219 match patterns.  The system administrator runs the @code{updatedb}
220 program to create the databases.  @code{locate} is run like this:
222 @example
223 locate @r{[}@var{option}@dots{}@r{]} @var{pattern}@dots{}
224 @end example
226 @noindent
227 This example prints the names of all files in the default file name
228 database whose name ends with @samp{Makefile} or @samp{makefile}.  Which
229 file names are stored in the database depends on how the system
230 administrator ran @code{updatedb}.
231 @example
232 locate '*[Mm]akefile'
233 @end example
235 The name @code{xargs}, pronounced EX-args, means ``combine arguments.''
236 @code{xargs} builds and executes command lines by gathering together
237 arguments it reads on the standard input.  Most often, these arguments
238 are lists of file names generated by @code{find}.  @code{xargs} is run
239 like this:
241 @example
242 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
243 @end example
245 @noindent
246 The following command searches the files listed in the file
247 @file{file-list} and prints all of the lines in them that contain the
248 word @samp{typedef}.
249 @example
250 xargs grep typedef < file-list
251 @end example
253 @node find Expressions
254 @section @code{find} Expressions
256 The expression that @code{find} uses to select files consists of one or
257 more @dfn{primaries}, each of which is a separate command line argument
258 to @code{find}.  @code{find} evaluates the expression each time it
259 processes a file.  An expression can contain any of the following types
260 of primaries:
262 @table @dfn
263 @item options
264 affect overall operation rather than the processing of a specific file;
265 @item tests
266 return a true or false value, depending on the file's attributes;
267 @item actions
268 have side effects and return a true or false value; and
269 @item operators
270 connect the other arguments and affect when and whether they are
271 evaluated.
272 @end table
274 You can omit the operator between two primaries; it defaults to
275 @samp{-and}.  @xref{Combining Primaries With Operators}, for ways to
276 connect primaries into more complex expressions.  If the expression
277 contains no actions other than @samp{-prune}, @samp{-print} is performed
278 on all files for which the entire expression is true (@pxref{Print File
279 Name}).
281 Options take effect immediately, rather than being evaluated for each
282 file when their place in the expression is reached.  Therefore, for
283 clarity, it is best to place them at the beginning of the expression.
285 Many of the primaries take arguments, which immediately follow them in
286 the next command line argument to @code{find}.  Some arguments are file
287 names, patterns, or other strings; others are numbers.  Numeric
288 arguments can be specified as
290 @table @code
291 @item +@var{n}
292 for greater than @var{n},
293 @item -@var{n}
294 for less than @var{n},
295 @item @var{n}
296 for exactly @var{n}.
297 @end table
299 @node Finding Files, Actions, Introduction, Top
300 @chapter Finding Files
302 By default, @code{find} prints to the standard output the names of the
303 files that match the given criteria.  @xref{Actions}, for how to get more
304 information about the matching files.
307 @menu
308 * Name::
309 * Links::
310 * Time::
311 * Size::
312 * Type::
313 * Owner::
314 * Permissions::
315 * Contents::
316 * Directories::
317 * Filesystems::
318 * Combining Primaries With Operators::
319 @end menu
321 @node Name
322 @section Name
324 Here are ways to search for files whose name matches a certain pattern.
325 @xref{Shell Pattern Matching}, for a description of the @var{pattern}
326 arguments to these tests.
328 Each of these tests has a case-sensitive version and a case-insensitive
329 version, whose name begins with @samp{i}.  In a case-insensitive
330 comparison, the patterns @samp{fo*} and @samp{F??} match the file names
331 @file{Foo}, @samp{FOO}, @samp{foo}, @samp{fOo}, etc.
333 @menu
334 * Base Name Patterns::
335 * Full Name Patterns::
336 * Fast Full Name Search::
337 * Shell Pattern Matching::      Wildcards used by these programs.
338 @end menu
340 @node Base Name Patterns
341 @subsection Base Name Patterns
343 @deffn Test -name pattern
344 @deffnx Test -iname pattern
345 True if the base of the file name (the path with the leading directories
346 removed) matches shell pattern @var{pattern}.  For @samp{-iname}, the
347 match is case-insensitive.  To ignore a whole directory tree, use
348 @samp{-prune} (@pxref{Directories}).  As an example, to find Texinfo
349 source files in @file{/usr/local/doc}:
351 @example
352 find /usr/local/doc -name '*.texi'
353 @end example
354 @end deffn
356 Patterns for @samp{-name} and @samp{-iname} will match a filename with
357 a leading @samp{.}.  For example the command @samp{find /tmp -name
358 \*bar} will match the file @file{/tmp/.foobar}.
361 @node Full Name Patterns
362 @subsection Full Name Patterns
364 @deffn Test -wholename pattern
365 @deffnx Test -iwholename pattern
366 True if the entire file name, starting with the command line argument
367 under which the file was found, matches shell pattern @var{pattern}.
368 For @samp{-iwholename}, the match is case-insensitive.  To ignore a whole
369 directory tree, use @samp{-prune} rather than checking every file in the
370 tree (@pxref{Directories}).
371 @end deffn
373 @deffn Test -path pattern
374 @deffnx Test -ipath pattern
375 These tests are deprecated, but work as for @samp{-wholename} and @samp{-iwholename},
376 respectively.  The @samp{-ipath} test is a GNU extension, but @samp{-path} is also 
377 provided by HP-UX @code{find}.
378 @end deffn
380 @deffn Test -regex expr
381 @deffnx Test -iregex expr
382 True if the entire file name matches regular expression @var{expr}.
383 This is a match on the whole path, not a search.  For example, to match
384 a file named @file{./fubar3}, you can use the regular expression
385 @samp{.*bar.} or @samp{.*b.*3}, but not @samp{b.*r3}.  @xref{Regexps, ,
386 Syntax of Regular Expressions, emacs, The GNU Emacs Manual}, for a
387 description of the syntax of regular expressions.  For @samp{-iregex},
388 the match is case-insensitive.
389 @end deffn
391 @node Fast Full Name Search
392 @subsection Fast Full Name Search
394 To search for files by name without having to actually scan the
395 directories on the disk (which can be slow), you can use the
396 @code{locate} program.  For each shell pattern you give it,
397 @code{locate} searches one or more databases of file names and displays
398 the file names that contain the pattern.  @xref{Shell Pattern Matching},
399 for details about shell patterns.
401 If a pattern is a plain string---it contains no
402 metacharacters---@code{locate} displays all file names in the database
403 that contain that string.  If a pattern contains
404 metacharacters, @code{locate} only displays file names that match the
405 pattern exactly.  As a result, patterns that contain metacharacters
406 should usually begin with a @samp{*}, and will most often end with one
407 as well.  The exceptions are patterns that are intended to explicitly
408 match the beginning or end of a file name.   
410 If you only want @code{locate} to match against the last component of
411 the filenames (the ``base name'' of the files) you can use the
412 @samp{--basename} option.  The opposite behaviour is the default, but
413 can be selected explicitly by using the option @samp{--wholename}.
415 The command
416 @example
417 locate @var{pattern}
418 @end example
420 is almost equivalent to
421 @example
422 find @var{directories} -name @var{pattern}
423 @end example
425 where @var{directories} are the directories for which the file name
426 databases contain information.  The differences are that the
427 @code{locate} information might be out of date, and that @code{locate}
428 handles wildcards in the pattern slightly differently than @code{find}
429 (@pxref{Shell Pattern Matching}).
431 The file name databases contain lists of files that were on the system
432 when the databases were last updated.  The system administrator can
433 choose the file name of the default database, the frequency with which
434 the databases are updated, and the directories for which they contain
435 entries.
437 Here is how to select which file name databases @code{locate} searches.
438 The default is system-dependent.
440 @table @code
441 @item --database=@var{path}
442 @itemx -d @var{path}
443 Instead of searching the default file name database, search the file
444 name databases in @var{path}, which is a colon-separated list of
445 database file names.  You can also use the environment variable
446 @code{LOCATE_PATH} to set the list of database files to search.  The
447 option overrides the environment variable if both are used.
448 @end table
450 @node Shell Pattern Matching
451 @subsection Shell Pattern Matching
453 @code{find} and @code{locate} can compare file names, or parts of file
454 names, to shell patterns.  A @dfn{shell pattern} is a string that may
455 contain the following special characters, which are known as
456 @dfn{wildcards} or @dfn{metacharacters}.
458 You must quote patterns that contain metacharacters to prevent the shell
459 from expanding them itself.  Double and single quotes both work; so does
460 escaping with a backslash.
462 @table @code
463 @item *
464 Matches any zero or more characters.
466 @item ?
467 Matches any one character.
469 @item [@var{string}]
470 Matches exactly one character that is a member of the string
471 @var{string}.  This is called a @dfn{character class}.  As a shorthand,
472 @var{string} may contain ranges, which consist of two characters with a
473 dash between them.  For example, the class @samp{[a-z0-9_]} matches a
474 lowercase letter, a number, or an underscore.  You can negate a class by
475 placing a @samp{!} or @samp{^} immediately after the opening bracket.
476 Thus, @samp{[^A-Z@@]} matches any character except an uppercase letter
477 or an at sign.
479 @item \
480 Removes the special meaning of the character that follows it.  This
481 works even in character classes.
482 @end table
484 In the @code{find} tests that do shell pattern matching (@samp{-name},
485 @samp{-wholename}, etc.), wildcards in the pattern will match a @samp{.}
486 at the beginning of a file name.  This is also the case for
487 @code{locate}.  Thus, @samp{find -name '*macs'} will match a file
488 named @file{.emacs}, as will @samp{locate '*macs'}.
490 Slash characters have no special significance in the shell pattern
491 matching that @code{find} and @code{locate} do, unlike in the shell, in
492 which wildcards do not match them.  Therefore, a pattern @samp{foo*bar}
493 can match a file name @samp{foo3/bar}, and a pattern @samp{./sr*sc} can
494 match a file name @samp{./src/misc}.
496 If you want to locate some files with the @samp{locate} command but
497 don't need to see the full list you can use the @samp{--limit} option
498 to see just a small number of results, or the @samp{--count} option to
499 display only the total number of matches.
501 @node Links
502 @section Links
504 There are two ways that files can be linked together.  @dfn{Symbolic
505 links} are a special type of file whose contents are a portion of the
506 name of another file.  @dfn{Hard links} are multiple directory entries
507 for one file; the file names all have the same index node (@dfn{inode})
508 number on the disk.
510 @menu
511 * Symbolic Links::
512 * Hard Links::
513 @end menu
515 @node Symbolic Links
516 @subsection Symbolic Links
518 @deffn Test -lname pattern
519 @deffnx Test -ilname pattern
520 True if the file is a symbolic link whose contents match shell pattern
521 @var{pattern}.  For @samp{-ilname}, the match is case-insensitive.
522 @xref{Shell Pattern Matching}, for details about the @var{pattern}
523 argument.  So, to list any symbolic links to @file{sysdep.c} in the
524 current directory and its subdirectories, you can do:
526 @example
527 find . -lname '*sysdep.c'
528 @end example
529 @end deffn
531 @deffn Option -follow
532 Dereference symbolic links.  The @samp{-follow} option is unlike most
533 other options in that it can be useful to place it after other tests,
534 because of its effect on the behaviour of the @samp{-newer},
535 @samp{-anewer} and @samp{-cnewer} tests.  The following differences in
536 behavior occur when this option is given:
538 @itemize @bullet
539 @item
540 @code{find} follows symbolic links to directories when searching
541 directory trees.
542 @item
543 @samp{-lname} and @samp{-ilname} always return false.
544 @item
545 @samp{-type} reports the types of the files that symbolic links point
547 @item
548 Implies @samp{-noleaf} (@pxref{Directories}).
549 @item 
550 If the argument to @samp{-newer} is a symbolic link, it is
551 dereferenced.  Note that because the timestamp of the file is
552 determined when the command line is being parsed, the position of the
553 @samp{-follow} option is important.   The @samp{-anewer} and
554 @samp{-cnewer} tests are affected similarly. 
555 @end itemize
556 @end deffn
558 @node Hard Links
559 @subsection Hard Links
561 To find hard links, first get the inode number of the file whose links
562 you want to find.  You can learn a file's inode number and the number of
563 links to it by running @samp{ls -i} or @samp{find -ls}.  If the file has
564 more than one link, you can search for the other links by passing that
565 inode number to @samp{-inum}.  Add the @samp{-xdev} option if you are
566 starting the search at a directory that has other filesystems mounted on
567 it, such as @file{/usr} on many systems.  Doing this saves needless
568 searching, since hard links to a file must be on the same filesystem.
569 @xref{Filesystems}.
571 @deffn Test -inum n
572 File has inode number @var{n}.
573 @end deffn
575 You can also search for files that have a certain number of links, with
576 @samp{-links}.  Directories normally have at least two hard links; their
577 @file{.} entry is the second one.  If they have subdirectories, each of
578 those also has a hard link called @file{..} to its parent directory.
580 @deffn Test -links n
581 File has @var{n} hard links.
582 @end deffn
584 @node Time
585 @section Time
587 Each file has three time stamps, which record the last time that certain
588 operations were performed on the file:
590 @enumerate
591 @item
592 access (read the file's contents)
593 @item
594 change the status (modify the file or its attributes)
595 @item
596 modify (change the file's contents)
597 @end enumerate
599 You can search for files whose time stamps are within a certain age
600 range, or compare them to other time stamps.
602 @menu
603 * Age Ranges::
604 * Comparing Timestamps::
605 @end menu
607 @node Age Ranges
608 @subsection Age Ranges
610 These tests are mainly useful with ranges (@samp{+@var{n}} and
611 @samp{-@var{n}}).
613 @deffn Test -atime n
614 @deffnx Test -ctime n
615 @deffnx Test -mtime n
616 True if the file was last accessed (or its status changed, or it was
617 modified) @var{n}*24 hours ago.
618 @end deffn
620 @deffn Test -amin n
621 @deffnx Test -cmin n
622 @deffnx Test -mmin n
623 True if the file was last accessed (or its status changed, or it was
624 modified) @var{n} minutes ago.  These tests provide finer granularity of
625 measurement than @samp{-atime} et al.  For example, to list files in
626 @file{/u/bill} that were last read from 2 to 6 minutes ago:
628 @example
629 find /u/bill -amin +2 -amin -6
630 @end example
631 @end deffn
633 @deffn Option -daystart
634 Measure times from the beginning of today rather than from 24 hours ago.
635 So, to list the regular files in your home directory that were modified
636 yesterday, do
638 @example
639 find ~ -daystart -type f -mtime 1
640 @end example
641 @end deffn
643 The @samp{-daystart} option is unlike most other options in that it
644 has an effect on the way that other tests are performed.  The affected
645 tests are @samp{-amin}, @samp{-cmin}, @samp{-mmin}, @samp{-atime},
646 @samp{-ctime} and @samp{-mtime}.
648 @node Comparing Timestamps
649 @subsection Comparing Timestamps
651 As an alternative to comparing timestamps to the current time, you can
652 compare them to another file's timestamp.  That file's timestamp could
653 be updated by another program when some event occurs.  Or you could set
654 it to a particular fixed date using the @code{touch} command.  For
655 example, to list files in @file{/usr} modified after February 1 of the
656 current year:
658 @c Idea from Rick Sladkey.
659 @example
660 touch -t 02010000 /tmp/stamp$$
661 find /usr -newer /tmp/stamp$$
662 rm -f /tmp/stamp$$
663 @end example
665 @deffn Test -anewer file
666 @deffnx Test -cnewer file
667 @deffnx Test -newer file
668 True if the file was last accessed (or its status changed, or it was
669 modified) more recently than @var{file} was modified.  These tests are
670 affected by @samp{-follow} only if @samp{-follow} comes before them on
671 the command line.  @xref{Symbolic Links}, for more information on
672 @samp{-follow}.  As an example, to list any files modified since
673 @file{/bin/sh} was last modified:
675 @example
676 find . -newer /bin/sh
677 @end example
678 @end deffn
680 @deffn Test -used n
681 True if the file was last accessed @var{n} days after its status was
682 last changed.  Useful for finding files that are not being used, and
683 could perhaps be archived or removed to save disk space.
684 @end deffn
686 @node Size
687 @section Size
689 @deffn Test -size n@r{[}bckwMG@r{]}
690 True if the file uses @var{n} units of space, rounding up.  The units
691 are 512-byte blocks by default, but they can be changed by adding a
692 one-character suffix to @var{n}:
694 @table @code
695 @item b
696 512-byte blocks (never 1024)
697 @item c
698 bytes
699 @item k
700 kilobytes (1024 bytes)
701 @item w
702 2-byte words
703 @item M
704 Megabytes
705 @item G
706 Gigabytes
707 @end table
709 The `b' suffix always considers blocks to be 512 bytes.  This is not
710 affected by the setting (or non-setting) of the POSIXLY_CORRECT
711 environment variable.  This behaviour is different to the behaviour of
712 the @samp{-ls} action).  If you want to use 1024-byte units, use the
713 `k' suffix instead.
715 The number can be prefixed with a `+' or a `-'.  A plus sign indicates
716 that the test should succeed if the file uses at least @var{n} units
717 of storage (this is the way I normally use this test) and a minus sign
718 indicates that the test should succeed if the file uses less than
719 @var{n} units of storage.   There is no `=' prefix, because that's the 
720 default anyway.  
722 The size does not count indirect blocks, but it does count blocks in
723 sparse files that are not actually allocated.  This handling of sparse
724 files differs from the output of the @samp{%k} and @samp{%b} format
725 specifiers for the @samp{-printf} predicate.
727 @end deffn
729 @deffn Test -empty
730 True if the file is empty and is either a regular file or a directory.
731 This might make it a good candidate for deletion.  This test is useful
732 with @samp{-depth} (@pxref{Directories}) and @samp{-delete}
733 (@pxref{Single File}).
734 @end deffn
736 @node Type
737 @section Type
739 @deffn Test -type c
740 True if the file is of type @var{c}:
742 @table @code
743 @item b
744 block (buffered) special
745 @item c
746 character (unbuffered) special
747 @item d
748 directory
749 @item p
750 named pipe (FIFO)
751 @item f
752 regular file
753 @item l
754 symbolic link
755 @item s
756 socket
757 @item D
758 door (Solaris)
759 @end table
760 @end deffn
762 @deffn Test -xtype c
763 The same as @samp{-type} unless the file is a symbolic link.  For
764 symbolic links: if @samp{-follow} has not been given, true if the file
765 is a link to a file of type @var{c}; if @samp{-follow} has been given,
766 true if @var{c} is @samp{l}.  In other words, for symbolic links,
767 @samp{-xtype} checks the type of the file that @samp{-type} does not
768 check.  @xref{Symbolic Links}, for more information on @samp{-follow}.
769 @end deffn
771 @node Owner
772 @section Owner
774 @deffn Test -user uname
775 @deffnx Test -group gname
776 True if the file is owned by user @var{uname} (belongs to group @var{gname}).
777 A numeric ID is allowed.
778 @end deffn
780 @deffn Test -uid n
781 @deffnx Test -gid n
782 True if the file's numeric user ID (group ID) is @var{n}.  These tests
783 support ranges (@samp{+@var{n}} and @samp{-@var{n}}), unlike
784 @samp{-user} and @samp{-group}.
785 @end deffn
787 @deffn Test -nouser
788 @deffnx Test -nogroup
789 True if no user corresponds to the file's numeric user ID (no group
790 corresponds to the numeric group ID).  These cases usually mean that the
791 files belonged to users who have since been removed from the system.
792 You probably should change the ownership of such files to an existing
793 user or group, using the @code{chown} or @code{chgrp} program.
794 @end deffn
796 @node Permissions
797 @section Permissions
799 @xref{File Permissions}, for information on how file permissions are
800 structured and how to specify them.
802 @deffn Test -perm mode
803 True if the
804 file's permissions are exactly @var{mode} (which can be numeric or symbolic).
805 Symbolic modes use mode 0 as a point of departure.
806 If @var{mode} starts with @samp{-}, true if
807 @emph{all} of the permissions set in @var{mode} are set for the file;
808 permissions not set in @var{mode} are ignored.
809 If @var{mode} starts with @samp{+}, true if
810 @emph{any} of the permissions set in @var{mode} are set for the file;
811 permissions not set in @var{mode} are ignored.
812 @end deffn
814 @node Contents
815 @section Contents
817 To search for files based on their contents, you can use the @code{grep}
818 program.  For example, to find out which C source files in the current
819 directory contain the string @samp{thing}, you can do:
821 @example
822 grep -l thing *.[ch]
823 @end example
825 If you also want to search for the string in files in subdirectories,
826 you can combine @code{grep} with @code{find} and @code{xargs}, like
827 this:
829 @example
830 find . -name '*.[ch]' | xargs grep -l thing
831 @end example
833 The @samp{-l} option causes @code{grep} to print only the names of files
834 that contain the string, rather than the lines that contain it.  The
835 string argument (@samp{thing}) is actually a regular expression, so it
836 can contain metacharacters.  This method can be refined a little by
837 using the @samp{-r} option to make @code{xargs} not run @code{grep} if
838 @code{find} produces no output, and using the @code{find} action
839 @samp{-print0} and the @code{xargs} option @samp{-0} to avoid
840 misinterpreting files whose names contain spaces:
842 @example
843 find . -name '*.[ch]' -print0 | xargs -r -0 grep -l thing
844 @end example
846 For a fuller treatment of finding files whose contents match a pattern,
847 see the manual page for @code{grep}.
849 @node Directories
850 @section Directories
852 Here is how to control which directories @code{find} searches, and how
853 it searches them.  These two options allow you to process a horizontal
854 slice of a directory tree.
856 @deffn Option -maxdepth levels
857 Descend at most @var{levels} (a non-negative integer) levels of
858 directories below the command line arguments.  @samp{-maxdepth 0} means
859 only apply the tests and actions to the command line arguments.
860 @end deffn
862 @deffn Option -mindepth levels
863 Do not apply any tests or actions at levels less than @var{levels} (a
864 non-negative integer).  @samp{-mindepth 1} means process all files
865 except the command line arguments.
866 @end deffn
868 @deffn Option -depth
869 Process each directory's contents before the directory itself.  Doing
870 this is a good idea when producing lists of files to archive with
871 @code{cpio} or @code{tar}.  If a directory does not have write
872 permission for its owner, its contents can still be restored from the
873 archive since the directory's permissions are restored after its contents.
874 @end deffn
876 @deffn Option -d
877 This is a deprecated synonym for @samp{-depth}, for compatibility with
878 Mac OS X, FreeBSD and OpenBSD.   The @samp{-depth} option is a POSIX
879 feature, so it is better to use that.
880 @end deffn
882 @deffn Action -prune
883 If @samp{-depth} is not given, true; do not descend into the current
884 directory.  If @samp{-depth} is given, false; no effect.  @samp{-prune}
885 only affects tests and actions that come after it in the expression, not
886 those that come before.
888 For example, to skip the directory @file{src/emacs} and all files and
889 directories under it, and print the names of the other files found:
891 @example
892 find . -wholename './src/emacs' -prune -o -print
893 @end example
894 @end deffn
896 @deffn Action -quit
897 Exit immediately with return value zero.  No child proceses will be
898 left running, but no more paths specified on the command line will be 
899 processed.   For example, @code{find /tmp/foo /tmp/bar -print -quit}
900 will print only @samp{/tmp/foo}.
901 @end deffn 
903 @deffn Option -noleaf
904 Do not optimize by assuming that directories contain 2 fewer
905 subdirectories than their hard link count.  This option is needed when
906 searching filesystems that do not follow the Unix directory-link
907 convention, such as CD-ROM or MS-DOS filesystems or AFS volume mount
908 points.  Each directory on a normal Unix filesystem has at least 2 hard
909 links: its name and its @file{.}  entry.  Additionally, its
910 subdirectories (if any) each have a @file{..}  entry linked to that
911 directory.  When @code{find} is examining a directory, after it has
912 statted 2 fewer subdirectories than the directory's link count, it knows
913 that the rest of the entries in the directory are non-directories
914 (@dfn{leaf} files in the directory tree).  If only the files' names need
915 to be examined, there is no need to stat them; this gives a significant
916 increase in search speed.
917 @end deffn
919 @deffn Option -ignore_readdir_race
920 If a file disappears after its name has been read from a directory but
921 before @code{find} gets around to examining the file with @code{stat},
922 don't issue an error message.  If you don't specify this option, an
923 error message will be issued.  This option can be useful in system
924 scripts (cron scripts, for example) that examine areas of the
925 filesystem that change frequently (mail queues, temporary directories,
926 and so forth), because this scenario is common for those sorts of
927 directories.  Completely silencing error messages from @code{find} is
928 undesirable, so this option neatly solves the problem.  There is no
929 way to search one part of the filesystem with this option on and part
930 of it with this option off, though.
931 @end deffn
933 @deffn Option -noignore_readdir_race
934 This option reverses the effect of the @samp{-ignore_readdir_race} option.
935 @end deffn
938 @node Filesystems
939 @section Filesystems
941 A @dfn{filesystem} is a section of a disk, either on the local host or
942 mounted from a remote host over a network.  Searching network
943 filesystems can be slow, so it is common to make @code{find} avoid them.
945 There are two ways to avoid searching certain filesystems.  One way is
946 to tell @code{find} to only search one filesystem:
948 @deffn Option -xdev
949 @deffnx Option -mount
950 Don't descend directories on other filesystems.  These options are synonyms.
951 @end deffn
953 The other way is to check the type of filesystem each file is on, and
954 not descend directories that are on undesirable filesystem types:
956 @deffn Test -fstype type
957 True if the file is on a filesystem of type @var{type}.  The valid
958 filesystem types vary among different versions of Unix; an incomplete
959 list of filesystem types that are accepted on some version of Unix or
960 another is:
961 @example
962 ext2 ext3 proc sysfs ufs 4.2 4.3 nfs tmp mfs S51K S52K
963 @end example
964 You can use @samp{-printf} with the @samp{%F} directive to see the types
965 of your filesystems.  The @samp{%D} directive shows the device number.
966 @xref{Print File Information}.  @samp{-fstype} is
967 usually used with @samp{-prune} to avoid searching remote filesystems
968 (@pxref{Directories}). 
969 @end deffn
971 @node Combining Primaries With Operators
972 @section Combining Primaries With Operators
974 Operators build a complex expression from tests and actions.
975 The operators are, in order of decreasing precedence:
977 @table @code
978 @item @asis{( @var{expr} )}
979 @findex ()
980 Force precedence.  True if @var{expr} is true.
982 @item @asis{! @var{expr}}
983 @itemx @asis{-not @var{expr}}
984 @findex !
985 @findex -not
986 True if @var{expr} is false.
988 @item @asis{@var{expr1 expr2}}
989 @itemx @asis{@var{expr1} -a @var{expr2}}
990 @itemx @asis{@var{expr1} -and @var{expr2}}
991 @findex -a
992 @findex -and
993 And; @var{expr2} is not evaluated if @var{expr1} is false.
995 @item @asis{@var{expr1} -o @var{expr2}}
996 @itemx @asis{@var{expr1} -or @var{expr2}}
997 @findex -o
998 @findex -or
999 Or; @var{expr2} is not evaluated if @var{expr1} is true.
1001 @item @asis{@var{expr1} , @var{expr2}}
1002 @findex ,
1003 List; both @var{expr1} and @var{expr2} are always evaluated.  True if
1004 @var{expr2} is true.  The value of @var{expr1} is discarded.  This
1005 operator lets you do multiple independent operations on one traversal,
1006 without depending on whether other operations succeeded.
1007 @end table
1009 @code{find} searches the directory tree rooted at each file name by
1010 evaluating the expression from left to right, according to the rules of
1011 precedence, until the outcome is known (the left hand side is false for
1012 @samp{-and}, true for @samp{-or}), at which point @code{find} moves on
1013 to the next file name.
1015 There are two other tests that can be useful in complex expressions:
1017 @deffn Test -true
1018 Always true.
1019 @end deffn
1021 @deffn Test -false
1022 Always false.
1023 @end deffn
1025 @node Actions, Common Tasks, Finding Files, Top
1026 @chapter Actions
1028 There are several ways you can print information about the files that
1029 match the criteria you gave in the @code{find} expression.  You can
1030 print the information either to the standard output or to a file that
1031 you name.  You can also execute commands that have the file names as
1032 arguments.  You can use those commands as further filters to select files.
1034 @menu
1035 * Print File Name::
1036 * Print File Information::
1037 * Run Commands::
1038 * Delete Files::
1039 * Adding Tests::
1040 @end menu
1042 @node Print File Name
1043 @section Print File Name
1045 @deffn Action -print
1046 True; print the full file name on the standard output, followed by a
1047 newline.
1048 @end deffn
1050 @deffn Action -fprint file
1051 True; print the full file name into file @var{file}, followed by a
1052 newline.  If @var{file} does not exist when @code{find} is run, it is
1053 created; if it does exist, it is truncated to 0 bytes.  The file names
1054 @file{/dev/stdout} and @file{/dev/stderr} are handled specially; they
1055 refer to the standard output and standard error output, respectively.
1056 @end deffn
1058 @node Print File Information
1059 @section Print File Information
1061 @deffn Action -ls
1062 True; list the current file in @samp{ls -dils} format on the standard
1063 output.  The output looks like this:
1065 @smallexample
1066 204744   17 -rw-r--r--   1 djm      staff       17337 Nov  2  1992 ./lwall-quotes
1067 @end smallexample
1069 The fields are:
1071 @enumerate
1072 @item
1073 The inode number of the file.  @xref{Hard Links}, for how to find files
1074 based on their inode number.
1076 @item
1077 the number of blocks in the file.  The block counts are of 1K blocks,
1078 unless the environment variable @code{POSIXLY_CORRECT} is set, in which
1079 case 512-byte blocks are used.  @xref{Size}, for how to find files based
1080 on their size.
1082 @item
1083 The file's type and permissions.  The type is shown as a dash for a
1084 regular file; for other file types, a letter like for @samp{-type} is
1085 used (@pxref{Type}).  The permissions are read, write, and execute for
1086 the file's owner, its group, and other users, respectively; a dash means
1087 the permission is not granted.  @xref{File Permissions}, for more details
1088 about file permissions.  @xref{Permissions}, for how to find files based
1089 on their permissions.
1091 @item
1092 The number of hard links to the file.
1094 @item
1095 The user who owns the file.
1097 @item
1098 The file's group.
1100 @item
1101 The file's size in bytes.
1103 @item
1104 The date the file was last modified.
1106 @item
1107 The file's name.  @samp{-ls} quotes non-printable characters in the file
1108 names using C-like backslash escapes.
1109 @end enumerate
1110 @end deffn
1112 @deffn Action -fls file
1113 True; like @samp{-ls} but write to @var{file} like @samp{-fprint}
1114 (@pxref{Print File Name}).
1115 @end deffn
1117 @deffn Action -printf format
1118 True; print @var{format} on the standard output, interpreting @samp{\}
1119 escapes and @samp{%} directives.  Field widths and precisions can be
1120 specified as with the @code{printf} C function.  Unlike @samp{-print},
1121 @samp{-printf} does not add a newline at the end of the string.
1122 @end deffn
1124 @deffn Action -fprintf file format
1125 True; like @samp{-printf} but write to @var{file} like @samp{-fprint}
1126 (@pxref{Print File Name}).
1127 @end deffn
1129 @menu
1130 * Escapes::
1131 * Format Directives::
1132 * Time Formats::
1133 @end menu
1135 @node Escapes
1136 @subsection Escapes
1138 The escapes that @samp{-printf} and @samp{-fprintf} recognize are:
1140 @table @code
1141 @item \a
1142 Alarm bell.
1143 @item \b
1144 Backspace.
1145 @item \c
1146 Stop printing from this format immediately and flush the output.
1147 @item \f
1148 Form feed.
1149 @item \n
1150 Newline.
1151 @item \r
1152 Carriage return.
1153 @item \t
1154 Horizontal tab.
1155 @item \v
1156 Vertical tab.
1157 @item \\
1158 A literal backslash (@samp{\}).
1159 @item \NNN
1160 The character whose ASCII code is NNN (octal).
1161 @end table
1163 A @samp{\} character followed by any other character is treated as an
1164 ordinary character, so they both are printed, and a warning message is
1165 printed to the standard error output (because it was probably a typo).
1167 @node Format Directives
1168 @subsection Format Directives
1170 @samp{-printf} and @samp{-fprintf} support the following format
1171 directives to print information about the file being processed.  The C
1172 @code{printf} function, field width and precision specifiers are
1173 supported, as applied to string (%s) types. That is, you can specify
1174 "minimum field width"."maximum field width" for each directive.
1176 @samp{%%} is a literal percent sign.  A @samp{%} character followed by
1177 an unrecognised character (i.e. not a known directive or printf field
1178 width and precision specifier), is discarded (but the unrecognised character
1179 is printed), and a warning message is printed to the standard error output
1180 (because it was probably a typo).
1182 @menu
1183 * Name Directives::
1184 * Ownership Directives::
1185 * Size Directives::
1186 * Location Directives::
1187 * Time Directives::
1188 * Formatting Flags::
1189 @end menu
1191 @node Name Directives
1192 @subsubsection Name Directives
1194 @table @code
1195 @item %p
1196 @c supports %-X.Yp
1197 File's name (not the absolute path name, but the name of the file as
1198 it was encountered by find - that is, as a relative path from one of
1199 the starting points).
1200 @item %f
1201 File's name with any leading directories removed (only the last element).
1202 @c supports %-X.Yf
1203 @item %h
1204 Leading directories of file's name (all but the last element and the
1205 slash before it).
1206 @c supports %-X.Yh
1207 @item %P
1208 File's name with the name of the command line argument under which
1209 it was found removed from the beginning.
1210 @c supports %-X.YP
1211 @item %H
1212 Command line argument under which file was found.
1213 @c supports %-X.YH
1214 @end table
1216 @node Ownership Directives
1217 @subsubsection Ownership Directives
1219 @table @code
1220 @item %g
1221 @c supports %-X.Yg
1222 File's group name, or numeric group ID if the group has no name.
1223 @item %G
1224 @c supports %-X.Yg
1225 @c TODO: Needs to support # flag and 0 flag
1226 File's numeric group ID.
1227 @item %u
1228 @c supports %-X.Yu
1229 File's user name, or numeric user ID if the user has no name.
1230 @item %U
1231 @c supports %-X.Yu
1232 @c TODO: Needs to support # flag
1233 File's numeric user ID.
1234 @item %m
1235 @c full support, including # and 0.
1236 File's permissions (in octal).   If you always want to have a leading
1237 zero on the number, use the '#' format flag, for example '%#m'.
1238 @end table
1240 @node Size Directives
1241 @subsubsection Size Directives
1243 @table @code
1244 @item %k
1245 Amount of disk space occupied by the file, measured in 1K blocks
1246 (rounded up).  This can be less than the length of the file if 
1247 it is a sparse file (that is, it has ``holes'').  
1248 @item %b
1249 File's size in 512-byte blocks (rounded up).  This also can be less 
1250 than the length of the file, if the file is sparse.
1251 @item %s
1252 File's size in bytes.
1253 @end table
1255 @node Location Directives
1256 @subsubsection Location Directives
1258 @table @code
1259 @item %d
1260 File's depth in the directory tree (depth below a file named on the
1261 command line, not depth below the root directory).  Files named on the
1262 command line have a depth of 0.  Subdirectories immediately below them
1263 have a depth of 1, and so on.
1264 @item %D
1265 The device number on which the file exists (the @code{st_dev} field of
1266 @code{struct stat}), in decimal.
1267 @item %F
1268 Type of the filesystem the file is on; this value can be used for
1269 @samp{-fstype} (@pxref{Directories}).
1270 @item %l
1271 Object of symbolic link (empty string if file is not a symbolic link).
1272 @item %i
1273 File's inode number (in decimal).
1274 @item %n
1275 Number of hard links to file.
1276 @item %y 
1277 Type of the file as used with @samp{-type}.   If the file is a symbolic
1278 link, @samp{l} will be printed.
1279 @item %Y
1280 Type of the file as used with @samp{-type}.   If the file is a symbolic
1281 link, it is dereferenced.  If the file is a broken symbolic link,
1282 @samp{N} is printed.
1284 @end table
1286 @node Time Directives
1287 @subsubsection Time Directives
1289 Some of these directives use the C @code{ctime} function.  Its output
1290 depends on the current locale, but it typically looks like
1292 @example
1293 Wed Nov  2 00:42:36 1994
1294 @end example
1296 @table @code
1297 @item %a
1298 File's last access time in the format returned by the C @code{ctime} function.
1299 @item %A@var{k}
1300 File's last access time in the format specified by @var{k}
1301 (@pxref{Time Formats}). 
1302 @item %c
1303 File's last status change time in the format returned by the C @code{ctime}
1304 function.
1305 @item %C@var{k}
1306 File's last status change time in the format specified by @var{k}
1307 (@pxref{Time Formats}).
1308 @item %t
1309 File's last modification time in the format returned by the C @code{ctime}
1310 function.
1311 @item %T@var{k}
1312 File's last modification time in the format specified by @var{k} 
1313 (@pxref{Time Formats}). 
1314 @end table
1316 @node Time Formats
1317 @subsection Time Formats
1319 Below are the formats for the directives @samp{%A}, @samp{%C}, and
1320 @samp{%T}, which print the file's timestamps.  Some of these formats
1321 might not be available on all systems, due to differences in the C
1322 @code{strftime} function between systems.
1324 @menu
1325 * Time Components::
1326 * Date Components::
1327 * Combined Time Formats::
1328 @end menu
1330 @node Time Components
1331 @subsubsection Time Components
1333 The following format directives print single components of the time.
1335 @table @code
1336 @item H
1337 hour (00..23)
1338 @item I
1339 hour (01..12)
1340 @item k
1341 hour ( 0..23)
1342 @item l
1343 hour ( 1..12)
1344 @item p
1345 locale's AM or PM
1346 @item Z
1347 time zone (e.g., EDT), or nothing if no time zone is determinable
1348 @item M
1349 minute (00..59)
1350 @item S
1351 second (00..61)
1352 @item @@
1353 seconds since Jan. 1, 1970, 00:00 GMT.
1354 @end table
1356 @node Date Components
1357 @subsubsection Date Components
1359 The following format directives print single components of the date.
1361 @table @code
1362 @item a
1363 locale's abbreviated weekday name (Sun..Sat)
1364 @item A
1365 locale's full weekday name, variable length (Sunday..Saturday)
1366 @item b
1367 @itemx h
1368 locale's abbreviated month name (Jan..Dec)
1369 @item B
1370 locale's full month name, variable length (January..December)
1371 @item m
1372 month (01..12)
1373 @item d
1374 day of month (01..31)
1375 @item w
1376 day of week (0..6)
1377 @item j
1378 day of year (001..366)
1379 @item U
1380 week number of year with Sunday as first day of week (00..53)
1381 @item W
1382 week number of year with Monday as first day of week (00..53)
1383 @item Y
1384 year (1970@dots{})
1385 @item y
1386 last two digits of year (00..99)
1387 @end table
1389 @node Combined Time Formats
1390 @subsubsection Combined Time Formats
1392 The following format directives print combinations of time and date
1393 components. 
1395 @table @code
1396 @item r
1397 time, 12-hour (hh:mm:ss [AP]M)
1398 @item T
1399 time, 24-hour (hh:mm:ss)
1400 @item X
1401 locale's time representation (H:M:S)
1402 @item c
1403 locale's date and time (Sat Nov 04 12:02:33 EST 1989)
1404 @item D
1405 date (mm/dd/yy)
1406 @item x
1407 locale's date representation (mm/dd/yy)
1408 @item +
1409 Date and time, separated by '+', for example `2004-04-28+22:22:05'.
1410 The time is given in the current timezone (which may be affected by
1411 setting the TZ environment variable).  This is a GNU extension.
1412 @end table
1414 @node Formatting Flags
1415 @subsubsection Formatting Flags
1417 The @samp{%m} and @samp{%d} directives support the @samp{#}, @samp{0}
1418 and @samp{+} flags, but the other directives do not, even if they
1419 print numbers.  Numeric directives that do not support these flags
1420 include 
1422 @samp{G},
1423 @samp{U},
1424 @samp{b},
1425 @samp{D},
1426 @samp{k} and
1427 @samp{n}.
1429 @node Run Commands
1430 @section Run Commands
1432 You can use the list of file names created by @code{find} or
1433 @code{locate} as arguments to other commands.  In this way you can
1434 perform arbitrary actions on the files.
1436 @menu
1437 * Single File::
1438 * Multiple Files::
1439 * Querying::
1440 @end menu
1442 @node Single File
1443 @subsection Single File
1445 Here is how to run a command on one file at a time.
1447 @deffn Action -exec command ;
1448 Execute @var{command}; true if 0 status is returned.  @code{find} takes
1449 all arguments after @samp{-exec} to be part of the command until an
1450 argument consisting of @samp{;} is reached.  It replaces the string
1451 @samp{@{@}} by the current file name being processed everywhere it
1452 occurs in the command.  Both of these constructions need to be escaped
1453 (with a @samp{\}) or quoted to protect them from expansion by the shell.
1454 The command is executed in the directory in which @code{find} was run.
1456 For example, to compare each C header file in the current directory with
1457 the file @file{/tmp/master}:
1459 @example
1460 find . -name '*.h' -exec diff -u '@{@}' /tmp/master ';'
1461 @end example
1462 @end deffn
1465 @deffn Action -exec command +
1466 This variant of @samp{-exec} not yet supported.   Once it has been
1467 implemented as specified in the POSIX standard, a @samp{@{@}} will expand to
1468 a list of names of matching files.  This expansion is done in such a
1469 way as to avoid exceeding the maximum command line length available on 
1470 the system.
1471 @end deffn
1474 @node Multiple Files
1475 @subsection Multiple Files
1477 Sometimes you need to process files alone.  But when you
1478 don't, it is faster to run a command on as many files as possible at a
1479 time, rather than once per file.  Doing this saves on the time it takes
1480 to start up the command each time.
1482 To run a command on more than one file at once, use the @code{xargs}
1483 command, which is invoked like this:
1485 @example
1486 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
1487 @end example
1489 @code{xargs} reads arguments from the standard input, delimited by
1490 blanks (which can be protected with double or single quotes or a
1491 backslash) or newlines.  It executes the @var{command} (default is
1492 @file{/bin/echo}) one or more times with any @var{initial-arguments}
1493 followed by arguments read from standard input.  Blank lines on the
1494 standard input are ignored.
1496 Instead of blank-delimited names, it is safer to use @samp{find -print0}
1497 or @samp{find -fprint0} and process the output by giving the @samp{-0}
1498 or @samp{--null} option to GNU @code{xargs}, GNU @code{tar}, GNU
1499 @code{cpio}, or @code{perl}.  The @code{locate} command also has a
1500 @samp{-0} or @samp{--null} option which does the same thing.
1502 You can use shell command substitution (backquotes) to process a list of
1503 arguments, like this:
1505 @example
1506 grep -l sprintf `find $HOME -name '*.c' -print`
1507 @end example
1509 However, that method produces an error if the length of the @samp{.c}
1510 file names exceeds the operating system's command-line length limit.
1511 @code{xargs} avoids that problem by running the command as many times as
1512 necessary without exceeding the limit:
1514 @example
1515 find $HOME -name '*.c' -print | xargs grep -l sprintf
1516 @end example
1518 However, if the command needs to have its standard input be a terminal
1519 (@code{less}, for example), you have to use the shell command
1520 substitution method.
1522 The @code{xargs} command will process all its input, building command
1523 lines and executing them, unless one of the commands exits with a
1524 status of 255 (this will cause xargs to issue an error message and
1525 stop) or it reads a line contains the end of file string specified
1526 with the @samp{--eof} option. 
1528 @menu
1529 * Unsafe File Name Handling::
1530 * Safe File Name Handling::
1531 * Limiting Command Size::
1532 * Interspersing File Names::
1533 @end menu
1535 @node Unsafe File Name Handling
1536 @subsubsection Unsafe File Name Handling
1538 Because file names can contain quotes, backslashes, blank characters,
1539 and even newlines, it is not safe to process them using @code{xargs} in its
1540 default mode of operation.  But since most files' names do not contain
1541 blanks, this problem occurs only infrequently.  If you are only
1542 searching through files that you know have safe names, then you need not
1543 be concerned about it.  
1545 @c This example is adapted from:
1546 @c From: pfalstad@stone.Princeton.EDU (Paul John Falstad)
1547 @c Newsgroups: comp.unix.shell
1548 @c Subject: Re: Beware xargs security holes
1549 @c Date: 16 Oct 90 19:12:06 GMT
1550 @c 
1551 In many applications, if @code{xargs} botches processing a file because
1552 its name contains special characters, some data might be lost.  The
1553 importance of this problem depends on the importance of the data and
1554 whether anyone notices the loss soon enough to correct it.  However,
1555 here is an extreme example of the problems that using blank-delimited
1556 names can cause.  If the following command is run daily from
1557 @code{cron}, then any user can remove any file on the system:
1559 @example
1560 find / -name '#*' -atime +7 -print | xargs rm
1561 @end example
1563 For example, you could do something like this:
1565 @example
1566 eg$ echo > '#
1567 vmunix'
1568 @end example
1570 @noindent
1571 and then @code{cron} would delete @file{/vmunix}, if it ran
1572 @code{xargs} with @file{/} as its current directory.
1574 To delete other files, for example @file{/u/joeuser/.plan}, you could do
1575 this:
1577 @example
1578 eg$ mkdir '#
1580 eg$ cd '#
1582 eg$ mkdir u u/joeuser u/joeuser/.plan'
1584 eg$ echo > u/joeuser/.plan'
1585 /#foo'
1586 eg$ cd ..
1587 eg$ find . -name '#*' -print | xargs echo
1588 ./# ./# /u/joeuser/.plan /#foo
1589 @end example
1591 @node Safe File Name Handling
1592 @subsubsection Safe File Name Handling
1594 Here is how to make @code{find} output file names so that they can be
1595 used by other programs without being mangled or misinterpreted.  You can
1596 process file names generated this way by giving the @samp{-0} or
1597 @samp{--null} option to GNU @code{xargs}, GNU @code{tar}, GNU
1598 @code{cpio}, or @code{perl}.
1600 @deffn Action -print0
1601 True; print the full file name on the standard output, followed by a
1602 null character.
1603 @end deffn
1605 @deffn Action -fprint0 file
1606 True; like @samp{-print0} but write to @var{file} like @samp{-fprint}
1607 (@pxref{Print File Name}).
1608 @end deffn
1610 As of findutils version 4.2.4, the @code{locate} program also has a
1611 @samp{--null} option which does the same thing.  For similarity with
1612 @code{xargs}, the short form of the option @samp{-0} can also be used.
1614 @node Limiting Command Size
1615 @subsubsection Limiting Command Size
1617 @code{xargs} gives you control over how many arguments it passes to the
1618 command each time it executes it.  By default, it uses up to
1619 @code{ARG_MAX} - 2k, or 128k, whichever is smaller, characters per
1620 command.  It uses as many lines and arguments as fit within that limit.
1621 The following options modify those values.
1623 @table @code
1624 @item --no-run-if-empty
1625 @itemx -r
1626 If the standard input does not contain any nonblanks, do not run the
1627 command.  By default, the command is run once even if there is no input.
1629 @item --max-lines@r{[}=@var{max-lines}@r{]}
1630 @itemx -l@r{[}@var{max-lines}@r{]}
1631 Use at most @var{max-lines} nonblank input lines per command line;
1632 @var{max-lines} defaults to 1 if omitted.  Trailing blanks cause an
1633 input line to be logically continued on the next input line, for the
1634 purpose of counting the lines.  Implies @samp{-x}.
1636 @item --max-args=@var{max-args}
1637 @itemx -n @var{max-args}
1638 Use at most @var{max-args} arguments per command line.  Fewer than
1639 @var{max-args} arguments will be used if the size (see the @samp{-s}
1640 option) is exceeded, unless the @samp{-x} option is given, in which case
1641 @code{xargs} will exit.
1643 @item --max-chars=@var{max-chars}
1644 @itemx -s @var{max-chars}
1645 Use at most @var{max-chars} characters per command line, including the
1646 command and initial arguments and the terminating nulls at the ends of
1647 the argument strings.  If you specify a value for this option which is
1648 too large or small, a warning message is printed and the appropriate
1649 upper or lower limit is used instead.
1651 @item --max-procs=@var{max-procs}
1652 @itemx -P @var{max-procs}
1653 Run up to @var{max-procs} processes at a time; the default is 1.  If
1654 @var{max-procs} is 0, @code{xargs} will run as many processes as
1655 possible at a time.  Use the @samp{-n}, @samp{-s}, or @samp{-l} option
1656 with @samp{-P}; otherwise chances are that the command will be run only
1657 once.
1658 @end table
1660 @node Interspersing File Names
1661 @subsubsection Interspersing File Names
1663 @code{xargs} can insert the name of the file it is processing between
1664 arguments you give for the command.  Unless you also give options to
1665 limit the command size (@pxref{Limiting Command Size}), this mode of
1666 operation is equivalent to @samp{find -exec} (@pxref{Single File}).
1668 @table @code
1669 @item --replace@r{[}=@var{replace-str}@r{]}
1670 @itemx -i@r{[}@var{replace-str}@r{]}
1671 Replace occurrences of @var{replace-str} in the initial arguments with
1672 names read from standard input.  Also, unquoted blanks do not
1673 terminate arguments; instead, the input is split at newlines only.  If
1674 @var{replace-str} is omitted, it defaults to @samp{@{@}} (like for
1675 @samp{find -exec}).  Implies @samp{-x} and @samp{-l 1}.  As an
1676 example, to sort each file the @file{bills} directory, leaving the
1677 output in that file name with @file{.sorted} appended, you could do:
1679 @example
1680 find bills -type f | xargs -iXX sort -o XX.sorted XX
1681 @end example
1683 @noindent
1684 The equivalent command using @samp{find -exec} is:
1686 @example
1687 find bills -type f -exec sort -o '@{@}.sorted' '@{@}' ';'
1688 @end example
1689 @end table
1691 @node Querying
1692 @subsection Querying
1694 To ask the user whether to execute a command on a single file, you can
1695 use the @code{find} primary @samp{-ok} instead of @samp{-exec}:
1697 @deffn Action -ok command ;
1698 Like @samp{-exec} (@pxref{Single File}), but ask the user first (on
1699 the standard input); if the response does not start with @samp{y} or
1700 @samp{Y}, do not run the command, and return false.
1701 @end deffn
1703 When processing multiple files with a single command, to query the user
1704 you give @code{xargs} the following option.  When using this option, you
1705 might find it useful to control the number of files processed per
1706 invocation of the command (@pxref{Limiting Command Size}).
1708 @table @code
1709 @item --interactive
1710 @itemx -p
1711 Prompt the user about whether to run each command line and read a line
1712 from the terminal.  Only run the command line if the response starts
1713 with @samp{y} or @samp{Y}.  Implies @samp{-t}.
1714 @end table
1716 @node Delete Files
1717 @section Delete Files
1719 @deffn Action -delete
1720 Delete files or directories; true if removal succeeded.  If the
1721 removal failed, an error message is issued.
1723 The use of the @samp{-delete} action on the command line automatically
1724 turns on the @samp{-depth} option (@pxref{find Expressions}).
1725 @end deffn
1727 @node Adding Tests
1728 @section Adding Tests
1730 You can test for file attributes that none of the @code{find} builtin
1731 tests check.  To do this, use @code{xargs} to run a program that filters
1732 a list of files printed by @code{find}.  If possible, use @code{find}
1733 builtin tests to pare down the list, so the program run by @code{xargs}
1734 has less work to do.  The tests builtin to @code{find} will likely run
1735 faster than tests that other programs perform.
1737 For example, here is a way to print the names of all of the unstripped
1738 binaries in the @file{/usr/local} directory tree.  Builtin tests avoid
1739 running @code{file} on files that are not regular files or are not
1740 executable.
1742 @example
1743 find /usr/local -type f -perm +a=x | xargs file | 
1744   grep 'not stripped' | cut -d: -f1
1745 @end example
1747 @noindent
1748 The @code{cut} program removes everything after the file name from the
1749 output of @code{file}.
1751 @c Idea from Martin Weitzel.
1752 If you want to place a special test somewhere in the middle of a
1753 @code{find} expression, you can use @samp{-exec} to run a program that
1754 performs the test.  Because @samp{-exec} evaluates to the exit status of
1755 the executed program, you can write a program (which can be a shell
1756 script) that tests for a special attribute and make it exit with a true
1757 (zero) or false (non-zero) status.  It is a good idea to place such a
1758 special test @emph{after} the builtin tests, because it starts a new
1759 process which could be avoided if a builtin test evaluates to false.
1760 Use this method only when @code{xargs} is not flexible enough, because
1761 starting one or more new processes to test each file is slower than
1762 using @code{xargs} to start one process that tests many files.
1764 Here is a shell script called @code{unstripped} that checks whether its
1765 argument is an unstripped binary file:
1767 @example
1768 #! /bin/sh
1769 file $1 | grep 'not stripped' > /dev/null
1770 @end example
1772 This script relies on the fact that the shell exits with the status of
1773 the last program it executed, in this case @code{grep}.  @code{grep}
1774 exits with a true status if it found any matches, false if not.  Here is
1775 an example of using the script (assuming it is in your search path).  It
1776 lists the stripped executables in the file @file{sbins} and the
1777 unstripped ones in @file{ubins}.
1779 @example
1780 find /usr/local -type f -perm +a=x \
1781   \( -exec unstripped '@{@}' \; -fprint ubins -o -fprint sbins \)
1782 @end example
1784 @node Common Tasks, Databases, Actions, Top
1785 @chapter Common Tasks
1787 The sections that follow contain some extended examples that both give a
1788 good idea of the power of these programs, and show you how to solve
1789 common real-world problems.
1791 @menu
1792 * Viewing And Editing::
1793 * Archiving::
1794 * Cleaning Up::
1795 * Strange File Names::
1796 * Fixing Permissions::
1797 * Classifying Files::
1798 @end menu
1800 @node Viewing And Editing
1801 @section Viewing And Editing
1803 To view a list of files that meet certain criteria, simply run your file
1804 viewing program with the file names as arguments.  Shells substitute a
1805 command enclosed in backquotes with its output, so the whole command
1806 looks like this:
1808 @example
1809 less `find /usr/include -name '*.h' | xargs grep -l mode_t`
1810 @end example
1812 @noindent
1813 You can edit those files by giving an editor name instead of a file
1814 viewing program.
1816 @node Archiving
1817 @section Archiving
1819 You can pass a list of files produced by @code{find} to a file archiving
1820 program.  GNU @code{tar} and @code{cpio} can both read lists of file
1821 names from the standard input---either delimited by nulls (the safe way)
1822 or by blanks (the lazy, risky default way).  To use null-delimited
1823 names, give them the @samp{--null} option.  You can store a file archive
1824 in a file, write it on a tape, or send it over a network to extract on
1825 another machine.
1827 One common use of @code{find} to archive files is to send a list of the
1828 files in a directory tree to @code{cpio}.  Use @samp{-depth} so if a
1829 directory does not have write permission for its owner, its contents can
1830 still be restored from the archive since the directory's permissions are
1831 restored after its contents.  Here is an example of doing this using
1832 @code{cpio}; you could use a more complex @code{find} expression to
1833 archive only certain files.
1835 @example
1836 find . -depth -print0 |
1837   cpio --create --null --format=crc --file=/dev/nrst0
1838 @end example
1840 You could restore that archive using this command:
1842 @example
1843 cpio --extract --null --make-dir --unconditional \
1844   --preserve --file=/dev/nrst0
1845 @end example
1847 Here are the commands to do the same things using @code{tar}:
1849 @example
1850 find . -depth -print0 |
1851   tar --create --null --files-from=- --file=/dev/nrst0
1853 tar --extract --null --preserve-perm --same-owner \
1854   --file=/dev/nrst0
1855 @end example
1857 @c Idea from Rick Sladkey.
1858 Here is an example of copying a directory from one machine to another:
1860 @example
1861 find . -depth -print0 | cpio -0o -Hnewc |
1862   rsh @var{other-machine} "cd `pwd` && cpio -i0dum"
1863 @end example
1865 @node Cleaning Up
1866 @section Cleaning Up
1868 @c Idea from Jim Meyering.
1869 This section gives examples of removing unwanted files in various situations.
1870 Here is a command to remove the CVS backup files created when an update
1871 requires a merge:
1873 @example
1874 find . -name '.#*' -print0 | xargs -0r rm -f
1875 @end example
1877 The command above works, but the following is safer:
1879 @example
1880 find . -name '.#*' -depth -delete
1881 @end example
1883 @c Idea from Franc,ois Pinard.
1884 You can run this command to clean out your clutter in @file{/tmp}.  You
1885 might place it in the file your shell runs when you log out
1886 (@file{.bash_logout}, @file{.logout}, or @file{.zlogout}, depending on
1887 which shell you use).
1889 @example
1890 find /tmp -depth -user "$LOGNAME" -type f -delete 
1891 @end example
1893 If your @code{find} command removes directories, you may find that 
1894 you get a spurious error message when @code{find} tries to recurse
1895 into a directory that has now been removed.  Using the @samp{-depth}
1896 option will normally resolve this problem.
1898 @c Idea from Noah Friedman.
1899 To remove old Emacs backup and auto-save files, you can use a command
1900 like the following.  It is especially important in this case to use
1901 null-terminated file names because Emacs packages like the VM mailer
1902 often create temporary file names with spaces in them, like @file{#reply
1903 to David J. MacKenzie<1>#}.
1905 @example
1906 find ~ \( -name '*~' -o -name '#*#' \) -print0 |
1907   xargs --no-run-if-empty --null rm -vf
1908 @end example
1910 Removing old files from @file{/tmp} is commonly done from @code{cron}:
1912 @c Idea from Kaveh Ghazi.
1913 @example
1914 find /tmp /var/tmp -not -type d -mtime +3 -delete
1915 find /tmp /var/tmp -depth -mindepth 1 -type d -empty -delete
1916 @end example
1918 The second @code{find} command above uses @samp{-depth} so it cleans out
1919 empty directories depth-first, hoping that the parents become empty and
1920 can be removed too.  It uses @samp{-mindepth} to avoid removing
1921 @file{/tmp} itself if it becomes totally empty.
1923 @node Strange File Names
1924 @section Strange File Names
1926 @c Idea from:
1927 @c From: tmatimar@isgtec.com (Ted Timar)
1928 @c Newsgroups: comp.unix.questions,comp.unix.shell,comp.answers,news.answers
1929 @c Subject: Unix - Frequently Asked Questions (2/7) [Frequent posting]
1930 @c Subject: How do I remove a file with funny characters in the filename ?
1931 @c Date: Thu Mar 18 17:16:55 EST 1993
1932 @code{find} can help you remove or rename a file with strange characters
1933 in its name.  People are sometimes stymied by files whose names contain
1934 characters such as spaces, tabs, control characters, or characters with
1935 the high bit set.  The simplest way to remove such files is:
1937 @example
1938 rm -i @var{some*pattern*that*matches*the*problem*file}
1939 @end example
1941 @code{rm} asks you whether to remove each file matching the given
1942 pattern.  If you are using an old shell, this approach might not work if
1943 the file name contains a character with the high bit set; the shell may
1944 strip it off.  A more reliable way is:
1946 @example
1947 find . -maxdepth 1 @var{tests} -ok rm '@{@}' \;
1948 @end example
1950 @noindent
1951 where @var{tests} uniquely identify the file.  The @samp{-maxdepth 1}
1952 option prevents @code{find} from wasting time searching for the file in
1953 any subdirectories; if there are no subdirectories, you may omit it.  A
1954 good way to uniquely identify the problem file is to figure out its
1955 inode number; use
1957 @example
1958 ls -i
1959 @end example
1961 Suppose you have a file whose name contains control characters, and you
1962 have found that its inode number is 12345.  This command prompts you for
1963 whether to remove it:
1965 @example
1966 find . -maxdepth 1 -inum 12345 -ok rm -f '@{@}' \;
1967 @end example
1969 If you don't want to be asked, perhaps because the file name may contain
1970 a strange character sequence that will mess up your screen when printed,
1971 then use @samp{-exec} instead of @samp{-ok}.
1973 If you want to rename the file instead, you can use @code{mv} instead of
1974 @code{rm}:
1976 @example
1977 find . -maxdepth 1 -inum 12345 -ok mv '@{@}' @var{new-file-name} \;
1978 @end example
1980 @node Fixing Permissions
1981 @section Fixing Permissions
1983 Suppose you want to make sure that everyone can write to the directories in a
1984 certain directory tree.  Here is a way to find directories lacking either
1985 user or group write permission (or both), and fix their permissions:
1987 @example
1988 find . -type d -not -perm -ug=w | xargs chmod ug+w
1989 @end example
1991 @noindent
1992 You could also reverse the operations, if you want to make sure that
1993 directories do @emph{not} have world write permission.
1995 @node Classifying Files
1996 @section Classifying Files
1998 @c Idea from:
1999 @c From: martin@mwtech.UUCP (Martin Weitzel)
2000 @c Newsgroups: comp.unix.wizards,comp.unix.questions
2001 @c Subject: Advanced usage of 'find' (Re: Unix security automating script)
2002 @c Date: 22 Mar 90 15:05:19 GMT
2003 If you want to classify a set of files into several groups based on
2004 different criteria, you can use the comma operator to perform multiple
2005 independent tests on the files.  Here is an example:
2007 @example
2008 find / -type d \( -perm -o=w -fprint allwrite , \
2009   -perm -o=x -fprint allexec \)
2011 echo "Directories that can be written to by everyone:"
2012 cat allwrite
2013 echo ""
2014 echo "Directories with search permissions for everyone:"
2015 cat allexec
2016 @end example
2018 @code{find} has only to make one scan through the directory tree (which
2019 is one of the most time consuming parts of its work).
2021 @node Databases, File Permissions, Common Tasks, Top
2022 @chapter File Name Databases
2024 The file name databases used by @code{locate} contain lists of files
2025 that were in particular directory trees when the databases were last
2026 updated.  The file name of the default database is determined when
2027 @code{locate} and @code{updatedb} are configured and installed.  The
2028 frequency with which the databases are updated and the directories for
2029 which they contain entries depend on how often @code{updatedb} is run,
2030 and with which arguments.
2032 @menu
2033 * Database Locations::
2034 * Database Formats::
2035 * Newline Handling::
2036 @end menu
2039 @node Database Locations
2040 @section Database Locations
2042 There can be multiple file name databases.  Users can select which
2043 databases @code{locate} searches using an environment variable or a
2044 command line option.  The system administrator can choose the file name
2045 of the default database, the frequency with which the databases are
2046 updated, and the directories for which they contain entries.  File name
2047 databases are updated by running the @code{updatedb} program, typically
2048 nightly.
2050 In networked environments, it often makes sense to build a database at
2051 the root of each filesystem, containing the entries for that filesystem.
2052 @code{updatedb} is then run for each filesystem on the fileserver where
2053 that filesystem is on a local disk, to prevent thrashing the network.
2054 Here are the options to @code{updatedb} to select which directories each
2055 database contains entries for:
2057 @table @code
2058 @item --findoptions='@var{OPTION}@dots{}'
2059 Global options to pass on to @code{find}.
2060 The environment variable @code{FINDOPTIONS} also sets this value.
2061 Default is none.
2063 @item --localpaths='@var{path}@dots{}'
2064 Non-network directories to put in the database.
2065 Default is @file{/}.
2067 @item --netpaths='@var{path}@dots{}'
2068 Network (NFS, AFS, RFS, etc.) directories to put in the database.
2069 The environment variable @code{NETPATHS} also sets this value.
2070 Default is none.
2073 @item --prunepaths='@var{path}@dots{}'
2074 Directories to not put in the database, which would otherwise be.
2075 The environment variable @code{PRUNEPATHS} also sets this value.
2076 Default is @file{/tmp /usr/tmp /var/tmp /afs}.
2078 @item --prunefs='@var{path}@dots{}'
2079 File systems to not put in the database, which would otherwise be.
2080 Note that files are pruned when a file system is reached;
2081 Any file system mounted under an undesired file system will be
2082 ignored.
2083 The environment variable @code{PRUNEFS} also sets this value.
2084 Default is @file{nfs NFS proc}.
2086 @item --output=@var{dbfile}
2087 The database file to build.
2088 Default is system-dependent, but typically @file{/usr/local/var/locatedb}.
2090 @item --localuser=@var{user}
2091 The user to search the non-network directories as, using @code{su}.
2092 Default is to search the non-network directories as the current user.
2093 You can also use the environment variable @code{LOCALUSER} to set this user.
2095 @item --netuser=@var{user}
2096 The user to search network directories as, using @code{su}.
2097 Default is @code{daemon}.
2098 You can also use the environment variable @code{NETUSER} to set this user.
2099 @end table
2101 @node Database Formats
2102 @section Database Formats
2104 The file name databases contain lists of files that were in particular
2105 directory trees when the databases were last updated.  The file name
2106 database format changed starting with GNU @code{locate} version 4.0 to
2107 allow machines with different byte orderings to share the databases.  The
2108 new GNU @code{locate} can read both the old and new database formats.
2109 However, old versions of @code{locate} and @code{find} produce incorrect
2110 results if given a new-format database.
2112 @menu
2113 * New Database Format::
2114 * Sample Database::
2115 * Old Database Format::
2116 @end menu
2118 @node New Database Format
2119 @subsection New Database Format
2121 @code{updatedb} runs a program called @code{frcode} to
2122 @dfn{front-compress} the list of file names, which reduces the database
2123 size by a factor of 4 to 5.  Front-compression (also known as
2124 incremental encoding) works as follows.
2126 The database entries are a sorted list (case-insensitively, for users'
2127 convenience).  Since the list is sorted, each entry is likely to share a
2128 prefix (initial string) with the previous entry.  Each database entry
2129 begins with an offset-differential count byte, which is the additional
2130 number of characters of prefix of the preceding entry to use beyond the
2131 number that the preceding entry is using of its predecessor.  (The
2132 counts can be negative.)  Following the count is a null-terminated ASCII
2133 remainder---the part of the name that follows the shared prefix.
2135 If the offset-differential count is larger than can be stored in a byte
2136 (+/-127), the byte has the value 0x80 and the count follows in a 2-byte
2137 word, with the high byte first (network byte order).
2139 Every database begins with a dummy entry for a file called
2140 @file{LOCATE02}, which @code{locate} checks for to ensure that the
2141 database file has the correct format; it ignores the entry in doing the
2142 search.
2144 Databases can not be concatenated together, even if the first (dummy)
2145 entry is trimmed from all but the first database.  This is because the
2146 offset-differential count in the first entry of the second and following
2147 databases will be wrong.
2149 @node Sample Database
2150 @subsection Sample Database
2152 Sample input to @code{frcode}:
2153 @c with nulls changed to newlines:
2155 @example
2156 /usr/src
2157 /usr/src/cmd/aardvark.c
2158 /usr/src/cmd/armadillo.c
2159 /usr/tmp/zoo
2160 @end example
2162 Length of the longest prefix of the preceding entry to share:
2164 @example
2165 0 /usr/src
2166 8 /cmd/aardvark.c
2167 14 rmadillo.c
2168 5 tmp/zoo
2169 @end example
2171 Output from @code{frcode}, with trailing nulls changed to newlines
2172 and count bytes made printable:
2174 @example
2175 0 LOCATE02
2176 0 /usr/src
2177 8 /cmd/aardvark.c
2178 6 rmadillo.c
2179 -9 tmp/zoo
2180 @end example
2182 (6 = 14 - 8, and -9 = 5 - 14)
2184 @node Old Database Format
2185 @subsection Old Database Format
2187 The old database format is used by Unix @code{locate} and @code{find}
2188 programs and earlier releases of the GNU ones.  @code{updatedb} produces
2189 this format if given the @samp{--old-format} option.
2191 @code{updatedb} runs programs called @code{bigram} and @code{code} to
2192 produce old-format databases.  The old format differs from the new one
2193 in the following ways.  Instead of each entry starting with an
2194 offset-differential count byte and ending with a null, byte values from
2195 0 through 28 indicate offset-differential counts from -14 through 14.
2196 The byte value indicating that a long offset-differential count follows
2197 is 0x1e (30), not 0x80.  The long counts are stored in host byte order,
2198 which is not necessarily network byte order, and host integer word size,
2199 which is usually 4 bytes.  They also represent a count 14 less than
2200 their value.  The database lines have no termination byte; the start of
2201 the next line is indicated by its first byte having a value <= 30.
2203 In addition, instead of starting with a dummy entry, the old database
2204 format starts with a 256 byte table containing the 128 most common
2205 bigrams in the file list.  A bigram is a pair of adjacent bytes.  Bytes
2206 in the database that have the high bit set are indexes (with the high
2207 bit cleared) into the bigram table.  The bigram and offset-differential
2208 count coding makes these databases 20-25% smaller than the new format,
2209 but makes them not 8-bit clean.  Any byte in a file name that is in the
2210 ranges used for the special codes is replaced in the database by a
2211 question mark, which not coincidentally is the shell wildcard to match a
2212 single character.
2214 The old format therefore can not faithfully store entries with non-ASCII
2215 characters. It therefore should not be used in internationalized
2216 environments.
2218 @node Newline Handling
2219 @section Newline Handling
2221 Within the database, filenames are terminated with a null character.
2222 This is the case for both the old and the new format.  
2224 When the new database format is being used, the compression technique
2225 used to generate the database though relies on the ability to sort the
2226 list of files before they are presented to @code{frcode}.
2228 If the system's sort command allows its input list of files to be
2229 separated with null characters via the @samp{-z} option, this option
2230 is used and therefore @code{updatedb} and @code{locate} will both
2231 correctly handle filenames containing newlines.  If the @code{sort}
2232 command lacks support for this, the list of files is delimited with
2233 the newline character, meaning that parts of filenames containing
2234 newlines will be incorrectly sorted.  This can result in both
2235 incorrect matches and incorrect failures to match.
2237 On the other hand, if you are using the old database format, filenames
2238 with embedded newlines are not correctly handled.  There is no
2239 technical limitation which enforces this, it's just that the
2240 @code{bigram} program has no been updated to support lists of
2241 filenames separated by nulls.
2243 So, if you are using the new database format (this is the default) and
2244 your system uses GNU @code{find}, newlines will be correctly handled
2245 at all times.  Otherwise, newlines may not be correctly handled.
2247 @node File Permissions, Reference, Databases, Top
2248 @chapter File Permissions
2250 @include perm.texi
2252 @node Reference, Primary Index, File Permissions, Top
2253 @chapter Reference
2255 Below are summaries of the command line syntax for the programs
2256 discussed in this manual.
2258 @menu
2259 * Invoking find::
2260 * Invoking locate::
2261 * Invoking updatedb::
2262 * Invoking xargs::
2263 @end menu
2265 @node Invoking find, Invoking locate, , Reference
2266 @section Invoking @code{find}
2268 @example
2269 find @r{[}@var{file}@dots{}@r{]} @r{[}@var{expression}@r{]}
2270 @end example
2272 @code{find} searches the directory tree rooted at each file name
2273 @var{file} by evaluating the @var{expression} on each file it finds in
2274 the tree.
2276 @code{find} considers the first argument that begins with @samp{-},
2277 @samp{(}, @samp{)}, @samp{,}, or @samp{!} to be the beginning of the
2278 expression; any arguments before it are paths to search, and any
2279 arguments after it are the rest of the expression.  If no paths are
2280 given, the current directory is used.  If no expression is given, the
2281 expression @samp{-print} is used.
2283 @code{find} exits with status 0 if all files are processed successfully,
2284 greater than 0 if errors occur.
2286 @xref{Primary Index}, for a summary of all of the tests, actions, and
2287 options that the expression can contain.
2289 @code{find} also recognizes two options for administrative use:
2291 @table @code
2292 @item --help
2293 Print a summary of the command-line argument format and exit.
2294 @item --version
2295 Print the version number of @code{find} and exit.
2296 @end table
2299 @menu
2300 * Warning Messages::
2301 @end menu
2304 @node Warning Messages,,, Invoking find
2305 @subsection Warning Messages
2307 If there is an error on the @code{find} command line, an error message
2308 is normally issued.  However, there are some usages that are
2309 inadvisable but which @code{find} should still accept.  Under these
2310 circumstances, @code{find} may issue a warning message.  By default,
2311 warnings are enabled only if @code{find} is being run interactively
2312 (specifically, if the standard input is a terminal).  Warning messages
2313 can be controlled explicitly by the use of options on the command
2314 line:
2316 @table @code
2317 @item -warn
2318 Issue warning messages where appropriate.
2319 @item -nowarn
2320 Do not issue warning messages.
2321 @end table
2323 These options take effect at the point on the command line where they
2324 are specified.  Therefore if you specify @samp{-nowarn} at the end of
2325 the command line, you will not see warning messages for any problems
2326 occurring before that.  The warning messages affected by the above
2327 options are triggered by:
2329 @itemize @minus
2330 @item
2331 Use of the @samp{-d} option which is deprecated; please use
2332 @samp{-depth} instead, since the latter is POSIX-compliant.
2333 @item
2334 Use of the @samp{-ipath} option which is deprecated; please use
2335 @samp{-iwholename} instead.
2336 @item 
2337 Specifying an option (for example @samp{-mindepth}) after a non-option
2338 (for example @samp{-type} or @samp{-print}) on the command line.
2339 @end itemize
2342 The default behaviour above is designed to work in that way so that
2343 existing shell scripts which use such constructs don't generate
2344 spurious errors, but people will be made aware of the problem.
2346 Some warning messages are issued for less common or more serious
2347 problems, and so cannot be turned off:
2349 @itemize @minus
2350 @item
2351 Use of an unrecognised backslash escape sequence with @samp{-fprintf}
2352 @item
2353 Use of an unrecognised formatting directive with @samp{-fprintf}
2354 @end itemize
2356 @node Invoking locate, Invoking updatedb, Invoking find, Reference
2357 @section Invoking @code{locate}
2359 @example
2360 locate @r{[}@var{option}@dots{}@r{]} @var{pattern}@dots{}
2361 @end example
2363 @table @code
2364 @item --database=@var{path}
2365 @itemx -d @var{path}
2366 Instead of searching the default file name database, search the file
2367 name databases in @var{path}, which is a colon-separated list of
2368 database file names.  You can also use the environment variable
2369 @code{LOCATE_PATH} to set the list of database files to search.  The
2370 option overrides the environment variable if both are used.
2372 @item --existing
2373 @itemx -e
2374 Only print out such names which currently exist (instead of such names
2375 which existed when the database was created).
2376 Note that this may slow down the program a lot, if there are many matches
2377 in the database.
2379 @item --ignore-case
2380 @itemx -i
2381 Ignore case distinctions in both the pattern and the file names.
2383 @item --null
2384 @itemx -0
2385 Results are separated with the ASCII NUL character rather than the
2386 newline character.
2388 @item --count
2389 @itemx -c
2390 Just print the number of results, not the results themselves.
2392 @item --limit=N
2393 @itemx -l N
2394 Limit the number of results printed to N.  If you use the
2395 @samp{--count} option, the value printed will never be larger than
2396 this limit.
2398 @item --wholename
2399 The specified pattern is matched against the whole name of the file in
2400 the locate database.  If the pattern contains metacharacters, it must
2401 match exactly.  If not, it must match part of the whole file name.
2402 This is the default behaviour.
2404 @item --basename
2405 The specified pattern is matched against just the last component of
2406 the name of the file in the locate database.  This last component is
2407 also called the ``base name''.  For example, the base name of
2408 @file{/tmp/mystuff/foo.old.c} is @file{foo.old.c}.  If the pattern
2409 contains metacharacters, it must match exactly.  If not, it must match
2410 part of the whole file name.
2412 @item --help
2413 Print a summary of the options to @code{locate} and exit.
2415 @item --version
2416 Print the version number of @code{locate} and exit.
2417 @end table
2419 @node Invoking updatedb, Invoking xargs, Invoking locate, Reference
2420 @section Invoking @code{updatedb}
2422 @example
2423 updatedb @r{[}@var{option}@dots{}@r{]}
2424 @end example
2426 @table @code
2427 @item --findoptions='@var{OPTION}@dots{}'
2428 Global options to pass on to @code{find}.
2429 The environment variable @code{FINDOPTIONS} also sets this value.
2430 Default is none.
2432 @item --localpaths='@var{path}@dots{}'
2433 Non-network directories to put in the database.
2434 Default is @file{/}.
2436 @item --netpaths='@var{path}@dots{}'
2437 Network (NFS, AFS, RFS, etc.) directories to put in the database.
2438 The environment variable @code{NETPATHS} also sets this value.
2439 Default is none.
2441 @item --prunepaths='@var{path}@dots{}'
2442 Directories to not put in the database, which would otherwise be.
2443 The environment variable @code{PRUNEPATHS} also sets this value.
2444 Default is @file{/tmp /usr/tmp /var/tmp /afs}.
2446 @item --prunefs='@var{path}@dots{}'
2447 File systems to omit from the database, which would otherwise be included.
2448 Note that files are pruned when a file system is reached;
2449 Any file system mounted under an undesired file system will be
2450 ignored.
2451 The environment variable @code{PRUNEFS} also sets this value.
2452 Default is @file{nfs NFS proc}.
2454 @item --output=@var{dbfile}
2455 The database file to build.
2456 Default is system-dependent, but typically @file{/usr/local/var/locatedb}.
2458 @item --localuser=@var{user}
2459 The user to search the non-network directories as, using @code{su}.
2460 Default is to search the non-network directories as the current user.
2461 You can also use the environment variable @code{LOCALUSER} to set this user.
2463 @item --netuser=@var{user}
2464 The user to search network directories as, using @code{su}(1).
2465 Default is @code{daemon}.
2466 You can also use the environment variable @code{NETUSER} to set this user.
2467 @end table
2469 @node Invoking xargs, ,  Invoking updatedb, Reference
2470 @section Invoking @code{xargs}
2472 @example
2473 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
2474 @end example
2476 @code{xargs} exits with the following status:
2478 @table @asis
2479 @item 0
2480 if it succeeds
2481 @item 123
2482 if any invocation of the command exited with status 1-125
2483 @item 124
2484 if the command exited with status 255
2485 @item 125
2486 if the command is killed by a signal
2487 @item 126
2488 if the command cannot be run
2489 @item 127
2490 if the command is not found
2491 @item 1
2492 if some other error occurred.
2493 @end table
2495 @table @code
2496 @item --null
2497 @itemx -0
2498 Input filenames are terminated by a null character instead of by
2499 whitespace, and the quotes and backslash are not special (every
2500 character is taken literally).  Disables the end of file string, which
2501 is treated like any other argument.
2503 @item --eof@r{[}=@var{eof-str}@r{]}
2504 @itemx -e@r{[}@var{eof-str}@r{]}
2505 Set the end of file string to @var{eof-str}.  If the end of file string
2506 occurs as a line of input, the rest of the input is ignored.  If
2507 @var{eof-str} is omitted, there is no end of file string.  If this
2508 option is not given, the end of file string defaults to @samp{_}.
2510 @item --help
2511 Print a summary of the options to @code{xargs} and exit.
2513 @item --replace@r{[}=@var{replace-str}@r{]}
2514 @itemx -i@r{[}@var{replace-str}@r{]}
2515 Replace occurrences of @var{replace-str} in the initial arguments with
2516 names read from standard input.  Also, unquoted blanks do not
2517 terminate arguments; instead, the input is split at newlines only.
2518 If @var{replace-str} is omitted, it defaults to @samp{@{@}}
2519 (like for @samp{find -exec}).  Implies @samp{-x} and @samp{-l 1}.
2521 @item --max-lines@r{[}=@var{max-lines}@r{]}
2522 @itemx -l@r{[}@var{max-lines}@r{]}
2523 Use at most @var{max-lines} nonblank input lines per command line;
2524 @var{max-lines} defaults to 1 if omitted.  Trailing blanks cause an
2525 input line to be logically continued on the next input line, for the
2526 purpose of counting the lines.  Implies @samp{-x}.
2528 @item --max-args=@var{max-args}
2529 @itemx -n @var{max-args}
2530 Use at most @var{max-args} arguments per command line.  Fewer than
2531 @var{max-args} arguments will be used if the size (see the @samp{-s}
2532 option) is exceeded, unless the @samp{-x} option is given, in which case
2533 @code{xargs} will exit.
2535 @item --interactive
2536 @itemx -p
2537 Prompt the user about whether to run each command line and read a line
2538 from the terminal.  Only run the command line if the response starts
2539 with @samp{y} or @samp{Y}.  Implies @samp{-t}.
2541 @item --no-run-if-empty
2542 @itemx -r
2543 If the standard input does not contain any nonblanks, do not run the
2544 command.  By default, the command is run once even if there is no input.
2546 @item --max-chars=@var{max-chars}
2547 @itemx -s @var{max-chars}
2548 Use at most @var{max-chars} characters per command line, including the
2549 command and initial arguments and the terminating nulls at the ends of
2550 the argument strings.
2552 @item --verbose
2553 @itemx -t
2554 Print the command line on the standard error output before executing
2557 @item --version
2558 Print the version number of @code{xargs} and exit.
2560 @item --exit
2561 @itemx -x
2562 Exit if the size (see the @samp{-s} option) is exceeded.
2565 @item --max-procs=@var{max-procs}
2566 @itemx -P @var{max-procs}
2567 Run up to @var{max-procs} processes at a time; the default is 1.  If
2568 @var{max-procs} is 0, @code{xargs} will run as many processes as
2569 possible at a time.
2570 @end table
2572 @node Primary Index, , Reference, Top
2573 @unnumbered @code{find} Primary Index
2575 This is a list of all of the primaries (tests, actions, and options)
2576 that make up @code{find} expressions for selecting files.  @xref{find
2577 Expressions}, for more information on expressions.
2579 @printindex fn
2581 @contents
2582 @bye
2584 @comment texi related words used by Emacs' spell checker ispell.el
2586 @comment LocalWords: texinfo setfilename settitle setchapternewpage
2587 @comment LocalWords: iftex finalout ifinfo DIR titlepage vskip pt
2588 @comment LocalWords: filll dir samp dfn noindent xref pxref
2589 @comment LocalWords: var deffn texi deffnx itemx emph asis
2590 @comment LocalWords: findex smallexample subsubsection cindex
2592 @comment other words used by Emacs' spell checker ispell.el
2593 @comment LocalWords: README fred updatedb xargs Plett Rendell akefile
2594 @comment LocalWords: args grep Filesystems fo foo fOo wildcards iname
2595 @comment LocalWords: ipath regex iregex expr fubar regexps
2596 @comment LocalWords: metacharacters macs sr sc inode lname ilname
2597 @comment LocalWords: sysdep noleaf ls inum xdev filesystems usr atime
2598 @comment LocalWords: ctime mtime amin cmin mmin al daystart Sladkey rm
2599 @comment LocalWords: anewer cnewer bckw rf xtype uname gname uid gid
2600 @comment LocalWords: nouser nogroup chown chgrp perm ch maxdepth
2601 @comment LocalWords: mindepth cpio src CD AFS statted stat fstype ufs
2602 @comment LocalWords: nfs tmp mfs printf fprint dils rw djm Nov lwall
2603 @comment LocalWords: POSIXLY fls fprintf strftime locale's EDT GMT AP
2604 @comment LocalWords: EST diff perl backquotes sprintf Falstad Oct cron
2605 @comment LocalWords: eg vmunix mkdir afs allexec allwrite ARG bigram
2606 @comment LocalWords: bigrams cd chmod comp crc CVS dbfile dum eof
2607 @comment LocalWords: fileserver filesystem fn frcode Ghazi Hnewc iXX
2608 @comment LocalWords: joeuser Kaveh localpaths localuser LOGNAME
2609 @comment LocalWords: Meyering mv netpaths netuser nonblank nonblanks
2610 @comment LocalWords: ois ok Pinard printindex proc procs prunefs
2611 @comment LocalWords: prunepaths pwd RFS rmadillo rmdir rsh sbins str
2612 @comment LocalWords: su Timar ubins ug unstripped vf VM Weitzel
2613 @comment LocalWords: wildcard zlogout