source: trunk/essentials/sys-apps/findutils/doc/find.texi

Last change on this file was 3170, checked in by bird, 18 years ago

findutils 4.3.2

File size: 177.4 KB
Line 
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
8
9@include version.texi
10
11@iftex
12@finalout
13@end iftex
14
15@dircategory Basics
16@direntry
17* Finding files: (find). Operating on files matching certain criteria.
18@end direntry
19
20@dircategory Individual utilities
21@direntry
22* find: (find)Invoking find. Finding and acting on files.
23* locate: (find)Invoking locate. Finding files in a database.
24* updatedb: (find)Invoking updatedb. Building the locate database.
25* xargs: (find)Invoking xargs. Operating on many files.
26@end direntry
27
28@copying
29
30This file documents the GNU utilities for finding files that match
31certain criteria and performing various operations on them.
32
33Copyright (C) 1994, 1996, 1998, 2000, 2001, 2003, 2004, 2005 Free
34Software Foundation, Inc.
35
36Permission is granted to make and distribute verbatim copies of
37this manual provided the copyright notice and this permission notice
38are preserved on all copies.
39
40@ignore
41Permission is granted to process this file through TeX and print the
42results, provided the printed document carries copying permission
43notice identical to this one except for the removal of this paragraph
44(this paragraph not being relevant to the printed manual).
45
46@end ignore
47Permission is granted to copy and distribute modified versions of this
48manual under the conditions for verbatim copying, provided that the
49entire resulting derived work is distributed under the terms of a
50permission notice identical to this one.
51
52Permission is granted to copy and distribute translations of this
53manual into another language, under the above conditions for modified
54versions, except that this permission notice may be stated in a
55translation approved by the Foundation.
56@end copying
57
58@titlepage
59@title Finding Files
60@subtitle Edition @value{EDITION}, for GNU @code{find} version @value{VERSION}
61@subtitle @value{UPDATED}
62@author by David MacKenzie and James Youngman
63
64@page
65@vskip 0pt plus 1filll
66@insertcopying{}
67@end titlepage
68
69@contents
70
71@ifnottex
72@node Top, Introduction, , (dir)
73@comment node-name, next, previous, up
74
75This file documents the GNU utilities for finding files that match
76certain criteria and performing various actions on them.
77
78This is edition @value{EDITION}, for @code{find} version @value{VERSION}.
79@end ifnottex
80
81@c The master menu, created with texinfo-master-menu, goes here.
82
83@menu
84* Introduction:: Summary of the tasks this manual describes.
85* Finding Files:: Finding files that match certain criteria.
86* Actions:: Doing things to files you have found.
87* Databases:: Maintaining file name databases.
88* File Permissions:: How to control access to files.
89* Reference:: Summary of how to invoke the programs.
90* Common Tasks:: Solutions to common real-world problems.
91* Worked Examples:: Examples demonstrating more complex points.
92* Security Considerations:: Security issues relating to findutils.
93* Error Messages:: Explanations of some messages you might see.
94* Primary Index:: The components of @code{find} expressions.
95@end menu
96
97@node Introduction, Finding Files, Top, Top
98@chapter Introduction
99
100This manual shows how to find files that meet criteria you specify,
101and how to perform various actions on the files that you find. The
102principal programs that you use to perform these tasks are
103@code{find}, @code{locate}, and @code{xargs}. Some of the examples in
104this manual use capabilities specific to the GNU versions of those
105programs.
106
107GNU @code{find} was originally written by Eric Decker, with
108enhancements by David MacKenzie, Jay Plett, and Tim Wood. GNU
109@code{xargs} was originally written by Mike Rendell, with enhancements
110by David MacKenzie. GNU @code{locate} and its associated utilities
111were originally written by James Woods, with enhancements by David
112MacKenzie. The idea for @samp{find -print0} and @samp{xargs -0} came
113from Dan Bernstein. The current maintainer of GNU findutils (and this
114manual) is James Youngman. Many other people have contributed bug
115fixes, small improvements, and helpful suggestions. Thanks!
116
117To report a bug in GNU findutils, please use the form on the Savannah
118web site at
119@code{http://savannah.gnu.org/bugs/?group=findutils}. Reporting bugs
120this way means that you will then be able to track progress in fixing
121the problem.
122
123If you don't have web access, you can also just send mail to the
124mailing list. The mailing list @email{bug-findutils@@gnu.org} carries
125discussion of bugs in findutils, questions and answers about the
126software and discussion of the development of the programs. To join
127the list, send email to @email{bug-findutils-request@@gnu.org}.
128
129Please read any relevant sections of this manual before asking for
130help on the mailing list. You may also find it helpful to read the
131NON-BUGS section of the @code{find} manual page.
132
133If you ask for help on the mailing list, people will be able to help
134you much more effectively if you include the following things:
135
136@itemize @bullet
137@item The version of the software you are running. You can find this
138out by running @samp{locate --version}.
139@item What you were trying to do
140@item The @emph{exact} command line you used
141@item The @emph{exact} output you got (if this is very long, try to
142find a smaller example which exhibits the same problem)
143@item The output you expected to get
144@end itemize
145
146@menu
147* Scope::
148* Overview::
149* find Expressions::
150@end menu
151
152@node Scope
153@section Scope
154
155For brevity, the word @dfn{file} in this manual means a regular file,
156a directory, a symbolic link, or any other kind of node that has a
157directory entry. A directory entry is also called a @dfn{file name}.
158A file name may contain some, all, or none of the directories in a
159path that leads to the file. These are all examples of what this
160manual calls ``file names'':
161
162@example
163parser.c
164README
165./budget/may-94.sc
166fred/.cshrc
167/usr/local/include/termcap.h
168@end example
169
170A @dfn{directory tree} is a directory and the files it contains, all
171of its subdirectories and the files they contain, etc. It can also be
172a single non-directory file.
173
174These programs enable you to find the files in one or more directory
175trees that:
176
177@itemize @bullet
178@item
179have names that contain certain text or match a certain pattern;
180@item
181are links to certain files;
182@item
183were last used during a certain period of time;
184@item
185are within a certain size range;
186@item
187are of a certain type (regular file, directory, symbolic link, etc.);
188@item
189are owned by a certain user or group;
190@item
191have certain access permissions or special mode bits;
192@item
193contain text that matches a certain pattern;
194@item
195are within a certain depth in the directory tree;
196@item
197or some combination of the above.
198@end itemize
199
200Once you have found the files you're looking for (or files that are
201potentially the ones you're looking for), you can do more to them than
202simply list their names. You can get any combination of the files'
203attributes, or process the files in many ways, either individually or
204in groups of various sizes. Actions that you might want to perform on
205the files you have found include, but are not limited to:
206
207@itemize @bullet
208@item
209view or edit
210@item
211store in an archive
212@item
213remove or rename
214@item
215change access permissions
216@item
217classify into groups
218@end itemize
219
220This manual describes how to perform each of those tasks, and more.
221
222@node Overview
223@section Overview
224
225The principal programs used for making lists of files that match given
226criteria and running commands on them are @code{find}, @code{locate},
227and @code{xargs}. An additional command, @code{updatedb}, is used by
228system administrators to create databases for @code{locate} to use.
229
230@code{find} searches for files in a directory hierarchy and prints
231information about the files it found. It is run like this:
232
233@example
234find @r{[}@var{file}@dots{}@r{]} @r{[}@var{expression}@r{]}
235@end example
236
237@noindent
238Here is a typical use of @code{find}. This example prints the names
239of all files in the directory tree rooted in @file{/usr/src} whose
240name ends with @samp{.c} and that are larger than 100 Kilobytes.
241@example
242find /usr/src -name '*.c' -size +100k -print
243@end example
244
245Notice that the wildcard must be enclosed in quotes in order to
246protect it from expansion by the shell.
247
248@code{locate} searches special file name databases for file names that
249match patterns. The system administrator runs the @code{updatedb}
250program to create the databases. @code{locate} is run like this:
251
252@example
253locate @r{[}@var{option}@dots{}@r{]} @var{pattern}@dots{}
254@end example
255
256@noindent
257This example prints the names of all files in the default file name
258database whose name ends with @samp{Makefile} or @samp{makefile}.
259Which file names are stored in the database depends on how the system
260administrator ran @code{updatedb}.
261@example
262locate '*[Mm]akefile'
263@end example
264
265The name @code{xargs}, pronounced EX-args, means ``combine
266arguments.'' @code{xargs} builds and executes command lines by
267gathering together arguments it reads on the standard input. Most
268often, these arguments are lists of file names generated by
269@code{find}. @code{xargs} is run like this:
270
271@example
272xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
273@end example
274
275@noindent
276The following command searches the files listed in the file
277@file{file-list} and prints all of the lines in them that contain the
278word @samp{typedef}.
279@example
280xargs grep typedef < file-list
281@end example
282
283@node find Expressions
284@section @code{find} Expressions
285
286The expression that @code{find} uses to select files consists of one
287or more @dfn{primaries}, each of which is a separate command line
288argument to @code{find}. @code{find} evaluates the expression each
289time it processes a file. An expression can contain any of the
290following types of primaries:
291
292@table @dfn
293@item options
294affect overall operation rather than the processing of a specific
295file;
296@item tests
297return a true or false value, depending on the file's attributes;
298@item actions
299have side effects and return a true or false value; and
300@item operators
301connect the other arguments and affect when and whether they are
302evaluated.
303@end table
304
305You can omit the operator between two primaries; it defaults to
306@samp{-and}. @xref{Combining Primaries With Operators}, for ways to
307connect primaries into more complex expressions. If the expression
308contains no actions other than @samp{-prune}, @samp{-print} is
309performed on all files for which the entire expression is true
310(@pxref{Print File Name}).
311
312Options take effect immediately, rather than being evaluated for each
313file when their place in the expression is reached. Therefore, for
314clarity, it is best to place them at the beginning of the expression.
315There are two exceptions to this; @samp{-daystart} and @samp{-follow}
316have different effects depending on where in the command line they
317appear. This can be confusing, so it's best to keep them at the
318beginning, too.
319
320Many of the primaries take arguments, which immediately follow them in
321the next command line argument to @code{find}. Some arguments are
322file names, patterns, or other strings; others are numbers. Numeric
323arguments can be specified as
324
325@table @code
326@item +@var{n}
327for greater than @var{n},
328@item -@var{n}
329for less than @var{n},
330@item @var{n}
331for exactly @var{n}.
332@end table
333
334@node Finding Files, Actions, Introduction, Top
335@chapter Finding Files
336
337By default, @code{find} prints to the standard output the names of the
338files that match the given criteria. @xref{Actions}, for how to get
339more information about the matching files.
340
341
342@menu
343* Name::
344* Links::
345* Time::
346* Size::
347* Type::
348* Owner::
349* Mode Bits::
350* Contents::
351* Directories::
352* Filesystems::
353* Combining Primaries With Operators::
354@end menu
355
356@node Name
357@section Name
358
359Here are ways to search for files whose name matches a certain
360pattern. @xref{Shell Pattern Matching}, for a description of the
361@var{pattern} arguments to these tests.
362
363Each of these tests has a case-sensitive version and a
364case-insensitive version, whose name begins with @samp{i}. In a
365case-insensitive comparison, the patterns @samp{fo*} and @samp{F??}
366match the file names @file{Foo}, @samp{FOO}, @samp{foo}, @samp{fOo},
367etc.
368
369@menu
370* Base Name Patterns::
371* Full Name Patterns::
372* Fast Full Name Search::
373* Shell Pattern Matching:: Wildcards used by these programs.
374@end menu
375
376@node Base Name Patterns
377@subsection Base Name Patterns
378
379@deffn Test -name pattern
380@deffnx Test -iname pattern
381True if the base of the file name (the path with the leading
382directories removed) matches shell pattern @var{pattern}. For
383@samp{-iname}, the match is case-insensitive.@footnote{Because we
384need to perform case-insensitive matching, the GNU fnmatch
385implementation is always used; if the C library includes the GNU
386implementation, we use that and otherwise we use the one from gnulib}
387To ignore a whole directory tree, use @samp{-prune}
388(@pxref{Directories}). As an example, to find Texinfo source files in
389@file{/usr/local/doc}:
390
391@example
392find /usr/local/doc -name '*.texi'
393@end example
394
395Notice that the wildcard must be enclosed in quotes in order to
396protect it from expansion by the shell.
397
398As of findutils version 4.2.2, patterns for @samp{-name} and
399@samp{-iname} will match a file name with a leading @samp{.}. For
400example the command @samp{find /tmp -name \*bar} will match the file
401@file{/tmp/.foobar}. Braces within the pattern (@samp{@{@}}) are not
402considered to be special (that is, @code{find . -name 'foo@{1,2@}'}
403matches a file named @file{foo@{1,2@}}, not the files @file{foo1} and
404@file{foo2}.
405@end deffn
406
407
408@node Full Name Patterns
409@subsection Full Name Patterns
410
411@deffn Test -wholename pattern
412@deffnx Test -iwholename pattern
413True if the entire file name, starting with the command line argument
414under which the file was found, matches shell pattern @var{pattern}.
415For @samp{-iwholename}, the match is case-insensitive. To ignore a
416whole directory tree, use @samp{-prune} rather than checking every
417file in the tree (@pxref{Directories}). The ``entire file name'' as
418used by @code{find} starts with the starting-point specified on the
419command line, and is not converted to an absolute pathname, so for
420example @code{cd /; find tmp -wholename /tmp} will never match
421anything.
422@end deffn
423
424@deffn Test -path pattern
425@deffnx Test -ipath pattern
426These tests are deprecated, but work as for @samp{-wholename} and
427@samp{-iwholename}, respectively. The @samp{-ipath} test is a GNU
428extension, but @samp{-path} is also provided by HP-UX @code{find}.
429@end deffn
430
431@deffn Test -regex expr
432@deffnx Test -iregex expr
433True if the entire file name matches regular expression @var{expr}.
434This is a match on the whole path, not a search. For example, to
435match a file named @file{./fubar3}, you can use the regular expression
436@samp{.*bar.} or @samp{.*b.*3}, but not @samp{f.*r3}. @xref{Regexps,
437, Syntax of Regular Expressions, emacs, The GNU Emacs Manual}, for a
438description of the syntax of regular expressions. For @samp{-iregex},
439the match is case-insensitive. There are several varieties of regular
440expressions; by default this test uses POSIX basic regular
441expressions, but this can be changed with the option
442@samp{-regextype}.
443@end deffn
444
445@deffn Option -regextype name
446This option controls the variety of regular expression syntax
447understood by the @samp{-regex} and @samp{-iregex} tests. This option
448is positional; that is, it only affects regular expressions which
449occur later in the command line. If this option is not given, GNU
450Emacs regular expressions are assumed. Currently-implemented types
451are
452
453
454@table @samp
455@item emacs
456Regular expressions compatible with GNU Emacs; this is also the
457default behaviour if this option is not used.
458@item posix-awk
459Regular expressions compatible with the POSIX awk command (not GNU awk)
460@item posix-basic
461POSIX Basic Regular Expressions.
462@item posix-egrep
463Regular expressions compatible with the POSIX egrep command
464@item posix-extended
465POSIX Extended Regular Expressions
466@end table
467
468@ref{Regular Expressions} for more information on the regular
469expression dialects understood by GNU findutils.
470
471
472@end deffn
473
474@node Fast Full Name Search
475@subsection Fast Full Name Search
476
477To search for files by name without having to actually scan the
478directories on the disk (which can be slow), you can use the
479@code{locate} program. For each shell pattern you give it,
480@code{locate} searches one or more databases of file names and
481displays the file names that contain the pattern. @xref{Shell Pattern
482Matching}, for details about shell patterns.
483
484If a pattern is a plain string---it contains no
485metacharacters---@code{locate} displays all file names in the database
486that contain that string. If a pattern contains
487metacharacters, @code{locate} only displays file names that match the
488pattern exactly. As a result, patterns that contain metacharacters
489should usually begin with a @samp{*}, and will most often end with one
490as well. The exceptions are patterns that are intended to explicitly
491match the beginning or end of a file name.
492
493If you only want @code{locate} to match against the last component of
494the file names (the ``base name'' of the files) you can use the
495@samp{--basename} option. The opposite behaviour is the default, but
496can be selected explicitly by using the option @samp{--wholename}.
497
498The command
499@example
500locate @var{pattern}
501@end example
502
503is almost equivalent to
504@example
505find @var{directories} -name @var{pattern}
506@end example
507
508where @var{directories} are the directories for which the file name
509databases contain information. The differences are that the
510@code{locate} information might be out of date, and that @code{locate}
511handles wildcards in the pattern slightly differently than @code{find}
512(@pxref{Shell Pattern Matching}).
513
514The file name databases contain lists of files that were on the system
515when the databases were last updated. The system administrator can
516choose the file name of the default database, the frequency with which
517the databases are updated, and the directories for which they contain
518entries.
519
520Here is how to select which file name databases @code{locate}
521searches. The default is system-dependent.
522
523@table @code
524@item --database=@var{path}
525@itemx -d @var{path}
526Instead of searching the default file name database, search the file
527name databases in @var{path}, which is a colon-separated list of
528database file names. You can also use the environment variable
529@code{LOCATE_PATH} to set the list of database files to search. The
530option overrides the environment variable if both are used.
531@end table
532
533@node Shell Pattern Matching
534@subsection Shell Pattern Matching
535
536@code{find} and @code{locate} can compare file names, or parts of file
537names, to shell patterns. A @dfn{shell pattern} is a string that may
538contain the following special characters, which are known as
539@dfn{wildcards} or @dfn{metacharacters}.
540
541You must quote patterns that contain metacharacters to prevent the
542shell from expanding them itself. Double and single quotes both work;
543so does escaping with a backslash.
544
545@table @code
546@item *
547Matches any zero or more characters.
548
549@item ?
550Matches any one character.
551
552@item [@var{string}]
553Matches exactly one character that is a member of the string
554@var{string}. This is called a @dfn{character class}. As a
555shorthand, @var{string} may contain ranges, which consist of two
556characters with a dash between them. For example, the class
557@samp{[a-z0-9_]} matches a lowercase letter, a number, or an
558underscore. You can negate a class by placing a @samp{!} or @samp{^}
559immediately after the opening bracket. Thus, @samp{[^A-Z@@]} matches
560any character except an uppercase letter or an at sign.
561
562@item \
563Removes the special meaning of the character that follows it. This
564works even in character classes.
565@end table
566
567In the @code{find} tests that do shell pattern matching (@samp{-name},
568@samp{-wholename}, etc.), wildcards in the pattern will match a
569@samp{.} at the beginning of a file name. This is also the case for
570@code{locate}. Thus, @samp{find -name '*macs'} will match a file
571named @file{.emacs}, as will @samp{locate '*macs'}.
572
573Slash characters have no special significance in the shell pattern
574matching that @code{find} and @code{locate} do, unlike in the shell,
575in which wildcards do not match them. Therefore, a pattern
576@samp{foo*bar} can match a file name @samp{foo3/bar}, and a pattern
577@samp{./sr*sc} can match a file name @samp{./src/misc}.
578
579If you want to locate some files with the @samp{locate} command but
580don't need to see the full list you can use the @samp{--limit} option
581to see just a small number of results, or the @samp{--count} option to
582display only the total number of matches.
583
584@node Links
585@section Links
586
587There are two ways that files can be linked together. @dfn{Symbolic
588links} are a special type of file whose contents are a portion of the
589name of another file. @dfn{Hard links} are multiple directory entries
590for one file; the file names all have the same index node
591(@dfn{inode}) number on the disk.
592
593@menu
594* Symbolic Links::
595* Hard Links::
596@end menu
597
598@node Symbolic Links
599@subsection Symbolic Links
600
601Symbolic links are names that reference other files. GNU @code{find}
602will handle symbolic links in one of two ways; firstly, it can
603dereference the links for you - this means that if it comes across a
604symbolic link, it examines the file that the link points to, in order
605to see if it matches the criteria you have specified. Secondly, it
606can check the link itself in case you might be looking for the actual
607link. If the file that the symbolic link points to is also within the
608directory hierarchy you are searching with the @code{find} command,
609you may not see a great deal of difference between these two
610alternatives.
611
612By default, @code{find} examines symbolic links themselves when it
613finds them (and, if it later comes across the linked-to file, it will
614examine that, too). If you would prefer @code{find} to dereference
615the links and examine the file that each link points to, specify the
616@samp{-L} option to @code{find}. You can explicitly specify the
617default behaviour by using the @samp{-P} option. The @samp{-H}
618option is a half-way-between option which ensures that any symbolic
619links listed on the command line are dereferenced, but other symbolic
620links are not.
621
622Symbolic links are different to ``hard links'' in the sense that you
623need permission to search the directories
624in the the linked-to file name to
625dereference the link. This can mean that even if you specify the
626@samp{-L} option, @code{find} may not be able to determine the
627properties of the file that the link points to (because you don't have
628sufficient permission). In this situation, @code{find} uses the
629properties of the link itself. This also occurs if a symbolic link
630exists but points to a file that is missing.
631
632The options controlling the behaviour of @code{find} with respect to
633links are as follows :-
634
635@table @samp
636@item -P
637@code{find} does not dereference symbolic links at all. This is the
638default behaviour. This option must be specified before any of the
639file names on the command line.
640@item -H
641@code{find} does not dereference symbolic links (except in the case of
642file names on the command line, which are dereferenced). If a
643symbolic link cannot be dereferenced, the information for the symbolic
644link itself is used. This option must be specified before any of the
645file names on the command line.
646@item -L
647@code{find} dereferences symbolic links where possible, and where this
648is not possible it uses the properties of the symbolic link itself.
649This option must be specified before any of the file names on the
650command line. Use of this option also implies the same behaviour as
651the @samp{-noleaf} option. If you later use the @samp{-H} or
652@samp{-P} options, this does not turn off @samp{-noleaf}.
653
654@item -follow
655This option forms part of the ``expression'' and must be specified
656after the file names, but it is otherwise equivalent to @samp{-L}.
657The @samp{-follow} option affects only those tests which appear after
658it on the command line. This option is deprecated. Where possible,
659you should use @samp{-L} instead.
660@end table
661
662The following differences in behavior occur when the @samp{-L} option
663is used:
664
665@itemize @bullet
666@item
667@code{find} follows symbolic links to directories when searching
668directory trees.
669@item
670@samp{-lname} and @samp{-ilname} always return false (unless they
671happen to match broken symbolic links).
672@item
673@samp{-type} reports the types of the files that symbolic links point
674to. This means that in combination with @samp{-L}, @samp{-type l}
675will be true only for broken symbolic links. To check for symbolic
676links when @samp{-L} has been specified, use @samp{-xtype l}.
677@item
678Implies @samp{-noleaf} (@pxref{Directories}).
679@end itemize
680
681If the @samp{-L} option or the @samp{-H} option is used,
682the file names used as arguments to @samp{-newer}, @samp{-anewer}, and
683@samp{-cnewer} are dereferenced and the timestamp from the pointed-to
684file is used instead (if possible -- otherwise the timestamp from the
685symbolic link is used).
686
687@deffn Test -lname pattern
688@deffnx Test -ilname pattern
689True if the file is a symbolic link whose contents match shell pattern
690@var{pattern}. For @samp{-ilname}, the match is case-insensitive.
691@xref{Shell Pattern Matching}, for details about the @var{pattern}
692argument. If the @samp{-L} option is in effect, this test will always
693return false for symbolic links unless they are broken. So, to list
694any symbolic links to @file{sysdep.c} in the current directory and its
695subdirectories, you can do:
696
697@example
698find . -lname '*sysdep.c'
699@end example
700@end deffn
701
702@node Hard Links
703@subsection Hard Links
704
705Hard links allow more than one name to refer to the same file. To
706find all the names which refer to the same file as NAME, use
707@samp{-samefile NAME}. If you are not using the @samp{-L} option, you
708can confine your search to one filesystem using the @samp{-xdev}
709option. This is useful because hard links cannot point outside a
710single filesystem, so this can cut down on needless searching.
711
712If the @samp{-L} option is in effect, and NAME is in fact a symbolic
713link, the symbolic link will be dereferenced. Hence you are searching
714for other links (hard or symbolic) to the file pointed to by NAME. If
715@samp{-L} is in effect but NAME is not itself a symbolic link, other
716symbolic links to the file NAME will be matched.
717
718You can also search for files by inode number. This can occasionally
719be useful in diagnosing problems with filesystems for example, because
720@code{fsck} tends to print inode numbers. Inode numbers also
721occasionally turn up in log messages for some types of software, and
722are used to support the @code{ftok()} library function.
723
724You can learn a file's inode number and the number of links to it by
725running @samp{ls -li} or @samp{find -ls}.
726
727You can search for hard links to inode number NUM by using @samp{-inum
728NUM}. If there are any filesystem mount points below the directory
729where you are starting the search, use the @samp{-xdev} option unless
730you are also using the @samp{-L} option. Using @samp{-xdev} this
731saves needless searching, since hard links to a file must be on the
732same filesystem. @xref{Filesystems}.
733
734@deffn Test -samefile NAME
735File is a hard link to the same inode as NAME. If the @samp{-L}
736option is in effect, symbolic links to the same file as NAME points to
737are also matched.
738@end deffn
739
740@deffn Test -inum n
741File has inode number @var{n}. The @samp{+} and @samp{-} qualifiers
742also work, though these are rarely useful. Much of the time it is
743easier to use @samp{-samefile} rather than this option.
744@end deffn
745
746You can also search for files that have a certain number of links,
747with @samp{-links}. Directories normally have at least two hard
748links; their @file{.} entry is the second one. If they have
749subdirectories, each of those also has a hard link called @file{..} to
750its parent directory. The @file{.} and @file{..} directory entries
751are not normally searched unless they are mentioned on the @code{find}
752command line.
753
754@deffn Test -links n
755File has @var{n} hard links.
756@end deffn
757
758@deffn Test -links +n
759File has more than @var{n} hard links.
760@end deffn
761
762@deffn Test -links -n
763File has fewer than @var{n} hard links.
764@end deffn
765
766@node Time
767@section Time
768
769Each file has three time stamps, which record the last time that
770certain operations were performed on the file:
771
772@enumerate
773@item
774access (read the file's contents)
775@item
776change the status (modify the file or its attributes)
777@item
778modify (change the file's contents)
779@end enumerate
780
781There is no timestamp that indicates when a file was @emph{created}.
782
783You can search for files whose time stamps are within a certain age
784range, or compare them to other time stamps.
785
786@menu
787* Age Ranges::
788* Comparing Timestamps::
789@end menu
790
791@node Age Ranges
792@subsection Age Ranges
793
794These tests are mainly useful with ranges (@samp{+@var{n}} and
795@samp{-@var{n}}).
796
797@deffn Test -atime n
798@deffnx Test -ctime n
799@deffnx Test -mtime n
800True if the file was last accessed (or its status changed, or it was
801modified) @var{n}*24 hours ago. The number of 24-hour periods since
802the file's timestamp is always rounded down; therefore 0 means ``less
803than 24 hours ago'', 1 means ``between 24 and 48 hours ago'', and so
804forth.
805@end deffn
806
807@deffn Test -amin n
808@deffnx Test -cmin n
809@deffnx Test -mmin n
810True if the file was last accessed (or its status changed, or it was
811modified) @var{n} minutes ago. These tests provide finer granularity
812of measurement than @samp{-atime} et al., but rounding is done in a
813similar way. For example, to list files in @file{/u/bill} that were
814last read from 2 to 6 minutes ago:
815
816@example
817find /u/bill -amin +2 -amin -6
818@end example
819@end deffn
820
821@deffn Option -daystart
822Measure times from the beginning of today rather than from 24 hours
823ago. So, to list the regular files in your home directory that were
824modified yesterday, do
825
826@example
827find ~ -daystart -type f -mtime 1
828@end example
829
830The @samp{-daystart} option is unlike most other options in that it
831has an effect on the way that other tests are performed. The affected
832tests are @samp{-amin}, @samp{-cmin}, @samp{-mmin}, @samp{-atime},
833@samp{-ctime} and @samp{-mtime}. The @samp{-daystart} option only
834affects the behaviour of any tests which appear after it on the
835command line.
836@end deffn
837
838@node Comparing Timestamps
839@subsection Comparing Timestamps
840
841As an alternative to comparing timestamps to the current time, you can
842compare them to another file's timestamp. That file's timestamp could
843be updated by another program when some event occurs. Or you could
844set it to a particular fixed date using the @code{touch} command. For
845example, to list files in @file{/usr} modified after February 1 of the
846current year:
847
848@c Idea from Rick Sladkey.
849@example
850touch -t 02010000 /tmp/stamp$$
851find /usr -newer /tmp/stamp$$
852rm -f /tmp/stamp$$
853@end example
854
855@deffn Test -anewer file
856@deffnx Test -cnewer file
857@deffnx Test -newer file
858True if the file was last accessed (or its status changed, or it was
859modified) more recently than @var{file} was modified. These tests are
860affected by @samp{-follow} only if @samp{-follow} comes before them on
861the command line. @xref{Symbolic Links}, for more information on
862@samp{-follow}. As an example, to list any files modified since
863@file{/bin/sh} was last modified:
864
865@example
866find . -newer /bin/sh
867@end example
868@end deffn
869
870@deffn Test -used n
871True if the file was last accessed @var{n} days after its status was
872last changed. Useful for finding files that are not being used, and
873could perhaps be archived or removed to save disk space.
874@end deffn
875
876@node Size
877@section Size
878
879@deffn Test -size n@r{[}bckwMG@r{]}
880True if the file uses @var{n} units of space, rounding up. The units
881are 512-byte blocks by default, but they can be changed by adding a
882one-character suffix to @var{n}:
883
884@table @code
885@item b
886512-byte blocks (never 1024)
887@item c
888bytes
889@item k
890kilobytes (1024 bytes)
891@item w
8922-byte words
893@item M
894Megabytes (units of 1048576 bytes)
895@item G
896Gigabytes (units of 1073741824 bytes)
897@end table
898
899The `b' suffix always considers blocks to be 512 bytes. This is not
900affected by the setting (or non-setting) of the POSIXLY_CORRECT
901environment variable. This behaviour is different to the behaviour of
902the @samp{-ls} action). If you want to use 1024-byte units, use the
903`k' suffix instead.
904
905The number can be prefixed with a `+' or a `-'. A plus sign indicates
906that the test should succeed if the file uses at least @var{n} units
907of storage (a common use of this test) and a minus sign
908indicates that the test should succeed if the file uses less than
909@var{n} units of storage. There is no `=' prefix, because that's the
910default anyway.
911
912The size does not count indirect blocks, but it does count blocks in
913sparse files that are not actually allocated. In other words, it's
914consistent with the result you get for @samp{ls -l} or @samp{wc -c}.
915This handling of sparse files differs from the output of the @samp{%k}
916and @samp{%b} format specifiers for the @samp{-printf} predicate.
917
918@end deffn
919
920@deffn Test -empty
921True if the file is empty and is either a regular file or a directory.
922This might help determine good candidates for deletion. This test is
923useful with @samp{-depth} (@pxref{Directories}) and @samp{-delete}
924(@pxref{Single File}).
925@end deffn
926
927@node Type
928@section Type
929
930@deffn Test -type c
931True if the file is of type @var{c}:
932
933@table @code
934@item b
935block (buffered) special
936@item c
937character (unbuffered) special
938@item d
939directory
940@item p
941named pipe (FIFO)
942@item f
943regular file
944@item l
945symbolic link; if @samp{-L} is in effect, this is true only for broken
946symbolic links. If you want to search for symbolic links when
947@samp{-L} is in effect, use @samp{-xtype} instead of @samp{-type}.
948@item s
949socket
950@item D
951door (Solaris)
952@end table
953@end deffn
954
955@deffn Test -xtype c
956This test behaves the same as @samp{-type} unless the file is a
957symbolic link. If the file is a symbolic link, the result is as
958follows (in the table below, @samp{X} should be understood to
959represent any letter except @samp{l}):
960
961@table @samp
962@item @samp{-P -xtype l}
963True if the symbolic link is broken
964@item @samp{-P -xtype X}
965True if the (ultimate) target file is of type @samp{X}.
966@item @samp{-L -xtype l}
967Always true
968@item @samp{-L -xtype X}
969False unless the symbolic link is broken
970@end table
971
972In other words, for symbolic links, @samp{-xtype} checks the type of
973the file that @samp{-type} does not check.
974
975The @samp{-H} option also affects the behaviour of @samp{-xtype}.
976When @samp{-H} is in effect, @samp{-xtype} behaves as if @samp{-L} had
977been specified when examining files listed on the command line, and as
978if @samp{-P} had been specified otherwise. If neither @samp{-H} nor
979@samp{-L} was specified, @samp{-xtype} behaves as if @samp{-P} had
980been specified.
981
982@xref{Symbolic Links}, for more information on @samp{-follow} and
983@samp{-L}.
984@end deffn
985
986@node Owner
987@section Owner
988
989@deffn Test -user uname
990@deffnx Test -group gname
991True if the file is owned by user @var{uname} (belongs to group
992@var{gname}). A numeric ID is allowed.
993@end deffn
994
995@deffn Test -uid n
996@deffnx Test -gid n
997True if the file's numeric user ID (group ID) is @var{n}. These tests
998support ranges (@samp{+@var{n}} and @samp{-@var{n}}), unlike
999@samp{-user} and @samp{-group}.
1000@end deffn
1001
1002@deffn Test -nouser
1003@deffnx Test -nogroup
1004True if no user corresponds to the file's numeric user ID (no group
1005corresponds to the numeric group ID). These cases usually mean that
1006the files belonged to users who have since been removed from the
1007system. You probably should change the ownership of such files to an
1008existing user or group, using the @code{chown} or @code{chgrp}
1009program.
1010@end deffn
1011
1012@node Mode Bits
1013@section File Mode Bits
1014
1015@xref{File Permissions}, for information on how file mode bits are
1016structured and how to specify them.
1017
1018Four tests determine what users can do with files. These are
1019@samp{-readable}, @samp{-writable}, @samp{-executable} and
1020@samp{-perm}. The first three tests ask the operating system if the
1021current user can perform the relevant operation on a file, while
1022@samp{-perm} just examines the file's mode. The file mode may give
1023a misleading impression of what the user can actually do, because the
1024file may have an access control list, or exist on a read-only
1025filesystem, for example. Of these four tests though, only
1026@samp{-perm} is specified by the POSIX standard.
1027
1028The @samp{-readable}, @samp{-writable} and @samp{-executable} tests
1029are implemented via the @code{access} system call. This is
1030implemented within the operating system itself. If the file being
1031considered is on an NFS filesystem, the remote system may allow or
1032forbid read or write operations for reasons of which the NFS client
1033cannot take account. This includes user-ID mapping, either in the
1034general sense or the more restricted sense in which remote superusers
1035are treated by the NFS server as if they are the local user
1036@samp{nobody} on the NFS server.
1037
1038None of the tests in this section should be used to verify that a user
1039is authorised to perform any operation (on the file being tested or
1040any other file) because of the possibility of a race condition. That
1041is, the situation may change between the test and an action being
1042taken on the basis of the result of that test.
1043
1044
1045@deffn Test -readable
1046True if the file can be read by the invoking user.
1047@end deffn
1048
1049@deffn Test -writable
1050True if the file can be written by the invoking user. This is an
1051in-principle check, and other things may prevent a successful write
1052operation; for example, the filesystem might be full.
1053@end deffn
1054
1055@deffn Test -executable
1056True if the file can be executed/searched by the invoking user.
1057@end deffn
1058
1059@deffn Test -perm pmode
1060
1061True if the file's mode bits match @var{pmode}, which can be
1062either a symbolic or numeric @var{mode} (@pxref{File Permissions})
1063optionally prefixed by @samp{-} or @samp{/}.
1064
1065A @var{pmode} that starts with neither @samp{-} nor @samp{/} matches
1066if @var{mode} exactly matches the file mode bits.
1067
1068A @var{pmode} that starts with @samp{-} matches if
1069@emph{all} the file mode bits set in @var{mode} are set for the file;
1070bits not set in @var{mode} are ignored.
1071
1072A @var{pmode} that starts with @samp{/} matches if
1073@emph{any} of the file mode bits set in @var{mode} are set for the file;
1074bits not set in @var{mode} are ignored.
1075This is a GNU extension.
1076
1077If you don't use the @samp{/} or @samp{-} form with a symbolic mode
1078string, you may have to specify a rather complex mode string. For
1079example @samp{-perm g=w} will only match files that have mode 0020
1080(that is, ones for which group write permission is the only file mode bit
1081set). It is more likely that you will want to use the @samp{/} or
1082@samp{-} forms, for example @samp{-perm -g=w}, which matches any file
1083with group write permission.
1084
1085
1086@table @samp
1087@item -perm 664
1088Match files that have read and write permission for their owner,
1089and group, but that the rest of the world can read but not write to.
1090Do not match files that meet these criteria but have other file mode
1091bits set (for example if someone can execute/search the file).
1092
1093@item -perm -664
1094Match files that have read and write permission for their owner,
1095and group, but that the rest of the world can read but not write to,
1096without regard to the presence of any extra file mode bits (for
1097example the executable bit). This matchs a file with mode
10980777, for example.
1099
1100@item -perm /222
1101Match files that are writable by somebody (their owner, or
1102their group, or anybody else).
1103
1104@item -perm /022
1105Match files that are writable by either their owner or their
1106group. The files don't have to be writable by both the owner and
1107group to be matched; either will do.
1108
1109@item -perm /g+w,o+w
1110As above.
1111
1112@item -perm /g=w,o=w
1113As above.
1114
1115@item -perm -022
1116Match files that are writable by both their owner and their
1117group.
1118
1119@item -perm -444 -perm /222 ! -perm /111
1120Match files that are readable for everybody, have at least one
1121write bit set (i.e. somebody can write to them), but that cannot be
1122executed/searched by anybody. Note that in some shells the @samp{!} must be
1123escaped;.
1124
1125@item -perm -a+r -perm /a+w ! -perm /a+x
1126As above.
1127
1128
1129@item -perm -g+w,o+w
1130As above.
1131@end table
1132
1133@quotation Warning
1134If you specify @samp{-perm /000} or @samp{-perm /mode} where the
1135symbolic mode @samp{mode} has no bits set, the test currently matches
1136no files. This differs from the behaviour of @samp{-perm -000}, which
1137matches all files. The behaviour of @samp{-perm /000} will be changed
1138to be consistent with the behaviour of @samp{-perm -000}. The change
1139will probably be made in early 2006.
1140@end quotation
1141
1142@end deffn
1143
1144@node Contents
1145@section Contents
1146
1147To search for files based on their contents, you can use the
1148@code{grep} program. For example, to find out which C source files in
1149the current directory contain the string @samp{thing}, you can do:
1150
1151@example
1152grep -l thing *.[ch]
1153@end example
1154
1155If you also want to search for the string in files in subdirectories,
1156you can combine @code{grep} with @code{find} and @code{xargs}, like
1157this:
1158
1159@example
1160find . -name '*.[ch]' | xargs grep -l thing
1161@end example
1162
1163The @samp{-l} option causes @code{grep} to print only the names of
1164files that contain the string, rather than the lines that contain it.
1165The string argument (@samp{thing}) is actually a regular expression,
1166so it can contain metacharacters. This method can be refined a little
1167by using the @samp{-r} option to make @code{xargs} not run @code{grep}
1168if @code{find} produces no output, and using the @code{find} action
1169@samp{-print0} and the @code{xargs} option @samp{-0} to avoid
1170misinterpreting files whose names contain spaces:
1171
1172@example
1173find . -name '*.[ch]' -print0 | xargs -r -0 grep -l thing
1174@end example
1175
1176For a fuller treatment of finding files whose contents match a
1177pattern, see the manual page for @code{grep}.
1178
1179@node Directories
1180@section Directories
1181
1182Here is how to control which directories @code{find} searches, and how
1183it searches them. These two options allow you to process a horizontal
1184slice of a directory tree.
1185
1186@deffn Option -maxdepth levels
1187Descend at most @var{levels} (a non-negative integer) levels of
1188directories below the command line arguments. @samp{-maxdepth 0}
1189means only apply the tests and actions to the command line arguments.
1190@end deffn
1191
1192@deffn Option -mindepth levels
1193Do not apply any tests or actions at levels less than @var{levels} (a
1194non-negative integer). @samp{-mindepth 1} means process all files
1195except the command line arguments.
1196@end deffn
1197
1198@deffn Option -depth
1199Process each directory's contents before the directory itself. Doing
1200this is a good idea when producing lists of files to archive with
1201@code{cpio} or @code{tar}. If a directory does not have write
1202permission for its owner, its contents can still be restored from the
1203archive since the directory's permissions are restored after its
1204contents.
1205@end deffn
1206
1207@deffn Option -d
1208This is a deprecated synonym for @samp{-depth}, for compatibility with
1209Mac OS X, FreeBSD and OpenBSD. The @samp{-depth} option is a POSIX
1210feature, so it is better to use that.
1211@end deffn
1212
1213@deffn Action -prune
1214If the file is a directory, do not descend into it. The result is
1215true. For example, to skip the directory @file{src/emacs} and all
1216files and directories under it, and print the names of the other files
1217found:
1218
1219@example
1220find . -wholename './src/emacs' -prune -o -print
1221@end example
1222
1223The above command will not print @file{./src/emacs} among its list of
1224results. This however is not due to the effect of the @samp{-prune}
1225action (which only prevents further descent, it doesn't make sure we
1226ignore that item). Instead, this effect is due to the use of
1227@samp{-o}. Since the left hand side of the ``or'' condition has
1228succeeded for @file{./src/emacs}, it is not necessary to evaluate the
1229right-hand-side (@samp{-print}) at all for this particular file. If
1230you wanted to print that directory name you could use either an extra
1231@samp{-print} action:
1232
1233@example
1234find . -wholename './src/emacs' -prune -print -o -print
1235@end example
1236
1237or use the comma operator:
1238
1239@example
1240find . -wholename './src/emacs' -prune , -print
1241@end example
1242
1243If the @samp{-depth} option is in effect, the subdirectories will have
1244already been visited in any case. Hence @samp{-prune} has no effect
1245and returns false.
1246@end deffn
1247
1248
1249@deffn Action -quit
1250Exit immediately (with return value zero if no errors have occurred).
1251No child processes will be left running, but no more files specified
1252on the command line will be processed. For example, @code{find
1253/tmp/foo /tmp/bar -print -quit} will print only @samp{/tmp/foo}. Any
1254command lines which have been built by @samp{-exec ... \+} or
1255@samp{-execdir ... \+} are invoked before the program is executed.
1256@end deffn
1257
1258@deffn Option -noleaf
1259Do not optimize by assuming that directories contain 2 fewer
1260subdirectories than their hard link count. This option is needed when
1261searching filesystems that do not follow the Unix directory-link
1262convention, such as CD-ROM or MS-DOS filesystems or AFS volume mount
1263points. Each directory on a normal Unix filesystem has at least 2
1264hard links: its name and its @file{.} entry. Additionally, its
1265subdirectories (if any) each have a @file{..} entry linked to that
1266directory. When @code{find} is examining a directory, after it has
1267statted 2 fewer subdirectories than the directory's link count, it
1268knows that the rest of the entries in the directory are
1269non-directories (@dfn{leaf} files in the directory tree). If only the
1270files' names need to be examined, there is no need to stat them; this
1271gives a significant increase in search speed.
1272@end deffn
1273
1274@deffn Option -ignore_readdir_race
1275If a file disappears after its name has been read from a directory but
1276before @code{find} gets around to examining the file with @code{stat},
1277don't issue an error message. If you don't specify this option, an
1278error message will be issued. This option can be useful in system
1279scripts (cron scripts, for example) that examine areas of the
1280filesystem that change frequently (mail queues, temporary directories,
1281and so forth), because this scenario is common for those sorts of
1282directories. Completely silencing error messages from @code{find} is
1283undesirable, so this option neatly solves the problem. There is no
1284way to search one part of the filesystem with this option on and part
1285of it with this option off, though. When this option is turned on and
1286find discovers that one of the start-point files specified on the
1287command line does not exist, no error message will be issued.
1288
1289@end deffn
1290
1291@deffn Option -noignore_readdir_race
1292This option reverses the effect of the @samp{-ignore_readdir_race}
1293option.
1294@end deffn
1295
1296
1297@node Filesystems
1298@section Filesystems
1299
1300A @dfn{filesystem} is a section of a disk, either on the local host or
1301mounted from a remote host over a network. Searching network
1302filesystems can be slow, so it is common to make @code{find} avoid
1303them.
1304
1305There are two ways to avoid searching certain filesystems. One way is
1306to tell @code{find} to only search one filesystem:
1307
1308@deffn Option -xdev
1309@deffnx Option -mount
1310Don't descend directories on other filesystems. These options are
1311synonyms.
1312@end deffn
1313
1314The other way is to check the type of filesystem each file is on, and
1315not descend directories that are on undesirable filesystem types:
1316
1317@deffn Test -fstype type
1318True if the file is on a filesystem of type @var{type}. The valid
1319filesystem types vary among different versions of Unix; an incomplete
1320list of filesystem types that are accepted on some version of Unix or
1321another is:
1322@example
1323ext2 ext3 proc sysfs ufs 4.2 4.3 nfs tmp mfs S51K S52K
1324@end example
1325You can use @samp{-printf} with the @samp{%F} directive to see the
1326types of your filesystems. The @samp{%D} directive shows the device
1327number. @xref{Print File Information}. @samp{-fstype} is usually
1328used with @samp{-prune} to avoid searching remote filesystems
1329(@pxref{Directories}).
1330@end deffn
1331
1332@node Combining Primaries With Operators
1333@section Combining Primaries With Operators
1334
1335Operators build a complex expression from tests and actions.
1336The operators are, in order of decreasing precedence:
1337
1338@table @code
1339@item @asis{( @var{expr} )}
1340@findex ()
1341Force precedence. True if @var{expr} is true.
1342
1343@item @asis{! @var{expr}}
1344@itemx @asis{-not @var{expr}}
1345@findex !
1346@findex -not
1347True if @var{expr} is false. In some shells, it is necessary to
1348protect the @samp{!} from shell interpretation by quoting it.
1349
1350@item @asis{@var{expr1 expr2}}
1351@itemx @asis{@var{expr1} -a @var{expr2}}
1352@itemx @asis{@var{expr1} -and @var{expr2}}
1353@findex -a
1354@findex -and
1355And; @var{expr2} is not evaluated if @var{expr1} is false.
1356
1357@item @asis{@var{expr1} -o @var{expr2}}
1358@itemx @asis{@var{expr1} -or @var{expr2}}
1359@findex -o
1360@findex -or
1361Or; @var{expr2} is not evaluated if @var{expr1} is true.
1362
1363@item @asis{@var{expr1} , @var{expr2}}
1364@findex ,
1365List; both @var{expr1} and @var{expr2} are always evaluated. True if
1366@var{expr2} is true. The value of @var{expr1} is discarded. This
1367operator lets you do multiple independent operations on one traversal,
1368without depending on whether other operations succeeded. The two
1369operations @var{expr1} and @var{expr2} are not always fully
1370independent, since @var{expr1} might have side effects like touching
1371or deleting files, or it might use @samp{-prune} which would also
1372affect @var{expr2}.
1373@end table
1374
1375@code{find} searches the directory tree rooted at each file name by
1376evaluating the expression from left to right, according to the rules
1377of precedence, until the outcome is known (the left hand side is false
1378for @samp{-and}, true for @samp{-or}), at which point @code{find}
1379moves on to the next file name.
1380
1381There are two other tests that can be useful in complex expressions:
1382
1383@deffn Test -true
1384Always true.
1385@end deffn
1386
1387@deffn Test -false
1388Always false.
1389@end deffn
1390
1391@node Actions, Databases, Finding Files, Top
1392@chapter Actions
1393
1394There are several ways you can print information about the files that
1395match the criteria you gave in the @code{find} expression. You can
1396print the information either to the standard output or to a file that
1397you name. You can also execute commands that have the file names as
1398arguments. You can use those commands as further filters to select
1399files.
1400
1401@menu
1402* Print File Name::
1403* Print File Information::
1404* Run Commands::
1405* Delete Files::
1406* Adding Tests::
1407@end menu
1408
1409@node Print File Name
1410@section Print File Name
1411
1412@deffn Action -print
1413True; print the entire file name on the standard output, followed by a
1414newline. If there is the faintest possibility that one of the files
1415for which you are searching might contain a newline, you should use
1416@samp{-print0} instead.
1417@end deffn
1418
1419@deffn Action -fprint file
1420True; print the entire file name into file @var{file}, followed by a
1421newline. If @var{file} does not exist when @code{find} is run, it is
1422created; if it does exist, it is truncated to 0 bytes. The named
1423output file is always created, even if no output is sent to it. The
1424file names @file{/dev/stdout} and @file{/dev/stderr} are handled
1425specially; they refer to the standard output and standard error
1426output, respectively.
1427
1428If there is the faintest possibility that one of the files for which
1429you are searching might contain a newline, you should use
1430@samp{-fprint0} instead.
1431@end deffn
1432
1433
1434@c @deffn Option -show-control-chars how
1435@c This option affects how some of @code{find}'s actions treat
1436@c unprintable characters in file names. If @samp{how} is
1437@c @samp{literal}, any subsequent actions (i.e. actions further on in the
1438@c command line) print file names as-is.
1439@c
1440@c If this option is not specified, it currently defaults to @samp{safe}.
1441@c If @samp{how} is @samp{safe}, C-like backslash escapes are used to
1442@c indicate the non-printable characters for @samp{-ls} and @samp{-fls}.
1443@c On the other hand, @samp{-print}, @samp{-fprint}, @samp{-fprintf} and
1444@c @code{-printf} all quote unprintable characters if the data is going
1445@c to a tty, and otherwise the data is emitted literally.
1446@c
1447@c @table @code
1448@c @item -ls
1449@c Escaped if @samp{how} is @samp{safe}
1450@c @item -fls
1451@c Escaped if @samp{how} is @samp{safe}
1452@c @item -print
1453@c Always quoted if stdout is a tty,
1454@c @samp{-show-control-chars} is ignored
1455@c @item -print0
1456@c Always literal, never escaped
1457@c @item -fprint
1458@c Always quoted if the destination is a tty;
1459@c @samp{-show-control-chars} is ignored
1460@c @item -fprint0
1461@c Always literal, never escaped
1462@c @item -fprintf
1463@c If the destination is a tty, the @samp{%f},
1464@c @samp{%F}, @samp{%h}, @samp{%l}, @samp{%p},
1465@c and @samp{%P} directives produce quoted
1466@c strings if stdout is a tty and are treated
1467@c literally otherwise.
1468@c @item -printf
1469@c As for @code{-fprintf}.
1470@c @end table
1471@c @end deffn
1472
1473
1474@node Print File Information
1475@section Print File Information
1476
1477@deffn Action -ls
1478True; list the current file in @samp{ls -dils} format on the standard
1479output. The output looks like this:
1480
1481@smallexample
1482204744 17 -rw-r--r-- 1 djm staff 17337 Nov 2 1992 ./lwall-quotes
1483@end smallexample
1484
1485The fields are:
1486
1487@enumerate
1488@item
1489The inode number of the file. @xref{Hard Links}, for how to find
1490files based on their inode number.
1491
1492@item
1493the number of blocks in the file. The block counts are of 1K blocks,
1494unless the environment variable @code{POSIXLY_CORRECT} is set, in
1495which case 512-byte blocks are used. @xref{Size}, for how to find
1496files based on their size.
1497
1498@item
1499The file's type and file mode bits. The type is shown as a dash for a
1500regular file; for other file types, a letter like for @samp{-type} is
1501used (@pxref{Type}). The file mode bits are read, write, and execute/search for
1502the file's owner, its group, and other users, respectively; a dash
1503means the permission is not granted. @xref{File Permissions}, for
1504more details about file permissions. @xref{Mode Bits}, for how to
1505find files based on their file mode bits.
1506
1507@item
1508The number of hard links to the file.
1509
1510@item
1511The user who owns the file.
1512
1513@item
1514The file's group.
1515
1516@item
1517The file's size in bytes.
1518
1519@item
1520The date the file was last modified.
1521
1522@item
1523The file's name. @samp{-ls} quotes non-printable characters in the
1524file names using C-like backslash escapes. This may change soon, as
1525the treatment of unprintable characters is harmonised for @samp{-ls},
1526@samp{-fls}, @samp{-print}, @samp{-fprint}, @samp{-printf} and
1527@samp{-fprintf}.
1528@end enumerate
1529@end deffn
1530
1531@deffn Action -fls file
1532True; like @samp{-ls} but write to @var{file} like @samp{-fprint}
1533(@pxref{Print File Name}). The named output file is always created,
1534even if no output is sent to it.
1535@end deffn
1536
1537@deffn Action -printf format
1538True; print @var{format} on the standard output, interpreting @samp{\}
1539escapes and @samp{%} directives. Field widths and precisions can be
1540specified as with the @code{printf} C function. Format flags (like
1541@samp{#} for example) may not work as you expect because many of the
1542fields, even numeric ones, are printed with %s. Numeric flags which
1543are affected in this way include G, U, b, D, k and n. This difference
1544in behaviour means though that the format flag @samp{-} will work; it
1545forces left-alignment of the field. Unlike @samp{-print},
1546@samp{-printf} does not add a newline at the end of the string. If
1547you want a newline at the end of the string, add a @samp{\n}.
1548@end deffn
1549
1550@deffn Action -fprintf file format
1551True; like @samp{-printf} but write to @var{file} like @samp{-fprint}
1552(@pxref{Print File Name}). The output file is always created, even if
1553no output is ever sent to it.
1554@end deffn
1555
1556@menu
1557* Escapes::
1558* Format Directives::
1559* Time Formats::
1560@end menu
1561
1562@node Escapes
1563@subsection Escapes
1564
1565The escapes that @samp{-printf} and @samp{-fprintf} recognise are:
1566
1567@table @code
1568@item \a
1569Alarm bell.
1570@item \b
1571Backspace.
1572@item \c
1573Stop printing from this format immediately and flush the output.
1574@item \f
1575Form feed.
1576@item \n
1577Newline.
1578@item \r
1579Carriage return.
1580@item \t
1581Horizontal tab.
1582@item \v
1583Vertical tab.
1584@item \\
1585A literal backslash (@samp{\}).
1586@item \0
1587ASCII NUL.
1588@item \NNN
1589The character whose ASCII code is NNN (octal).
1590@end table
1591
1592A @samp{\} character followed by any other character is treated as an
1593ordinary character, so they both are printed, and a warning message is
1594printed to the standard error output (because it was probably a typo).
1595
1596@node Format Directives
1597@subsection Format Directives
1598
1599@samp{-printf} and @samp{-fprintf} support the following format
1600directives to print information about the file being processed. The C
1601@code{printf} function, field width and precision specifiers are
1602supported, as applied to string (%s) types. That is, you can specify
1603"minimum field width"."maximum field width" for each directive.
1604Format flags (like @samp{#} for example) may not work as you expect
1605because many of the fields, even numeric ones, are printed with %s.
1606The format flag @samp{-} does work; it forces left-alignment of the
1607field.
1608
1609@samp{%%} is a literal percent sign. A @samp{%} character followed by
1610an unrecognised character (i.e. not a known directive or @code{printf}
1611field width and precision specifier), is discarded (but the
1612unrecognised character is printed), and a warning message is printed
1613to the standard error output (because it was probably a typo). Don't
1614rely on this behaviour, because other directives may be added in the
1615future.
1616
1617A @samp{%} at the end of the format argument causes undefined
1618behaviour since there is no following character. In some locales, it
1619may hide your door keys, while in others it may remove the final page
1620from the novel you are reading.
1621
1622@menu
1623* Name Directives::
1624* Ownership Directives::
1625* Size Directives::
1626* Location Directives::
1627* Time Directives::
1628* Formatting Flags::
1629@end menu
1630
1631@node Name Directives
1632@subsubsection Name Directives
1633
1634@table @code
1635@item %p
1636@c supports %-X.Yp
1637File's name (not the absolute path name, but the name of the file as
1638it was encountered by @code{find} - that is, as a relative path from
1639one of the starting points).
1640@item %f
1641File's name with any leading directories removed (only the last
1642element).
1643@c supports %-X.Yf
1644@item %h
1645Leading directories of file's name (all but the last element and the
1646slash before it). If the file's name contains no slashes (for example
1647because it was named on the command line and is in the current working
1648directory), then ``%h'' expands to ``.''. This prevents ``%h/%f''
1649expanding to ``/foo'', which would be surprising and probably not
1650desirable.
1651@c supports %-X.Yh
1652@item %P
1653File's name with the name of the command line argument under which
1654it was found removed from the beginning.
1655@c supports %-X.YP
1656@item %H
1657Command line argument under which file was found.
1658@c supports %-X.YH
1659@end table
1660
1661@node Ownership Directives
1662@subsubsection Ownership Directives
1663
1664@table @code
1665@item %g
1666@c supports %-X.Yg
1667File's group name, or numeric group ID if the group has no name.
1668@item %G
1669@c supports %-X.Yg
1670@c TODO: Needs to support # flag and 0 flag
1671File's numeric group ID.
1672@item %u
1673@c supports %-X.Yu
1674File's user name, or numeric user ID if the user has no name.
1675@item %U
1676@c supports %-X.Yu
1677@c TODO: Needs to support # flag
1678File's numeric user ID.
1679@item %m
1680@c full support, including # and 0.
1681File's mode bits (in octal). If you always want to have a leading
1682zero on the number, use the '#' format flag, for example '%#m'.
1683
1684The file mode bit numbers used are the traditional Unix
1685numbers, which will be as expected on most systems, but if your
1686system's file mode bit layout differs from the traditional Unix
1687semantics, you will see a difference between the mode as printed by
1688@samp{%m} and the mode as it appears in @code{struct stat}.
1689
1690@item %M
1691File's type and mode bits (in symbolic form, as for @code{ls}). This
1692directive is supported in findutils 4.2.5 and later.
1693@end table
1694
1695@node Size Directives
1696@subsubsection Size Directives
1697
1698@table @code
1699@item %k
1700The amount of disk space used for this file in 1K blocks. Since disk
1701space is allocated in multiples of the filesystem block size this is
1702usually greater than %s/1024, but it can also be smaller if the file
1703is a sparse file (that is, it has ``holes'').
1704@item %b
1705The amount of disk space used for this file in 512-byte blocks. Since
1706disk space is allocated in multiples of the filesystem block size this
1707is usually greater than %s/1024, but it can also be smaller if the
1708file is a sparse file (that is, it has ``holes'').
1709@item %s
1710File's size in bytes.
1711@end table
1712
1713@node Location Directives
1714@subsubsection Location Directives
1715
1716@table @code
1717@item %d
1718File's depth in the directory tree (depth below a file named on the
1719command line, not depth below the root directory). Files named on the
1720command line have a depth of 0. Subdirectories immediately below them
1721have a depth of 1, and so on.
1722@item %D
1723The device number on which the file exists (the @code{st_dev} field of
1724@code{struct stat}), in decimal.
1725@item %F
1726Type of the filesystem the file is on; this value can be used for
1727@samp{-fstype} (@pxref{Directories}).
1728@item %l
1729Object of symbolic link (empty string if file is not a symbolic link).
1730@item %i
1731File's inode number (in decimal).
1732@item %n
1733Number of hard links to file.
1734@item %y
1735Type of the file as used with @samp{-type}. If the file is a symbolic
1736link, @samp{l} will be printed.
1737@item %Y
1738Type of the file as used with @samp{-type}. If the file is a symbolic
1739link, it is dereferenced. If the file is a broken symbolic link,
1740@samp{N} is printed.
1741
1742@end table
1743
1744@node Time Directives
1745@subsubsection Time Directives
1746
1747Some of these directives use the C @code{ctime} function. Its output
1748depends on the current locale, but it typically looks like
1749
1750@example
1751Wed Nov 2 00:42:36 1994
1752@end example
1753
1754@table @code
1755@item %a
1756File's last access time in the format returned by the C @code{ctime}
1757function.
1758@item %A@var{k}
1759File's last access time in the format specified by @var{k}
1760(@pxref{Time Formats}).
1761@item %c
1762File's last status change time in the format returned by the C
1763@code{ctime} function.
1764@item %C@var{k}
1765File's last status change time in the format specified by @var{k}
1766(@pxref{Time Formats}).
1767@item %t
1768File's last modification time in the format returned by the C
1769@code{ctime} function.
1770@item %T@var{k}
1771File's last modification time in the format specified by @var{k}
1772(@pxref{Time Formats}).
1773@end table
1774
1775@node Time Formats
1776@subsection Time Formats
1777
1778Below are the formats for the directives @samp{%A}, @samp{%C}, and
1779@samp{%T}, which print the file's timestamps. Some of these formats
1780might not be available on all systems, due to differences in the C
1781@code{strftime} function between systems.
1782
1783@menu
1784* Time Components::
1785* Date Components::
1786* Combined Time Formats::
1787@end menu
1788
1789@node Time Components
1790@subsubsection Time Components
1791
1792The following format directives print single components of the time.
1793
1794@table @code
1795@item H
1796hour (00..23)
1797@item I
1798hour (01..12)
1799@item k
1800hour ( 0..23)
1801@item l
1802hour ( 1..12)
1803@item p
1804locale's AM or PM
1805@item Z
1806time zone (e.g., EDT), or nothing if no time zone is determinable
1807@item M
1808minute (00..59)
1809@item S
1810second (00..61)
1811@item @@
1812seconds since Jan. 1, 1970, 00:00 GMT.
1813@end table
1814
1815@node Date Components
1816@subsubsection Date Components
1817
1818The following format directives print single components of the date.
1819
1820@table @code
1821@item a
1822locale's abbreviated weekday name (Sun..Sat)
1823@item A
1824locale's full weekday name, variable length (Sunday..Saturday)
1825@item b
1826@itemx h
1827locale's abbreviated month name (Jan..Dec)
1828@item B
1829locale's full month name, variable length (January..December)
1830@item m
1831month (01..12)
1832@item d
1833day of month (01..31)
1834@item w
1835day of week (0..6)
1836@item j
1837day of year (001..366)
1838@item U
1839week number of year with Sunday as first day of week (00..53)
1840@item W
1841week number of year with Monday as first day of week (00..53)
1842@item Y
1843year (1970@dots{})
1844@item y
1845last two digits of year (00..99)
1846@end table
1847
1848@node Combined Time Formats
1849@subsubsection Combined Time Formats
1850
1851The following format directives print combinations of time and date
1852components.
1853
1854@table @code
1855@item r
1856time, 12-hour (hh:mm:ss [AP]M)
1857@item T
1858time, 24-hour (hh:mm:ss)
1859@item X
1860locale's time representation (H:M:S)
1861@item c
1862locale's date and time (Sat Nov 04 12:02:33 EST 1989)
1863@item D
1864date (mm/dd/yy)
1865@item x
1866locale's date representation (mm/dd/yy)
1867@item +
1868Date and time, separated by '+', for example `2004-04-28+22:22:05'.
1869The time is given in the current timezone (which may be affected by
1870setting the TZ environment variable). This is a GNU extension.
1871@end table
1872
1873@node Formatting Flags
1874@subsubsection Formatting Flags
1875
1876The @samp{%m} and @samp{%d} directives support the @samp{#}, @samp{0}
1877and @samp{+} flags, but the other directives do not, even if they
1878print numbers. Numeric directives that do not support these flags
1879include
1880
1881@samp{G},
1882@samp{U},
1883@samp{b},
1884@samp{D},
1885@samp{k} and
1886@samp{n}.
1887
1888All fields support the format flag @samp{-}, which makes fields
1889left-aligned. That is, if the field width is greater than the actual
1890contents of the field, the requisite number of spaces are printed
1891after the field content instead of before it.
1892
1893@node Run Commands
1894@section Run Commands
1895
1896You can use the list of file names created by @code{find} or
1897@code{locate} as arguments to other commands. In this way you can
1898perform arbitrary actions on the files.
1899
1900@menu
1901* Single File::
1902* Multiple Files::
1903* Querying::
1904@end menu
1905
1906@node Single File
1907@subsection Single File
1908
1909Here is how to run a command on one file at a time.
1910
1911@deffn Action -execdir command ;
1912Execute @var{command}; true if zero sftatus is returned. @code{find}
1913takes all arguments after @samp{-exec} to be part of the command until
1914an argument consisting of @samp{;} is reached. It replaces the string
1915@samp{@{@}} by the current file name being processed everywhere it
1916occurs in the command. Both of these constructions need to be escaped
1917(with a @samp{\}) or quoted to protect them from expansion by the
1918shell. The command is executed in the directory in which @code{find}
1919was run.
1920
1921For example, to compare each C header file in or below the current
1922directory with the file @file{/tmp/master}:
1923
1924@example
1925find . -name '*.h' -execdir diff -u '@{@}' /tmp/master ';'
1926@end example
1927@end deffn
1928
1929If you use`@samp{-execdir}, you must ensure that the current directory
1930is not on @var{$PATH}, because otherwise an attacker could make
1931@samp{find} run commands of their choice simply by leaving a
1932suitably-named file in the right directory. GNU find will refuse to
1933run if you use @samp{-execdir} and the current directory is in
1934@var{$PATH}.
1935
1936Another similar option, @samp{-exec} is supported, but is less secure.
1937@xref{Security Considerations}, for a discussion of the security
1938problems surrounding @samp{-exec}.
1939
1940
1941@deffn Action -exec command ;
1942This insecure variant of the @samp{-execdir} action is specified by
1943POSIX. The main difference is that the command is executed in the
1944directory from which @code{find} was invoked, meaning that @samp{@{@}}
1945is expanded to a relative path starting with the name of one of the
1946starting directories, rather than just the basename of the matched
1947file.
1948
1949While some implementations of @code{find} replace the @samp{@{@}} only
1950where it appears on its own in an argument, GNU @code{find} replaces
1951@samp{@{@}} wherever it appears.
1952@end deffn
1953
1954
1955@node Multiple Files
1956@subsection Multiple Files
1957
1958Sometimes you need to process files one of the time. But usually this
1959is not necessary, and, it is faster to run a command on as many files
1960as possible at a time, rather than once per file. Doing this saves on
1961the time it takes to start up the command each time.
1962
1963The @samp{-execdir} and @samp{-exec} actions have variants that build
1964command lines containing as many matched files as possible.
1965
1966@deffn Action -execdir command @{@} +
1967This works as for @samp{-execdir command ;}, except that the
1968@samp{@{@}} at the end of the command is expanded to a list of names
1969of matching files. This expansion is done in such a way as to avoid
1970exceeding the maximum command line length available on the system.
1971Only one @samp{@{@}} is allowed within the command, and it must appear
1972at the end, immediately before the @samp{+}. A @samp{+} appearing in
1973any position other than immediately after @samp{@{@}} is not
1974considered to be special (that is, it does not terminate the command).
1975@end deffn
1976
1977
1978@deffn Action -exec command @{@} +
1979This insecure variant of the @samp{-execdir} action is specified by
1980POSIX. The main difference is that the command is executed in the
1981directory from which @code{find} was invoked, meaning that @samp{@{@}}
1982is expanded to a relative path starting with the name of one of the
1983starting directories, rather than just the basename of the matched
1984file.
1985@end deffn
1986
1987Before @code{find} exits, any partially-built command lines are
1988executed. This happens even if the exit was caused by the
1989@samp{-quit} action. However, some types of error (for example not
1990being able to invoke @code{stat()} on the current directory) can cause
1991an immediate fatal exit. In this situation, any partially-built
1992command lines will not be invoked (this prevents possible infinite
1993loops).
1994
1995Another, but less secure, way to run a command on more than one file
1996at once, is to use the @code{xargs} command, which is invoked like
1997this:
1998
1999@example
2000xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
2001@end example
2002
2003@code{xargs} normally reads arguments from the standard input. These
2004arguments are delimited by blanks (which can be protected with double
2005or single quotes or a backslash) or newlines. It executes the
2006@var{command} (default is @file{/bin/echo}) one or more times with any
2007@var{initial-arguments} followed by arguments read from standard
2008input. Blank lines on the standard input are ignored.
2009
2010Instead of blank-delimited names, it is safer to use @samp{find
2011-print0} or @samp{find -fprint0} and process the output by giving the
2012@samp{-0} or @samp{--null} option to GNU @code{xargs}, GNU @code{tar},
2013GNU @code{cpio}, or @code{perl}. The @code{locate} command also has a
2014@samp{-0} or @samp{--null} option which does the same thing.
2015
2016You can use shell command substitution (backquotes) to process a list
2017of arguments, like this:
2018
2019@example
2020grep -l sprintf `find $HOME -name '*.c' -print`
2021@end example
2022
2023However, that method produces an error if the length of the @samp{.c}
2024file names exceeds the operating system's command line length limit.
2025@code{xargs} avoids that problem by running the command as many times
2026as necessary without exceeding the limit:
2027
2028@example
2029find $HOME -name '*.c' -print | xargs grep -l sprintf
2030@end example
2031
2032However, if the command needs to have its standard input be a terminal
2033(@code{less}, for example), you have to use the shell command
2034substitution method or use the @samp{--arg-file} option of
2035@code{xargs}.
2036
2037The @code{xargs} command will process all its input, building command
2038lines and executing them, unless one of the commands exits with a
2039status of 255 (this will cause xargs to issue an error message and
2040stop) or it reads a line contains the end of file string specified
2041with the @samp{--eof} option.
2042
2043@menu
2044* Unsafe File Name Handling::
2045* Safe File Name Handling::
2046* Unusual Characters in File Names::
2047* Limiting Command Size::
2048* Interspersing File Names::
2049@end menu
2050
2051@node Unsafe File Name Handling
2052@subsubsection Unsafe File Name Handling
2053
2054Because file names can contain quotes, backslashes, blank characters,
2055and even newlines, it is not safe to process them using @code{xargs}
2056in its default mode of operation. But since most files' names do not
2057contain blanks, this problem occurs only infrequently. If you are
2058only searching through files that you know have safe names, then you
2059need not be concerned about it.
2060
2061@c This example is adapted from:
2062@c From: pfalstad@stone.Princeton.EDU (Paul John Falstad)
2063@c Newsgroups: comp.unix.shell
2064@c Subject: Re: Beware xargs security holes
2065@c Date: 16 Oct 90 19:12:06 GMT
2066@c
2067In many applications, if @code{xargs} botches processing a file
2068because its name contains special characters, some data might be lost.
2069The importance of this problem depends on the importance of the data
2070and whether anyone notices the loss soon enough to correct it.
2071However, here is an extreme example of the problems that using
2072blank-delimited names can cause. If the following command is run
2073daily from @code{cron}, then any user can remove any file on the
2074system:
2075
2076@example
2077find / -name '#*' -atime +7 -print | xargs rm
2078@end example
2079
2080For example, you could do something like this:
2081
2082@example
2083eg$ echo > '#
2084vmunix'
2085@end example
2086
2087@noindent
2088and then @code{cron} would delete @file{/vmunix}, if it ran
2089@code{xargs} with @file{/} as its current directory.
2090
2091To delete other files, for example @file{/u/joeuser/.plan}, you could
2092do this:
2093
2094@example
2095eg$ mkdir '#
2096'
2097eg$ cd '#
2098'
2099eg$ mkdir u u/joeuser u/joeuser/.plan'
2100'
2101eg$ echo > u/joeuser/.plan'
2102/#foo'
2103eg$ cd ..
2104eg$ find . -name '#*' -print | xargs echo
2105./# ./# /u/joeuser/.plan /#foo
2106@end example
2107
2108@node Safe File Name Handling
2109@subsubsection Safe File Name Handling
2110
2111Here is how to make @code{find} output file names so that they can be
2112used by other programs without being mangled or misinterpreted. You
2113can process file names generated this way by giving the @samp{-0} or
2114@samp{--null} option to GNU @code{xargs}, GNU @code{tar}, GNU
2115@code{cpio}, or @code{perl}.
2116
2117@deffn Action -print0
2118True; print the entire file name on the standard output, followed by a
2119null character.
2120@end deffn
2121
2122@deffn Action -fprint0 file
2123True; like @samp{-print0} but write to @var{file} like @samp{-fprint}
2124(@pxref{Print File Name}). The output file is always created.
2125@end deffn
2126
2127As of findutils version 4.2.4, the @code{locate} program also has a
2128@samp{--null} option which does the same thing. For similarity with
2129@code{xargs}, the short form of the option @samp{-0} can also be used.
2130
2131If you want to be able to handle file names safely but need to run
2132commands which want to be connected to a terminal on their input, you
2133can use the @samp{--arg-file} option to @code{xargs} like this:
2134
2135@example
2136find / -name xyzzy -print0 > list
2137xargs --null --arg-file=list munge
2138@end example
2139
2140The example above runs the @code{munge} program on all the files named
2141@file{xyzzy} that we can find, but @code{munge}'s input will still be
2142the terminal (or whatever the shell was using as standard input). If
2143your shell has the ``process substitution'' feature @samp{<(...)}, you
2144can do this in just one step:
2145
2146@example
2147xargs --null --arg-file=<(find / -name xyzzy -print0) munge
2148@end example
2149
2150@node Unusual Characters in File Names
2151@subsubsection Unusual Characters in File Names
2152As discussed above, you often need to be careful about how the names
2153of files are handled by @code{find} and other programs. If the output
2154of @code{find} is not going to another program but instead is being
2155shown on a terminal, this can still be a problem. For example, some
2156character sequences can reprogram the function keys on some terminals.
2157@xref{Security Considerations}, for a discussion of other security
2158problems relating to @code{find}.
2159
2160Unusual characters are handled differently by various
2161actions, as described below.
2162
2163@table @samp
2164@item -print0
2165@itemx -fprint0
2166Always print the exact file name, unchanged, even if the output is
2167going to a terminal.
2168@item -ok
2169@itemx -okdir
2170Always print the exact file name, unchanged. This will probably
2171change in a future release.
2172@item -ls
2173@itemx -fls
2174Unusual characters are always escaped. White space, backslash, and
2175double quote characters are printed using C-style escaping (for
2176example @samp{\f}, @samp{\"}). Other unusual characters are printed
2177using an octal escape. Other printable characters (for @samp{-ls} and
2178@samp{-fls} these are the characters between octal 041 and 0176) are
2179printed as-is.
2180@item -printf
2181@itemx -fprintf
2182If the output is not going to a terminal, it is printed as-is.
2183Otherwise, the result depends on which directive is in use:
2184
2185@table @asis
2186@item %D, %F, %H, %Y, %y
2187These expand to values which are not under control of files' owners,
2188and so are printed as-is.
2189@item %a, %b, %c, %d, %g, %G, %i, %k, %m, %M, %n, %s, %t, %u, %U
2190These have values which are under the control of files' owners but
2191which cannot be used to send arbitrary data to the terminal, and so
2192these are printed as-is.
2193@item %f, %h, %l, %p, %P
2194The output of these directives is quoted if the output is going to a
2195terminal.
2196
2197This quoting is performed in the same way as for GNU @code{ls}. This
2198is not the same quoting mechanism as the one used for @samp{-ls} and
2199@samp{fls}. If you are able to decide what format to use for the
2200output of @code{find} then it is normally better to use @samp{\0} as a
2201terminator than to use newline, as file names can contain white space
2202and newline characters.
2203@end table
2204@item -print
2205@itemx -fprint
2206Quoting is handled in the same way as for the @samp{%p} directive of
2207@samp{-printf} and @samp{-fprintf}. If you are using @code{find} in a
2208script or in a situation where the matched files might have arbitrary
2209names, you should consider using @samp{-print0} instead of
2210@samp{-print}.
2211@end table
2212
2213
2214The @code{locate} program quotes and escapes unusual characters in
2215file names in the same way as @code{find}'s @samp{-print} action.
2216
2217The behaviours described above may change soon, as the treatment of
2218unprintable characters is harmonised for @samp{-ls}, @samp{-fls},
2219@samp{-print}, @samp{-fprint}, @samp{-printf} and @samp{-fprintf}.
2220
2221@node Limiting Command Size
2222@subsubsection Limiting Command Size
2223
2224@code{xargs} gives you control over how many arguments it passes to
2225the command each time it executes it. By default, it uses up to
2226@code{ARG_MAX} - 2k, or 128k, whichever is smaller, characters per
2227command. It uses as many lines and arguments as fit within that
2228limit. The following options modify those values.
2229
2230@table @code
2231@item --no-run-if-empty
2232@itemx -r
2233If the standard input does not contain any nonblanks, do not run the
2234command. By default, the command is run once even if there is no
2235input. This option is a GNU extension.
2236
2237@item --max-lines@r{[}=@var{max-lines}@r{]}
2238@itemx -L @var{max-lines}
2239@itemx -l@r{[}@var{max-lines}@r{]}
2240Use at most @var{max-lines} nonblank input lines per command line;
2241@var{max-lines} defaults to 1 if omitted; omitting the argument is not
2242allowed in the case of the @samp{-L} option. Trailing blanks cause an
2243input line to be logically continued on the next input line, for the
2244purpose of counting the lines. Implies @samp{-x}. The preferred name
2245for this option is @samp{-L} as this is specified by POSIX.
2246
2247@item --max-args=@var{max-args}
2248@itemx -n @var{max-args}
2249Use at most @var{max-args} arguments per command line. Fewer than
2250@var{max-args} arguments will be used if the size (see the @samp{-s}
2251option) is exceeded, unless the @samp{-x} option is given, in which
2252case @code{xargs} will exit.
2253
2254@item --max-chars=@var{max-chars}
2255@itemx -s @var{max-chars}
2256Use at most @var{max-chars} characters per command line, including the
2257command initial arguments and the terminating nulls at the ends of the
2258argument strings. If you specify a value for this option which is too
2259large or small, a warning message is printed and the appropriate upper
2260or lower limit is used instead. You can use @samp{--show-limits}
2261option to understand the command-line limits applying to @code{xargs}
2262and how this is affected by any other options.
2263
2264@item --max-procs=@var{max-procs}
2265@itemx -P @var{max-procs}
2266Run up to @var{max-procs} processes at a time; the default is 1. If
2267@var{max-procs} is 0, @code{xargs} will run as many processes as
2268possible at a time. Use the @samp{-n}, @samp{-s}, or @samp{-L} option
2269with @samp{-P}; otherwise chances are that the command will be run
2270only once.
2271@end table
2272
2273@node Interspersing File Names
2274@subsubsection Interspersing File Names
2275
2276@code{xargs} can insert the name of the file it is processing between
2277arguments you give for the command. Unless you also give options to
2278limit the command size (@pxref{Limiting Command Size}), this mode of
2279operation is equivalent to @samp{find -exec} (@pxref{Single File}).
2280
2281@table @code
2282@item --replace@r{[}=@var{replace-str}@r{]}
2283@itemx -I @var{replace-str}
2284@itemx -i @var{replace-str}
2285Replace occurrences of @var{replace-str} in the initial arguments with
2286names read from the input. Also, unquoted blanks do not terminate
2287arguments; instead, the input is split at newlines only. For the
2288@samp{-i} option, if @var{replace-str} is omitted for @samp{--replace}
2289or @samp{-i}, it defaults to @samp{@{@}} (like for @samp{find -exec}).
2290Implies @samp{-x} and @samp{-l 1}. @samp{-i} is deprecated in favour
2291of @samp{-I}. As an example, to sort each file in the @file{bills}
2292directory, leaving the output in that file name with @file{.sorted}
2293appended, you could do:
2294
2295@example
2296find bills -type f | xargs -I XX sort -o XX.sorted XX
2297@end example
2298
2299@noindent
2300The equivalent command using @samp{find -execdir} is:
2301
2302@example
2303find bills -type f -execdir sort -o '@{@}.sorted' '@{@}' ';'
2304@end example
2305@end table
2306
2307
2308When you use the @samp{-I} option, each line read from the input is
2309buffered internally. This means that there is an upper limit on the
2310length of input line that xargs will accept when used with the
2311@samp{-I} option. To work around this limitation, you can use the
2312@samp{-s} option to increase the amount of buffer space that xargs
2313uses, and you can also use an extra invocation of xargs to ensure that
2314very long lines do not occur. For example:
2315
2316@example
2317somecommand | xargs -s 50000 echo | xargs -I '@{@}' -s 100000 rm '@{@}'
2318@end example
2319
2320Here, the first invocation of @code{xargs} has no input line length
2321limit because it doesn't use the @samp{-I} option. The second
2322invocation of @code{xargs} does have such a limit, but we have ensured
2323that the it never encounters a line which is longer than it can
2324handle.
2325
2326This is not an ideal solution. Instead, the @samp{-I} option should
2327not impose a line length limit (apart from any limit imposed by the
2328operating system) and so one might consider this limitation to be a
2329bug. A better solution would be to allow @code{xargs -I} to
2330automatically move to a larger value for the @samp{-s} option when
2331this is needed.
2332
2333This sort of problem doesn't occur with the output of @code{find}
2334because it emits just one filename per line.
2335
2336@node Querying
2337@subsection Querying
2338
2339To ask the user whether to execute a command on a single file, you can
2340use the @code{find} primary @samp{-okdir} instead of @samp{-execdir},
2341and the @code{find} primary @samp{-ok} instead of @samp{-exec}:
2342
2343@deffn Action -okdir command ;
2344Like @samp{-execdir} (@pxref{Single File}), but ask the user first (on
2345the standard input); if the response does not start with @samp{y} or
2346@samp{Y}, do not run the command, and return false. If the command is
2347run, its standard input is redirected from @file{/dev/null}.
2348@end deffn
2349
2350@deffn Action -ok command ;
2351This insecure variant of the @samp{-okdir} action is specified by
2352POSIX. The main difference is that the command is executed in the
2353directory from which @code{find} was invoked, meaning that @samp{@{@}}
2354is expanded to a relative path starting with the name of one of the
2355starting directories, rather than just the basename of the matched
2356file. If the command is run, its standard input is redirected from
2357@file{/dev/null}.
2358@end deffn
2359
2360When processing multiple files with a single command, to query the
2361user you give @code{xargs} the following option. When using this
2362option, you might find it useful to control the number of files
2363processed per invocation of the command (@pxref{Limiting Command
2364Size}).
2365
2366@table @code
2367@item --interactive
2368@itemx -p
2369Prompt the user about whether to run each command line and read a line
2370from the terminal. Only run the command line if the response starts
2371with @samp{y} or @samp{Y}. Implies @samp{-t}.
2372@end table
2373
2374@node Delete Files
2375@section Delete Files
2376
2377@deffn Action -delete
2378Delete files or directories; true if removal succeeded. If the
2379removal failed, an error message is issued.
2380
2381The use of the @samp{-delete} action on the command line automatically
2382turns on the @samp{-depth} option (@pxref{find Expressions}).
2383@end deffn
2384
2385@node Adding Tests
2386@section Adding Tests
2387
2388You can test for file attributes that none of the @code{find} builtin
2389tests check. To do this, use @code{xargs} to run a program that
2390filters a list of files printed by @code{find}. If possible, use
2391@code{find} builtin tests to pare down the list, so the program run by
2392@code{xargs} has less work to do. The tests builtin to @code{find}
2393will likely run faster than tests that other programs perform.
2394
2395For reasons of efficiency it is often useful to limit the number of
2396times an external program has to be run. For this reason, it is often
2397a good idea to implement ``extended'' tests by using @code{xargs}.
2398
2399For example, here is a way to print the names of all of the unstripped
2400binaries in the @file{/usr/local} directory tree. Builtin tests avoid
2401running @code{file} on files that are not regular files or are not
2402executable.
2403
2404@example
2405find /usr/local -type f -perm /a=x | xargs file |
2406 grep 'not stripped' | cut -d: -f1
2407@end example
2408
2409@noindent
2410The @code{cut} program removes everything after the file name from the
2411output of @code{file}.
2412
2413However, using @code{xargs} can present important security problems
2414(@pxref{Security Considerations}). These can be avoided by using
2415@samp{-execdir}. The @samp{-execdir} action is also a useful way of
2416putting your own test in the middle of a set of other tests or actions
2417for @code{find} (for example, you might want to use @samp{-prune}).
2418
2419@c Idea from Martin Weitzel.
2420To place a special test somewhere in the middle of a @code{find}
2421expression, you can use @samp{-execdir} (or, less securely,
2422@samp{-exec}) to run a program that performs the test. Because
2423@samp{-execdir} evaluates to the exit status of the executed program,
2424you can use a program (which can be a shell script) that tests for a
2425special attribute and make it exit with a true (zero) or false
2426(non-zero) status. It is a good idea to place such a special test
2427@emph{after} the builtin tests, because it starts a new process which
2428could be avoided if a builtin test evaluates to false.
2429
2430Here is a shell script called @code{unstripped} that checks whether
2431its argument is an unstripped binary file:
2432
2433@example
2434#! /bin/sh
2435file "$1" | grep -q "not stripped"
2436@end example
2437
2438
2439This script relies on the shell exiting with the status of
2440the last command in the pipeline, in this case @code{grep}. The
2441@code{grep} command exits with a true status if it found any matches,
2442false if not. Here is an example of using the script (assuming it is
2443in your search path). It lists the stripped executables (and shell
2444scripts) in the file @file{sbins} and the unstripped ones in
2445@file{ubins}.
2446
2447@example
2448find /usr/local -type f -perm /a=x \
2449 \( -execdir unstripped '@{@}' \; -fprint ubins -o -fprint sbins \)
2450@end example
2451
2452
2453@node Databases, File Permissions, Actions, Top
2454@chapter File Name Databases
2455
2456The file name databases used by @code{locate} contain lists of files
2457that were in particular directory trees when the databases were last
2458updated. The file name of the default database is determined when
2459@code{locate} and @code{updatedb} are configured and installed. The
2460frequency with which the databases are updated and the directories for
2461which they contain entries depend on how often @code{updatedb} is run,
2462and with which arguments.
2463
2464You can obtain some statistics about the databases by using
2465@samp{locate --statistics}.
2466
2467@menu
2468* Database Locations::
2469* Database Formats::
2470* Newline Handling::
2471@end menu
2472
2473
2474@node Database Locations
2475@section Database Locations
2476
2477There can be multiple file name databases. Users can select which
2478databases @code{locate} searches using the @code{LOCATE_PATH}
2479environment variable or a command line option. The system
2480administrator can choose the file name of the default database, the
2481frequency with which the databases are updated, and the directories
2482for which they contain entries. File name databases are updated by
2483running the @code{updatedb} program, typically nightly.
2484
2485In networked environments, it often makes sense to build a database at
2486the root of each filesystem, containing the entries for that
2487filesystem. @code{updatedb} is then run for each filesystem on the
2488fileserver where that filesystem is on a local disk, to prevent
2489thrashing the network.
2490
2491@xref{Invoking updatedb},
2492for the description of the options to @code{updatedb}, which specify
2493which directories would each database contain entries for.
2494
2495
2496@node Database Formats
2497@section Database Formats
2498
2499The file name databases contain lists of files that were in particular
2500directory trees when the databases were last updated. The file name
2501database format changed starting with GNU @code{locate} version 4.0 to
2502allow machines with different byte orderings to share the databases.
2503The new GNU @code{locate} can read both the old and new database
2504formats. However, old versions of @code{locate} and @code{find}
2505produce incorrect results if given a new-format database.
2506
2507Support for the old database format will eventually be discontinued,
2508first in @code{updatedb} and later in @code{xargs}.
2509
2510If you run @samp{locate --statistics}, the resulting summary indicates
2511the type of each @code{locate} database.
2512
2513
2514@menu
2515* New Database Format::
2516* Sample Database::
2517* Old Database Format::
2518@end menu
2519
2520@node New Database Format
2521@subsection New Database Format
2522
2523@code{updatedb} runs a program called @code{frcode} to
2524@dfn{front-compress} the list of file names, which reduces the
2525database size by a factor of 4 to 5. Front-compression (also known as
2526incremental encoding) works as follows.
2527
2528The database entries are a sorted list (case-insensitively, for users'
2529convenience). Since the list is sorted, each entry is likely to share
2530a prefix (initial string) with the previous entry. Each database
2531entry begins with an offset-differential count byte, which is the
2532additional number of characters of prefix of the preceding entry to
2533use beyond the number that the preceding entry is using of its
2534predecessor. (The counts can be negative.) Following the count is a
2535null-terminated ASCII remainder---the part of the name that follows
2536the shared prefix.
2537
2538If the offset-differential count is larger than can be stored in a
2539byte (+/-127), the byte has the value 0x80 and the count follows in a
25402-byte word, with the high byte first (network byte order).
2541
2542Every database begins with a dummy entry for a file called
2543@file{LOCATE02}, which @code{locate} checks for to ensure that the
2544database file has the correct format; it ignores the entry in doing
2545the search.
2546
2547Databases cannot be concatenated together, even if the first (dummy)
2548entry is trimmed from all but the first database. This is because the
2549offset-differential count in the first entry of the second and
2550following databases will be wrong.
2551
2552In the output of @samp{locate --statistics}, the new database format
2553is referred to as @samp{LOCATE02}.
2554
2555@node Sample Database
2556@subsection Sample Database
2557
2558Sample input to @code{frcode}:
2559@c with nulls changed to newlines:
2560
2561@example
2562/usr/src
2563/usr/src/cmd/aardvark.c
2564/usr/src/cmd/armadillo.c
2565/usr/tmp/zoo
2566@end example
2567
2568Length of the longest prefix of the preceding entry to share:
2569
2570@example
25710 /usr/src
25728 /cmd/aardvark.c
257314 rmadillo.c
25745 tmp/zoo
2575@end example
2576
2577Output from @code{frcode}, with trailing nulls changed to newlines
2578and count bytes made printable:
2579
2580@example
25810 LOCATE02
25820 /usr/src
25838 /cmd/aardvark.c
25846 rmadillo.c
2585-9 tmp/zoo
2586@end example
2587
2588(6 = 14 - 8, and -9 = 5 - 14)
2589
2590@node Old Database Format
2591@subsection Old Database Format
2592
2593The old database format is used by Unix @code{locate} and @code{find}
2594programs and earlier releases of the GNU ones. @code{updatedb}
2595produces this format if given the @samp{--old-format} option.
2596
2597@code{updatedb} runs programs called @code{bigram} and @code{code} to
2598produce old-format databases. The old format differs from the new one
2599in the following ways. Instead of each entry starting with an
2600offset-differential count byte and ending with a null, byte values
2601from 0 through 28 indicate offset-differential counts from -14 through
260214. The byte value indicating that a long offset-differential count
2603follows is 0x1e (30), not 0x80. The long counts are stored in host
2604byte order, which is not necessarily network byte order, and host
2605integer word size, which is usually 4 bytes. They also represent a
2606count 14 less than their value. The database lines have no
2607termination byte; the start of the next line is indicated by its first
2608byte having a value <= 30.
2609
2610In addition, instead of starting with a dummy entry, the old database
2611format starts with a 256 byte table containing the 128 most common
2612bigrams in the file list. A bigram is a pair of adjacent bytes.
2613Bytes in the database that have the high bit set are indexes (with the
2614high bit cleared) into the bigram table. The bigram and
2615offset-differential count coding makes these databases 20-25% smaller
2616than the new format, but makes them not 8-bit clean. Any byte in a
2617file name that is in the ranges used for the special codes is replaced
2618in the database by a question mark, which not coincidentally is the
2619shell wildcard to match a single character.
2620
2621The old format therefore cannot faithfully store entries with
2622non-ASCII characters. It therefore should not be used in
2623internationalised environments.
2624
2625The output of @samp{locate --statistics} will give an incorrect count
2626of the number of file names containing newlines or high-bit characters
2627for old-format databases.
2628
2629@node Newline Handling
2630@section Newline Handling
2631
2632Within the database, file names are terminated with a null character.
2633This is the case for both the old and the new format.
2634
2635When the new database format is being used, the compression technique
2636used to generate the database though relies on the ability to sort the
2637list of files before they are presented to @code{frcode}.
2638
2639If the system's sort command allows its input list of files to be
2640separated with null characters via the @samp{-z} option, this option
2641is used and therefore @code{updatedb} and @code{locate} will both
2642correctly handle file names containing newlines. If the @code{sort}
2643command lacks support for this, the list of files is delimited with
2644the newline character, meaning that parts of file names containing
2645newlines will be incorrectly sorted. This can result in both
2646incorrect matches and incorrect failures to match.
2647
2648On the other hand, if you are using the old database format, file
2649names with embedded newlines are not correctly handled. There is no
2650technical limitation which enforces this, it's just that the
2651@code{bigram} program has not been updated to support lists of file
2652names separated by nulls.
2653
2654So, if you are using the new database format (this is the default) and
2655your system uses GNU @code{sort}, newlines will be correctly handled
2656at all times. Otherwise, newlines may not be correctly handled.
2657
2658@node File Permissions, Reference, Databases, Top
2659@chapter File Permissions
2660
2661@include perm.texi
2662
2663@node Reference, Common Tasks, File Permissions, Top
2664@chapter Reference
2665
2666Below are summaries of the command line syntax for the programs
2667discussed in this manual.
2668
2669@menu
2670* Invoking find::
2671* Invoking locate::
2672* Invoking updatedb::
2673* Invoking xargs::
2674* Regular Expressions::
2675* Environment Variables::
2676@end menu
2677
2678@node Invoking find, Invoking locate, , Reference
2679@section Invoking @code{find}
2680
2681@example
2682find @r{[-H] [-L] [-P] [-D @var{debugoptions}] [-O@var{level}]} @r{[}@var{file}@dots{}@r{]} @r{[}@var{expression}@r{]}
2683@end example
2684
2685@code{find} searches the directory tree rooted at each file name
2686@var{file} by evaluating the @var{expression} on each file it finds in
2687the tree.
2688
2689The command line may begin with the @samp{-H}, @samp{-L}, @samp{-P},
2690@samp{-D} and @samp{-O} options. These are followed by a list of
2691files or directories that should be searched. If no files to search
2692are specified, the current directory (@file{.}) is used.
2693
2694This list of files to search is followed by a list of expressions
2695describing the files we wish to search for. The first part of the
2696expression is recognised by the fact that it begins with @samp{-}
2697followed by some other letters (for example @samp{-print}), or is
2698either @samp{(} or @samp{!}. Any arguments after it are the rest of
2699the expression.
2700
2701If no expression is given, the expression @samp{-print} is used.
2702
2703The @code{find} command exits with status zero if all files matched
2704are processed successfully, greater than zero if errors occur.
2705
2706The @code{find} program also recognises two options for administrative
2707use:
2708
2709@table @samp
2710@item --help
2711Print a summary of the command line usage and exit.
2712@item --version
2713Print the version number of @code{find} and exit.
2714@end table
2715
2716The @samp{-version} option is a synonym for @samp{--version}
2717
2718
2719@menu
2720* Filesystem Traversal Options::
2721* Warning Messages::
2722* Optimisation Options::
2723* Debug Options::
2724* Find Expressions::
2725@end menu
2726
2727@node Filesystem Traversal Options, Warning Messages,, Invoking find
2728@subsection Filesystem Traversal Options
2729
2730The options @samp{-H}, @samp{-L} or @samp{-P} may be specified at the
2731start of the command line (if none of these is specified, @samp{-P} is
2732assumed). If you specify more than one of these options, the last one
2733specified takes effect (but note that the @samp{-follow} option is
2734equivalent to @samp{-L}).
2735
2736@table @code
2737@item -P
2738Never follow symbolic links (this is the default), except in the case
2739of the @samp{-xtype} predicate.
2740@item -L
2741Always follow symbolic links, except in the case of the @samp{-xtype}
2742predicate.
2743@item -H
2744Follow symbolic links specified in the list of files to search, or
2745which are otherwise specified on the command line.
2746@end table
2747
2748If @code{find} would follow a symbolic link, but cannot for any reason
2749(for example, because it has insufficient permissions or the link is
2750broken), it falls back on using the properties of the symbolic link
2751itself. @ref{Symbolic Links} for a more complete description of how
2752symbolic links are handled.
2753
2754@node Warning Messages, Optimisation Options, Filesystem Traversal Options, Invoking find
2755@subsection Warning Messages
2756
2757If there is an error on the @code{find} command line, an error message
2758is normally issued. However, there are some usages that are
2759inadvisable but which @code{find} should still accept. Under these
2760circumstances, @code{find} may issue a warning message. By default,
2761warnings are enabled only if @code{find} is being run interactively
2762(specifically, if the standard input is a terminal). Warning messages
2763can be controlled explicitly by the use of options on the command
2764line:
2765
2766@table @code
2767@item -warn
2768Issue warning messages where appropriate.
2769@item -nowarn
2770Do not issue warning messages.
2771@end table
2772
2773These options take effect at the point on the command line where they
2774are specified. Therefore it's not useful to specify @samp{-nowarn} at
2775the end of the command line. The warning messages affected by the
2776above options are triggered by:
2777
2778@itemize @minus
2779@item
2780Use of the @samp{-d} option which is deprecated; please use
2781@samp{-depth} instead, since the latter is POSIX-compliant.
2782@item
2783Use of the @samp{-ipath} option which is deprecated; please use
2784@samp{-iwholename} instead.
2785@item
2786Specifying an option (for example @samp{-mindepth}) after a non-option
2787(for example @samp{-type} or @samp{-print}) on the command line.
2788@end itemize
2789
2790The default behaviour above is designed to work in that way so that
2791existing shell scripts don't generate spurious errors, but people will
2792be made aware of the problem.
2793
2794Some warning messages are issued for less common or more serious
2795problems, and consequently cannot be turned off:
2796
2797@itemize @minus
2798@item
2799Use of an unrecognised backslash escape sequence with @samp{-fprintf}
2800@item
2801Use of an unrecognised formatting directive with @samp{-fprintf}
2802@end itemize
2803
2804@node Optimisation Options, Debug Options, Warning Messages, Invoking find
2805@subsection Optimisation Options
2806
2807The @samp{-O@var{level}} option sets @code{find}'s optimisation level
2808to @var{level}. The default optimisation level is 1.
2809
2810At certain optimisation levels, @code{find} reorders tests to speed up
2811execution while preserving the overall effect; that is, predicates
2812with side effects are not reordered relative to each other. The
2813optimisations performed at each optimisation level are as follows.
2814
2815@table @samp
2816@item 0
2817Currently equivalent to optimisation level 1.
2818
2819@item 1
2820This is the default optimisation level and corresponds to the
2821traditional behaviour. Expressions are reordered so that tests based
2822only on the names of files (for example@samp{ -name} and
2823@samp{-regex}) are performed first.
2824
2825@item 2
2826Any @samp{-type} or @samp{-xtype} tests are performed after any tests
2827based only on the names of files, but before any tests that require
2828information from the inode. On many modern versions of Unix, file
2829types are returned by @code{readdir()} and so these predicates are
2830faster to evaluate than predicates which need to stat the file first.
2831
2832@item 3
2833At this optimisation level, the full cost-based query optimiser is
2834enabled. The order of tests is modified so that cheap (i.e. fast)
2835tests are performed first and more expensive ones are performed later,
2836if necessary. Within each cost band, predicates are evaluated earlier
2837or later according to whether they are likely to succeed or not. For
2838@samp{-o}, predicates which are likely to succeed are evaluated
2839earlier, and for @samp{-a}, predicates which are likely to fail are
2840evaluated earlier.
2841@end table
2842
2843
2844@node Debug Options, Find Expressions, Optimisation Options, Invoking find
2845@subsection Debug Options
2846
2847The @samp{-D} option makes @code{find} produce diagnostic output.
2848Much of the information is useful only for diagnosing problems, and so
2849most people will not find this option helpful.
2850
2851The list of debug options should be comma separated. Compatibility of
2852the debug options is not guaranteed between releases of findutils.
2853For a complete list of valid debug options, see the output of
2854@code{find -D help}. Valid debug options include:
2855@table @samp
2856@item help
2857Explain the debugging options.
2858@item tree
2859Show the expression tree in its original and optimised form.
2860@item stat
2861Print messages as files are examined with the stat and lstat system
2862calls. The find program tries to minimise such calls.
2863@item opt
2864Prints diagnostic information relating to the optimisation of the
2865expression tree; see the @samp{-O} option.
2866@end table
2867
2868@node Find Expressions,, Debug Options, Invoking find
2869@subsection Find Expressions
2870
2871The final part of the @code{find} command line is a list of
2872expressions. @xref{Primary Index}, for a summary of all of the tests,
2873actions, and options that the expression can contain. If the
2874expression is missing, @samp{-print} is assumed.
2875
2876@node Invoking locate, Invoking updatedb, Invoking find, Reference
2877@section Invoking @code{locate}
2878
2879@example
2880locate @r{[}@var{option}@dots{}@r{]} @var{pattern}@dots{}
2881@end example
2882
2883For each @var{pattern} given @code{locate} searches one or more file
2884name databases returning each match of @var{pattern}.
2885
2886For each @var{pattern} given @code{locate} searches one or more file
2887name databases returning each match of @var{pattern}.
2888
2889@table @code
2890@item --all
2891@itemx -A
2892Print only names which match all non-option arguments, not those
2893matching one or more non-option arguments.
2894
2895@item --basename
2896@itemx -b
2897The specified pattern is matched against just the last component of
2898the name of a file in the @code{locate} database. This last
2899component is also called the ``base name''. For example, the base
2900name of @file{/tmp/mystuff/foo.old.c} is @file{foo.old.c}. If the
2901pattern contains metacharacters, it must match the base name exactly.
2902If not, it must match part of the base name.
2903
2904@item --count
2905@itemx -c
2906Instead of printing the matched file names, just print the total
2907number of matches found, unless @samp{--print} (@samp{-p}) is also
2908present.
2909
2910
2911@item --database=@var{path}
2912@itemx -d @var{path}
2913Instead of searching the default @code{locate} database, @code{locate} search the file
2914name databases in @var{path}, which is a colon-separated list of
2915database file names. You can also use the environment variable
2916@code{LOCATE_PATH} to set the list of database files to search. The
2917option overrides the environment variable if both are used. Empty
2918elements in @var{path} (that is, a leading or trailing colon, or two
2919colons in a row) are taken to stand for the default database.
2920A database can be supplied on stdin, using @samp{-} as an element
2921of @samp{path}. If more than one element of @samp{path} is @samp{-},
2922later instances are ignored (but a warning message is printed).
2923
2924@item --existing
2925@itemx -e
2926Only print out such names which currently exist (instead of such names
2927which existed when the database was created). Note that this may slow
2928down the program a lot, if there are many matches in the database.
2929The way in which broken symbolic links are treated is affected by the
2930@samp{-L}, @samp{-P} and @samp{-H} options. Please note that it is
2931possible for the file to be deleted after @code{locate} has checked
2932that it exists, but before you use it.
2933
2934@item --non-existing
2935@itemx -E
2936Only print out such names which currently do not exist (instead of
2937such names which existed when the database was created). Note that
2938this may slow down the program a lot, if there are many matches in the
2939database. The way in which broken symbolic links are treated is
2940affected by the @samp{-L}, @samp{-P} and @samp{-H} options. Please
2941note that @code{locate} checks that the file does not exist, but a
2942file of the same name might be created after @code{locate}'s check but
2943before you read @code{locate}'s output.
2944
2945@item --follow
2946@itemx -L
2947If testing for the existence of files (with the @samp{-e} or @samp{-E}
2948options), consider broken symbolic links to be non-existing. This is
2949the default behaviour.
2950
2951@item --nofollow
2952@itemx -P
2953@itemx -H
2954If testing for the existence of files (with the @samp{-e} or @samp{-E}
2955options), treat broken symbolic links as if they were existing files.
2956The @samp{-H} form of this option is provided purely for similarity
2957with @code{find}; the use of @samp{-P} is recommended over @samp{-H}.
2958
2959@item --ignore-case
2960@itemx -i
2961Ignore case distinctions in both the pattern and the file names.
2962
2963@item --limit=N
2964@itemx -l N
2965Limit the number of results printed to N. When used with the
2966@samp{--count} option, the value printed will never be larger than
2967this limit.
2968
2969@item --mmap
2970@itemx -m
2971Accepted but does nothing. The option is supported only to provide
2972compatibility with BSD's @code{locate}.
2973
2974@item --null
2975@itemx -0
2976Results are separated with the ASCII NUL character rather than the
2977newline character. To get the full benefit of the use of this option,
2978use the new @code{locate} database format (that is the default
2979anyway).
2980
2981@item --print
2982@itemx -p
2983Print search results when they normally would not, because of the
2984presence of @samp{--statistics} (@samp{-S}) or @samp{--count}
2985(@samp{-c}).
2986
2987@item --wholename
2988@itemx -w
2989The specified pattern is matched against the whole name of the file in
2990the @code{locate} database. If the pattern contains metacharacters,
2991it must match exactly. If not, it must match part of the whole file
2992name. This is the default behaviour.
2993
2994@item --regex
2995@itemx -r
2996Instead of using substring or shell glob matching, the pattern
2997specified on the command line is understood to be a regular
2998expression. GNU Emacs-style regular expressions are assumed unless
2999the @samp{--regextype} option is also given. File names from the
3000@code{locate} database are matched using the specified regular
3001expression. If the @samp{-i} flag is also given, matching is
3002case-insensitive. Matches are performed against the whole path name,
3003and so by default a pathname will be matched if any part of it matches
3004the specified regular expression. The regular expression may use
3005@samp{^} or @samp{$} to anchor a match at the beginning or end of a
3006pathname.
3007
3008@item --regextype
3009This option changes the regular expression syntax and behaviour used
3010by the @samp{--regex} option. @ref{Regular Expressions} for more
3011information on the regular expression dialects understood by GNU
3012findutils.
3013
3014@item --stdio
3015@itemx -s
3016Accepted but does nothing. The option is supported only to provide
3017compatibility with BSD's @code{locate}.
3018
3019@item --statistics
3020@itemx -S
3021Print some summary information for each @code{locate} database. No
3022search is performed unless non-option arguments are given.
3023Although the BSD version of locate also has this option, the format of the
3024output is different.
3025
3026@item --help
3027Print a summary of the command line usage for @code{locate} and exit.
3028
3029@item --version
3030Print the version number of @code{locate} and exit.
3031@end table
3032
3033@node Invoking updatedb, Invoking xargs, Invoking locate, Reference
3034@section Invoking @code{updatedb}
3035
3036@example
3037updatedb @r{[}@var{option}@dots{}@r{]}
3038@end example
3039
3040@code{updatedb} creates and updates the database of file names used by
3041@code{locate}. @code{updatedb} generates a list of files similar to
3042the output of @code{find} and then uses utilities for optimizing the
3043database for performance. @code{updatedb} is often run periodically
3044as a @code{cron} job and configured with environment variables or
3045command options. Typically, operating systems have a shell script
3046that ``exports'' configurations for variable definitions and uses
3047another schell script that ``sources'' the configuration file into the
3048environment and then executes @code{updatedb} in the environment.
3049
3050@code{updatedb} creates and updates the database of file names used by
3051@code{locate}. @code{updatedb} generates a list of files similar to
3052the output of @code{find} and then uses utilities for optimizing the
3053database for performance. @code{updatedb} is often run periodically
3054as a @code{cron} job and configured with environment variables or
3055command options. Typically, operating systems have a shell script
3056that ``exports'' configurations for variable definitions and uses
3057another schell script that ``sources'' the configuration file into the
3058environment and then executes @code{updatedb} in the environment.
3059
3060@table @code
3061@item --findoptions='@var{OPTION}@dots{}'
3062Global options to pass on to @code{find}.
3063The environment variable @code{FINDOPTIONS} also sets this value.
3064Default is none.
3065
3066@item --localpaths='@var{path}@dots{}'
3067Non-network directories to put in the database.
3068Default is @file{/}.
3069
3070@item --netpaths='@var{path}@dots{}'
3071Network (NFS, AFS, RFS, etc.) directories to put in the database.
3072The environment variable @code{NETPATHS} also sets this value.
3073Default is none.
3074
3075@item --prunepaths='@var{path}@dots{}'
3076Directories to omit from the database, which would otherwise be
3077included. The environment variable @code{PRUNEPATHS} also sets this
3078value. Default is @file{/tmp /usr/tmp /var/tmp /afs}. The paths are
3079used as regular expressions (with @code{find ... -regex}, so you need
3080to specify these paths in the same way that @code{find} will encounter
3081them. This means for example that the paths must not include trailing
3082slashes.
3083
3084@item --prunefs='@var{path}@dots{}'
3085Filesystems to omit from the database, which would otherwise be
3086included. Note that files are pruned when a filesystem is reached;
3087Any filesystem mounted under an undesired filesystem will be ignored.
3088The environment variable @code{PRUNEFS} also sets this value. Default
3089is @file{nfs NFS proc}.
3090
3091@item --output=@var{dbfile}
3092The database file to build. Default is system-dependent, but
3093typically @file{/usr/local/var/locatedb}.
3094
3095@item --localuser=@var{user}
3096The user to search the non-network directories as, using @code{su}.
3097Default is to search the non-network directories as the current user.
3098You can also use the environment variable @code{LOCALUSER} to set this user.
3099
3100@item --netuser=@var{user}
3101The user to search network directories as, using @code{su}. Default
3102@code{user} is @code{daemon}. You can also use the environment variable
3103@code{NETUSER} to set this user.
3104
3105@item --old-format
3106Generate a @code{locate} database in the old format, for compatibility
3107with versions of @code{locate} other than GNU @code{locate}. Using
3108this option means that @code{locate} will not be able to properly
3109handle non-ASCII characters in file names (that is, file names
3110containing characters which have the eighth bit set, such as many of
3111the characters from the ISO-8859-1 character set).
3112@item --help
3113Print a summary of the command line usage and exit.
3114@item --version
3115Print the version number of @code{updatedb} and exit.
3116@end table
3117
3118@node Invoking xargs, Regular Expressions, Invoking updatedb, Reference
3119@section Invoking @code{xargs}
3120
3121@example
3122xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
3123@end example
3124
3125@code{xargs} exits with the following status:
3126
3127@table @asis
3128@item 0
3129if it succeeds
3130@item 123
3131if any invocation of the command exited with status 1-125
3132@item 124
3133if the command exited with status 255
3134@item 125
3135if the command is killed by a signal
3136@item 126
3137if the command cannot be run
3138@item 127
3139if the command is not found
3140@item 1
3141if some other error occurred.
3142@end table
3143
3144Exit codes greater than 128 are used by the shell to indicate that
3145a program died due to a fatal signal.
3146
3147@table @code
3148@item --arg-file@r{=@var{inputfile}}
3149@itemx -a o@r{@var{inputfile}}
3150Read names from the file @var{inputfile} instead of standard input.
3151If you use this option, the standard input stream remains unchanged
3152when commands are run. Otherwise, stdin is redirected from
3153@file{/dev/null}.
3154
3155@item --null
3156@itemx -0
3157Input file names are terminated by a null character instead of by
3158whitespace, and any quotes and backslash characters are not considered
3159special (every character is taken literally). Disables the end of
3160file string, which is treated like any other argument.
3161
3162@item --delimiter @var{delim}
3163@itemx -d @var{delim}
3164
3165Input file names are terminated by the specified character @var{delim}
3166instead of by whitespace, and any quotes and backslash characters are
3167not considered special (every character is taken literally). Disables
3168the end of file string, which is treated like any other argument.
3169
3170The specified delimiter may be a single character, a C-style character
3171escape such as @samp{\n}, or an octal or hexadecimal escape code.
3172Octal and hexadecimal escape codes are understood as for the
3173@code{printf} command. Multibyte characters are not supported.
3174
3175
3176@item -E @var{eof-str}
3177@itemx --eof@r{[}=@var{eof-str}@r{]}
3178@itemx -e@r{[}@var{eof-str}@r{]}
3179Set the end of file string to @var{eof-str}. If the end of file
3180string occurs as a line of input, the rest of the input is ignored.
3181If @var{eof-str} is omitted (@samp{-e}) or blank (either @samp{-e} or
3182@samp{-E}), there is no end of file string. The @samp{-e} form of
3183this option is deprecated in favour of the POSIX-compliant @samp{-E}
3184option, which you should use instead. As of GNU xargs version 4.2.9,
3185the default behaviour of xargs is not to have a logical end-of-file
3186marker. The POSIX standard (IEEE Std 1003.1, 2004 Edition) allows
3187this.
3188
3189@item --help
3190Print a summary of the options to @code{xargs} and exit.
3191
3192@item -I @var{replace-str}
3193@itemx --replace@r{[}=@var{replace-str}@r{]}
3194@itemx -i@r{[}@var{replace-str}@r{]}
3195Replace occurrences of @var{replace-str} in the initial arguments with
3196names read from standard input. Also, unquoted blanks do not
3197terminate arguments; instead, the input is split at newlines only. If
3198@var{replace-str} is omitted (omitting it is allowed only for
3199@samp{-i}), it defaults to @samp{@{@}} (like for @samp{find -exec}).
3200Implies @samp{-x} and @samp{-l 1}. The @samp{-i} option is deprecated
3201in favour of the @samp{-I} option.
3202
3203@item -L @var{max-lines}
3204@itemx --max-lines@r{[}=@var{max-lines}@r{]}
3205@itemx -l@r{[}@var{max-lines}@r{]}
3206Use at most @var{max-lines} non-blank input lines per command line.
3207For @samp{-l}, @var{max-lines} defaults to 1 if omitted. For
3208@samp{-L}, the argument is mandatory. Trailing blanks cause an input
3209line to be logically continued on the next input line, for the purpose
3210of counting the lines. Implies @samp{-x}. The @samp{-l} form of this
3211option is deprecated in favour of the POSIX-compliant @samp{-L}
3212option.
3213
3214@item --max-args=@var{max-args}
3215@itemx -n @var{max-args}
3216Use at most @var{max-args} arguments per command line. Fewer than
3217@var{max-args} arguments will be used if the size (see the @samp{-s}
3218option) is exceeded, unless the @samp{-x} option is given, in which
3219case @code{xargs} will exit.
3220
3221@item --interactive
3222@itemx -p
3223Prompt the user about whether to run each command line and read a line
3224from the terminal. Only run the command line if the response starts
3225with @samp{y} or @samp{Y}. Implies @samp{-t}.
3226
3227@item --no-run-if-empty
3228@itemx -r
3229If the standard input is completely empty, do not run the
3230command. By default, the command is run once even if there is no
3231input.
3232
3233@item --max-chars=@var{max-chars}
3234@itemx -s @var{max-chars}
3235Use at most @var{max-chars} characters per command line, including the
3236command, initial arguments and any terminating nulls at the ends of
3237the argument strings.
3238
3239@item --show-limits
3240Display the limits on the command-line length which are imposed by the
3241operating system, @code{xargs}' choice of buffer size and the
3242@samp{-s} option. Pipe the input from @file{/dev/null} (and perhaps
3243specify @samp{--no-run-if-empty}) if you don't want @code{xargs} to do
3244anything.
3245
3246@item --verbose
3247@itemx -t
3248Print the command line on the standard error output before executing
3249it.
3250
3251@item --version
3252Print the version number of @code{xargs} and exit.
3253
3254@item --exit
3255@itemx -x
3256Exit if the size (see the @samp{-s} option) is exceeded.
3257
3258
3259@item --max-procs=@var{max-procs}
3260@itemx -P @var{max-procs}
3261Run simultaneously up to @var{max-procs} processes at once; the default is 1. If
3262@var{max-procs} is 0, @code{xargs} will run as many processes as
3263possible simultaneously.
3264@end table
3265
3266
3267@node Regular Expressions, Environment Variables, Invoking xargs, Reference
3268@section Regular Expressions
3269
3270The @samp{-regex} and @samp{-iregex} tests of @code{find} allow
3271matching by regular expression, as does the @samp{--regex} option of
3272@code{locate}. There are many different types of Regular Expression,
3273but the type used by @code{find} and @code{locate} is the same as is
3274used in GNU Emacs. Both programs provide an option which allows you
3275to select an alternative regular expression syntax; for @code{find}
3276this is the @samp{-regextype} option, and for @code{locate} this is
3277the @samp{--regextype} option.
3278
3279These options take a single argument, which indicates the specific
3280regular expression syntax and behaviour that should be used. This
3281should be one of the following:
3282
3283@include regexprops.texi
3284
3285@node Environment Variables,, Regular Expressions, Reference
3286@section Environment Variables
3287@table @var
3288@item LANG
3289Provides a default value for the internationalisation variables that
3290are unset or null.
3291@item LC_ALL
3292If set to a non-empty string value, override the values of all the
3293other internationalisation variables.
3294@item LC_COLLATE
3295The POSIX standard specifies that this variable affects the pattern
3296matching to be used for the `\-name' option. GNU find uses the
3297GNU version of the @code{fnmatch} library function.
3298
3299POSIX also specifies that the `LC_COLLATE' environment
3300variable affects the interpretation of the user's response to the
3301query issued by `\-ok', but this is not the case for GNU find.
3302@item LC_CTYPE
3303This variable affects the treatment of character classes used with
3304the @samp{-name} test, if the system's
3305@code{fnmatch} library function supports this. It has no effect on the behaviour
3306of the @samp{-ok} expression.
3307@item LC_MESSAGES
3308Determines the locale to be used for internationalised messages.
3309@item NLSPATH
3310Determines the location of the internationalisation message catalogues.
3311@item PATH
3312Affects the directories which are searched to find the executables
3313invoked by @samp{-exec}, @samp{-execdir} @samp{-ok} and @samp{-okdir}.
3314If the @var{PATH} environment variable includes the current directory
3315(by explicitly including @samp{.} or by having an empty element), and
3316the find command line includes @samp{-execdir} or @samp{-okdir},
3317@code{find} will refuse to run. @xref{Security Considerations}, for a
3318more detailed discussion of security matters.
3319
3320@item POSIXLY_CORRECT
3321Determines the block size used by @samp{-ls} and @samp{-fls}.
3322If @var{POSIXLY_CORRECT} is set, blocks are units of 512 bytes. Otherwise
3323they are units of 1024 bytes.
3324
3325@item TZ
3326Affects the time zone used for some of the time-related format
3327directives of @samp{-printf} and @samp{-fprintf}.
3328@end table
3329
3330
3331
3332@node Common Tasks, Worked Examples, Reference, Top
3333@chapter Common Tasks
3334
3335The sections that follow contain some extended examples that both give
3336a good idea of the power of these programs, and show you how to solve
3337common real-world problems.
3338
3339@menu
3340* Viewing And Editing::
3341* Archiving::
3342* Cleaning Up::
3343* Strange File Names::
3344* Fixing Permissions::
3345* Classifying Files::
3346@end menu
3347
3348@node Viewing And Editing
3349@section Viewing And Editing
3350
3351To view a list of files that meet certain criteria, simply run your
3352file viewing program with the file names as arguments. Shells
3353substitute a command enclosed in backquotes with its output, so the
3354whole command looks like this:
3355
3356@example
3357less `find /usr/include -name '*.h' | xargs grep -l mode_t`
3358@end example
3359
3360@noindent
3361You can edit those files by giving an editor name instead of a file
3362viewing program:
3363
3364@example
3365emacs `find /usr/include -name '*.h' | xargs grep -l mode_t`
3366@end example
3367
3368Because there is a limit to the length of any individual command line,
3369there is a limit to the number of files that can be handled in this
3370way. We can get around this difficulty by using xargs like this:
3371
3372@example
3373find /usr/include -name '*.h' | xargs grep -l mode_t > todo
3374xargs --arg-file=todo emacs
3375@end example
3376
3377Here, @code{xargs} will run @code{emacs} as many times as necessary to
3378visit all of the files listed in the file @file{todo}.
3379
3380@node Archiving
3381@section Archiving
3382
3383You can pass a list of files produced by @code{find} to a file
3384archiving program. GNU @code{tar} and @code{cpio} can both read lists
3385of file names from the standard input---either delimited by nulls (the
3386safe way) or by blanks (the lazy, risky default way). To use
3387null-delimited names, give them the @samp{--null} option. You can
3388store a file archive in a file, write it on a tape, or send it over a
3389network to extract on another machine.
3390
3391One common use of @code{find} to archive files is to send a list of
3392the files in a directory tree to @code{cpio}. Use @samp{-depth} so if
3393a directory does not have write permission for its owner, its contents
3394can still be restored from the archive since the directory's
3395permissions are restored after its contents. Here is an example of
3396doing this using @code{cpio}; you could use a more complex @code{find}
3397expression to archive only certain files.
3398
3399@example
3400find . -depth -print0 |
3401 cpio --create --null --format=crc --file=/dev/nrst0
3402@end example
3403
3404You could restore that archive using this command:
3405
3406@example
3407cpio --extract --null --make-dir --unconditional \
3408 --preserve --file=/dev/nrst0
3409@end example
3410
3411Here are the commands to do the same things using @code{tar}:
3412
3413@example
3414find . -depth -print0 |
3415 tar --create --null --files-from=- --file=/dev/nrst0
3416
3417tar --extract --null --preserve-perm --same-owner \
3418 --file=/dev/nrst0
3419@end example
3420
3421@c Idea from Rick Sladkey.
3422Here is an example of copying a directory from one machine to another:
3423
3424@example
3425find . -depth -print0 | cpio -0o -Hnewc |
3426 rsh @var{other-machine} "cd `pwd` && cpio -i0dum"
3427@end example
3428
3429@node Cleaning Up
3430@section Cleaning Up
3431
3432@c Idea from Jim Meyering.
3433This section gives examples of removing unwanted files in various
3434situations. Here is a command to remove the CVS backup files created
3435when an update requires a merge:
3436
3437@example
3438find . -name '.#*' -print0 | xargs -0r rm -f
3439@end example
3440
3441The command above works, but the following is safer:
3442
3443@example
3444find . -name '.#*' -depth -delete
3445@end example
3446
3447@c Idea from Franc,ois Pinard.
3448You can run this command to clean out your clutter in @file{/tmp}.
3449You might place it in the file your shell runs when you log out
3450(@file{.bash_logout}, @file{.logout}, or @file{.zlogout}, depending on
3451which shell you use).
3452
3453@example
3454find /tmp -depth -user "$LOGNAME" -type f -delete
3455@end example
3456
3457If your @code{find} command removes directories, you may find that
3458you get a spurious error message when @code{find} tries to recurse
3459into a directory that has now been removed. Using the @samp{-depth}
3460option will normally resolve this problem.
3461
3462@c Idea from Noah Friedman.
3463To remove old Emacs backup and auto-save files, you can use a command
3464like the following. It is especially important in this case to use
3465null-terminated file names because Emacs packages like the VM mailer
3466often create temporary file names with spaces in them, like
3467@file{#reply to David J. MacKenzie<1>#}.
3468
3469@example
3470find ~ \( -name '*~' -o -name '#*#' \) -print0 |
3471 xargs --no-run-if-empty --null rm -vf
3472@end example
3473
3474Removing old files from @file{/tmp} is commonly done from @code{cron}:
3475
3476@c Idea from Kaveh Ghazi.
3477@example
3478find /tmp /var/tmp -not -type d -mtime +3 -delete
3479find /tmp /var/tmp -depth -mindepth 1 -type d -empty -delete
3480@end example
3481
3482The second @code{find} command above uses @samp{-depth} so it cleans
3483out empty directories depth-first, hoping that the parents become
3484empty and can be removed too. It uses @samp{-mindepth} to avoid
3485removing @file{/tmp} itself if it becomes totally empty.
3486
3487@node Strange File Names
3488@section Strange File Names
3489
3490@c Idea from:
3491@c From: tmatimar@isgtec.com (Ted Timar)
3492@c Newsgroups: comp.unix.questions,comp.unix.shell,comp.answers,news.answers
3493@c Subject: Unix - Frequently Asked Questions (2/7) [Frequent posting]
3494@c Subject: How do I remove a file with funny characters in the filename ?
3495@c Date: Thu Mar 18 17:16:55 EST 1993
3496@code{find} can help you remove or rename a file with strange
3497characters in its name. People are sometimes stymied by files whose
3498names contain characters such as spaces, tabs, control characters, or
3499characters with the high bit set. The simplest way to remove such
3500files is:
3501
3502@example
3503rm -i @var{some*pattern*that*matches*the*problem*file}
3504@end example
3505
3506@code{rm} asks you whether to remove each file matching the given
3507pattern. If you are using an old shell, this approach might not work
3508if the file name contains a character with the high bit set; the shell
3509may strip it off. A more reliable way is:
3510
3511@example
3512find . -maxdepth 1 @var{tests} -okdir rm '@{@}' \;
3513@end example
3514
3515@noindent
3516where @var{tests} uniquely identify the file. The @samp{-maxdepth 1}
3517option prevents @code{find} from wasting time searching for the file
3518in any subdirectories; if there are no subdirectories, you may omit
3519it. A good way to uniquely identify the problem file is to figure out
3520its inode number; use
3521
3522@example
3523ls -i
3524@end example
3525
3526Suppose you have a file whose name contains control characters, and
3527you have found that its inode number is 12345. This command prompts
3528you for whether to remove it:
3529
3530@example
3531find . -maxdepth 1 -inum 12345 -okdir rm -f '@{@}' \;
3532@end example
3533
3534If you don't want to be asked, perhaps because the file name may
3535contain a strange character sequence that will mess up your screen
3536when printed, then use @samp{-execdir} instead of @samp{-okdir}.
3537
3538If you want to rename the file instead, you can use @code{mv} instead
3539of @code{rm}:
3540
3541@example
3542find . -maxdepth 1 -inum 12345 -okdir mv '@{@}' @var{new-file-name} \;
3543@end example
3544
3545@node Fixing Permissions
3546@section Fixing Permissions
3547
3548Suppose you want to make sure that everyone can write to the
3549directories in a certain directory tree. Here is a way to find
3550directories lacking either user or group write permission (or both),
3551and fix their permissions:
3552
3553@example
3554find . -type d -not -perm -ug=w | xargs chmod ug+w
3555@end example
3556
3557@noindent
3558You could also reverse the operations, if you want to make sure that
3559directories do @emph{not} have world write permission.
3560
3561@node Classifying Files
3562@section Classifying Files
3563
3564@c Idea from:
3565@c From: martin@mwtech.UUCP (Martin Weitzel)
3566@c Newsgroups: comp.unix.wizards,comp.unix.questions
3567@c Subject: Advanced usage of 'find' (Re: Unix security automating script)
3568@c Date: 22 Mar 90 15:05:19 GMT
3569If you want to classify a set of files into several groups based on
3570different criteria, you can use the comma operator to perform multiple
3571independent tests on the files. Here is an example:
3572
3573@example
3574find / -type d \( -perm -o=w -fprint allwrite , \
3575 -perm -o=x -fprint allexec \)
3576
3577echo "Directories that can be written to by everyone:"
3578cat allwrite
3579echo ""
3580echo "Directories with search permissions for everyone:"
3581cat allexec
3582@end example
3583
3584@code{find} has only to make one scan through the directory tree
3585(which is one of the most time consuming parts of its work).
3586
3587@node Worked Examples, Security Considerations, Common Tasks, Top
3588@chapter Worked Examples
3589
3590The tools in the findutils package, and in particular @code{find},
3591have a large number of options. This means that quite often,
3592there is more than one way to do things. Some of the options
3593and facilities only exist for compatibility with other tools, and
3594findutils provides improved ways of doing things.
3595
3596This chapter describes a number of useful tasks that are commonly
3597performed, and compares the different ways of achieving them.
3598
3599@menu
3600* Deleting Files::
3601* Updating A Timestamp File::
3602@end menu
3603
3604@node Deleting Files
3605@section Deleting Files
3606
3607One of the most common tasks that @code{find} is used for is locating
3608files that can be deleted. This might include:
3609
3610@itemize
3611@item
3612Files last modified more than 3 years ago which haven't been accessed
3613for at least 2 years
3614@item
3615Files belonging to a certain user
3616@item
3617Temporary files which are no longer required
3618@end itemize
3619
3620This example concentrates on the actual deletion task rather than on
3621sophisticated ways of locating the files that need to be deleted.
3622We'll assume that the files we want to delete are old files underneath
3623@file{/var/tmp/stuff}.
3624
3625@subsection The Traditional Way
3626
3627The traditional way to delete files in @file{var/tmp/stuff} that have
3628not been modified in over 90 days would have been:
3629
3630@smallexample
3631find /var/tmp/stuff -mtime +90 -exec /bin/rm @{@} \;
3632@end smallexample
3633
3634The above command uses @samp{-exec} to run the @code{/bin/rm} command
3635to remove each file. This approach works and in fact would have
3636worked in Version 7 Unix in 1979. However, there are a number of
3637problems with this approach.
3638
3639
3640The most obvious problem with the approach above is that it causes
3641@code{find} to fork every time it finds a file that needs to delete,
3642and the child process then has to use the @code{exec} system call to
3643launch @code{/bin/rm}. All this is quite inefficient. If we are
3644going to use @code{/bin/rm} to do this job, it is better to make it
3645delete more than one file at a time.
3646
3647The most obvious way of doing this is to use the shell's command
3648expansion feature:
3649
3650@smallexample
3651/bin/rm `find /var/tmp/stuff -mtime +90 -print`
3652@end smallexample
3653or you could use the more modern form
3654@smallexample
3655/bin/rm $(find /var/tmp/stuff -mtime +90 -print)
3656@end smallexample
3657
3658The commands above are much more efficient than the first attempt.
3659However, there is a problem with them. The shell has a maximum
3660command length which is imposed by the operating system (the actual
3661limit varies between systems). This means that while the command
3662expansion technique will usually work, it will suddenly fail when
3663there are lots of files to delete. Since the task is to delete
3664unwanted files, this is precisely the time we don't want things to go
3665wrong.
3666
3667@subsection Making Use of xargs
3668
3669So, is there a way to be more efficient in the use of @code{fork()}
3670and @code{exec()} without running up against this limit?
3671Yes, we can be almost optimally efficient by making use
3672of the @code{xargs} command. The @code{xargs} command reads arguments
3673from its standard input and builds them into command lines. We can
3674use it like this:
3675
3676@smallexample
3677find /var/tmp/stuff -mtime +90 -print | xargs /bin/rm
3678@end smallexample
3679
3680For example if the files found by @code{find} are
3681@file{/var/tmp/stuff/A},
3682@file{/var/tmp/stuff/B} and
3683@file{/var/tmp/stuff/C} then @code{xargs} might issue the commands
3684
3685@smallexample
3686/bin/rm /var/tmp/stuff/A /var/tmp/stuff/B
3687/bin/rm /var/tmp/stuff/C
3688@end smallexample
3689
3690The above assumes that @code{xargs} has a very small maximum command
3691line length. The real limit is much larger but the idea is that
3692@code{xargs} will run @code{/bin/rm} as many times as necessary to get
3693the job done, given the limits on command line length.
3694
3695This usage of @code{xargs} is pretty efficient, and the @code{xargs}
3696command is widely implemented (all modern versions of Unix offer it).
3697So far then, the news is all good. However, there is bad news too.
3698
3699@subsection Unusual characters in filenames
3700
3701Unix-like systems allow any characters to appear in file names with
3702the exception of the ASCII NUL character and the backslash.
3703Backslashes can occur in path names (as the directory separator) but
3704not in the names of actual directory entries. This means that the
3705list of files that @code{xargs} reads could in fact contain white space
3706characters --- spaces, tabs and newline characters. Since by default,
3707@code{xargs} assumes that the list of files it is reading uses white
3708space as an argument separator, it cannot correctly handle the case
3709where a filename actually includes white space. This makes the
3710default behaviour of @code{xargs} almost useless for handling
3711arbitrary data.
3712
3713To solve this problem, GNU findutils introduced the @samp{-print0}
3714action for @code{find}. This uses the ASCII NUL character to separate
3715the entries in the file list that it produces. This is the ideal
3716choice of separator since it is the only character that cannot appear
3717within a path name. The @samp{-0} option to @code{xargs} makes it
3718assume that arguments are separated with ASCII NUL instead of white
3719space. It also turns off another misfeature in the default behaviour
3720of @code{xargs}, which is that it pays attention to quote characters
3721in its input. Some versions of @code{xargs} also terminate when they
3722see a lone @samp{_} in the input, but GNU @code{find} no longer does
3723that (since it has become an optional behaviour in the Unix standard).
3724
3725So, putting @code{find -print0} together with @code{xargs -0} we get
3726this command:
3727
3728@smallexample
3729find /var/tmp/stuff -mtime +90 -print0 | xargs -0 /bin/rm
3730@end smallexample
3731
3732The result is an efficient way of proceeding that
3733correctly handles all the possible characters that could appear in the
3734list of files to delete. This is good news. However, there is, as
3735I'm sure you're expecting, also more bad news. The problem is that
3736this is not a portable construct; although other versions of Unix
3737(notable BSD-derived ones) support @samp{-print0}, it's not
3738universal. So, is there a more universal mechanism?
3739
3740@subsection Going back to -exec
3741
3742There is indeed a more universal mechanism, which is a slight
3743modification to the @samp{-exec} action. The normal @samp{-exec}
3744action assumes that the command to run is terminated with a semicolon
3745(the semicolon normally has to be quoted in order to protect it from
3746interpretation as the shell command separator). The SVR4 edition of
3747Unix introduced a slight variation, which involves terminating the
3748command with @samp{+} instead:
3749
3750@smallexample
3751find /var/tmp/stuff -mtime +90 -exec /bin/rm @{@} \+
3752@end smallexample
3753
3754The above use of @samp{-exec} causes @code{find} to build up a long
3755command line and then issue it. This can be less efficient than some
3756uses of @code{xargs}; for example @code{xargs} allows new command
3757lines to be built up while the previous command is still executing, and
3758allows you to specify a number of commands to run in parallel.
3759However, the @code{find @dots{} -exec @dots{} +} construct has the advantage
3760of wide portability. GNU findutils did not support @samp{-exec @dots{} +}
3761until version 4.2.12; one of the reasons for this is that it already
3762had the @samp{-print0} action in any case.
3763
3764
3765@subsection A more secure version of -exec
3766
3767The command above seems to be efficient and portable. However,
3768within it lurks a security problem. The problem is shared with
3769all the commands we've tried in this worked example so far, too. The
3770security problem is a race condition; that is, if it is possible for
3771somebody to manipulate the filesystem that you are searching while you
3772are searching it, it is possible for them to persuade your @code{find}
3773command to cause the deletion of a file that you can delete but they
3774normally cannot.
3775
3776The problem occurs because the @samp{-exec} action is defined by the
3777@acronym{POSIX} standard to invoke its command with the same working directory
3778as @code{find} had when it was started. This means that the arguments
3779which replace the @{@} include a relative path from @code{find}'s
3780starting point down the file that needs to be deleted. For example,
3781
3782@smallexample
3783find /var/tmp/stuff -mtime +90 -exec /bin/rm @{@} \+
3784@end smallexample
3785
3786might actually issue the command:
3787
3788@smallexample
3789/bin/rm /var/tmp/stuff/A /var/tmp/stuff/B /var/tmp/stuff/passwd
3790@end smallexample
3791
3792Notice the file @file{/var/tmp/stuff/passwd}. Likewise, the command:
3793
3794@smallexample
3795cd /var/tmp && find stuff -mtime +90 -exec /bin/rm @{@} \+
3796@end smallexample
3797
3798might actually issue the command:
3799
3800@smallexample
3801/bin/rm stuff/A stuff/B stuff/passwd
3802@end smallexample
3803
3804If an attacker can rename @file{stuff} to something else (making use
3805of their write permissions in @file{/var/tmp}) they can replace it
3806with a symbolic link to @file{/etc}. That means that the
3807@code{/bin/rm} command will be invoked on @file{/etc/passwd}. If you
3808are running your @code{find} command as root, the attacker has just managed
3809to delete a vital file. All they needed to do to achieve this was
3810replace a subdirectory with a symbolic link at the vital moment.
3811
3812There is however, a simple solution to the problem. This is an action
3813which works a lot like @code{-exec} but doesn't need to traverse a
3814chain of directories to reach the file that it needs to work on. This
3815is the @samp{-execdir} action, which was introduced by the BSD family
3816of operating systems. The command,
3817
3818@smallexample
3819find /var/tmp/stuff -mtime +90 -execdir /bin/rm @{@} \+
3820@end smallexample
3821
3822might delete a set of files by performing these actions:
3823
3824@enumerate
3825@item
3826Change directory to /var/tmp/stuff/foo
3827@item
3828Invoke @code{/bin/rm ./file1 ./file2 ./file3}
3829@item
3830Change directory to /var/tmp/stuff/bar
3831@item
3832Invoke @code{/bin/rm ./file99 ./file100 ./file101}
3833@end enumerate
3834
3835This is a much more secure method. We are no longer exposed to a race
3836condition. For many typical uses of @code{find}, this is the best
3837strategy. It's reasonably efficient, but the length of the command
3838line is limited not just by the operating system limits, but also by
3839how many files we actually need to delete from each directory.
3840
3841Is it possible to do any better? In the case of general file
3842processing, no. However, in the specific case of deleting files it is
3843indeed possible to do better.
3844
3845@subsection Using the -delete action
3846
3847The most efficient and secure method of solving this problem is to use
3848the @samp{-delete} action:
3849
3850@smallexample
3851find /var/tmp/stuff -mtime +90 -delete
3852@end smallexample
3853
3854This alternative is more efficient than any of the @samp{-exec} or
3855@samp{-execdir} actions, since it entirely avoids the overhead of
3856forking a new process and using @code{exec} to run @code{/bin/rm}. It
3857is also normally more efficient than @code{xargs} for the same
3858reason. The file deletion is performed from the directory containing
3859the entry to be deleted, so the @samp{-delete} action has the same
3860security advantages as the @samp{-execdir} action has.
3861
3862The @samp{-delete} action was introduced by the BSD family of
3863operating systems.
3864
3865@subsection Improving things still further
3866
3867Is it possible to improve things still further? Not without either
3868modifying the system library to the operating system or having more specific
3869knowledge of the layout of the filesystem and disk I/O subsystem, or
3870both.
3871
3872The @code{find} command traverses the filesystem, reading
3873directories. It then issues a separate system call for each file to
3874be deleted. If we could modify the operating system, there are
3875potential gains that could be made:
3876
3877@itemize
3878@item
3879We could have a system call to which we pass more than one filename
3880for deletion
3881@item
3882Alternatively, we could pass in a list of inode numbers (on GNU/Linux
3883systems, @code{readdir()} also returns the inode number of each
3884directory entry) to be deleted.
3885@end itemize
3886
3887The above possibilities sound interesting, but from the kernel's point
3888of view it is difficult to enforce standard Unix access controls for
3889such processing by inode number. Such a facility would probably
3890need to be restricted to the superuser.
3891
3892Another way of improving performance would be to increase the
3893parallelism of the process. For example if the directory hierarchy we
3894are searching is actually spread across a number of disks, we might
3895somehow be able to arrange for @code{find} to process each disk in
3896parallel. In practice GNU @code{find} doesn't have such an intimate
3897understanding of the system's filesystem layout and disk I/O
3898subsystem.
3899
3900However, since the system administrator can have such an understanding
3901they can take advantage of it like so:
3902
3903@smallexample
3904find /var/tmp/stuff1 -mtime +90 -delete &
3905find /var/tmp/stuff2 -mtime +90 -delete &
3906find /var/tmp/stuff3 -mtime +90 -delete &
3907find /var/tmp/stuff4 -mtime +90 -delete &
3908wait
3909@end smallexample
3910
3911In the example above, four separate instances of @code{find} are used
3912to search four subdirectories in parallel. The @code{wait} command
3913simply waits for all of these to complete. Whether this approach is
3914more or less efficient than a single instance of @code{find} depends
3915on a number of things:
3916
3917@itemize
3918@item
3919Are the directories being searched in parallel actually on separate
3920disks? If not, this parallel search might just result in a lot of
3921disk head movement and so the speed might even be slower.
3922@item
3923Other activity - are other programs also doing things on those disks?
3924@end itemize
3925
3926
3927@subsection Conclusion
3928
3929The fastest and most secure way to delete files with the help of
3930@code{find} is to use @samp{-delete}. Using @code{xargs -0 -P N} can
3931also make effective use of the disk, but it is not as secure.
3932
3933In the case where we're doing things other than deleting files, the
3934most secure alternative is @samp{-execdir @dots{} +}, but this is not as
3935portable as the insecure action @samp{-exec @dots{} +}.
3936
3937The @samp{-delete} action is not completely portable, but the only
3938other possibillity which is as secure (@samp{-execdir}) is no more
3939portable. The most efficient portable alternative is @samp{-exec
3940@dots{}+}, but this is insecure and isn't supported by versions of GNU
3941findutils prior to 4.2.12.
3942
3943
3944@node Updating A Timestamp File
3945@section Updating A Timestamp File
3946
3947Suppose we have a directory full of files which is maintained with a
3948set of automated tools; perhaps one set of tools updates them and
3949another set of tools uses the result. In this situation, it might be
3950useful for the second set of tools to know if the files have recently
3951been changed. It might be useful, for example, to have a 'timestamp'
3952file which gives the timestamp on the newest file in the collection.
3953
3954We can use @code{find} to achieve this, but there are several
3955different ways to do it.
3956
3957@subsection Updating the Timestamp The Wrong Way
3958
3959The obvious but wrong answer is just to use @samp{-newer}:-
3960
3961@smallexample
3962find subdir -newer timestamp -exec touch -r @{@} timestamp \;
3963@end smallexample
3964
3965This does the right sort of thing but has a bug. Suppose that two
3966files in the subdirectory have been updated, and that these are called
3967@file{file1} and @file{file2}. The command above will update
3968@file{timestamp} with the modification time of @file{file1} or that of
3969@file{file2}, but we don't know which one. Since the timestamps on
3970@file{file1} and @file{file2} will in general be different, this could
3971well be the wrong value.
3972
3973One solution to this problem is to modify @code{find} to recheck the
3974modification time of @file{timestamp} every time a file is to be
3975compared against it, but that will reduce the performance of
3976@code{find}.
3977
3978@subsection Using the test utility to compare timestamps
3979
3980The @code{test} command can be used to compare timestamps:
3981
3982@smallexample
3983find subdir -exec test @{@} -nt timestamp \; -exec touch -r @{@} timestamp \;
3984@end smallexample
3985
3986This will ensure that any changes made to the modification time of
3987@file{timestamp} that take place during the execution of @code{find}
3988are taken into account. This resolves our earlier problem, but
3989unfortunately this runs much more slowly.
3990
3991@subsection A combined approach
3992
3993We can of course still use @samp{-newer} to cut down on the number of
3994calls to @code{test}:
3995
3996@smallexample
3997find subdir -newer timestamp -a \
3998 -exec test @{@} -nt timestamp \; -a \
3999 -exec touch -r @{@} timestamp \;
4000@end smallexample
4001
4002Here, the @samp{-newer} test excludes all the files which are
4003definitely older than the timestamp, but all the files which are newer
4004than the old value of the timestamp are compared against the current
4005updated timestamp.
4006
4007This is indeed faster in general, but the speed difference will depend
4008on how many updated files there are.
4009
4010@subsection Using -printf and sort to compare timestamps
4011
4012It is possible to use the @samp{-printf} action to abandon the use of
4013@code{test} entirely:
4014
4015@smallexample
4016newest=$(find subdir -newer timestamp -printf "%A@:%p\n" |
4017 sort -n |
4018 tail -1 |
4019 cut -d: -f2- )
4020touch -r "$@{newest:-timestamp@}" timestamp
4021@end smallexample
4022
4023The command above works by generating a list of the timestamps and
4024names of all the files which are newer than the timestamp. The
4025@code{sort}, @code{tail} and @code{cut} commands simply pull out the
4026name of the file with the largest timestamp value (that is, the latest
4027file). The @code{touch} command is then used to update the timestamp,
4028
4029The @code{"$@{newest:-timestamp@}"} expression simply expands to the
4030value of @code{$newest} if that variable is set, but to
4031@file{timestamp} otherwise. This ensures that an argument is always
4032given to the @samp{-r} option of the @code{touch} command.
4033
4034This approach seems quite efficient, but unfortunately it has a bug.
4035Many operating systems now keep file modification time information at
4036a granularity which is finer than one second. Unfortunately the
4037@samp{%A@@} format for @samp{-printf} only prints a whole-number value
4038currently; that is, these values are at a one-second granularity.
4039This means that in our example above, @samp{$newest} will be the name
4040of a file which is no more than one second older than the newest file,
4041but may indeed be older.
4042
4043It would be possible to solve this problem with some kind of loop:
4044
4045@smallexample
4046while true; do
4047 newest=$(find subdir -newer timestamp -printf "%A@@:%p\n" |
4048 sort -n |
4049 tail -1 |
4050 cut -d: -f2- )
4051 if test -z "$newest" ; then
4052 break
4053 else
4054 touch -r "$newest" timestamp
4055 fi
4056done
4057@end smallexample
4058
4059A better fix for this problem would be to allow the @samp{%A@@} format
4060to produce a result having a fractional part, too. While this is
4061planned for GNU @code{find}, it hasn't been done yet.
4062
4063@subsection Coping with sub-second timestamp resolution
4064
4065Another tool which often works with timestamps is @code{make}. We can
4066use @code{find} to generate a @file{Makefile} file on the fly and then
4067use @code{make} to update the timestamps:
4068
4069@smallexample
4070makefile=$(mktemp)
4071find subdir \
4072 \( \! -xtype l \) \
4073 -newer timestamp \
4074 -printf "timestamp:: %p\n\ttouch -r %p timestamp\n\n" > "$makefile"
4075make -f "$makefile"
4076rm -f "$makefile"
4077@end smallexample
4078
4079Unfortunately although the solution above is quite elegant, it fails
4080to cope with white space within file names, and adjusting it to do so
4081would require a rather complex shell script.
4082
4083
4084@subsection Coping with odd filenames too
4085
4086We can fix both of these problems (looping and problems with white
4087space), and do things more efficiently too. The following command
4088works with newlines and doesn't need to sort the list of filenames.
4089
4090@smallexample
4091find subdir -newer timestamp -printf "%A@@:%p\0" |
4092 perl -0 newest.pl |
4093 xargs --no-run-if-empty --null -i \
4094 find @{@} -maxdepth 0 -newer timestamp -exec touch -r @{@} timestamp \;
4095@end smallexample
4096
4097The first @code{find} command generates a list of files which are
4098newer than the original timestamp file, and prints a list of them with
4099their timestamps. The @file{newest.pl} script simply filters out all
4100the filenames which have timestamps which are older than whatever the
4101newest file is:-
4102
4103@smallexample
4104@verbatim
4105#! /usr/bin/perl -0
4106my @newest = ();
4107my $latest_stamp = undef;
4108while (<>) {
4109 my ($stamp, $name) = split(/:/);
4110 if (!defined($latest_stamp) || ($tstamp > $latest_stamp)) {
4111 $latest_stamp = $stamp;
4112 @newest = ();
4113 }
4114 if ($tstamp >= $latest_stamp) {
4115 push @newest, $name;
4116 }
4117}
4118print join("\0", @newest);
4119@end verbatim
4120@end smallexample
4121
4122This prints a list of zero or more files, all of which are newer than
4123the original timestamp file, and which have the same timestamp as each
4124other, to the nearest second. The second @code{find} command takes
4125each resulting file one at a time, and if that is newer than the
4126timestamp file, the timestamp is updated.
4127
4128@node Security Considerations, Error Messages, Worked Examples, Top
4129@chapter Security Considerations
4130
4131Security considerations are important if you are using @code{find} or
4132@code{xargs} to search for or process files that don't belong to you
4133or which other people have control. Security considerations
4134relating to @code{locate} may also apply if you have files which you
4135do not want others to see.
4136
4137The most severe forms of security problems affecting
4138@code{find} and related programs are when third parties bring
4139about a situation allowing them to do something
4140they would normally not be able to accomplish. This is called @emph{privilege
4141elevation}. This might include deleting files they would not normally
4142be able to delete. It is common for the operating system to periodically
4143invoke @code{find} for self-maintenance purposes. These invocations of
4144@code{find} are particularly problematic from a security point of view
4145as these are often invoked by the superuser and search the entire
4146filesystem hierarchy. Generally, the severity of any associated problem depends
4147on what the system is going to do with the files found by @code{find}.
4148
4149@menu
4150* Levels of Risk:: What is your level of exposure to security problems?
4151* Security Considerations for find:: Security problems with find
4152* Security Considerations for xargs:: Security problems with xargs
4153* Security Considerations for locate:: Security problems with locate
4154* Security Summary:: That was all very complex, what does it boil down to?
4155@end menu
4156
4157
4158@node Levels of Risk
4159@section Levels of Risk
4160
4161There are some security risks inherent in the use of @code{find},
4162@code{xargs} and (to a lesser extent) @code{locate}. The severity of
4163these risks depends on what sort of system you are using:
4164
4165@table @strong
4166@item High risk
4167Multi-user systems where you do not control (or trust) the other
4168users, and on which you execute @code{find}, including areas where
4169those other users can manipulate the filesystem (for example beneath
4170@file{/home} or @file{/tmp}).
4171
4172@item Medium Risk
4173Systems where the actions of other users can create file names chosen
4174by them, but to which they don't have access while @code{find} is
4175being run. This access might include leaving programs running (shell
4176background jobs, @code{at} or @code{cron} tasks, for example). On
4177these sorts of systems, carefully written commands (avoiding use of
4178@samp{-print} for example) should not expose you to a high degree of
4179risk. Most systems fall into this category.
4180
4181@item Low Risk
4182Systems to which untrusted parties do not have access, cannot create
4183file names of their own choice (even remotely) and which contain no
4184security flaws which might enable an untrusted third party to gain
4185access. Most systems do not fall into this category because there are
4186many ways in which external parties can affect the names of files that
4187are created on your system. The system on which I am writing this for
4188example automatically downloads software updates from the Internet;
4189the names of the files in which these updates exist are chosen by
4190third parties@footnote{Of course, I trust these parties to a large
4191extent anyway, because I install software provided by them; I choose
4192to trust them in this way, and that's a deliberate choice}.
4193@end table
4194
4195In the discussion above, ``risk'' denotes the likelihood that someone
4196can cause @code{find}, @code{xargs}, @code{locate} or some other
4197program which is controlled by them to do something you did not
4198intend. The levels of risk suggested do not take any account of the
4199consequences of this sort of event. That is, if you operate a ``low
4200risk'' type system, but the consequences of a security problem are
4201disastrous, then you should still give serious thought to all the
4202possible security problems, many of which of course will not be
4203discussed here -- this section of the manual is intended to be
4204informative but not comprehensive or exhaustive.
4205
4206If you are responsible for the operation of a system where the
4207consequences of a security problem could be very important, you should
4208do two things:-
4209
4210@enumerate
4211@item Define a security policy which defines who is allowed to do what
4212on your system.
4213@item Seek competent advice on how to enforce your policy, detect
4214breaches of that policy, and take account of any potential problems
4215that might fall outside the scope of your policy.
4216@end enumerate
4217
4218
4219@node Security Considerations for find
4220@section Security Considerations for @code{find}
4221
4222
4223Some of the actions @code{find} might take have a direct effect;
4224these include @code{-exec} and @code{-delete}. However, it is also
4225common to use @code{-print} explicitly or implicitly, and so if
4226@code{find} produces the wrong list of file names, that can also be a
4227security problem; consider the case for example where @code{find} is
4228producing a list of files to be deleted.
4229
4230We normally assume that the @code{find} command line expresses the
4231file selection criteria and actions that the user had in mind -- that
4232is, the command line is ``trusted'' data.
4233
4234From a security analysis point of view, the output of @code{find}
4235should be correct; that is, the output should contain only the names
4236of those files which meet the user's criteria specified on the command
4237line. This applies for the @code{-exec} and @code{-delete} actions;
4238one can consider these to be part of the output.
4239
4240On the other hand, the contents of the filesystem can be manipulated
4241by other people, and hence we regard this as ``untrusted'' data. This
4242implies that the @code{find} command line is a filter which converts
4243the untrusted contents of the filesystem into a correct list of output
4244files.
4245
4246The filesystem will in general change while @code{find} is searching
4247it; in fact, most of the potential security problems with @code{find}
4248relate to this issue in some way.
4249
4250@dfn{Race conditions} are a general class of security problem where the
4251relative ordering of actions taken by @code{find} (for example) and
4252something else are critically important in getting the correct and expected result@footnote{This is more or less the
4253definition of the term ``race condition''} .
4254
4255For @code{find}, an attacker might move or rename files or directories in
4256the hope that an action might be taken against a file which was not
4257normally intended to be affected. Alternatively, this sort of attack
4258might be intended to persuade @code{find} to search part of the
4259filesystem which would not normally be included in the search
4260(defeating the @code{-prune} action for example).
4261
4262@menu
4263* Problems with -exec and filenames::
4264* Changing the Current Working Directory::
4265* Race Conditions with -exec::
4266* Race Conditions with -print and -print0::
4267@end menu
4268
4269@node Problems with -exec and filenames
4270@subsection Problems with -exec and filenames
4271
4272It is safe in many cases to use the @samp{-execdir} action with any
4273file name. Because @samp{-execdir} prefixes the arguments it passes
4274to programs with @samp{./}, you will not accidentally pass an argument
4275which is interpreted as an option. For example the file @file{-f}
4276would be passed to @code{rm} as @file{./-f}, which is harmless.
4277
4278However, your degree of safety does depend on the nature of the
4279program you are running. For example constructs such as these two commands
4280
4281@example
4282find -exec sh -c "something @{@}" \;
4283find -execdir sh -c "something @{@}" \;
4284@end example
4285
4286are very dangerous. The reason for this is that the @samp{@{@}} is
4287expanded to a filename which might contain a semicolon or other
4288characters special to the shell. If for example someone creates the
4289file @file{/tmp/foo; rm -rf $HOME} then the two commands above could
4290delete someone's home directory.
4291
4292So for this reason do not run any command which will pass untrusted
4293data (such as the names of files) to commands which interpret
4294arguments as commands to be further interpreted (for example
4295@samp{sh}).
4296
4297@node Changing the Current Working Directory
4298@subsection Changing the Current Working Directory
4299
4300As @code{find} searches the filesystem, it finds subdirectories and
4301then searches within them by changing its working directory. First,
4302@code{find} reaches and recognises a subdirectory. It then decides if that
4303subdirectory meets the criteria for being searched; that is, any
4304@samp{-xdev} or @samp{-prune} expressions are taken into account. The
4305@code{find} program will then change working directory and proceed to
4306search the directory.
4307
4308A race condition attack might take the form that once the checks
4309relevant to @samp{-xdev} and @samp{-prune} have been done, an attacker
4310might rename the directory that was being considered, and put in its
4311place a symbolic link that actually points somewhere else.
4312
4313The idea behind this attack is to fool @code{find} into going into the
4314wrong directory. This would leave @code{find} with a working
4315directory chosen by an attacker, bypassing any protection apparently
4316provided by @samp{-xdev} and @samp{-prune}, and any protection
4317provided by being able to @emph{not} list particular directories on
4318the @code{find} command line. This form of attack is particularly
4319problematic if the attacker can predict when the @code{find} command
4320will be run, as is the case with @code{cron} tasks for example.
4321
4322GNU @code{find} has specific safeguards to prevent this general class
4323of problem. The exact form of these safeguards depends on the
4324properties of your system.
4325
4326@menu
4327* O_NOFOLLOW:: Safely changing directory using fchdir().
4328* Systems without O_NOFOLLOW:: Checking for symbolic links after chdir().
4329@end menu
4330
4331@node O_NOFOLLOW
4332@subsubsection O_NOFOLLOW
4333
4334If your system supports the O_NOFOLLOW flag @footnote{GNU/Linux
4335(kernel version 2.1.126 and later) and FreeBSD (3.0-CURRENT and later)
4336support this} to the @code{open(2)} system call, @code{find} uses it
4337when safely changing directory. The target directory is first opened
4338and then @code{find} changes working directory with the
4339@code{fchdir()} system call. This ensures that symbolic links are not
4340followed, preventing the sort of race condition attack in which use
4341is made of symbolic links.
4342
4343If for any reason this approach does not work, @code{find} will fall
4344back on the method which is normally used if O_NOFOLLOW is not
4345supported.
4346
4347You can tell if your system supports O_NOFOLLOW by running
4348
4349@example
4350find --version
4351@end example
4352
4353This will tell you the version number and which features are enabled.
4354For example, if I run this on my system now, this gives:
4355@example
4356GNU find version 4.2.18-CVS
4357Features enabled: D_TYPE O_NOFOLLOW(enabled)
4358@end example
4359
4360Here, you can see that I am running a version of @code{find} which was
4361built from the development (CVS) code prior to the release of
4362findutils-4.2.18, and that the D_TYPE and O_NOFOLLOW features are
4363present. O_NOFOLLOW is qualified with ``enabled''. This simply means
4364that the current system seems to support O_NOFOLLOW. This check is
4365needed because it is possible to build @code{find} on a system that
4366defines O_NOFOLLOW and then run it on a system that ignores the
4367O_NOFOLLOW flag. We try to detect such cases at startup by checking
4368the operating system and version number; when this happens you will
4369see ``O_NOFOLLOW(disabled)'' instead.
4370
4371@node Systems without O_NOFOLLOW
4372@subsubsection Systems without O_NOFOLLOW
4373
4374The strategy for preventing this type of problem on systems that lack
4375support for the O_NOFOLLOW flag is more complex. Each time
4376@code{find} changes directory, it examines the directory it is about
4377to move to, issues the @code{chdir()} system call, and then checks
4378that it has ended up in the subdirectory it expected. If all is as
4379expected, processing continues as normal. However, there are two main
4380reasons why the directory might change: the use of an automounter and
4381the someone removing the old directory and replacing it with something
4382else while @code{find} is trying to descend into it.
4383
4384Where a filesystem ``automounter'' is in use it can be the case that
4385the use of the @code{chdir()} system call can itself cause a new
4386filesystem to be mounted at that point. On systems that do not
4387support O_NOFOLLOW, this will cause @code{find}'s security check to
4388fail.
4389
4390However, this does not normally represent a security problem, since
4391the automounter configuration is normally set up by the system
4392administrator. Therefore, if the @code{chdir()} sanity check fails,
4393@code{find} will make one more attempt. If that succeeds, execution
4394carries on as normal. This is the usual case for automounters.
4395
4396Where an attacker is trying to exploit a race condition, the problem
4397may not have gone away on the second attempt. If this is the case,
4398@code{find} will issue a warning message and then ignore that
4399subdirectory. When this happens, actions such as @samp{-exec} or
4400@samp{-print} may already have taken place for the problematic
4401subdirectory. This is because @code{find} applies tests and actions
4402to directories before searching within them (unless @samp{-depth} was
4403specified).
4404
4405Because of the nature of the directory-change operation and security
4406check, in the worst case the only things that @code{find} would have
4407done with the directory are to move into it and back out to the
4408original parent. No operations would have been performed within that
4409directory.
4410
4411@node Race Conditions with -exec
4412@subsection Race Conditions with -exec
4413
4414The @samp{-exec} action causes another program to be run. It passes
4415to the program the name of the file which is being considered at the
4416time. The invoked program will typically then perform some action
4417on that file. Once again, there is a race condition which can be
4418exploited here. We shall take as a specific example the command
4419
4420@example
4421find /tmp -path /tmp/umsp/passwd -exec /bin/rm
4422@end example
4423
4424In this simple example, we are identifying just one file to be deleted
4425and invoking @code{/bin/rm} to delete it. A problem exists because
4426there is a time gap between the point where @code{find} decides that
4427it needs to process the @samp{-exec} action and the point where the
4428@code{/bin/rm} command actually issues the @code{unlink()} system
4429call to delete the file from the filesystem. Within this time period, an attacker can rename the
4430@file{/tmp/umsp} directory, replacing it with a symbolic link to
4431@file{/etc}. There is no way for @code{/bin/rm} to determine that it
4432is working on the same file that @code{find} had in mind. Once the
4433symbolic link is in place, the attacker has persuaded @code{find} to
4434cause the deletion of the @file{/etc/passwd} file, which is not the
4435effect intended by the command which was actually invoked.
4436
4437One possible defence against this type of attack is to modify the
4438behaviour of @samp{-exec} so that the @code{/bin/rm} command is run
4439with the argument @file{./passwd} and a suitable choice of working
4440directory. This would allow the normal sanity check that @code{find}
4441performs to protect against this form of attack too. Unfortunately,
4442this strategy cannot be used as the POSIX standard specifies that the
4443current working directory for commands invoked with @samp{-exec} must
4444be the same as the current working directory from which @code{find}
4445was invoked. This means that the @samp{-exec} action is inherently
4446insecure and can't be fixed.
4447
4448GNU @code{find} implements a more secure variant of the @samp{-exec}
4449action, @samp{-execdir}. The @samp{-execdir} action
4450ensures that it is not necessary to dereference subdirectories to
4451process target files. The current directory used to invoke programs
4452is the same as the directory in which the file to be processed exists
4453(@file{/tmp/umsp} in our example, and only the basename of the file to
4454be processed is passed to the invoked command, with a @samp{./}
4455prepended (giving @file{./passwd} in our example).
4456
4457The @samp{-execdir} action refuses to do anything if the current
4458directory is included in the @var{$PATH} environment variable. This
4459is necessary because @samp{-execdir} runs programs in the same
4460directory in which it finds files -- in general, such a directory
4461might be writable by untrusted users. For similar reasons,
4462@samp{-execdir} does not allow @samp{@{@}} to appear in the name of
4463the command to be run.
4464
4465@node Race Conditions with -print and -print0
4466@subsection Race Conditions with -print and -print0
4467
4468The @samp{-print} and @samp{-print0} actions can be used to produce a
4469list of files matching some criteria, which can then be used with some
4470other command, perhaps with @code{xargs}. Unfortunately, this means
4471that there is an unavoidable time gap between @code{find} deciding
4472that one or more files meet its criteria and the relevant command
4473being executed. For this reason, the @samp{-print} and @samp{-print0}
4474actions are just as insecure as @samp{-exec}.
4475
4476In fact, since the construction
4477
4478@example
4479find @dots{} -print | xargs @enddots{}
4480@end example
4481
4482does not cope correctly with newlines or other ``white space'' in
4483file names, and copes poorly with file names containing quotes, the
4484@samp{-print} action is less secure even than @samp{-print0}.
4485
4486
4487@comment node-name, next, previous, up
4488@comment @node Security Considerations for xargs
4489@node Security Considerations for xargs
4490@section Security Considerations for @code{xargs}
4491
4492The description of the race conditions affecting the @samp{-print}
4493action of @code{find} shows that @code{xargs} cannot be secure if it
4494is possible for an attacker to modify a filesystem after @code{find}
4495has started but before @code{xargs} has completed all its actions.
4496
4497However, there are other security issues that exist even if it is not
4498possible for an attacker to have access to the filesystem in real
4499time. Firstly, if it is possible for an attacker to create files with
4500names of their choice on the filesystem, then @code{xargs} is
4501insecure unless the @samp{-0} option is used. If a file with the name
4502@file{/home/someuser/foo/bar\n/etc/passwd} exists (assume that
4503@samp{\n} stands for a newline character), then @code{find @dots{} -print}
4504can be persuaded to print three separate lines:
4505
4506@example
4507/home/someuser/foo/bar
4508
4509/etc/passwd
4510@end example
4511
4512If it finds a blank line in the input, @code{xargs} will ignore it.
4513Therefore, if some action is to be taken on the basis of this list of
4514files, the @file{/etc/passwd} file would be included even if this was
4515not the intent of the person running find. There are circumstances in
4516which an attacker can use this to their advantage. The same
4517consideration applies to file names containing ordinary spaces rather
4518than newlines, except that of course the list of file names will no
4519longer contain an ``extra'' newline.
4520
4521This problem is an unavoidable consequence of the default behaviour of
4522the @code{xargs} command, which is specified by the POSIX standard.
4523The only ways to avoid this problem are either to avoid all use of
4524@code{xargs} in favour for example of @samp{find -exec} or (where
4525available) @samp{find -execdir}, or to use the @samp{-0} option, which
4526ensures that @code{xargs} considers file names to be separated by
4527ASCII NUL characters rather than whitespace. However, useful as this
4528option is, the POSIX standard does not make it mandatory.
4529
4530@comment node-name, next, previous, up
4531@node Security Considerations for locate
4532@section Security Considerations for @code{locate}
4533
4534It is fairly unusual for the output of @code{locate} to be fed into
4535another command. However, if this were to be done, this would raise
4536the same set of security issues as the use of @samp{find @dots{} -print}.
4537Although the problems relating to whitespace in file names can be
4538resolved by using @code{locate}'s @samp{-0} option, this still leaves
4539the race condition problems associated with @samp{find @dots{} -print0}.
4540There is no way to avoid these problems in the case of @code{locate}.
4541
4542@node Security Summary
4543@section Summary
4544
4545Where untrusted parties can create files on the system, or affect the
4546names of files that are created, all uses for @code{find},
4547@code{locate} and @code{xargs} have known security problems except the
4548following:
4549
4550@table @asis
4551@item Informational use only
4552Uses where the programs are used to prepare lists of file names upon
4553which no further action will ever be taken.
4554
4555@item @samp{-delete}
4556Use of the @samp{-delete} action with @code{find} to delete files
4557which meet specified criteria
4558
4559@item @samp{-execdir}
4560Use of the @samp{-execdir} action with @code{find} where the
4561@env{PATH} environment variable contains directories which contain
4562only trusted programs.
4563@end table
4564
4565@comment node-name, next, previous, up
4566@node Error Messages, Primary Index, Security Considerations, Top
4567@chapter Error Messages
4568
4569This section describes some of the error messages sometimes made by
4570@code{find}, @code{xargs}, or @code{locate}, explains them and in some
4571cases provides advice as to what you should do about this.
4572
4573This manual is written in English. The GNU findutils software
4574features translations of error messages for many languages. For this
4575reason the error messages produced by
4576the programs are made to be as self-explanatory as possible. This approach avoids leaving people to
4577figure out which test an English-language error message
4578corresponds to. Error messages which are self-explanatory
4579will not normally be mentioned in this document. For
4580those messages mentioned in this document, only the
4581English-language version of the message will be listed.
4582
4583@menu
4584* Error Messages From find::
4585* Error Messages From xargs::
4586* Error Messages From locate::
4587* Error Messages From updatedb::
4588@end menu
4589
4590@node Error Messages From find, Error Messages From xargs, , Error Messages
4591@section Error Messages From @code{find}
4592
4593@table @samp
4594@item invalid predicate `-foo'
4595This means that the @code{find} command line included something that
4596started with a dash or other special character. The @code{find}
4597program tried to interpret this as a test, action or option, but
4598didn't recognise it. If it was intended to be a test, check what was
4599specified against the documentation. If, on the other hand, the
4600string is the name of a file which has been expanded from a wildcard
4601(for example because you have a @samp{*} on the command line),
4602consider using @samp{./*} or just @samp{.} instead.
4603
4604@item unexpected extra predicate
4605This usually happens if you have an extra bracket on the command line
4606(for example @samp{find . -print \)}).
4607
4608@item Warning: filesystem /path/foo has recently been mounted
4609@itemx Warning: filesystem /path/foo has recently been unmounted
4610These messages might appear when @code{find} moves into a directory
4611and finds that the device number and inode are different to what it
4612expected them to be. If the directory @code{find} has moved into is
4613on an network filesystem (NFS), it will not issue this message, because
4614@code{automount} frequently mounts new filesystems on directories as
4615you move into them (that is how it knows you want to use the
4616filesystem). So, if you do see this message, be wary ---
4617@code{automount} may not have been responsible. Consider the
4618possibility that someone else is manipulating the filesystem while
4619@code{find} is running. Some people might do this in order to mislead
4620@code{find} or persuade it to look at one set of files when it thought
4621it was looking at another set.
4622
4623@item /path/foo changed during execution of find (old device number 12345, new device number 6789, filesystem type is <whatever>) [ref XXX]
4624This message is issued when @code{find} moves into a directory and ends up
4625somewhere it didn't expect to be. This happens in one of two
4626circumstances. Firstly, this happens when @code{automount} intervenes
4627on a system where @code{find} doesn't know how to determine what
4628the current set of mounted filesystems is.
4629
4630Secondly, this can happen when the device number of a directory
4631appears to change during a change of current directory, but
4632@code{find} is moving up the filesystem hierarchy rather than down into it.
4633In order to prevent @code{find} wandering off into some unexpected
4634part of the filesystem, we stop it at this point.
4635
4636@item Don't know how to use getmntent() to read `/etc/mtab'. This is a bug.
4637This message is issued when a problem similar to the above occurs on a
4638system where @code{find} doesn't know how to figure out the current
4639list of mount points. Ask for help on @email{bug-findutils@@gnu.org}.
4640
4641@item /path/foo/bar changed during execution of find (old inode number 12345, new inode number 67893, filesystem type is <whatever>) [ref XXX]"),
4642This message is issued when @code{find} moves into a directory and
4643discovers that the inode number of that directory
4644is different from the inode number that it obtained when it examined the
4645directory previously. This usually means that while
4646@code{find} was deep in a directory hierarchy doing a
4647time consuming operation, somebody has moved one of the parent directories to
4648another location in the same filesystem. This may or may not have been done
4649maliciously. In any case, @code{find} stops at this point
4650to avoid traversing parts of the filesystem that it wasn't
4651intended. You can use @code{ls -li} or @code{find /path -inum
465212345 -o -inum 67893} to find out more about what has happened.
4653
4654@item sanity check of the fnmatch() library function failed.
4655Please submit a bug report. You may well be asked questions about
4656your system, and if you compiled the @code{findutils} code yourself,
4657you should keep your copy of the build tree around. The likely
4658explanation is that your system has a buggy implementation of
4659@code{fnmatch} that looks enough like the GNU version to fool
4660@code{configure}, but which doesn't work properly.
4661
4662@item cannot fork
4663This normally happens if you use the @code{-exec} action or
4664something similar (@code{-ok} and so forth) but the system has run out
4665of free process slots. This is either because the system is very busy
4666and the system has reached its maximum process limit, or because you
4667have a resource limit in place and you've reached it. Check the
4668system for runaway processes (with @code{ps}, if possible). Some process
4669slots are normally reserved for use by @samp{root}.
4670
4671@item some-program terminated by signal 99
4672Some program which was launched with @code{-exec} or similar was killed
4673with a fatal signal. This is just an advisory message.
4674@end table
4675
4676
4677@node Error Messages From xargs, Error Messages From locate, Error Messages From find, Error Messages
4678@section Error Messages From xargs
4679
4680@table @samp
4681@item environment is too large for exec
4682This message means that you have so many environment variables set (or
4683such large values for them) that there is no room within the
4684system-imposed limits on program command line argument length to
4685invoke any program. This is an unlikely situation and is more likely
4686result of an attempt to test the limits of @code{xargs}, or break it.
4687Please try unsetting some environment variables, or exiting the
4688current shell. You can also use @samp{xargs --show-limits} to
4689understand the relevant sizes.
4690
4691@item can not fit single argument within argument list size limit
4692You are using the @samp{-I} option and @code{xargs} doesn't have
4693enough space to build a command line because it has read a really
4694large item and it doesn't fit. You can probably work around this
4695problem with the @samp{-s} option, but the default size is pretty
4696large. This is a rare situation and is more likely an attempt to test
4697the limits of @code{xargs}, or break it. Otherwise, you will need to
4698try to shorten the problematic argument or not use @code{xargs}.
4699
4700@item cannot fork
4701See the description of the similar message for @code{find}.
4702
4703@item <program>: exited with status 255; aborting
4704When a command run by @code{xargs} exits with status 255, @code{xargs}
4705is supposed to stop. If this is not what you intended, wrap the
4706program you are trying to invoke in a shell script which doesn't
4707return status 255.
4708
4709@item <program>: terminated by signal 99
4710See the description of the similar message for @code{find}.
4711@end table
4712
4713@node Error Messages From locate, Error Messages From updatedb, Error Messages From xargs, Error Messages
4714@section Error Messages From @code{locate}
4715
4716@table @samp
4717@item warning: database `/usr/local/var/locatedb' is more than 8 days old
4718The @code{locate} program relies on a database which is periodically
4719built by the @code{updatedb} program. That hasn't happened in a long
4720time. To fix this problem, run @code{updatedb} manually. This can
4721often happen on systems that are generally not left on, so the
4722periodic ``cron'' task which normally does this doesn't get a chance
4723to run.
4724
4725@item locate database `/usr/local/var/locatedb' is corrupt or invalid
4726This should not happen. Re-run @code{updatedb}. If that works, but
4727@code{locate} still produces this error, run @code{locate --version}
4728and @code{updatedb --version}. These should produce the same output.
4729If not, you are using a mixed toolset; check your @samp{$PATH}
4730environment variable and your shell aliases (if you have any). If
4731both programs claim to be GNU versions, this is a bug; all versions of
4732these programs should interoperate without problem. Ask for help on
4733@email{bug-findutils@@gnu.org}.
4734@end table
4735
4736
4737@node Error Messages From updatedb, , Error Messages From locate, Error Messages
4738@section Error Messages From updatedb
4739
4740The @code{updatedb} program (and the programs it invokes) do issue
4741error messages, but none seem to be candidates for guidance. If
4742you are having a problem understanding one of these, ask for help on
4743@email{bug-findutils@@gnu.org}.
4744
4745
4746@node Primary Index, , Error Messages, Top
4747@unnumbered @code{find} Primary Index
4748
4749This is a list of all of the primaries (tests, actions, and options)
4750that make up @code{find} expressions for selecting files. @xref{find
4751Expressions}, for more information on expressions.
4752
4753@printindex fn
4754
4755@bye
4756
4757@comment texi related words used by Emacs' spell checker ispell.el
4758
4759@comment LocalWords: texinfo setfilename settitle setchapternewpage
4760@comment LocalWords: iftex finalout ifinfo DIR titlepage vskip pt
4761@comment LocalWords: filll dir samp dfn noindent xref pxref
4762@comment LocalWords: var deffn texi deffnx itemx emph asis
4763@comment LocalWords: findex smallexample subsubsection cindex
4764@comment LocalWords: dircategory direntry itemize
4765
4766@comment other words used by Emacs' spell checker ispell.el
4767@comment LocalWords: README fred updatedb xargs Plett Rendell akefile
4768@comment LocalWords: args grep Filesystems fo foo fOo wildcards iname
4769@comment LocalWords: ipath regex iregex expr fubar regexps
4770@comment LocalWords: metacharacters macs sr sc inode lname ilname
4771@comment LocalWords: sysdep noleaf ls inum xdev filesystems usr atime
4772@comment LocalWords: ctime mtime amin cmin mmin al daystart Sladkey rm
4773@comment LocalWords: anewer cnewer bckw rf xtype uname gname uid gid
4774@comment LocalWords: nouser nogroup chown chgrp perm ch maxdepth
4775@comment LocalWords: mindepth cpio src CD AFS statted stat fstype ufs
4776@comment LocalWords: nfs tmp mfs printf fprint dils rw djm Nov lwall
4777@comment LocalWords: POSIXLY fls fprintf strftime locale's EDT GMT AP
4778@comment LocalWords: EST diff perl backquotes sprintf Falstad Oct cron
4779@comment LocalWords: eg vmunix mkdir afs allexec allwrite ARG bigram
4780@comment LocalWords: bigrams cd chmod comp crc CVS dbfile dum eof
4781@comment LocalWords: fileserver filesystem fn frcode Ghazi Hnewc iXX
4782@comment LocalWords: joeuser Kaveh localpaths localuser LOGNAME
4783@comment LocalWords: Meyering mv netpaths netuser nonblank nonblanks
4784@comment LocalWords: ois ok Pinard printindex proc procs prunefs
4785@comment LocalWords: prunepaths pwd RFS rmadillo rmdir rsh sbins str
4786@comment LocalWords: su Timar ubins ug unstripped vf VM Weitzel
4787@comment LocalWords: wildcard zlogout basename execdir wholename iwholename
4788@comment LocalWords: timestamp timestamps Solaris FreeBSD OpenBSD POSIX
Note: See TracBrowser for help on using the repository browser.