Documented the behaviour of -daystart and -follow in more detail
[findutils.git] / doc / find.texi
blob2c9573c1b8aed6a68af3c5377d2c512d74164f47
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 The command
411 @example
412 locate @var{pattern}
413 @end example
415 is almost equivalent to
416 @example
417 find @var{directories} -name @var{pattern}
418 @end example
420 where @var{directories} are the directories for which the file name
421 databases contain information.  The differences are that the
422 @code{locate} information might be out of date, and that @code{locate}
423 handles wildcards in the pattern slightly differently than @code{find}
424 (@pxref{Shell Pattern Matching}).
426 The file name databases contain lists of files that were on the system
427 when the databases were last updated.  The system administrator can
428 choose the file name of the default database, the frequency with which
429 the databases are updated, and the directories for which they contain
430 entries.
432 Here is how to select which file name databases @code{locate} searches.
433 The default is system-dependent.
435 @table @code
436 @item --database=@var{path}
437 @itemx -d @var{path}
438 Instead of searching the default file name database, search the file
439 name databases in @var{path}, which is a colon-separated list of
440 database file names.  You can also use the environment variable
441 @code{LOCATE_PATH} to set the list of database files to search.  The
442 option overrides the environment variable if both are used.
443 @end table
445 @node Shell Pattern Matching
446 @subsection Shell Pattern Matching
448 @code{find} and @code{locate} can compare file names, or parts of file
449 names, to shell patterns.  A @dfn{shell pattern} is a string that may
450 contain the following special characters, which are known as
451 @dfn{wildcards} or @dfn{metacharacters}.
453 You must quote patterns that contain metacharacters to prevent the shell
454 from expanding them itself.  Double and single quotes both work; so does
455 escaping with a backslash.
457 @table @code
458 @item *
459 Matches any zero or more characters.
461 @item ?
462 Matches any one character.
464 @item [@var{string}]
465 Matches exactly one character that is a member of the string
466 @var{string}.  This is called a @dfn{character class}.  As a shorthand,
467 @var{string} may contain ranges, which consist of two characters with a
468 dash between them.  For example, the class @samp{[a-z0-9_]} matches a
469 lowercase letter, a number, or an underscore.  You can negate a class by
470 placing a @samp{!} or @samp{^} immediately after the opening bracket.
471 Thus, @samp{[^A-Z@@]} matches any character except an uppercase letter
472 or an at sign.
474 @item \
475 Removes the special meaning of the character that follows it.  This
476 works even in character classes.
477 @end table
479 In the @code{find} tests that do shell pattern matching (@samp{-name},
480 @samp{-wholename}, etc.), wildcards in the pattern do not match a @samp{.}
481 at the beginning of a file name.  This is not the case for
482 @code{locate}.  Thus, @samp{find -name '*macs'} does not match a file
483 named @file{.emacs}, but @samp{locate '*macs'} does.
485 Slash characters have no special significance in the shell pattern
486 matching that @code{find} and @code{locate} do, unlike in the shell, in
487 which wildcards do not match them.  Therefore, a pattern @samp{foo*bar}
488 can match a file name @samp{foo3/bar}, and a pattern @samp{./sr*sc} can
489 match a file name @samp{./src/misc}.
491 @node Links
492 @section Links
494 There are two ways that files can be linked together.  @dfn{Symbolic
495 links} are a special type of file whose contents are a portion of the
496 name of another file.  @dfn{Hard links} are multiple directory entries
497 for one file; the file names all have the same index node (@dfn{inode})
498 number on the disk.
500 @menu
501 * Symbolic Links::
502 * Hard Links::
503 @end menu
505 @node Symbolic Links
506 @subsection Symbolic Links
508 @deffn Test -lname pattern
509 @deffnx Test -ilname pattern
510 True if the file is a symbolic link whose contents match shell pattern
511 @var{pattern}.  For @samp{-ilname}, the match is case-insensitive.
512 @xref{Shell Pattern Matching}, for details about the @var{pattern}
513 argument.  So, to list any symbolic links to @file{sysdep.c} in the
514 current directory and its subdirectories, you can do:
516 @example
517 find . -lname '*sysdep.c'
518 @end example
519 @end deffn
521 @deffn Option -follow
522 Dereference symbolic links.  The @samp{-follow} option is unlike most
523 other options in that it can be useful to place it after other tests,
524 because of its effect on the behaviour of the @samp{-newer},
525 @samp{-anewer} and @samp{-cnewer} tests.  The following differences in
526 behavior occur when this option is given:
528 @itemize @bullet
529 @item
530 @code{find} follows symbolic links to directories when searching
531 directory trees.
532 @item
533 @samp{-lname} and @samp{-ilname} always return false.
534 @item
535 @samp{-type} reports the types of the files that symbolic links point
537 @item
538 Implies @samp{-noleaf} (@pxref{Directories}).
539 @item 
540 If the argument to @samp{-newer} is a symbolic link, it is
541 dereferenced.  Note that because the timestamp of the file is
542 determined when the command line is being parsed, the position of the
543 @samp{-follow} option is important.   The @samp{-anewer} and
544 @samp{-cnewer} tests are affected similarly. 
545 @end itemize
546 @end deffn
548 @node Hard Links
549 @subsection Hard Links
551 To find hard links, first get the inode number of the file whose links
552 you want to find.  You can learn a file's inode number and the number of
553 links to it by running @samp{ls -i} or @samp{find -ls}.  If the file has
554 more than one link, you can search for the other links by passing that
555 inode number to @samp{-inum}.  Add the @samp{-xdev} option if you are
556 starting the search at a directory that has other filesystems mounted on
557 it, such as @file{/usr} on many systems.  Doing this saves needless
558 searching, since hard links to a file must be on the same filesystem.
559 @xref{Filesystems}.
561 @deffn Test -inum n
562 File has inode number @var{n}.
563 @end deffn
565 You can also search for files that have a certain number of links, with
566 @samp{-links}.  Directories normally have at least two hard links; their
567 @file{.} entry is the second one.  If they have subdirectories, each of
568 those also has a hard link called @file{..} to its parent directory.
570 @deffn Test -links n
571 File has @var{n} hard links.
572 @end deffn
574 @node Time
575 @section Time
577 Each file has three time stamps, which record the last time that certain
578 operations were performed on the file:
580 @enumerate
581 @item
582 access (read the file's contents)
583 @item
584 change the status (modify the file or its attributes)
585 @item
586 modify (change the file's contents)
587 @end enumerate
589 You can search for files whose time stamps are within a certain age
590 range, or compare them to other time stamps.
592 @menu
593 * Age Ranges::
594 * Comparing Timestamps::
595 @end menu
597 @node Age Ranges
598 @subsection Age Ranges
600 These tests are mainly useful with ranges (@samp{+@var{n}} and
601 @samp{-@var{n}}).
603 @deffn Test -atime n
604 @deffnx Test -ctime n
605 @deffnx Test -mtime n
606 True if the file was last accessed (or its status changed, or it was
607 modified) @var{n}*24 hours ago.
608 @end deffn
610 @deffn Test -amin n
611 @deffnx Test -cmin n
612 @deffnx Test -mmin n
613 True if the file was last accessed (or its status changed, or it was
614 modified) @var{n} minutes ago.  These tests provide finer granularity of
615 measurement than @samp{-atime} et al.  For example, to list files in
616 @file{/u/bill} that were last read from 2 to 6 minutes ago:
618 @example
619 find /u/bill -amin +2 -amin -6
620 @end example
621 @end deffn
623 @deffn Option -daystart
624 Measure times from the beginning of today rather than from 24 hours ago.
625 So, to list the regular files in your home directory that were modified
626 yesterday, do
628 @example
629 find ~ -daystart -type f -mtime 1
630 @end example
631 @end deffn
633 The @samp{-daystart} option is unlike most other options in that it
634 has an effect on the way that other tests are performed.  The affected
635 tests are @samp{-amin}, @samp{-cmin}, @samp{-mmin}, @samp{-atime},
636 @samp{-ctime} and @samp{-mtime}.
638 @node Comparing Timestamps
639 @subsection Comparing Timestamps
641 As an alternative to comparing timestamps to the current time, you can
642 compare them to another file's timestamp.  That file's timestamp could
643 be updated by another program when some event occurs.  Or you could set
644 it to a particular fixed date using the @code{touch} command.  For
645 example, to list files in @file{/usr} modified after February 1 of the
646 current year:
648 @c Idea from Rick Sladkey.
649 @example
650 touch -t 02010000 /tmp/stamp$$
651 find /usr -newer /tmp/stamp$$
652 rm -f /tmp/stamp$$
653 @end example
655 @deffn Test -anewer file
656 @deffnx Test -cnewer file
657 @deffnx Test -newer file
658 True if the file was last accessed (or its status changed, or it was
659 modified) more recently than @var{file} was modified.  These tests are
660 affected by @samp{-follow} only if @samp{-follow} comes before them on
661 the command line.  @xref{Symbolic Links}, for more information on
662 @samp{-follow}.  As an example, to list any files modified since
663 @file{/bin/sh} was last modified:
665 @example
666 find . -newer /bin/sh
667 @end example
668 @end deffn
670 @deffn Test -used n
671 True if the file was last accessed @var{n} days after its status was
672 last changed.  Useful for finding files that are not being used, and
673 could perhaps be archived or removed to save disk space.
674 @end deffn
676 @node Size
677 @section Size
679 @deffn Test -size n@r{[}bckwMG@r{]}
680 True if the file uses @var{n} units of space, rounding up.  The units
681 are 512-byte blocks by default, but they can be changed by adding a
682 one-character suffix to @var{n}:
684 @table @code
685 @item b
686 512-byte blocks (never 1024)
687 @item c
688 bytes
689 @item k
690 kilobytes (1024 bytes)
691 @item w
692 2-byte words
693 @item M
694 Megabytes
695 @item G
696 Gigabytes
697 @end table
699 The `b' suffix always considers blocks to be 512 bytes.  This is not
700 affected by the setting (or non-setting) of the POSIXLY_CORRECT
701 environment variable.  This behaviour is different to the behaviour of
702 the @samp{-ls} action).  If you want to use 1024-byte units, use the
703 `k' suffix instead.
705 The number can be prefixed with a `+' or a `-'.  A plus sign indicates
706 that the test should succeed if the file uses at least @var{n} units
707 of storage (this is the way I normally use this test) and a minus sign
708 indicates that the test should succeed if the file uses less than
709 @var{n} units of storage.   There is no `=' prefix, because that's the 
710 default anyway.  
712 The size does not count indirect blocks, but it does count blocks in
713 sparse files that are not actually allocated.  This handling of sparse
714 files differs from the output of the @samp{%k} and @samp{%b} format
715 specifiers for the @samp{-printf} predicate.
717 @end deffn
719 @deffn Test -empty
720 True if the file is empty and is either a regular file or a directory.
721 This might make it a good candidate for deletion.  This test is useful
722 with @samp{-depth} (@pxref{Directories}) and @samp{-delete}
723 (@pxref{Single File}).
724 @end deffn
726 @node Type
727 @section Type
729 @deffn Test -type c
730 True if the file is of type @var{c}:
732 @table @code
733 @item b
734 block (buffered) special
735 @item c
736 character (unbuffered) special
737 @item d
738 directory
739 @item p
740 named pipe (FIFO)
741 @item f
742 regular file
743 @item l
744 symbolic link
745 @item s
746 socket
747 @item D
748 door (Solaris)
749 @end table
750 @end deffn
752 @deffn Test -xtype c
753 The same as @samp{-type} unless the file is a symbolic link.  For
754 symbolic links: if @samp{-follow} has not been given, true if the file
755 is a link to a file of type @var{c}; if @samp{-follow} has been given,
756 true if @var{c} is @samp{l}.  In other words, for symbolic links,
757 @samp{-xtype} checks the type of the file that @samp{-type} does not
758 check.  @xref{Symbolic Links}, for more information on @samp{-follow}.
759 @end deffn
761 @node Owner
762 @section Owner
764 @deffn Test -user uname
765 @deffnx Test -group gname
766 True if the file is owned by user @var{uname} (belongs to group @var{gname}).
767 A numeric ID is allowed.
768 @end deffn
770 @deffn Test -uid n
771 @deffnx Test -gid n
772 True if the file's numeric user ID (group ID) is @var{n}.  These tests
773 support ranges (@samp{+@var{n}} and @samp{-@var{n}}), unlike
774 @samp{-user} and @samp{-group}.
775 @end deffn
777 @deffn Test -nouser
778 @deffnx Test -nogroup
779 True if no user corresponds to the file's numeric user ID (no group
780 corresponds to the numeric group ID).  These cases usually mean that the
781 files belonged to users who have since been removed from the system.
782 You probably should change the ownership of such files to an existing
783 user or group, using the @code{chown} or @code{chgrp} program.
784 @end deffn
786 @node Permissions
787 @section Permissions
789 @xref{File Permissions}, for information on how file permissions are
790 structured and how to specify them.
792 @deffn Test -perm mode
793 True if the
794 file's permissions are exactly @var{mode} (which can be numeric or symbolic).
795 Symbolic modes use mode 0 as a point of departure.
796 If @var{mode} starts with @samp{-}, true if
797 @emph{all} of the permissions set in @var{mode} are set for the file;
798 permissions not set in @var{mode} are ignored.
799 If @var{mode} starts with @samp{+}, true if
800 @emph{any} of the permissions set in @var{mode} are set for the file;
801 permissions not set in @var{mode} are ignored.
802 @end deffn
804 @node Contents
805 @section Contents
807 To search for files based on their contents, you can use the @code{grep}
808 program.  For example, to find out which C source files in the current
809 directory contain the string @samp{thing}, you can do:
811 @example
812 grep -l thing *.[ch]
813 @end example
815 If you also want to search for the string in files in subdirectories,
816 you can combine @code{grep} with @code{find} and @code{xargs}, like
817 this:
819 @example
820 find . -name '*.[ch]' | xargs grep -l thing
821 @end example
823 The @samp{-l} option causes @code{grep} to print only the names of files
824 that contain the string, rather than the lines that contain it.  The
825 string argument (@samp{thing}) is actually a regular expression, so it
826 can contain metacharacters.  This method can be refined a little by
827 using the @samp{-r} option to make @code{xargs} not run @code{grep} if
828 @code{find} produces no output, and using the @code{find} action
829 @samp{-print0} and the @code{xargs} option @samp{-0} to avoid
830 misinterpreting files whose names contain spaces:
832 @example
833 find . -name '*.[ch]' -print0 | xargs -r -0 grep -l thing
834 @end example
836 For a fuller treatment of finding files whose contents match a pattern,
837 see the manual page for @code{grep}.
839 @node Directories
840 @section Directories
842 Here is how to control which directories @code{find} searches, and how
843 it searches them.  These two options allow you to process a horizontal
844 slice of a directory tree.
846 @deffn Option -maxdepth levels
847 Descend at most @var{levels} (a non-negative integer) levels of
848 directories below the command line arguments.  @samp{-maxdepth 0} means
849 only apply the tests and actions to the command line arguments.
850 @end deffn
852 @deffn Option -mindepth levels
853 Do not apply any tests or actions at levels less than @var{levels} (a
854 non-negative integer).  @samp{-mindepth 1} means process all files
855 except the command line arguments.
856 @end deffn
858 @deffn Option -depth
859 Process each directory's contents before the directory itself.  Doing
860 this is a good idea when producing lists of files to archive with
861 @code{cpio} or @code{tar}.  If a directory does not have write
862 permission for its owner, its contents can still be restored from the
863 archive since the directory's permissions are restored after its contents.
864 @end deffn
866 @deffn Option -d
867 This is a deprecated synonym for @samp{-depth}, for compatibility with
868 Mac OS X, FreeBSD and OpenBSD.   The @samp{-depth} option is a POSIX
869 feature, so it is better to use that.
870 @end deffn
872 @deffn Action -prune
873 If @samp{-depth} is not given, true; do not descend into the current
874 directory.  If @samp{-depth} is given, false; no effect.  @samp{-prune}
875 only affects tests and actions that come after it in the expression, not
876 those that come before.
878 For example, to skip the directory @file{src/emacs} and all files and
879 directories under it, and print the names of the other files found:
881 @example
882 find . -wholename './src/emacs' -prune -o -print
883 @end example
884 @end deffn
886 @deffn Action -quit
887 Exit immediately with return value zero.  No child proceses will be
888 left running, but no more paths specified on the command line will be 
889 processed.   For example, @code{find /tmp/foo /tmp/bar -print -quit}
890 will print only @samp{/tmp/foo}.
891 @end deffn 
893 @deffn Option -noleaf
894 Do not optimize by assuming that directories contain 2 fewer
895 subdirectories than their hard link count.  This option is needed when
896 searching filesystems that do not follow the Unix directory-link
897 convention, such as CD-ROM or MS-DOS filesystems or AFS volume mount
898 points.  Each directory on a normal Unix filesystem has at least 2 hard
899 links: its name and its @file{.}  entry.  Additionally, its
900 subdirectories (if any) each have a @file{..}  entry linked to that
901 directory.  When @code{find} is examining a directory, after it has
902 statted 2 fewer subdirectories than the directory's link count, it knows
903 that the rest of the entries in the directory are non-directories
904 (@dfn{leaf} files in the directory tree).  If only the files' names need
905 to be examined, there is no need to stat them; this gives a significant
906 increase in search speed.
907 @end deffn
909 @deffn Option -ignore_readdir_race
910 If a file disappears after its name has been read from a directory but
911 before @code{find} gets around to examining the file with @code{stat},
912 don't issue an error message.  If you don't specify this option, an
913 error message will be issued.  This option can be useful in system
914 scripts (cron scripts, for example) that examine areas of the
915 filesystem that change frequently (mail queues, temporary directories,
916 and so forth), because this scenario is common for those sorts of
917 directories.  Completely silencing error messages from @code{find} is
918 undesirable, so this option neatly solves the problem.  There is no
919 way to search one part of the filesystem with this option on and part
920 of it with this option off, though.
921 @end deffn
923 @deffn Option -noignore_readdir_race
924 This option reverses the effect of the @samp{-ignore_readdir_race} option.
925 @end deffn
928 @node Filesystems
929 @section Filesystems
931 A @dfn{filesystem} is a section of a disk, either on the local host or
932 mounted from a remote host over a network.  Searching network
933 filesystems can be slow, so it is common to make @code{find} avoid them.
935 There are two ways to avoid searching certain filesystems.  One way is
936 to tell @code{find} to only search one filesystem:
938 @deffn Option -xdev
939 @deffnx Option -mount
940 Don't descend directories on other filesystems.  These options are synonyms.
941 @end deffn
943 The other way is to check the type of filesystem each file is on, and
944 not descend directories that are on undesirable filesystem types:
946 @deffn Test -fstype type
947 True if the file is on a filesystem of type @var{type}.  The valid
948 filesystem types vary among different versions of Unix; an incomplete
949 list of filesystem types that are accepted on some version of Unix or
950 another is:
951 @example
952 ext2 ext3 proc sysfs ufs 4.2 4.3 nfs tmp mfs S51K S52K
953 @end example
954 You can use @samp{-printf} with the @samp{%F} directive to see the types
955 of your filesystems.  The @samp{%D} directive shows the device number.
956 @xref{Print File Information}.  @samp{-fstype} is
957 usually used with @samp{-prune} to avoid searching remote filesystems
958 (@pxref{Directories}). 
959 @end deffn
961 @node Combining Primaries With Operators
962 @section Combining Primaries With Operators
964 Operators build a complex expression from tests and actions.
965 The operators are, in order of decreasing precedence:
967 @table @code
968 @item @asis{( @var{expr} )}
969 @findex ()
970 Force precedence.  True if @var{expr} is true.
972 @item @asis{! @var{expr}}
973 @itemx @asis{-not @var{expr}}
974 @findex !
975 @findex -not
976 True if @var{expr} is false.
978 @item @asis{@var{expr1 expr2}}
979 @itemx @asis{@var{expr1} -a @var{expr2}}
980 @itemx @asis{@var{expr1} -and @var{expr2}}
981 @findex -a
982 @findex -and
983 And; @var{expr2} is not evaluated if @var{expr1} is false.
985 @item @asis{@var{expr1} -o @var{expr2}}
986 @itemx @asis{@var{expr1} -or @var{expr2}}
987 @findex -o
988 @findex -or
989 Or; @var{expr2} is not evaluated if @var{expr1} is true.
991 @item @asis{@var{expr1} , @var{expr2}}
992 @findex ,
993 List; both @var{expr1} and @var{expr2} are always evaluated.  True if
994 @var{expr2} is true.  The value of @var{expr1} is discarded.  This
995 operator lets you do multiple independent operations on one traversal,
996 without depending on whether other operations succeeded.
997 @end table
999 @code{find} searches the directory tree rooted at each file name by
1000 evaluating the expression from left to right, according to the rules of
1001 precedence, until the outcome is known (the left hand side is false for
1002 @samp{-and}, true for @samp{-or}), at which point @code{find} moves on
1003 to the next file name.
1005 There are two other tests that can be useful in complex expressions:
1007 @deffn Test -true
1008 Always true.
1009 @end deffn
1011 @deffn Test -false
1012 Always false.
1013 @end deffn
1015 @node Actions, Common Tasks, Finding Files, Top
1016 @chapter Actions
1018 There are several ways you can print information about the files that
1019 match the criteria you gave in the @code{find} expression.  You can
1020 print the information either to the standard output or to a file that
1021 you name.  You can also execute commands that have the file names as
1022 arguments.  You can use those commands as further filters to select files.
1024 @menu
1025 * Print File Name::
1026 * Print File Information::
1027 * Run Commands::
1028 * Delete Files::
1029 * Adding Tests::
1030 @end menu
1032 @node Print File Name
1033 @section Print File Name
1035 @deffn Action -print
1036 True; print the full file name on the standard output, followed by a
1037 newline.
1038 @end deffn
1040 @deffn Action -fprint file
1041 True; print the full file name into file @var{file}, followed by a
1042 newline.  If @var{file} does not exist when @code{find} is run, it is
1043 created; if it does exist, it is truncated to 0 bytes.  The file names
1044 @file{/dev/stdout} and @file{/dev/stderr} are handled specially; they
1045 refer to the standard output and standard error output, respectively.
1046 @end deffn
1048 @node Print File Information
1049 @section Print File Information
1051 @deffn Action -ls
1052 True; list the current file in @samp{ls -dils} format on the standard
1053 output.  The output looks like this:
1055 @smallexample
1056 204744   17 -rw-r--r--   1 djm      staff       17337 Nov  2  1992 ./lwall-quotes
1057 @end smallexample
1059 The fields are:
1061 @enumerate
1062 @item
1063 The inode number of the file.  @xref{Hard Links}, for how to find files
1064 based on their inode number.
1066 @item
1067 the number of blocks in the file.  The block counts are of 1K blocks,
1068 unless the environment variable @code{POSIXLY_CORRECT} is set, in which
1069 case 512-byte blocks are used.  @xref{Size}, for how to find files based
1070 on their size.
1072 @item
1073 The file's type and permissions.  The type is shown as a dash for a
1074 regular file; for other file types, a letter like for @samp{-type} is
1075 used (@pxref{Type}).  The permissions are read, write, and execute for
1076 the file's owner, its group, and other users, respectively; a dash means
1077 the permission is not granted.  @xref{File Permissions}, for more details
1078 about file permissions.  @xref{Permissions}, for how to find files based
1079 on their permissions.
1081 @item
1082 The number of hard links to the file.
1084 @item
1085 The user who owns the file.
1087 @item
1088 The file's group.
1090 @item
1091 The file's size in bytes.
1093 @item
1094 The date the file was last modified.
1096 @item
1097 The file's name.  @samp{-ls} quotes non-printable characters in the file
1098 names using C-like backslash escapes.
1099 @end enumerate
1100 @end deffn
1102 @deffn Action -fls file
1103 True; like @samp{-ls} but write to @var{file} like @samp{-fprint}
1104 (@pxref{Print File Name}).
1105 @end deffn
1107 @deffn Action -printf format
1108 True; print @var{format} on the standard output, interpreting @samp{\}
1109 escapes and @samp{%} directives.  Field widths and precisions can be
1110 specified as with the @code{printf} C function.  Unlike @samp{-print},
1111 @samp{-printf} does not add a newline at the end of the string.
1112 @end deffn
1114 @deffn Action -fprintf file format
1115 True; like @samp{-printf} but write to @var{file} like @samp{-fprint}
1116 (@pxref{Print File Name}).
1117 @end deffn
1119 @menu
1120 * Escapes::
1121 * Format Directives::
1122 * Time Formats::
1123 @end menu
1125 @node Escapes
1126 @subsection Escapes
1128 The escapes that @samp{-printf} and @samp{-fprintf} recognize are:
1130 @table @code
1131 @item \a
1132 Alarm bell.
1133 @item \b
1134 Backspace.
1135 @item \c
1136 Stop printing from this format immediately and flush the output.
1137 @item \f
1138 Form feed.
1139 @item \n
1140 Newline.
1141 @item \r
1142 Carriage return.
1143 @item \t
1144 Horizontal tab.
1145 @item \v
1146 Vertical tab.
1147 @item \\
1148 A literal backslash (@samp{\}).
1149 @item \NNN
1150 The character whose ASCII code is NNN (octal).
1151 @end table
1153 A @samp{\} character followed by any other character is treated as an
1154 ordinary character, so they both are printed, and a warning message is
1155 printed to the standard error output (because it was probably a typo).
1157 @node Format Directives
1158 @subsection Format Directives
1160 @samp{-printf} and @samp{-fprintf} support the following format
1161 directives to print information about the file being processed.
1162 The C @code{printf} function, field width and precision specifiers
1163 are supported, as applied to string (%s) types. I.E. you can specify
1164 "minimum field width"."maximum field width" for each directive.
1166 @samp{%%} is a literal percent sign.  A @samp{%} character followed by
1167 an unrecognised character (i.e. not a known directive or printf field
1168 width and precision specifier), is discarded (but the unrecognised character
1169 is printed), and a warning message is printed to the standard error output
1170 (because it was probably a typo).
1172 @menu
1173 * Name Directives::
1174 * Ownership Directives::
1175 * Size Directives::
1176 * Location Directives::
1177 * Time Directives::
1178 @end menu
1180 @node Name Directives
1181 @subsubsection Name Directives
1183 @table @code
1184 @item %p
1185 File's name.
1186 @item %f
1187 File's name with any leading directories removed (only the last element).
1188 @item %h
1189 Leading directories of file's name (all but the last element and the
1190 slash before it).
1191 @item %P
1192 File's name with the name of the command line argument under which
1193 it was found removed from the beginning.
1194 @item %H
1195 Command line argument under which file was found.
1196 @end table
1198 @node Ownership Directives
1199 @subsubsection Ownership Directives
1201 @table @code
1202 @item %g
1203 File's group name, or numeric group ID if the group has no name.
1204 @item %G
1205 File's numeric group ID.
1206 @item %u
1207 File's user name, or numeric user ID if the user has no name.
1208 @item %U
1209 File's numeric user ID.
1210 @item %m
1211 File's permissions (in octal).
1212 @end table
1214 @node Size Directives
1215 @subsubsection Size Directives
1217 @table @code
1218 @item %k
1219 Amount of disk space occupied by the file, measured in 1K blocks
1220 (rounded up).  This can be less than the length of the file if 
1221 it is a sparse file (that is, it has ``holes'').
1222 @item %b
1223 File's size in 512-byte blocks (rounded up).  This also can be less 
1224 than the length of the file, if the file is sparse.
1225 @item %s
1226 File's size in bytes.
1227 @end table
1229 @node Location Directives
1230 @subsubsection Location Directives
1232 @table @code
1233 @item %d
1234 File's depth in the directory tree (depth below a file named on the
1235 command line, not depth below the root directory).  Files named on the
1236 command line have a depth of 0.  Subdirectories immediately below them
1237 have a depth of 1, and so on.
1238 @item %D
1239 The device number on which the file exists (the @code{st_dev} field of
1240 @code{struct stat}), in decimal.
1241 @item %F
1242 Type of the filesystem the file is on; this value can be used for
1243 @samp{-fstype} (@pxref{Directories}).
1244 @item %l
1245 Object of symbolic link (empty string if file is not a symbolic link).
1246 @item %i
1247 File's inode number (in decimal).
1248 @item %n
1249 Number of hard links to file.
1250 @item %y 
1251 Type of the file as used with @samp{-type}.   If the file is a symbolic
1252 link, @samp{l} will be printed.
1253 @item %Y
1254 Type of the file as used with @samp{-type}.   If the file is a symbolic
1255 link, it is dereferenced.  If the file is a broken symbolic link,
1256 @samp{N} is printed.
1258 @end table
1260 @node Time Directives
1261 @subsubsection Time Directives
1263 Some of these directives use the C @code{ctime} function.  Its output
1264 depends on the current locale, but it typically looks like
1266 @example
1267 Wed Nov  2 00:42:36 1994
1268 @end example
1270 @table @code
1271 @item %a
1272 File's last access time in the format returned by the C @code{ctime} function.
1273 @item %A@var{k}
1274 File's last access time in the format specified by @var{k}
1275 (@pxref{Time Formats}). 
1276 @item %c
1277 File's last status change time in the format returned by the C @code{ctime}
1278 function.
1279 @item %C@var{k}
1280 File's last status change time in the format specified by @var{k}
1281 (@pxref{Time Formats}).
1282 @item %t
1283 File's last modification time in the format returned by the C @code{ctime}
1284 function.
1285 @item %T@var{k}
1286 File's last modification time in the format specified by @var{k} 
1287 (@pxref{Time Formats}). 
1288 @end table
1290 @node Time Formats
1291 @subsection Time Formats
1293 Below are the formats for the directives @samp{%A}, @samp{%C}, and
1294 @samp{%T}, which print the file's timestamps.  Some of these formats
1295 might not be available on all systems, due to differences in the C
1296 @code{strftime} function between systems.
1298 @menu
1299 * Time Components::
1300 * Date Components::
1301 * Combined Time Formats::
1302 @end menu
1304 @node Time Components
1305 @subsubsection Time Components
1307 The following format directives print single components of the time.
1309 @table @code
1310 @item H
1311 hour (00..23)
1312 @item I
1313 hour (01..12)
1314 @item k
1315 hour ( 0..23)
1316 @item l
1317 hour ( 1..12)
1318 @item p
1319 locale's AM or PM
1320 @item Z
1321 time zone (e.g., EDT), or nothing if no time zone is determinable
1322 @item M
1323 minute (00..59)
1324 @item S
1325 second (00..61)
1326 @item @@
1327 seconds since Jan. 1, 1970, 00:00 GMT.
1328 @end table
1330 @node Date Components
1331 @subsubsection Date Components
1333 The following format directives print single components of the date.
1335 @table @code
1336 @item a
1337 locale's abbreviated weekday name (Sun..Sat)
1338 @item A
1339 locale's full weekday name, variable length (Sunday..Saturday)
1340 @item b
1341 @itemx h
1342 locale's abbreviated month name (Jan..Dec)
1343 @item B
1344 locale's full month name, variable length (January..December)
1345 @item m
1346 month (01..12)
1347 @item d
1348 day of month (01..31)
1349 @item w
1350 day of week (0..6)
1351 @item j
1352 day of year (001..366)
1353 @item U
1354 week number of year with Sunday as first day of week (00..53)
1355 @item W
1356 week number of year with Monday as first day of week (00..53)
1357 @item Y
1358 year (1970@dots{})
1359 @item y
1360 last two digits of year (00..99)
1361 @end table
1363 @node Combined Time Formats
1364 @subsubsection Combined Time Formats
1366 The following format directives print combinations of time and date
1367 components. 
1369 @table @code
1370 @item r
1371 time, 12-hour (hh:mm:ss [AP]M)
1372 @item T
1373 time, 24-hour (hh:mm:ss)
1374 @item X
1375 locale's time representation (H:M:S)
1376 @item c
1377 locale's date and time (Sat Nov 04 12:02:33 EST 1989)
1378 @item D
1379 date (mm/dd/yy)
1380 @item x
1381 locale's date representation (mm/dd/yy)
1382 @end table
1384 @node Run Commands
1385 @section Run Commands
1387 You can use the list of file names created by @code{find} or
1388 @code{locate} as arguments to other commands.  In this way you can
1389 perform arbitrary actions on the files.
1391 @menu
1392 * Single File::
1393 * Multiple Files::
1394 * Querying::
1395 @end menu
1397 @node Single File
1398 @subsection Single File
1400 Here is how to run a command on one file at a time.
1402 @deffn Action -exec command ;
1403 Execute @var{command}; true if 0 status is returned.  @code{find} takes
1404 all arguments after @samp{-exec} to be part of the command until an
1405 argument consisting of @samp{;} is reached.  It replaces the string
1406 @samp{@{@}} by the current file name being processed everywhere it
1407 occurs in the command.  Both of these constructions need to be escaped
1408 (with a @samp{\}) or quoted to protect them from expansion by the shell.
1409 The command is executed in the directory in which @code{find} was run.
1411 For example, to compare each C header file in the current directory with
1412 the file @file{/tmp/master}:
1414 @example
1415 find . -name '*.h' -exec diff -u '@{@}' /tmp/master ';'
1416 @end example
1417 @end deffn
1420 @deffn Action -exec command +
1421 This variant of @samp{-exec} not yet supported.   Once it has been
1422 implemented as specified in the POSIX standard, a @samp{@{@}} will expand to
1423 a list of names of matching files.  This expansion is done in such a
1424 way as to avoid exceeding the maximum command line length available on 
1425 the system.
1426 @end deffn
1429 @node Multiple Files
1430 @subsection Multiple Files
1432 Sometimes you need to process files alone.  But when you
1433 don't, it is faster to run a command on as many files as possible at a
1434 time, rather than once per file.  Doing this saves on the time it takes
1435 to start up the command each time.
1437 To run a command on more than one file at once, use the @code{xargs}
1438 command, which is invoked like this:
1440 @example
1441 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
1442 @end example
1444 @code{xargs} reads arguments from the standard input, delimited by
1445 blanks (which can be protected with double or single quotes or a
1446 backslash) or newlines.  It executes the @var{command} (default is
1447 @file{/bin/echo}) one or more times with any @var{initial-arguments}
1448 followed by arguments read from standard input.  Blank lines on the
1449 standard input are ignored.
1451 Instead of blank-delimited names, it is safer to use @samp{find -print0}
1452 or @samp{find -fprint0} and process the output by giving the @samp{-0}
1453 or @samp{--null} option to GNU @code{xargs}, GNU @code{tar}, GNU
1454 @code{cpio}, or @code{perl}.
1456 You can use shell command substitution (backquotes) to process a list of
1457 arguments, like this:
1459 @example
1460 grep -l sprintf `find $HOME -name '*.c' -print`
1461 @end example
1463 However, that method produces an error if the length of the @samp{.c}
1464 file names exceeds the operating system's command-line length limit.
1465 @code{xargs} avoids that problem by running the command as many times as
1466 necessary without exceeding the limit:
1468 @example
1469 find $HOME -name '*.c' -print | xargs grep -l sprintf
1470 @end example
1472 However, if the command needs to have its standard input be a terminal
1473 (@code{less}, for example), you have to use the shell command
1474 substitution method.
1476 The @code{xargs} command will process all its input, building command
1477 lines and executing them, unless one of the commands exits with a
1478 status of 255 (this will cause xargs to issue an error message and
1479 stop) or it reads a line contains the end of file string specified
1480 with the @samp{--eof} option. 
1482 @menu
1483 * Unsafe File Name Handling::
1484 * Safe File Name Handling::
1485 * Limiting Command Size::
1486 * Interspersing File Names::
1487 @end menu
1489 @node Unsafe File Name Handling
1490 @subsubsection Unsafe File Name Handling
1492 Because file names can contain quotes, backslashes, blank characters,
1493 and even newlines, it is not safe to process them using @code{xargs} in its
1494 default mode of operation.  But since most files' names do not contain
1495 blanks, this problem occurs only infrequently.  If you are only
1496 searching through files that you know have safe names, then you need not
1497 be concerned about it.
1499 @c This example is adapted from:
1500 @c From: pfalstad@stone.Princeton.EDU (Paul John Falstad)
1501 @c Newsgroups: comp.unix.shell
1502 @c Subject: Re: Beware xargs security holes
1503 @c Date: 16 Oct 90 19:12:06 GMT
1504 @c 
1505 In many applications, if @code{xargs} botches processing a file because
1506 its name contains special characters, some data might be lost.  The
1507 importance of this problem depends on the importance of the data and
1508 whether anyone notices the loss soon enough to correct it.  However,
1509 here is an extreme example of the problems that using blank-delimited
1510 names can cause.  If the following command is run daily from
1511 @code{cron}, then any user can remove any file on the system:
1513 @example
1514 find / -name '#*' -atime +7 -print | xargs rm
1515 @end example
1517 For example, you could do something like this:
1519 @example
1520 eg$ echo > '#
1521 vmunix'
1522 @end example
1524 @noindent
1525 and then @code{cron} would delete @file{/vmunix}, if it ran
1526 @code{xargs} with @file{/} as its current directory.
1528 To delete other files, for example @file{/u/joeuser/.plan}, you could do
1529 this:
1531 @example
1532 eg$ mkdir '#
1534 eg$ cd '#
1536 eg$ mkdir u u/joeuser u/joeuser/.plan'
1538 eg$ echo > u/joeuser/.plan'
1539 /#foo'
1540 eg$ cd ..
1541 eg$ find . -name '#*' -print | xargs echo
1542 ./# ./# /u/joeuser/.plan /#foo
1543 @end example
1545 @node Safe File Name Handling
1546 @subsubsection Safe File Name Handling
1548 Here is how to make @code{find} output file names so that they can be
1549 used by other programs without being mangled or misinterpreted.  You can
1550 process file names generated this way by giving the @samp{-0} or
1551 @samp{--null} option to GNU @code{xargs}, GNU @code{tar}, GNU
1552 @code{cpio}, or @code{perl}.
1554 @deffn Action -print0
1555 True; print the full file name on the standard output, followed by a
1556 null character.
1557 @end deffn
1559 @deffn Action -fprint0 file
1560 True; like @samp{-print0} but write to @var{file} like @samp{-fprint}
1561 (@pxref{Print File Name}).
1562 @end deffn
1564 @node Limiting Command Size
1565 @subsubsection Limiting Command Size
1567 @code{xargs} gives you control over how many arguments it passes to the
1568 command each time it executes it.  By default, it uses up to
1569 @code{ARG_MAX} - 2k, or 20k, whichever is smaller, characters per
1570 command.  It uses as many lines and arguments as fit within that limit.
1571 The following options modify those values.
1573 @table @code
1574 @item --no-run-if-empty
1575 @itemx -r
1576 If the standard input does not contain any nonblanks, do not run the
1577 command.  By default, the command is run once even if there is no input.
1579 @item --max-lines@r{[}=@var{max-lines}@r{]}
1580 @itemx -l@r{[}@var{max-lines}@r{]}
1581 Use at most @var{max-lines} nonblank input lines per command line;
1582 @var{max-lines} defaults to 1 if omitted.  Trailing blanks cause an
1583 input line to be logically continued on the next input line, for the
1584 purpose of counting the lines.  Implies @samp{-x}.
1586 @item --max-args=@var{max-args}
1587 @itemx -n @var{max-args}
1588 Use at most @var{max-args} arguments per command line.  Fewer than
1589 @var{max-args} arguments will be used if the size (see the @samp{-s}
1590 option) is exceeded, unless the @samp{-x} option is given, in which case
1591 @code{xargs} will exit.
1593 @item --max-chars=@var{max-chars}
1594 @itemx -s @var{max-chars}
1595 Use at most @var{max-chars} characters per command line, including the
1596 command and initial arguments and the terminating nulls at the ends of
1597 the argument strings.
1599 @item --max-procs=@var{max-procs}
1600 @itemx -P @var{max-procs}
1601 Run up to @var{max-procs} processes at a time; the default is 1.  If
1602 @var{max-procs} is 0, @code{xargs} will run as many processes as
1603 possible at a time.  Use the @samp{-n}, @samp{-s}, or @samp{-l} option
1604 with @samp{-P}; otherwise chances are that the command will be run only
1605 once.
1606 @end table
1608 @node Interspersing File Names
1609 @subsubsection Interspersing File Names
1611 @code{xargs} can insert the name of the file it is processing between
1612 arguments you give for the command.  Unless you also give options to
1613 limit the command size (@pxref{Limiting Command Size}), this mode of
1614 operation is equivalent to @samp{find -exec} (@pxref{Single File}).
1616 @table @code
1617 @item --replace@r{[}=@var{replace-str}@r{]}
1618 @itemx -i@r{[}@var{replace-str}@r{]}
1619 Replace occurrences of @var{replace-str} in the initial arguments with
1620 names read from standard input.  Also, unquoted blanks do not terminate
1621 arguments.  If @var{replace-str} is omitted, it defaults to @samp{@{@}}
1622 (like for @samp{find -exec}).  Implies @samp{-x} and @samp{-l 1}.  As an
1623 example, to sort each file the @file{bills} directory, leaving the
1624 output in that file name with @file{.sorted} appended, you could do:
1626 @example
1627 find bills -type f | xargs -iXX sort -o XX.sorted XX
1628 @end example
1630 @noindent
1631 The equivalent command using @samp{find -exec} is:
1633 @example
1634 find bills -type f -exec sort -o '@{@}.sorted' '@{@}' ';'
1635 @end example
1636 @end table
1638 @node Querying
1639 @subsection Querying
1641 To ask the user whether to execute a command on a single file, you can
1642 use the @code{find} primary @samp{-ok} instead of @samp{-exec}:
1644 @deffn Action -ok command ;
1645 Like @samp{-exec} (@pxref{Single File}), but ask the user first (on
1646 the standard input); if the response does not start with @samp{y} or
1647 @samp{Y}, do not run the command, and return false.
1648 @end deffn
1650 When processing multiple files with a single command, to query the user
1651 you give @code{xargs} the following option.  When using this option, you
1652 might find it useful to control the number of files processed per
1653 invocation of the command (@pxref{Limiting Command Size}).
1655 @table @code
1656 @item --interactive
1657 @itemx -p
1658 Prompt the user about whether to run each command line and read a line
1659 from the terminal.  Only run the command line if the response starts
1660 with @samp{y} or @samp{Y}.  Implies @samp{-t}.
1661 @end table
1663 @node Delete Files
1664 @section Delete Files
1666 @deffn Action -delete
1667 Delete files or directories; true if removal succeeded.  If the
1668 removal failed, an error message is issued.
1670 The use of the @samp{-delete} action on the command line automatically
1671 turns on the @samp{-depth} option (@pxref{find Expressions}).
1672 @end deffn
1674 @node Adding Tests
1675 @section Adding Tests
1677 You can test for file attributes that none of the @code{find} builtin
1678 tests check.  To do this, use @code{xargs} to run a program that filters
1679 a list of files printed by @code{find}.  If possible, use @code{find}
1680 builtin tests to pare down the list, so the program run by @code{xargs}
1681 has less work to do.  The tests builtin to @code{find} will likely run
1682 faster than tests that other programs perform.
1684 For example, here is a way to print the names of all of the unstripped
1685 binaries in the @file{/usr/local} directory tree.  Builtin tests avoid
1686 running @code{file} on files that are not regular files or are not
1687 executable.
1689 @example
1690 find /usr/local -type f -perm +a=x | xargs file | 
1691   grep 'not stripped' | cut -d: -f1
1692 @end example
1694 @noindent
1695 The @code{cut} program removes everything after the file name from the
1696 output of @code{file}.
1698 @c Idea from Martin Weitzel.
1699 If you want to place a special test somewhere in the middle of a
1700 @code{find} expression, you can use @samp{-exec} to run a program that
1701 performs the test.  Because @samp{-exec} evaluates to the exit status of
1702 the executed program, you can write a program (which can be a shell
1703 script) that tests for a special attribute and make it exit with a true
1704 (zero) or false (non-zero) status.  It is a good idea to place such a
1705 special test @emph{after} the builtin tests, because it starts a new
1706 process which could be avoided if a builtin test evaluates to false.
1707 Use this method only when @code{xargs} is not flexible enough, because
1708 starting one or more new processes to test each file is slower than
1709 using @code{xargs} to start one process that tests many files.
1711 Here is a shell script called @code{unstripped} that checks whether its
1712 argument is an unstripped binary file:
1714 @example
1715 #! /bin/sh
1716 file $1 | grep 'not stripped' > /dev/null
1717 @end example
1719 This script relies on the fact that the shell exits with the status of
1720 the last program it executed, in this case @code{grep}.  @code{grep}
1721 exits with a true status if it found any matches, false if not.  Here is
1722 an example of using the script (assuming it is in your search path).  It
1723 lists the stripped executables in the file @file{sbins} and the
1724 unstripped ones in @file{ubins}.
1726 @example
1727 find /usr/local -type f -perm +a=x \
1728   \( -exec unstripped '@{@}' \; -fprint ubins -o -fprint sbins \)
1729 @end example
1731 @node Common Tasks, Databases, Actions, Top
1732 @chapter Common Tasks
1734 The sections that follow contain some extended examples that both give a
1735 good idea of the power of these programs, and show you how to solve
1736 common real-world problems.
1738 @menu
1739 * Viewing And Editing::
1740 * Archiving::
1741 * Cleaning Up::
1742 * Strange File Names::
1743 * Fixing Permissions::
1744 * Classifying Files::
1745 @end menu
1747 @node Viewing And Editing
1748 @section Viewing And Editing
1750 To view a list of files that meet certain criteria, simply run your file
1751 viewing program with the file names as arguments.  Shells substitute a
1752 command enclosed in backquotes with its output, so the whole command
1753 looks like this:
1755 @example
1756 less `find /usr/include -name '*.h' | xargs grep -l mode_t`
1757 @end example
1759 @noindent
1760 You can edit those files by giving an editor name instead of a file
1761 viewing program.
1763 @node Archiving
1764 @section Archiving
1766 You can pass a list of files produced by @code{find} to a file archiving
1767 program.  GNU @code{tar} and @code{cpio} can both read lists of file
1768 names from the standard input---either delimited by nulls (the safe way)
1769 or by blanks (the lazy, risky default way).  To use null-delimited
1770 names, give them the @samp{--null} option.  You can store a file archive
1771 in a file, write it on a tape, or send it over a network to extract on
1772 another machine.
1774 One common use of @code{find} to archive files is to send a list of the
1775 files in a directory tree to @code{cpio}.  Use @samp{-depth} so if a
1776 directory does not have write permission for its owner, its contents can
1777 still be restored from the archive since the directory's permissions are
1778 restored after its contents.  Here is an example of doing this using
1779 @code{cpio}; you could use a more complex @code{find} expression to
1780 archive only certain files.
1782 @example
1783 find . -depth -print0 |
1784   cpio --create --null --format=crc --file=/dev/nrst0
1785 @end example
1787 You could restore that archive using this command:
1789 @example
1790 cpio --extract --null --make-dir --unconditional \
1791   --preserve --file=/dev/nrst0
1792 @end example
1794 Here are the commands to do the same things using @code{tar}:
1796 @example
1797 find . -depth -print0 |
1798   tar --create --null --files-from=- --file=/dev/nrst0
1800 tar --extract --null --preserve-perm --same-owner \
1801   --file=/dev/nrst0
1802 @end example
1804 @c Idea from Rick Sladkey.
1805 Here is an example of copying a directory from one machine to another:
1807 @example
1808 find . -depth -print0 | cpio -0o -Hnewc |
1809   rsh @var{other-machine} "cd `pwd` && cpio -i0dum"
1810 @end example
1812 @node Cleaning Up
1813 @section Cleaning Up
1815 @c Idea from Jim Meyering.
1816 This section gives examples of removing unwanted files in various situations.
1817 Here is a command to remove the CVS backup files created when an update
1818 requires a merge:
1820 @example
1821 find . -name '.#*' -print0 | xargs -0r rm -f
1822 @end example
1824 The command above works, but the following is safer:
1826 @example
1827 find . -name '.#*' -depth -delete
1828 @end example
1830 @c Idea from Franc,ois Pinard.
1831 You can run this command to clean out your clutter in @file{/tmp}.  You
1832 might place it in the file your shell runs when you log out
1833 (@file{.bash_logout}, @file{.logout}, or @file{.zlogout}, depending on
1834 which shell you use).
1836 @example
1837 find /tmp -depth -user $LOGNAME -type f -delete 
1838 @end example
1840 If your @code{find} command removes directories, you may find that 
1841 you get a spurious error message when @code{find} tries to recurse
1842 into a directory that has now been removed.  Using the @samp{-depth}
1843 option will normally resolve this problem.
1845 @c Idea from Noah Friedman.
1846 To remove old Emacs backup and auto-save files, you can use a command
1847 like the following.  It is especially important in this case to use
1848 null-terminated file names because Emacs packages like the VM mailer
1849 often create temporary file names with spaces in them, like @file{#reply
1850 to David J. MacKenzie<1>#}.
1852 @example
1853 find ~ \( -name '*~' -o -name '#*#' \) -print0 |
1854   xargs --no-run-if-empty --null rm -vf
1855 @end example
1857 Removing old files from @file{/tmp} is commonly done from @code{cron}:
1859 @c Idea from Kaveh Ghazi.
1860 @example
1861 find /tmp /var/tmp -not -type d -mtime +3 -delete
1862 find /tmp /var/tmp -depth -mindepth 1 -type d -empty -delete
1863 @end example
1865 The second @code{find} command above uses @samp{-depth} so it cleans out
1866 empty directories depth-first, hoping that the parents become empty and
1867 can be removed too.  It uses @samp{-mindepth} to avoid removing
1868 @file{/tmp} itself if it becomes totally empty.
1870 @node Strange File Names
1871 @section Strange File Names
1873 @c Idea from:
1874 @c From: tmatimar@isgtec.com (Ted Timar)
1875 @c Newsgroups: comp.unix.questions,comp.unix.shell,comp.answers,news.answers
1876 @c Subject: Unix - Frequently Asked Questions (2/7) [Frequent posting]
1877 @c Subject: How do I remove a file with funny characters in the filename ?
1878 @c Date: Thu Mar 18 17:16:55 EST 1993
1879 @code{find} can help you remove or rename a file with strange characters
1880 in its name.  People are sometimes stymied by files whose names contain
1881 characters such as spaces, tabs, control characters, or characters with
1882 the high bit set.  The simplest way to remove such files is:
1884 @example
1885 rm -i @var{some*pattern*that*matches*the*problem*file}
1886 @end example
1888 @code{rm} asks you whether to remove each file matching the given
1889 pattern.  If you are using an old shell, this approach might not work if
1890 the file name contains a character with the high bit set; the shell may
1891 strip it off.  A more reliable way is:
1893 @example
1894 find . -maxdepth 1 @var{tests} -ok rm '@{@}' \;
1895 @end example
1897 @noindent
1898 where @var{tests} uniquely identify the file.  The @samp{-maxdepth 1}
1899 option prevents @code{find} from wasting time searching for the file in
1900 any subdirectories; if there are no subdirectories, you may omit it.  A
1901 good way to uniquely identify the problem file is to figure out its
1902 inode number; use
1904 @example
1905 ls -i
1906 @end example
1908 Suppose you have a file whose name contains control characters, and you
1909 have found that its inode number is 12345.  This command prompts you for
1910 whether to remove it:
1912 @example
1913 find . -maxdepth 1 -inum 12345 -ok rm -f '@{@}' \;
1914 @end example
1916 If you don't want to be asked, perhaps because the file name may contain
1917 a strange character sequence that will mess up your screen when printed,
1918 then use @samp{-exec} instead of @samp{-ok}.
1920 If you want to rename the file instead, you can use @code{mv} instead of
1921 @code{rm}:
1923 @example
1924 find . -maxdepth 1 -inum 12345 -ok mv '@{@}' @var{new-file-name} \;
1925 @end example
1927 @node Fixing Permissions
1928 @section Fixing Permissions
1930 Suppose you want to make sure that everyone can write to the directories in a
1931 certain directory tree.  Here is a way to find directories lacking either
1932 user or group write permission (or both), and fix their permissions:
1934 @example
1935 find . -type d -not -perm -ug=w | xargs chmod ug+w
1936 @end example
1938 @noindent
1939 You could also reverse the operations, if you want to make sure that
1940 directories do @emph{not} have world write permission.
1942 @node Classifying Files
1943 @section Classifying Files
1945 @c Idea from:
1946 @c From: martin@mwtech.UUCP (Martin Weitzel)
1947 @c Newsgroups: comp.unix.wizards,comp.unix.questions
1948 @c Subject: Advanced usage of 'find' (Re: Unix security automating script)
1949 @c Date: 22 Mar 90 15:05:19 GMT
1950 If you want to classify a set of files into several groups based on
1951 different criteria, you can use the comma operator to perform multiple
1952 independent tests on the files.  Here is an example:
1954 @example
1955 find / -type d \( -perm -o=w -fprint allwrite , \
1956   -perm -o=x -fprint allexec \)
1958 echo "Directories that can be written to by everyone:"
1959 cat allwrite
1960 echo ""
1961 echo "Directories with search permissions for everyone:"
1962 cat allexec
1963 @end example
1965 @code{find} has only to make one scan through the directory tree (which
1966 is one of the most time consuming parts of its work).
1968 @node Databases, File Permissions, Common Tasks, Top
1969 @chapter File Name Databases
1971 The file name databases used by @code{locate} contain lists of files
1972 that were in particular directory trees when the databases were last
1973 updated.  The file name of the default database is determined when
1974 @code{locate} and @code{updatedb} are configured and installed.  The
1975 frequency with which the databases are updated and the directories for
1976 which they contain entries depend on how often @code{updatedb} is run,
1977 and with which arguments.
1979 @menu
1980 * Database Locations::
1981 * Database Formats::
1982 @end menu
1984 @node Database Locations
1985 @section Database Locations
1987 There can be multiple file name databases.  Users can select which
1988 databases @code{locate} searches using an environment variable or a
1989 command line option.  The system administrator can choose the file name
1990 of the default database, the frequency with which the databases are
1991 updated, and the directories for which they contain entries.  File name
1992 databases are updated by running the @code{updatedb} program, typically
1993 nightly.
1995 In networked environments, it often makes sense to build a database at
1996 the root of each filesystem, containing the entries for that filesystem.
1997 @code{updatedb} is then run for each filesystem on the fileserver where
1998 that filesystem is on a local disk, to prevent thrashing the network.
1999 Here are the options to @code{updatedb} to select which directories each
2000 database contains entries for:
2002 @table @code
2003 @item --localpaths='@var{path}@dots{}'
2004 Non-network directories to put in the database.
2005 Default is @file{/}.
2007 @item --netpaths='@var{path}@dots{}'
2008 Network (NFS, AFS, RFS, etc.) directories to put in the database.
2009 The environment variable @code{NETPATHS} also sets this value.
2010 Default is none.
2013 @item --prunepaths='@var{path}@dots{}'
2014 Directories to not put in the database, which would otherwise be.
2015 The environment variable @code{PRUNEPATHS} also sets this value.
2016 Default is @file{/tmp /usr/tmp /var/tmp /afs}.
2018 @item --prunefs='@var{path}@dots{}'
2019 File systems to not put in the database, which would otherwise be.
2020 Note that files are pruned when a file system is reached;
2021 Any file system mounted under an undesired file system will be
2022 ignored.
2023 The environment variable @code{PRUNEFS} also sets this value.
2024 Default is @file{nfs NFS proc}.
2026 @item --output=@var{dbfile}
2027 The database file to build.
2028 Default is system-dependent, but typically @file{/usr/local/var/locatedb}.
2030 @item --localuser=@var{user}
2031 The user to search the non-network directories as, using @code{su}.
2032 Default is to search the non-network directories as the current user.
2033 You can also use the environment variable @code{LOCALUSER} to set this user.
2035 @item --netuser=@var{user}
2036 The user to search network directories as, using @code{su}.
2037 Default is @code{daemon}.
2038 You can also use the environment variable @code{NETUSER} to set this user.
2039 @end table
2041 @node Database Formats
2042 @section Database Formats
2044 The file name databases contain lists of files that were in particular
2045 directory trees when the databases were last updated.  The file name
2046 database format changed starting with GNU @code{locate} version 4.0 to
2047 allow machines with different byte orderings to share the databases.  The
2048 new GNU @code{locate} can read both the old and new database formats.
2049 However, old versions of @code{locate} and @code{find} produce incorrect
2050 results if given a new-format database.
2052 @menu
2053 * New Database Format::
2054 * Sample Database::
2055 * Old Database Format::
2056 @end menu
2058 @node New Database Format
2059 @subsection New Database Format
2061 @code{updatedb} runs a program called @code{frcode} to
2062 @dfn{front-compress} the list of file names, which reduces the database
2063 size by a factor of 4 to 5.  Front-compression (also known as
2064 incremental encoding) works as follows.
2066 The database entries are a sorted list (case-insensitively, for users'
2067 convenience).  Since the list is sorted, each entry is likely to share a
2068 prefix (initial string) with the previous entry.  Each database entry
2069 begins with an offset-differential count byte, which is the additional
2070 number of characters of prefix of the preceding entry to use beyond the
2071 number that the preceding entry is using of its predecessor.  (The
2072 counts can be negative.)  Following the count is a null-terminated ASCII
2073 remainder---the part of the name that follows the shared prefix.
2075 If the offset-differential count is larger than can be stored in a byte
2076 (+/-127), the byte has the value 0x80 and the count follows in a 2-byte
2077 word, with the high byte first (network byte order).
2079 Every database begins with a dummy entry for a file called
2080 @file{LOCATE02}, which @code{locate} checks for to ensure that the
2081 database file has the correct format; it ignores the entry in doing the
2082 search.
2084 Databases can not be concatenated together, even if the first (dummy)
2085 entry is trimmed from all but the first database.  This is because the
2086 offset-differential count in the first entry of the second and following
2087 databases will be wrong.
2089 @node Sample Database
2090 @subsection Sample Database
2092 Sample input to @code{frcode}:
2093 @c with nulls changed to newlines:
2095 @example
2096 /usr/src
2097 /usr/src/cmd/aardvark.c
2098 /usr/src/cmd/armadillo.c
2099 /usr/tmp/zoo
2100 @end example
2102 Length of the longest prefix of the preceding entry to share:
2104 @example
2105 0 /usr/src
2106 8 /cmd/aardvark.c
2107 14 rmadillo.c
2108 5 tmp/zoo
2109 @end example
2111 Output from @code{frcode}, with trailing nulls changed to newlines
2112 and count bytes made printable:
2114 @example
2115 0 LOCATE02
2116 0 /usr/src
2117 8 /cmd/aardvark.c
2118 6 rmadillo.c
2119 -9 tmp/zoo
2120 @end example
2122 (6 = 14 - 8, and -9 = 5 - 14)
2124 @node Old Database Format
2125 @subsection Old Database Format
2127 The old database format is used by Unix @code{locate} and @code{find}
2128 programs and earlier releases of the GNU ones.  @code{updatedb} produces
2129 this format if given the @samp{--old-format} option.
2131 @code{updatedb} runs programs called @code{bigram} and @code{code} to
2132 produce old-format databases.  The old format differs from the new one
2133 in the following ways.  Instead of each entry starting with an
2134 offset-differential count byte and ending with a null, byte values from
2135 0 through 28 indicate offset-differential counts from -14 through 14.
2136 The byte value indicating that a long offset-differential count follows
2137 is 0x1e (30), not 0x80.  The long counts are stored in host byte order,
2138 which is not necessarily network byte order, and host integer word size,
2139 which is usually 4 bytes.  They also represent a count 14 less than
2140 their value.  The database lines have no termination byte; the start of
2141 the next line is indicated by its first byte having a value <= 30.
2143 In addition, instead of starting with a dummy entry, the old database
2144 format starts with a 256 byte table containing the 128 most common
2145 bigrams in the file list.  A bigram is a pair of adjacent bytes.  Bytes
2146 in the database that have the high bit set are indexes (with the high
2147 bit cleared) into the bigram table.  The bigram and offset-differential
2148 count coding makes these databases 20-25% smaller than the new format,
2149 but makes them not 8-bit clean.  Any byte in a file name that is in the
2150 ranges used for the special codes is replaced in the database by a
2151 question mark, which not coincidentally is the shell wildcard to match a
2152 single character.
2154 The old format therefore can not faithfully store entries with non-ASCII
2155 characters. It therefore should not be used in internationalized
2156 environments.
2158 @node File Permissions, Reference, Databases, Top
2159 @chapter File Permissions
2161 @include perm.texi
2163 @node Reference, Primary Index, File Permissions, Top
2164 @chapter Reference
2166 Below are summaries of the command line syntax for the programs
2167 discussed in this manual.
2169 @menu
2170 * Invoking find::
2171 * Invoking locate::
2172 * Invoking updatedb::
2173 * Invoking xargs::
2174 @end menu
2176 @node Invoking find, Invoking locate, , Reference
2177 @section Invoking @code{find}
2179 @example
2180 find @r{[}@var{file}@dots{}@r{]} @r{[}@var{expression}@r{]}
2181 @end example
2183 @code{find} searches the directory tree rooted at each file name
2184 @var{file} by evaluating the @var{expression} on each file it finds in
2185 the tree.
2187 @code{find} considers the first argument that begins with @samp{-},
2188 @samp{(}, @samp{)}, @samp{,}, or @samp{!} to be the beginning of the
2189 expression; any arguments before it are paths to search, and any
2190 arguments after it are the rest of the expression.  If no paths are
2191 given, the current directory is used.  If no expression is given, the
2192 expression @samp{-print} is used.
2194 @code{find} exits with status 0 if all files are processed successfully,
2195 greater than 0 if errors occur.
2197 @xref{Primary Index}, for a summary of all of the tests, actions, and
2198 options that the expression can contain.
2200 @code{find} also recognizes two options for administrative use:
2202 @table @code
2203 @item --help
2204 Print a summary of the command-line argument format and exit.
2205 @item --version
2206 Print the version number of @code{find} and exit.
2207 @end table
2209 @node Invoking locate, Invoking updatedb, Invoking find, Reference
2210 @section Invoking @code{locate}
2212 @example
2213 locate @r{[}@var{option}@dots{}@r{]} @var{pattern}@dots{}
2214 @end example
2216 @table @code
2217 @item --database=@var{path}
2218 @itemx -d @var{path}
2219 Instead of searching the default file name database, search the file
2220 name databases in @var{path}, which is a colon-separated list of
2221 database file names.  You can also use the environment variable
2222 @code{LOCATE_PATH} to set the list of database files to search.  The
2223 option overrides the environment variable if both are used.
2225 @item --existing
2226 @itemx -e
2227 Only print out such names which currently exist (instead of such names
2228 which existed when the database was created).
2229 Note that this may slow down the program a lot, if there are many matches
2230 in the database.
2232 @item --ignore-case
2233 @itemx -i
2234 Ignore case distinctions in both the pattern and the file names.
2236 @item --help
2237 Print a summary of the options to @code{locate} and exit.
2239 @item --version
2240 Print the version number of @code{locate} and exit.
2241 @end table
2243 @node Invoking updatedb, Invoking xargs, Invoking locate, Reference
2244 @section Invoking @code{updatedb}
2246 @example
2247 updatedb @r{[}@var{option}@dots{}@r{]}
2248 @end example
2250 @table @code
2251 @item --localpaths='@var{path}@dots{}'
2252 Non-network directories to put in the database.
2253 Default is @file{/}.
2255 @item --netpaths='@var{path}@dots{}'
2256 Network (NFS, AFS, RFS, etc.) directories to put in the database.
2257 The environment variable @code{NETPATHS} also sets this value.
2258 Default is none.
2260 @item --prunepaths='@var{path}@dots{}'
2261 Directories to not put in the database, which would otherwise be.
2262 The environment variable @code{PRUNEPATHS} also sets this value.
2263 Default is @file{/tmp /usr/tmp /var/tmp /afs}.
2265 @item --prunefs='@var{path}@dots{}'
2266 File systems to not put in the database, which would otherwise be.
2267 Note that files are pruned when a file system is reached;
2268 Any file system mounted under an undesired file system will be
2269 ignored.
2270 The environment variable @code{PRUNEFS} also sets this value.
2271 Default is @file{nfs NFS proc}.
2273 @item --output=@var{dbfile}
2274 The database file to build.
2275 Default is system-dependent, but typically @file{/usr/local/var/locatedb}.
2277 @item --localuser=@var{user}
2278 The user to search the non-network directories as, using @code{su}.
2279 Default is to search the non-network directories as the current user.
2280 You can also use the environment variable @code{LOCALUSER} to set this user.
2282 @item --netuser=@var{user}
2283 The user to search network directories as, using @code{su}(1).
2284 Default is @code{daemon}.
2285 You can also use the environment variable @code{NETUSER} to set this user.
2286 @end table
2288 @node Invoking xargs, ,  Invoking updatedb, Reference
2289 @section Invoking @code{xargs}
2291 @example
2292 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
2293 @end example
2295 @code{xargs} exits with the following status:
2297 @table @asis
2298 @item 0
2299 if it succeeds
2300 @item 123
2301 if any invocation of the command exited with status 1-125
2302 @item 124
2303 if the command exited with status 255
2304 @item 125
2305 if the command is killed by a signal
2306 @item 126
2307 if the command cannot be run
2308 @item 127
2309 if the command is not found
2310 @item 1
2311 if some other error occurred.
2312 @end table
2314 @table @code
2315 @item --null
2316 @itemx -0
2317 Input filenames are terminated by a null character instead of by
2318 whitespace, and the quotes and backslash are not special (every
2319 character is taken literally).  Disables the end of file string, which
2320 is treated like any other argument.
2322 @item --eof@r{[}=@var{eof-str}@r{]}
2323 @itemx -e@r{[}@var{eof-str}@r{]}
2324 Set the end of file string to @var{eof-str}.  If the end of file string
2325 occurs as a line of input, the rest of the input is ignored.  If
2326 @var{eof-str} is omitted, there is no end of file string.  If this
2327 option is not given, the end of file string defaults to @samp{_}.
2329 @item --help
2330 Print a summary of the options to @code{xargs} and exit.
2332 @item --replace@r{[}=@var{replace-str}@r{]}
2333 @itemx -i@r{[}@var{replace-str}@r{]}
2334 Replace occurrences of @var{replace-str} in the initial arguments with
2335 names read from standard input.  Also, unquoted blanks do not terminate
2336 arguments.  If @var{replace-str} is omitted, it defaults to @samp{@{@}}
2337 (like for @samp{find -exec}).  Implies @samp{-x} and @samp{-l 1}.
2339 @item --max-lines@r{[}=@var{max-lines}@r{]}
2340 @itemx -l@r{[}@var{max-lines}@r{]}
2341 Use at most @var{max-lines} nonblank input lines per command line;
2342 @var{max-lines} defaults to 1 if omitted.  Trailing blanks cause an
2343 input line to be logically continued on the next input line, for the
2344 purpose of counting the lines.  Implies @samp{-x}.
2346 @item --max-args=@var{max-args}
2347 @itemx -n @var{max-args}
2348 Use at most @var{max-args} arguments per command line.  Fewer than
2349 @var{max-args} arguments will be used if the size (see the @samp{-s}
2350 option) is exceeded, unless the @samp{-x} option is given, in which case
2351 @code{xargs} will exit.
2353 @item --interactive
2354 @itemx -p
2355 Prompt the user about whether to run each command line and read a line
2356 from the terminal.  Only run the command line if the response starts
2357 with @samp{y} or @samp{Y}.  Implies @samp{-t}.
2359 @item --no-run-if-empty
2360 @itemx -r
2361 If the standard input does not contain any nonblanks, do not run the
2362 command.  By default, the command is run once even if there is no input.
2364 @item --max-chars=@var{max-chars}
2365 @itemx -s @var{max-chars}
2366 Use at most @var{max-chars} characters per command line, including the
2367 command and initial arguments and the terminating nulls at the ends of
2368 the argument strings.
2370 @item --verbose
2371 @itemx -t
2372 Print the command line on the standard error output before executing
2375 @item --version
2376 Print the version number of @code{xargs} and exit.
2378 @item --exit
2379 @itemx -x
2380 Exit if the size (see the @samp{-s} option) is exceeded.
2383 @item --max-procs=@var{max-procs}
2384 @itemx -P @var{max-procs}
2385 Run up to @var{max-procs} processes at a time; the default is 1.  If
2386 @var{max-procs} is 0, @code{xargs} will run as many processes as
2387 possible at a time.
2388 @end table
2390 @node Primary Index, , Reference, Top
2391 @unnumbered @code{find} Primary Index
2393 This is a list of all of the primaries (tests, actions, and options)
2394 that make up @code{find} expressions for selecting files.  @xref{find
2395 Expressions}, for more information on expressions.
2397 @printindex fn
2399 @contents
2400 @bye
2402 @comment texi related words used by Emacs' spell checker ispell.el
2404 @comment LocalWords: texinfo setfilename settitle setchapternewpage
2405 @comment LocalWords: iftex finalout ifinfo DIR titlepage vskip pt
2406 @comment LocalWords: filll dir samp dfn noindent xref pxref
2407 @comment LocalWords: var deffn texi deffnx itemx emph asis
2408 @comment LocalWords: findex smallexample subsubsection cindex
2410 @comment other words used by Emacs' spell checker ispell.el
2411 @comment LocalWords: README fred updatedb xargs Plett Rendell akefile
2412 @comment LocalWords: args grep Filesystems fo foo fOo wildcards iname
2413 @comment LocalWords: ipath regex iregex expr fubar regexps
2414 @comment LocalWords: metacharacters macs sr sc inode lname ilname
2415 @comment LocalWords: sysdep noleaf ls inum xdev filesystems usr atime
2416 @comment LocalWords: ctime mtime amin cmin mmin al daystart Sladkey rm
2417 @comment LocalWords: anewer cnewer bckw rf xtype uname gname uid gid
2418 @comment LocalWords: nouser nogroup chown chgrp perm ch maxdepth
2419 @comment LocalWords: mindepth cpio src CD AFS statted stat fstype ufs
2420 @comment LocalWords: nfs tmp mfs printf fprint dils rw djm Nov lwall
2421 @comment LocalWords: POSIXLY fls fprintf strftime locale's EDT GMT AP
2422 @comment LocalWords: EST diff perl backquotes sprintf Falstad Oct cron
2423 @comment LocalWords: eg vmunix mkdir afs allexec allwrite ARG bigram
2424 @comment LocalWords: bigrams cd chmod comp crc CVS dbfile dum eof
2425 @comment LocalWords: fileserver filesystem fn frcode Ghazi Hnewc iXX
2426 @comment LocalWords: joeuser Kaveh localpaths localuser LOGNAME
2427 @comment LocalWords: Meyering mv netpaths netuser nonblank nonblanks
2428 @comment LocalWords: ois ok Pinard printindex proc procs prunefs
2429 @comment LocalWords: prunepaths pwd RFS rmadillo rmdir rsh sbins str
2430 @comment LocalWords: su Timar ubins ug unstripped vf VM Weitzel
2431 @comment LocalWords: wildcard zlogout