1 \input texinfo @c -*-texinfo-*-
4 @settitle Finding Files
5 @c For double-sided printing, uncomment:
6 @c @setchapternewpage odd
17 * Finding files: (find). Operating on files matching certain criteria.
22 This file documents the GNU utilities for finding files that match
23 certain criteria and performing various operations on them.
25 Copyright (C) 1994,1996,1998,2000,2001,2003,2004 Free Software Foundation, Inc.
27 Permission is granted to make and distribute verbatim copies of
28 this manual provided the copyright notice and this permission notice
29 are preserved on all copies.
32 Permission is granted to process this file through TeX and print the
33 results, provided the printed document carries copying permission
34 notice identical to this one except for the removal of this paragraph
35 (this paragraph not being relevant to the printed manual).
38 Permission is granted to copy and distribute modified versions of this
39 manual under the conditions for verbatim copying, provided that the entire
40 resulting derived work is distributed under the terms of a permission
41 notice identical to this one.
43 Permission is granted to copy and distribute translations of this manual
44 into another language, under the above conditions for modified versions,
45 except that this permission notice may be stated in a translation approved
51 @subtitle Edition @value{EDITION}, for GNU @code{find} version @value{VERSION}
52 @subtitle @value{UPDATED}
53 @author by David MacKenzie
56 @vskip 0pt plus 1filll
57 Copyright @copyright{} 1994,1996,1998,2000,2001,2003,2004 Free Software Foundation, Inc.
59 Permission is granted to make and distribute verbatim copies of
60 this manual provided the copyright notice and this permission notice
61 are preserved on all copies.
63 Permission is granted to copy and distribute modified versions of this
64 manual under the conditions for verbatim copying, provided that the entire
65 resulting derived work is distributed under the terms of a permission
66 notice identical to this one.
68 Permission is granted to copy and distribute translations of this manual
69 into another language, under the above conditions for modified versions,
70 except that this permission notice may be stated in a translation approved
74 @node Top, Introduction, , (dir)
75 @comment node-name, next, previous, up
78 This file documents the GNU utilities for finding files that match
79 certain criteria and performing various actions on them.
80 This is edition @value{EDITION}, for @code{find} version @value{VERSION}.
83 @c The master menu, created with texinfo-master-menu, goes here.
86 * Introduction:: Summary of the tasks this manual describes.
87 * Finding Files:: Finding files that match certain criteria.
88 * Actions:: Doing things to files you have found.
89 * Common Tasks:: Solutions to common real-world problems.
90 * Databases:: Maintaining file name databases.
91 * File Permissions:: How to control access to files.
92 * Reference:: Summary of how to invoke the programs.
93 * Error Messages:: Explanations of some messages you might see.
94 * Primary Index:: The components of @code{find} expressions.
97 @node Introduction, Finding Files, Top, Top
100 This manual shows how to find files that meet criteria you specify, and
101 how to perform various actions on the files that you find. The
102 principal programs that you use to perform these tasks are @code{find},
103 @code{locate}, and @code{xargs}. Some of the examples in this manual
104 use capabilities specific to the GNU versions of those programs.
106 GNU @code{find} was originally written by Eric Decker, with enhancements
107 by David MacKenzie, Jay Plett, and Tim Wood. GNU @code{xargs} was
108 originally written by Mike Rendell, with enhancements by David
109 MacKenzie. GNU @code{locate} and its associated utilities were
110 originally written by James Woods, with enhancements by David MacKenzie.
111 The idea for @samp{find -print0} and @samp{xargs -0} came from Dan
112 Bernstein. The current maintainer of GNU findutils (and this manual) is
113 James Youngman. Many other people have contributed bug fixes, small
114 improvements, and helpful suggestions. Thanks!
116 Mail suggestions and bug reports for these programs to
117 @code{bug-findutils@@gnu.org}. Please include the version
118 number, which you can get by running @samp{find --version}.
129 For brevity, the word @dfn{file} in this manual means a regular file, a
130 directory, a symbolic link, or any other kind of node that has a
131 directory entry. A directory entry is also called a @dfn{file name}. A
132 file name may contain some, all, or none of the directories in a path
133 that leads to the file. These are all examples of what this manual
134 calls ``file names'':
141 /usr/local/include/termcap.h
144 A @dfn{directory tree} is a directory and the files it contains, all of
145 its subdirectories and the files they contain, etc. It can also be a
146 single non-directory file.
148 These programs enable you to find the files in one or more directory
153 have names that contain certain text or match a certain pattern;
155 are links to certain files;
157 were last used during a certain period of time;
159 are within a certain size range;
161 are of a certain type (regular file, directory, symbolic link, etc.);
163 are owned by a certain user or group;
165 have certain access permissions;
167 contain text that matches a certain pattern;
169 are within a certain depth in the directory tree;
171 or some combination of the above.
174 Once you have found the files you're looking for (or files that are
175 potentially the ones you're looking for), you can do more to them than
176 simply list their names. You can get any combination of the files'
177 attributes, or process the files in many ways, either individually or in
178 groups of various sizes. Actions that you might want to perform on the
179 files you have found include, but are not limited to:
189 change access permissions
194 This manual describes how to perform each of those tasks, and more.
199 The principal programs used for making lists of files that match given
200 criteria and running commands on them are @code{find}, @code{locate},
201 and @code{xargs}. An additional command, @code{updatedb}, is used by
202 system administrators to create databases for @code{locate} to use.
204 @code{find} searches for files in a directory hierarchy and prints
205 information about the files it found. It is run like this:
208 find @r{[}@var{file}@dots{}@r{]} @r{[}@var{expression}@r{]}
212 Here is a typical use of @code{find}. This example prints the names of
213 all files in the directory tree rooted in @file{/usr/src} whose name
214 ends with @samp{.c} and that are larger than 100 Kilobytes.
216 find /usr/src -name '*.c' -size +100k -print
219 @code{locate} searches special file name databases for file names that
220 match patterns. The system administrator runs the @code{updatedb}
221 program to create the databases. @code{locate} is run like this:
224 locate @r{[}@var{option}@dots{}@r{]} @var{pattern}@dots{}
228 This example prints the names of all files in the default file name
229 database whose name ends with @samp{Makefile} or @samp{makefile}. Which
230 file names are stored in the database depends on how the system
231 administrator ran @code{updatedb}.
233 locate '*[Mm]akefile'
236 The name @code{xargs}, pronounced EX-args, means ``combine arguments.''
237 @code{xargs} builds and executes command lines by gathering together
238 arguments it reads on the standard input. Most often, these arguments
239 are lists of file names generated by @code{find}. @code{xargs} is run
243 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
247 The following command searches the files listed in the file
248 @file{file-list} and prints all of the lines in them that contain the
251 xargs grep typedef < file-list
254 @node find Expressions
255 @section @code{find} Expressions
257 The expression that @code{find} uses to select files consists of one or
258 more @dfn{primaries}, each of which is a separate command line argument
259 to @code{find}. @code{find} evaluates the expression each time it
260 processes a file. An expression can contain any of the following types
265 affect overall operation rather than the processing of a specific file;
267 return a true or false value, depending on the file's attributes;
269 have side effects and return a true or false value; and
271 connect the other arguments and affect when and whether they are
275 You can omit the operator between two primaries; it defaults to
276 @samp{-and}. @xref{Combining Primaries With Operators}, for ways to
277 connect primaries into more complex expressions. If the expression
278 contains no actions other than @samp{-prune}, @samp{-print} is performed
279 on all files for which the entire expression is true (@pxref{Print File
282 Options take effect immediately, rather than being evaluated for each
283 file when their place in the expression is reached. Therefore, for
284 clarity, it is best to place them at the beginning of the expression.
286 Many of the primaries take arguments, which immediately follow them in
287 the next command line argument to @code{find}. Some arguments are file
288 names, patterns, or other strings; others are numbers. Numeric
289 arguments can be specified as
293 for greater than @var{n},
295 for less than @var{n},
300 @node Finding Files, Actions, Introduction, Top
301 @chapter Finding Files
303 By default, @code{find} prints to the standard output the names of the
304 files that match the given criteria. @xref{Actions}, for how to get more
305 information about the matching files.
319 * Combining Primaries With Operators::
325 Here are ways to search for files whose name matches a certain pattern.
326 @xref{Shell Pattern Matching}, for a description of the @var{pattern}
327 arguments to these tests.
329 Each of these tests has a case-sensitive version and a case-insensitive
330 version, whose name begins with @samp{i}. In a case-insensitive
331 comparison, the patterns @samp{fo*} and @samp{F??} match the file names
332 @file{Foo}, @samp{FOO}, @samp{foo}, @samp{fOo}, etc.
335 * Base Name Patterns::
336 * Full Name Patterns::
337 * Fast Full Name Search::
338 * Shell Pattern Matching:: Wildcards used by these programs.
341 @node Base Name Patterns
342 @subsection Base Name Patterns
344 @deffn Test -name pattern
345 @deffnx Test -iname pattern
346 True if the base of the file name (the path with the leading directories
347 removed) matches shell pattern @var{pattern}. For @samp{-iname}, the
348 match is case-insensitive. To ignore a whole directory tree, use
349 @samp{-prune} (@pxref{Directories}). As an example, to find Texinfo
350 source files in @file{/usr/local/doc}:
353 find /usr/local/doc -name '*.texi'
357 Patterns for @samp{-name} and @samp{-iname} will match a filename with
358 a leading @samp{.}. For example the command @samp{find /tmp -name
359 \*bar} will match the file @file{/tmp/.foobar}.
362 @node Full Name Patterns
363 @subsection Full Name Patterns
365 @deffn Test -wholename pattern
366 @deffnx Test -iwholename pattern
367 True if the entire file name, starting with the command line argument
368 under which the file was found, matches shell pattern @var{pattern}.
369 For @samp{-iwholename}, the match is case-insensitive. To ignore a whole
370 directory tree, use @samp{-prune} rather than checking every file in the
371 tree (@pxref{Directories}).
374 @deffn Test -path pattern
375 @deffnx Test -ipath pattern
376 These tests are deprecated, but work as for @samp{-wholename} and @samp{-iwholename},
377 respectively. The @samp{-ipath} test is a GNU extension, but @samp{-path} is also
378 provided by HP-UX @code{find}.
381 @deffn Test -regex expr
382 @deffnx Test -iregex expr
383 True if the entire file name matches regular expression @var{expr}.
384 This is a match on the whole path, not a search. For example, to match
385 a file named @file{./fubar3}, you can use the regular expression
386 @samp{.*bar.} or @samp{.*b.*3}, but not @samp{b.*r3}. @xref{Regexps, ,
387 Syntax of Regular Expressions, emacs, The GNU Emacs Manual}, for a
388 description of the syntax of regular expressions. For @samp{-iregex},
389 the match is case-insensitive.
392 @node Fast Full Name Search
393 @subsection Fast Full Name Search
395 To search for files by name without having to actually scan the
396 directories on the disk (which can be slow), you can use the
397 @code{locate} program. For each shell pattern you give it,
398 @code{locate} searches one or more databases of file names and displays
399 the file names that contain the pattern. @xref{Shell Pattern Matching},
400 for details about shell patterns.
402 If a pattern is a plain string---it contains no
403 metacharacters---@code{locate} displays all file names in the database
404 that contain that string. If a pattern contains
405 metacharacters, @code{locate} only displays file names that match the
406 pattern exactly. As a result, patterns that contain metacharacters
407 should usually begin with a @samp{*}, and will most often end with one
408 as well. The exceptions are patterns that are intended to explicitly
409 match the beginning or end of a file name.
411 If you only want @code{locate} to match against the last component of
412 the filenames (the ``base name'' of the files) you can use the
413 @samp{--basename} option. The opposite behaviour is the default, but
414 can be selected explicitly by using the option @samp{--wholename}.
421 is almost equivalent to
423 find @var{directories} -name @var{pattern}
426 where @var{directories} are the directories for which the file name
427 databases contain information. The differences are that the
428 @code{locate} information might be out of date, and that @code{locate}
429 handles wildcards in the pattern slightly differently than @code{find}
430 (@pxref{Shell Pattern Matching}).
432 The file name databases contain lists of files that were on the system
433 when the databases were last updated. The system administrator can
434 choose the file name of the default database, the frequency with which
435 the databases are updated, and the directories for which they contain
438 Here is how to select which file name databases @code{locate} searches.
439 The default is system-dependent.
442 @item --database=@var{path}
444 Instead of searching the default file name database, search the file
445 name databases in @var{path}, which is a colon-separated list of
446 database file names. You can also use the environment variable
447 @code{LOCATE_PATH} to set the list of database files to search. The
448 option overrides the environment variable if both are used.
451 @node Shell Pattern Matching
452 @subsection Shell Pattern Matching
454 @code{find} and @code{locate} can compare file names, or parts of file
455 names, to shell patterns. A @dfn{shell pattern} is a string that may
456 contain the following special characters, which are known as
457 @dfn{wildcards} or @dfn{metacharacters}.
459 You must quote patterns that contain metacharacters to prevent the shell
460 from expanding them itself. Double and single quotes both work; so does
461 escaping with a backslash.
465 Matches any zero or more characters.
468 Matches any one character.
471 Matches exactly one character that is a member of the string
472 @var{string}. This is called a @dfn{character class}. As a shorthand,
473 @var{string} may contain ranges, which consist of two characters with a
474 dash between them. For example, the class @samp{[a-z0-9_]} matches a
475 lowercase letter, a number, or an underscore. You can negate a class by
476 placing a @samp{!} or @samp{^} immediately after the opening bracket.
477 Thus, @samp{[^A-Z@@]} matches any character except an uppercase letter
481 Removes the special meaning of the character that follows it. This
482 works even in character classes.
485 In the @code{find} tests that do shell pattern matching (@samp{-name},
486 @samp{-wholename}, etc.), wildcards in the pattern will match a @samp{.}
487 at the beginning of a file name. This is also the case for
488 @code{locate}. Thus, @samp{find -name '*macs'} will match a file
489 named @file{.emacs}, as will @samp{locate '*macs'}.
491 Slash characters have no special significance in the shell pattern
492 matching that @code{find} and @code{locate} do, unlike in the shell, in
493 which wildcards do not match them. Therefore, a pattern @samp{foo*bar}
494 can match a file name @samp{foo3/bar}, and a pattern @samp{./sr*sc} can
495 match a file name @samp{./src/misc}.
497 If you want to locate some files with the @samp{locate} command but
498 don't need to see the full list you can use the @samp{--limit} option
499 to see just a small number of results, or the @samp{--count} option to
500 display only the total number of matches.
505 There are two ways that files can be linked together. @dfn{Symbolic
506 links} are a special type of file whose contents are a portion of the
507 name of another file. @dfn{Hard links} are multiple directory entries
508 for one file; the file names all have the same index node (@dfn{inode})
517 @subsection Symbolic Links
519 Symbolic links are names that reference other files. GNU @code{find}
520 will handle symbolic links in one of two ways; firstly, it can
521 dereference the links for you - this means that if it comes across a
522 symbolic link, it examines the file that the link points to, in order
523 to see if it matches the criteria you have specified. Secondly, it
524 can check the link itself in case you might be looking for the actual
525 link. If the file that the symbolic link points to is also within the
526 directory hierarchy you are searching with the @code{find} command,
527 you may not see a great deal of difference between these two
530 By default, @code{find} examines symbolic links themselves when it
531 finds them (and, if it later comes across the linked-to file, it will
532 examine that, too). If you would prefer @code{find} to dereference
533 the links and examine the file that each link points to, specify the
534 @samp{-L} option to @code{find}. You can explicitly specify the
535 default behaviour by using the @samp{-P} option. The @samp{-H}
536 option is a half-way-between option which ensures that any symbolic
537 links listed on the command line are dereferenced, but other symbolic
540 Symbolic links are different to ``hard links'' in the sense that you
541 need permissions upon the linked-to file in order to be able to
542 dereference the link. This can mean that even if you specify the
543 @samp{-L} option, find may not be able to determine the properties of
544 the file that the link points to (because you don't have sufficient
545 permissions). In this situation, @samp{find} uses the properties of
546 the link itself. This also occurs if a symbolic link exists but
547 points to a file that is missing.
549 The options controlling the behaviour of @code{find} with respect to
550 links are as follows :-
554 @code{find} does not dereference symbolic links at all. This is the
555 default behaviour. This option must be specified before any of the
556 path names on the command line.
558 @code{find} does not dereference symbolic links (except in the case of
559 file names on the command line, which are dereferenced). If a
560 symbolic link cannot be dereferenced, the information for the symbolic
561 link itself is used. This option must be specified before any of the
562 path names on the command line.
564 @code{find} dereferences symbolic links where possible, and where this
565 is not possible it uses the properties of the symbolic link itself.
566 This option must be specified before any of the path names on the
567 command line. Use of this option also implies the same behaviour as
568 the @samp{-noleaf} option. If you later use the @samp{-H} or
569 @samp{-P} options, this does not turn off @samp{-noleaf}.
572 This option forms part of the ``expression'' and must be specified
573 after the path names, but it is otherwise equivalent to @samp{-L}.
576 The following differences in behavior occur when the @samp{-L} option
581 @code{find} follows symbolic links to directories when searching
584 @samp{-lname} and @samp{-ilname} always return false (unless they
585 happen to match broken symbolic links).
587 @samp{-type} reports the types of the files that symbolic links point
590 Implies @samp{-noleaf} (@pxref{Directories}).
593 If the @samp{-L} option or the @samp{-H} option is used,
594 the filenames used as arguments to @samp{-newer}, @samp{-anewer}, and
595 @samp{-cnewer} are dereferenced and the timestamp from the pointed-to
596 file is used instead (if possible -- otherwise the timestamp from the
597 symbolic link is used).
599 @deffn Test -lname pattern
600 @deffnx Test -ilname pattern
601 True if the file is a symbolic link whose contents match shell pattern
602 @var{pattern}. For @samp{-ilname}, the match is case-insensitive.
603 @xref{Shell Pattern Matching}, for details about the @var{pattern}
604 argument. If the @samp{-L} option is in effect, this test will always
605 fail for symbolic links unless they are broken. So, to list any
606 symbolic links to @file{sysdep.c} in the current directory and its
607 subdirectories, you can do:
610 find . -lname '*sysdep.c'
615 @subsection Hard Links
617 To find hard links, first get the inode number of the file whose links
618 you want to find. You can learn a file's inode number and the number of
619 links to it by running @samp{ls -i} or @samp{find -ls}. If the file has
620 more than one link, you can search for the other links by passing that
621 inode number to @samp{-inum}. Add the @samp{-xdev} option if you are
622 starting the search at a directory that has other filesystems mounted on
623 it, such as @file{/usr} on many systems. Doing this saves needless
624 searching, since hard links to a file must be on the same filesystem.
628 File has inode number @var{n}. The @samp{+} and @samp{-} qualifiers
629 also work, though these are rarely useful.
632 You can also search for files that have a certain number of links, with
633 @samp{-links}. Directories normally have at least two hard links; their
634 @file{.} entry is the second one. If they have subdirectories, each of
635 those also has a hard link called @file{..} to its parent directory.
638 File has @var{n} hard links.
641 @deffn Test -links +n
642 File has more than @var{n} hard links.
645 @deffn Test -links -n
646 File has fewer than @var{n} hard links.
652 Each file has three time stamps, which record the last time that certain
653 operations were performed on the file:
657 access (read the file's contents)
659 change the status (modify the file or its attributes)
661 modify (change the file's contents)
664 There is no timestamp that indicates when a file was @emph{created}.
666 You can search for files whose time stamps are within a certain age
667 range, or compare them to other time stamps.
671 * Comparing Timestamps::
675 @subsection Age Ranges
677 These tests are mainly useful with ranges (@samp{+@var{n}} and
681 @deffnx Test -ctime n
682 @deffnx Test -mtime n
683 True if the file was last accessed (or its status changed, or it was
684 modified) @var{n}*24 hours ago. The number of 24-hour periods since
685 the file's timestamp is always rounded down; therefore 0 means ``less
686 than 24 hours ago'', 1 means ``between 24 and 48 hours ago'', and so
693 True if the file was last accessed (or its status changed, or it was
694 modified) @var{n} minutes ago. These tests provide finer granularity of
695 measurement than @samp{-atime} et al., but rounding is done in a
696 similar way. For example, to list files in
697 @file{/u/bill} that were last read from 2 to 6 minutes ago:
700 find /u/bill -amin +2 -amin -6
704 @deffn Option -daystart
705 Measure times from the beginning of today rather than from 24 hours ago.
706 So, to list the regular files in your home directory that were modified
710 find ~ -daystart -type f -mtime 1
714 The @samp{-daystart} option is unlike most other options in that it
715 has an effect on the way that other tests are performed. The affected
716 tests are @samp{-amin}, @samp{-cmin}, @samp{-mmin}, @samp{-atime},
717 @samp{-ctime} and @samp{-mtime}.
719 @node Comparing Timestamps
720 @subsection Comparing Timestamps
722 As an alternative to comparing timestamps to the current time, you can
723 compare them to another file's timestamp. That file's timestamp could
724 be updated by another program when some event occurs. Or you could set
725 it to a particular fixed date using the @code{touch} command. For
726 example, to list files in @file{/usr} modified after February 1 of the
729 @c Idea from Rick Sladkey.
731 touch -t 02010000 /tmp/stamp$$
732 find /usr -newer /tmp/stamp$$
736 @deffn Test -anewer file
737 @deffnx Test -cnewer file
738 @deffnx Test -newer file
739 True if the file was last accessed (or its status changed, or it was
740 modified) more recently than @var{file} was modified. These tests are
741 affected by @samp{-follow} only if @samp{-follow} comes before them on
742 the command line. @xref{Symbolic Links}, for more information on
743 @samp{-follow}. As an example, to list any files modified since
744 @file{/bin/sh} was last modified:
747 find . -newer /bin/sh
752 True if the file was last accessed @var{n} days after its status was
753 last changed. Useful for finding files that are not being used, and
754 could perhaps be archived or removed to save disk space.
760 @deffn Test -size n@r{[}bckwMG@r{]}
761 True if the file uses @var{n} units of space, rounding up. The units
762 are 512-byte blocks by default, but they can be changed by adding a
763 one-character suffix to @var{n}:
767 512-byte blocks (never 1024)
771 kilobytes (1024 bytes)
780 The `b' suffix always considers blocks to be 512 bytes. This is not
781 affected by the setting (or non-setting) of the POSIXLY_CORRECT
782 environment variable. This behaviour is different to the behaviour of
783 the @samp{-ls} action). If you want to use 1024-byte units, use the
786 The number can be prefixed with a `+' or a `-'. A plus sign indicates
787 that the test should succeed if the file uses at least @var{n} units
788 of storage (this is the way I normally use this test) and a minus sign
789 indicates that the test should succeed if the file uses less than
790 @var{n} units of storage. There is no `=' prefix, because that's the
793 The size does not count indirect blocks, but it does count blocks in
794 sparse files that are not actually allocated. This handling of sparse
795 files differs from the output of the @samp{%k} and @samp{%b} format
796 specifiers for the @samp{-printf} predicate.
801 True if the file is empty and is either a regular file or a directory.
802 This might make it a good candidate for deletion. This test is useful
803 with @samp{-depth} (@pxref{Directories}) and @samp{-delete}
804 (@pxref{Single File}).
811 True if the file is of type @var{c}:
815 block (buffered) special
817 character (unbuffered) special
834 The same as @samp{-type} unless the file is a symbolic link. For
835 symbolic links: if @samp{-follow} has not been given, true if the file
836 is a link to a file of type @var{c}; if @samp{-follow} has been given,
837 true if @var{c} is @samp{l}. In other words, for symbolic links,
838 @samp{-xtype} checks the type of the file that @samp{-type} does not
839 check. @xref{Symbolic Links}, for more information on @samp{-follow}.
845 @deffn Test -user uname
846 @deffnx Test -group gname
847 True if the file is owned by user @var{uname} (belongs to group @var{gname}).
848 A numeric ID is allowed.
853 True if the file's numeric user ID (group ID) is @var{n}. These tests
854 support ranges (@samp{+@var{n}} and @samp{-@var{n}}), unlike
855 @samp{-user} and @samp{-group}.
859 @deffnx Test -nogroup
860 True if no user corresponds to the file's numeric user ID (no group
861 corresponds to the numeric group ID). These cases usually mean that the
862 files belonged to users who have since been removed from the system.
863 You probably should change the ownership of such files to an existing
864 user or group, using the @code{chown} or @code{chgrp} program.
870 @xref{File Permissions}, for information on how file permissions are
871 structured and how to specify them.
873 @deffn Test -perm mode
875 file's permissions are exactly @var{mode} (which can be numeric or symbolic).
876 Symbolic modes use mode 0 as a point of departure.
877 If @var{mode} starts with @samp{-}, true if
878 @emph{all} of the permissions set in @var{mode} are set for the file;
879 permissions not set in @var{mode} are ignored.
880 If @var{mode} starts with @samp{+}, true if
881 @emph{any} of the permissions set in @var{mode} are set for the file;
882 permissions not set in @var{mode} are ignored.
888 To search for files based on their contents, you can use the @code{grep}
889 program. For example, to find out which C source files in the current
890 directory contain the string @samp{thing}, you can do:
896 If you also want to search for the string in files in subdirectories,
897 you can combine @code{grep} with @code{find} and @code{xargs}, like
901 find . -name '*.[ch]' | xargs grep -l thing
904 The @samp{-l} option causes @code{grep} to print only the names of files
905 that contain the string, rather than the lines that contain it. The
906 string argument (@samp{thing}) is actually a regular expression, so it
907 can contain metacharacters. This method can be refined a little by
908 using the @samp{-r} option to make @code{xargs} not run @code{grep} if
909 @code{find} produces no output, and using the @code{find} action
910 @samp{-print0} and the @code{xargs} option @samp{-0} to avoid
911 misinterpreting files whose names contain spaces:
914 find . -name '*.[ch]' -print0 | xargs -r -0 grep -l thing
917 For a fuller treatment of finding files whose contents match a pattern,
918 see the manual page for @code{grep}.
923 Here is how to control which directories @code{find} searches, and how
924 it searches them. These two options allow you to process a horizontal
925 slice of a directory tree.
927 @deffn Option -maxdepth levels
928 Descend at most @var{levels} (a non-negative integer) levels of
929 directories below the command line arguments. @samp{-maxdepth 0} means
930 only apply the tests and actions to the command line arguments.
933 @deffn Option -mindepth levels
934 Do not apply any tests or actions at levels less than @var{levels} (a
935 non-negative integer). @samp{-mindepth 1} means process all files
936 except the command line arguments.
940 Process each directory's contents before the directory itself. Doing
941 this is a good idea when producing lists of files to archive with
942 @code{cpio} or @code{tar}. If a directory does not have write
943 permission for its owner, its contents can still be restored from the
944 archive since the directory's permissions are restored after its contents.
948 This is a deprecated synonym for @samp{-depth}, for compatibility with
949 Mac OS X, FreeBSD and OpenBSD. The @samp{-depth} option is a POSIX
950 feature, so it is better to use that.
954 If @samp{-depth} is not given, true; do not descend into the current
955 directory. If @samp{-depth} is given, false; no effect. @samp{-prune}
956 only affects tests and actions that come after it in the expression, not
957 those that come before.
959 For example, to skip the directory @file{src/emacs} and all files and
960 directories under it, and print the names of the other files found:
963 find . -wholename './src/emacs' -prune -o -print
968 Exit immediately with return value zero. No child processes will be
969 left running, but no more paths specified on the command line will be
970 processed. For example, @code{find /tmp/foo /tmp/bar -print -quit}
971 will print only @samp{/tmp/foo}.
974 @deffn Option -noleaf
975 Do not optimize by assuming that directories contain 2 fewer
976 subdirectories than their hard link count. This option is needed when
977 searching filesystems that do not follow the Unix directory-link
978 convention, such as CD-ROM or MS-DOS filesystems or AFS volume mount
979 points. Each directory on a normal Unix filesystem has at least 2 hard
980 links: its name and its @file{.} entry. Additionally, its
981 subdirectories (if any) each have a @file{..} entry linked to that
982 directory. When @code{find} is examining a directory, after it has
983 statted 2 fewer subdirectories than the directory's link count, it knows
984 that the rest of the entries in the directory are non-directories
985 (@dfn{leaf} files in the directory tree). If only the files' names need
986 to be examined, there is no need to stat them; this gives a significant
987 increase in search speed.
990 @deffn Option -ignore_readdir_race
991 If a file disappears after its name has been read from a directory but
992 before @code{find} gets around to examining the file with @code{stat},
993 don't issue an error message. If you don't specify this option, an
994 error message will be issued. This option can be useful in system
995 scripts (cron scripts, for example) that examine areas of the
996 filesystem that change frequently (mail queues, temporary directories,
997 and so forth), because this scenario is common for those sorts of
998 directories. Completely silencing error messages from @code{find} is
999 undesirable, so this option neatly solves the problem. There is no
1000 way to search one part of the filesystem with this option on and part
1001 of it with this option off, though.
1004 @deffn Option -noignore_readdir_race
1005 This option reverses the effect of the @samp{-ignore_readdir_race} option.
1010 @section Filesystems
1012 A @dfn{filesystem} is a section of a disk, either on the local host or
1013 mounted from a remote host over a network. Searching network
1014 filesystems can be slow, so it is common to make @code{find} avoid them.
1016 There are two ways to avoid searching certain filesystems. One way is
1017 to tell @code{find} to only search one filesystem:
1020 @deffnx Option -mount
1021 Don't descend directories on other filesystems. These options are synonyms.
1024 The other way is to check the type of filesystem each file is on, and
1025 not descend directories that are on undesirable filesystem types:
1027 @deffn Test -fstype type
1028 True if the file is on a filesystem of type @var{type}. The valid
1029 filesystem types vary among different versions of Unix; an incomplete
1030 list of filesystem types that are accepted on some version of Unix or
1033 ext2 ext3 proc sysfs ufs 4.2 4.3 nfs tmp mfs S51K S52K
1035 You can use @samp{-printf} with the @samp{%F} directive to see the types
1036 of your filesystems. The @samp{%D} directive shows the device number.
1037 @xref{Print File Information}. @samp{-fstype} is
1038 usually used with @samp{-prune} to avoid searching remote filesystems
1039 (@pxref{Directories}).
1042 @node Combining Primaries With Operators
1043 @section Combining Primaries With Operators
1045 Operators build a complex expression from tests and actions.
1046 The operators are, in order of decreasing precedence:
1049 @item @asis{( @var{expr} )}
1051 Force precedence. True if @var{expr} is true.
1053 @item @asis{! @var{expr}}
1054 @itemx @asis{-not @var{expr}}
1057 True if @var{expr} is false.
1059 @item @asis{@var{expr1 expr2}}
1060 @itemx @asis{@var{expr1} -a @var{expr2}}
1061 @itemx @asis{@var{expr1} -and @var{expr2}}
1064 And; @var{expr2} is not evaluated if @var{expr1} is false.
1066 @item @asis{@var{expr1} -o @var{expr2}}
1067 @itemx @asis{@var{expr1} -or @var{expr2}}
1070 Or; @var{expr2} is not evaluated if @var{expr1} is true.
1072 @item @asis{@var{expr1} , @var{expr2}}
1074 List; both @var{expr1} and @var{expr2} are always evaluated. True if
1075 @var{expr2} is true. The value of @var{expr1} is discarded. This
1076 operator lets you do multiple independent operations on one traversal,
1077 without depending on whether other operations succeeded.
1080 @code{find} searches the directory tree rooted at each file name by
1081 evaluating the expression from left to right, according to the rules of
1082 precedence, until the outcome is known (the left hand side is false for
1083 @samp{-and}, true for @samp{-or}), at which point @code{find} moves on
1084 to the next file name.
1086 There are two other tests that can be useful in complex expressions:
1096 @node Actions, Common Tasks, Finding Files, Top
1099 There are several ways you can print information about the files that
1100 match the criteria you gave in the @code{find} expression. You can
1101 print the information either to the standard output or to a file that
1102 you name. You can also execute commands that have the file names as
1103 arguments. You can use those commands as further filters to select files.
1107 * Print File Information::
1113 @node Print File Name
1114 @section Print File Name
1116 @deffn Action -print
1117 True; print the full file name on the standard output, followed by a
1121 @deffn Action -fprint file
1122 True; print the full file name into file @var{file}, followed by a
1123 newline. If @var{file} does not exist when @code{find} is run, it is
1124 created; if it does exist, it is truncated to 0 bytes. The file names
1125 @file{/dev/stdout} and @file{/dev/stderr} are handled specially; they
1126 refer to the standard output and standard error output, respectively.
1129 @node Print File Information
1130 @section Print File Information
1133 True; list the current file in @samp{ls -dils} format on the standard
1134 output. The output looks like this:
1137 204744 17 -rw-r--r-- 1 djm staff 17337 Nov 2 1992 ./lwall-quotes
1144 The inode number of the file. @xref{Hard Links}, for how to find files
1145 based on their inode number.
1148 the number of blocks in the file. The block counts are of 1K blocks,
1149 unless the environment variable @code{POSIXLY_CORRECT} is set, in which
1150 case 512-byte blocks are used. @xref{Size}, for how to find files based
1154 The file's type and permissions. The type is shown as a dash for a
1155 regular file; for other file types, a letter like for @samp{-type} is
1156 used (@pxref{Type}). The permissions are read, write, and execute for
1157 the file's owner, its group, and other users, respectively; a dash means
1158 the permission is not granted. @xref{File Permissions}, for more details
1159 about file permissions. @xref{Permissions}, for how to find files based
1160 on their permissions.
1163 The number of hard links to the file.
1166 The user who owns the file.
1172 The file's size in bytes.
1175 The date the file was last modified.
1178 The file's name. @samp{-ls} quotes non-printable characters in the file
1179 names using C-like backslash escapes.
1183 @deffn Action -fls file
1184 True; like @samp{-ls} but write to @var{file} like @samp{-fprint}
1185 (@pxref{Print File Name}).
1188 @deffn Action -printf format
1189 True; print @var{format} on the standard output, interpreting @samp{\}
1190 escapes and @samp{%} directives. Field widths and precisions can be
1191 specified as with the @code{printf} C function. Format flags (like
1192 @samp{#} for example) may not work as you expect because many of the
1193 fields, even numeric ones, are printed with %s. This means though
1194 that the format flag @samp{-} will work; it forces left-alignment of
1195 the field. Unlike @samp{-print}, @samp{-printf} does not add a
1196 newline at the end of the string. If you want a newline at the end of
1197 the string, add a @samp{\n}.
1200 @deffn Action -fprintf file format
1201 True; like @samp{-printf} but write to @var{file} like @samp{-fprint}
1202 (@pxref{Print File Name}).
1207 * Format Directives::
1214 The escapes that @samp{-printf} and @samp{-fprintf} recognize are:
1222 Stop printing from this format immediately and flush the output.
1234 A literal backslash (@samp{\}).
1236 The character whose ASCII code is NNN (octal).
1239 A @samp{\} character followed by any other character is treated as an
1240 ordinary character, so they both are printed, and a warning message is
1241 printed to the standard error output (because it was probably a typo).
1243 @node Format Directives
1244 @subsection Format Directives
1246 @samp{-printf} and @samp{-fprintf} support the following format
1247 directives to print information about the file being processed. The C
1248 @code{printf} function, field width and precision specifiers are
1249 supported, as applied to string (%s) types. That is, you can specify
1250 "minimum field width"."maximum field width" for each directive.
1251 Format flags (like @samp{#} for example) may not work as you expect
1252 because many of the fields, even numeric ones, are printed with %s.
1253 The format flag @samp{-} does work; it forces left-alignment of the
1256 @samp{%%} is a literal percent sign. A @samp{%} character followed by
1257 an unrecognised character (i.e. not a known directive or printf field
1258 width and precision specifier), is discarded (but the unrecognised character
1259 is printed), and a warning message is printed to the standard error output
1260 (because it was probably a typo).
1264 * Ownership Directives::
1266 * Location Directives::
1268 * Formatting Flags::
1271 @node Name Directives
1272 @subsubsection Name Directives
1277 File's name (not the absolute path name, but the name of the file as
1278 it was encountered by find - that is, as a relative path from one of
1279 the starting points).
1281 File's name with any leading directories removed (only the last element).
1284 Leading directories of file's name (all but the last element and the
1288 File's name with the name of the command line argument under which
1289 it was found removed from the beginning.
1292 Command line argument under which file was found.
1296 @node Ownership Directives
1297 @subsubsection Ownership Directives
1302 File's group name, or numeric group ID if the group has no name.
1305 @c TODO: Needs to support # flag and 0 flag
1306 File's numeric group ID.
1309 File's user name, or numeric user ID if the user has no name.
1312 @c TODO: Needs to support # flag
1313 File's numeric user ID.
1315 @c full support, including # and 0.
1316 File's permissions (in octal). If you always want to have a leading
1317 zero on the number, use the '#' format flag, for example '%#m'.
1320 @node Size Directives
1321 @subsubsection Size Directives
1325 Amount of disk space occupied by the file, measured in 1K blocks
1326 (rounded up). This can be less than the length of the file if
1327 it is a sparse file (that is, it has ``holes'').
1329 File's size in 512-byte blocks (rounded up). This also can be less
1330 than the length of the file, if the file is sparse.
1332 File's size in bytes.
1335 @node Location Directives
1336 @subsubsection Location Directives
1340 File's depth in the directory tree (depth below a file named on the
1341 command line, not depth below the root directory). Files named on the
1342 command line have a depth of 0. Subdirectories immediately below them
1343 have a depth of 1, and so on.
1345 The device number on which the file exists (the @code{st_dev} field of
1346 @code{struct stat}), in decimal.
1348 Type of the filesystem the file is on; this value can be used for
1349 @samp{-fstype} (@pxref{Directories}).
1351 Object of symbolic link (empty string if file is not a symbolic link).
1353 File's inode number (in decimal).
1355 Number of hard links to file.
1357 Type of the file as used with @samp{-type}. If the file is a symbolic
1358 link, @samp{l} will be printed.
1360 Type of the file as used with @samp{-type}. If the file is a symbolic
1361 link, it is dereferenced. If the file is a broken symbolic link,
1362 @samp{N} is printed.
1366 @node Time Directives
1367 @subsubsection Time Directives
1369 Some of these directives use the C @code{ctime} function. Its output
1370 depends on the current locale, but it typically looks like
1373 Wed Nov 2 00:42:36 1994
1378 File's last access time in the format returned by the C @code{ctime} function.
1380 File's last access time in the format specified by @var{k}
1381 (@pxref{Time Formats}).
1383 File's last status change time in the format returned by the C @code{ctime}
1386 File's last status change time in the format specified by @var{k}
1387 (@pxref{Time Formats}).
1389 File's last modification time in the format returned by the C @code{ctime}
1392 File's last modification time in the format specified by @var{k}
1393 (@pxref{Time Formats}).
1397 @subsection Time Formats
1399 Below are the formats for the directives @samp{%A}, @samp{%C}, and
1400 @samp{%T}, which print the file's timestamps. Some of these formats
1401 might not be available on all systems, due to differences in the C
1402 @code{strftime} function between systems.
1407 * Combined Time Formats::
1410 @node Time Components
1411 @subsubsection Time Components
1413 The following format directives print single components of the time.
1427 time zone (e.g., EDT), or nothing if no time zone is determinable
1433 seconds since Jan. 1, 1970, 00:00 GMT.
1436 @node Date Components
1437 @subsubsection Date Components
1439 The following format directives print single components of the date.
1443 locale's abbreviated weekday name (Sun..Sat)
1445 locale's full weekday name, variable length (Sunday..Saturday)
1448 locale's abbreviated month name (Jan..Dec)
1450 locale's full month name, variable length (January..December)
1454 day of month (01..31)
1458 day of year (001..366)
1460 week number of year with Sunday as first day of week (00..53)
1462 week number of year with Monday as first day of week (00..53)
1466 last two digits of year (00..99)
1469 @node Combined Time Formats
1470 @subsubsection Combined Time Formats
1472 The following format directives print combinations of time and date
1477 time, 12-hour (hh:mm:ss [AP]M)
1479 time, 24-hour (hh:mm:ss)
1481 locale's time representation (H:M:S)
1483 locale's date and time (Sat Nov 04 12:02:33 EST 1989)
1487 locale's date representation (mm/dd/yy)
1489 Date and time, separated by '+', for example `2004-04-28+22:22:05'.
1490 The time is given in the current timezone (which may be affected by
1491 setting the TZ environment variable). This is a GNU extension.
1494 @node Formatting Flags
1495 @subsubsection Formatting Flags
1497 The @samp{%m} and @samp{%d} directives support the @samp{#}, @samp{0}
1498 and @samp{+} flags, but the other directives do not, even if they
1499 print numbers. Numeric directives that do not support these flags
1509 All fields support the format flag @samp{-}, which makes fields
1510 left-aligned. That is, if the field width is greater than the actual
1511 contents of the field, the requisite number of spaces are printed
1512 after the field content instead of before it.
1515 @section Run Commands
1517 You can use the list of file names created by @code{find} or
1518 @code{locate} as arguments to other commands. In this way you can
1519 perform arbitrary actions on the files.
1528 @subsection Single File
1530 Here is how to run a command on one file at a time.
1532 @deffn Action -exec command ;
1533 Execute @var{command}; true if 0 status is returned. @code{find} takes
1534 all arguments after @samp{-exec} to be part of the command until an
1535 argument consisting of @samp{;} is reached. It replaces the string
1536 @samp{@{@}} by the current file name being processed everywhere it
1537 occurs in the command. Both of these constructions need to be escaped
1538 (with a @samp{\}) or quoted to protect them from expansion by the shell.
1539 The command is executed in the directory in which @code{find} was run.
1541 For example, to compare each C header file in the current directory with
1542 the file @file{/tmp/master}:
1545 find . -name '*.h' -exec diff -u '@{@}' /tmp/master ';'
1550 @deffn Action -exec command +
1551 This variant of @samp{-exec} not yet supported. Once it has been
1552 implemented as specified in the POSIX standard, a @samp{@{@}} will expand to
1553 a list of names of matching files. This expansion is done in such a
1554 way as to avoid exceeding the maximum command line length available on
1559 @node Multiple Files
1560 @subsection Multiple Files
1562 Sometimes you need to process files alone. But when you
1563 don't, it is faster to run a command on as many files as possible at a
1564 time, rather than once per file. Doing this saves on the time it takes
1565 to start up the command each time.
1567 To run a command on more than one file at once, use the @code{xargs}
1568 command, which is invoked like this:
1571 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
1574 @code{xargs} normally reads arguments from the standard input. These
1575 arguments are delimited by blanks (which can be protected with double
1576 or single quotes or a backslash) or newlines. It executes the
1577 @var{command} (default is @file{/bin/echo}) one or more times with any
1578 @var{initial-arguments} followed by arguments read from standard
1579 input. Blank lines on the standard input are ignored.
1581 Instead of blank-delimited names, it is safer to use @samp{find -print0}
1582 or @samp{find -fprint0} and process the output by giving the @samp{-0}
1583 or @samp{--null} option to GNU @code{xargs}, GNU @code{tar}, GNU
1584 @code{cpio}, or @code{perl}. The @code{locate} command also has a
1585 @samp{-0} or @samp{--null} option which does the same thing.
1587 You can use shell command substitution (backquotes) to process a list of
1588 arguments, like this:
1591 grep -l sprintf `find $HOME -name '*.c' -print`
1594 However, that method produces an error if the length of the @samp{.c}
1595 file names exceeds the operating system's command-line length limit.
1596 @code{xargs} avoids that problem by running the command as many times as
1597 necessary without exceeding the limit:
1600 find $HOME -name '*.c' -print | xargs grep -l sprintf
1603 However, if the command needs to have its standard input be a terminal
1604 (@code{less}, for example), you have to use the shell command
1605 substitution method or use the @samp{--arg-file} option of
1608 The @code{xargs} command will process all its input, building command
1609 lines and executing them, unless one of the commands exits with a
1610 status of 255 (this will cause xargs to issue an error message and
1611 stop) or it reads a line contains the end of file string specified
1612 with the @samp{--eof} option.
1615 * Unsafe File Name Handling::
1616 * Safe File Name Handling::
1617 * Limiting Command Size::
1618 * Interspersing File Names::
1621 @node Unsafe File Name Handling
1622 @subsubsection Unsafe File Name Handling
1624 Because file names can contain quotes, backslashes, blank characters,
1625 and even newlines, it is not safe to process them using @code{xargs} in its
1626 default mode of operation. But since most files' names do not contain
1627 blanks, this problem occurs only infrequently. If you are only
1628 searching through files that you know have safe names, then you need not
1629 be concerned about it.
1631 @c This example is adapted from:
1632 @c From: pfalstad@stone.Princeton.EDU (Paul John Falstad)
1633 @c Newsgroups: comp.unix.shell
1634 @c Subject: Re: Beware xargs security holes
1635 @c Date: 16 Oct 90 19:12:06 GMT
1637 In many applications, if @code{xargs} botches processing a file because
1638 its name contains special characters, some data might be lost. The
1639 importance of this problem depends on the importance of the data and
1640 whether anyone notices the loss soon enough to correct it. However,
1641 here is an extreme example of the problems that using blank-delimited
1642 names can cause. If the following command is run daily from
1643 @code{cron}, then any user can remove any file on the system:
1646 find / -name '#*' -atime +7 -print | xargs rm
1649 For example, you could do something like this:
1657 and then @code{cron} would delete @file{/vmunix}, if it ran
1658 @code{xargs} with @file{/} as its current directory.
1660 To delete other files, for example @file{/u/joeuser/.plan}, you could do
1668 eg$ mkdir u u/joeuser u/joeuser/.plan'
1670 eg$ echo > u/joeuser/.plan'
1673 eg$ find . -name '#*' -print | xargs echo
1674 ./# ./# /u/joeuser/.plan /#foo
1677 @node Safe File Name Handling
1678 @subsubsection Safe File Name Handling
1680 Here is how to make @code{find} output file names so that they can be
1681 used by other programs without being mangled or misinterpreted. You can
1682 process file names generated this way by giving the @samp{-0} or
1683 @samp{--null} option to GNU @code{xargs}, GNU @code{tar}, GNU
1684 @code{cpio}, or @code{perl}.
1686 @deffn Action -print0
1687 True; print the full file name on the standard output, followed by a
1691 @deffn Action -fprint0 file
1692 True; like @samp{-print0} but write to @var{file} like @samp{-fprint}
1693 (@pxref{Print File Name}).
1696 As of findutils version 4.2.4, the @code{locate} program also has a
1697 @samp{--null} option which does the same thing. For similarity with
1698 @code{xargs}, the short form of the option @samp{-0} can also be used.
1700 If you want to be able to handle file names safely but need to run
1701 commands which want to be connected to a terminal on their input, you
1702 can use the @samp{--arg-file} option to @code{xargs} like this:
1705 find / -name xyzzy -print0 > list
1706 xargs --null --arg-file=list munge
1709 The example above runs the @code{munge} program on all the files named
1710 @file{xyzzy} that we can find, but @code{munge}'s input will still be
1711 the terminal (or whatever the shell was using as standard input). If
1712 your shell has the ``process substitution'' feature @samp{<(...)}, you
1713 can do this in just one step:
1716 xargs --null --arg-file=<(find / -name xyzzy -print0) munge
1719 @node Limiting Command Size
1720 @subsubsection Limiting Command Size
1722 @code{xargs} gives you control over how many arguments it passes to the
1723 command each time it executes it. By default, it uses up to
1724 @code{ARG_MAX} - 2k, or 128k, whichever is smaller, characters per
1725 command. It uses as many lines and arguments as fit within that limit.
1726 The following options modify those values.
1729 @item --no-run-if-empty
1731 If the standard input does not contain any nonblanks, do not run the
1732 command. By default, the command is run once even if there is no input.
1734 @item --max-lines@r{[}=@var{max-lines}@r{]}
1735 @itemx -l@r{[}@var{max-lines}@r{]}
1736 Use at most @var{max-lines} nonblank input lines per command line;
1737 @var{max-lines} defaults to 1 if omitted. Trailing blanks cause an
1738 input line to be logically continued on the next input line, for the
1739 purpose of counting the lines. Implies @samp{-x}.
1741 @item --max-args=@var{max-args}
1742 @itemx -n @var{max-args}
1743 Use at most @var{max-args} arguments per command line. Fewer than
1744 @var{max-args} arguments will be used if the size (see the @samp{-s}
1745 option) is exceeded, unless the @samp{-x} option is given, in which case
1746 @code{xargs} will exit.
1748 @item --max-chars=@var{max-chars}
1749 @itemx -s @var{max-chars}
1750 Use at most @var{max-chars} characters per command line, including the
1751 command and initial arguments and the terminating nulls at the ends of
1752 the argument strings. If you specify a value for this option which is
1753 too large or small, a warning message is printed and the appropriate
1754 upper or lower limit is used instead.
1756 @item --max-procs=@var{max-procs}
1757 @itemx -P @var{max-procs}
1758 Run up to @var{max-procs} processes at a time; the default is 1. If
1759 @var{max-procs} is 0, @code{xargs} will run as many processes as
1760 possible at a time. Use the @samp{-n}, @samp{-s}, or @samp{-l} option
1761 with @samp{-P}; otherwise chances are that the command will be run only
1765 @node Interspersing File Names
1766 @subsubsection Interspersing File Names
1768 @code{xargs} can insert the name of the file it is processing between
1769 arguments you give for the command. Unless you also give options to
1770 limit the command size (@pxref{Limiting Command Size}), this mode of
1771 operation is equivalent to @samp{find -exec} (@pxref{Single File}).
1774 @item --replace@r{[}=@var{replace-str}@r{]}
1775 @itemx -i@r{[}@var{replace-str}@r{]}
1776 Replace occurrences of @var{replace-str} in the initial arguments with
1777 names read from the input. Also, unquoted blanks do not
1778 terminate arguments; instead, the input is split at newlines only. If
1779 @var{replace-str} is omitted, it defaults to @samp{@{@}} (like for
1780 @samp{find -exec}). Implies @samp{-x} and @samp{-l 1}. As an
1781 example, to sort each file the @file{bills} directory, leaving the
1782 output in that file name with @file{.sorted} appended, you could do:
1785 find bills -type f | xargs -iXX sort -o XX.sorted XX
1789 The equivalent command using @samp{find -exec} is:
1792 find bills -type f -exec sort -o '@{@}.sorted' '@{@}' ';'
1797 @subsection Querying
1799 To ask the user whether to execute a command on a single file, you can
1800 use the @code{find} primary @samp{-ok} instead of @samp{-exec}:
1802 @deffn Action -ok command ;
1803 Like @samp{-exec} (@pxref{Single File}), but ask the user first (on
1804 the standard input); if the response does not start with @samp{y} or
1805 @samp{Y}, do not run the command, and return false.
1808 When processing multiple files with a single command, to query the user
1809 you give @code{xargs} the following option. When using this option, you
1810 might find it useful to control the number of files processed per
1811 invocation of the command (@pxref{Limiting Command Size}).
1816 Prompt the user about whether to run each command line and read a line
1817 from the terminal. Only run the command line if the response starts
1818 with @samp{y} or @samp{Y}. Implies @samp{-t}.
1822 @section Delete Files
1824 @deffn Action -delete
1825 Delete files or directories; true if removal succeeded. If the
1826 removal failed, an error message is issued.
1828 The use of the @samp{-delete} action on the command line automatically
1829 turns on the @samp{-depth} option (@pxref{find Expressions}).
1833 @section Adding Tests
1835 You can test for file attributes that none of the @code{find} builtin
1836 tests check. To do this, use @code{xargs} to run a program that filters
1837 a list of files printed by @code{find}. If possible, use @code{find}
1838 builtin tests to pare down the list, so the program run by @code{xargs}
1839 has less work to do. The tests builtin to @code{find} will likely run
1840 faster than tests that other programs perform.
1842 For example, here is a way to print the names of all of the unstripped
1843 binaries in the @file{/usr/local} directory tree. Builtin tests avoid
1844 running @code{file} on files that are not regular files or are not
1848 find /usr/local -type f -perm +a=x | xargs file |
1849 grep 'not stripped' | cut -d: -f1
1853 The @code{cut} program removes everything after the file name from the
1854 output of @code{file}.
1856 @c Idea from Martin Weitzel.
1857 If you want to place a special test somewhere in the middle of a
1858 @code{find} expression, you can use @samp{-exec} to run a program that
1859 performs the test. Because @samp{-exec} evaluates to the exit status of
1860 the executed program, you can write a program (which can be a shell
1861 script) that tests for a special attribute and make it exit with a true
1862 (zero) or false (non-zero) status. It is a good idea to place such a
1863 special test @emph{after} the builtin tests, because it starts a new
1864 process which could be avoided if a builtin test evaluates to false.
1865 Use this method only when @code{xargs} is not flexible enough, because
1866 starting one or more new processes to test each file is slower than
1867 using @code{xargs} to start one process that tests many files.
1869 Here is a shell script called @code{unstripped} that checks whether its
1870 argument is an unstripped binary file:
1874 file $1 | grep 'not stripped' > /dev/null
1877 This script relies on the fact that the shell exits with the status of
1878 the last program it executed, in this case @code{grep}. @code{grep}
1879 exits with a true status if it found any matches, false if not. Here is
1880 an example of using the script (assuming it is in your search path). It
1881 lists the stripped executables in the file @file{sbins} and the
1882 unstripped ones in @file{ubins}.
1885 find /usr/local -type f -perm +a=x \
1886 \( -exec unstripped '@{@}' \; -fprint ubins -o -fprint sbins \)
1889 @node Common Tasks, Databases, Actions, Top
1890 @chapter Common Tasks
1892 The sections that follow contain some extended examples that both give a
1893 good idea of the power of these programs, and show you how to solve
1894 common real-world problems.
1897 * Viewing And Editing::
1900 * Strange File Names::
1901 * Fixing Permissions::
1902 * Classifying Files::
1905 @node Viewing And Editing
1906 @section Viewing And Editing
1908 To view a list of files that meet certain criteria, simply run your file
1909 viewing program with the file names as arguments. Shells substitute a
1910 command enclosed in backquotes with its output, so the whole command
1914 less `find /usr/include -name '*.h' | xargs grep -l mode_t`
1918 You can edit those files by giving an editor name instead of a file
1922 emacs `find /usr/include -name '*.h' | xargs grep -l mode_t`
1925 Because there is a limit to the length of any individual command line,
1926 there is a limit to the number of files that can be handled in this
1927 way. We can get around this difficulty by using xargs like this:
1930 find /usr/include -name '*.h' | xargs grep -l mode_t > todo
1931 xargs --arg-file=todo emacs
1934 Here, @code{xargs} will run @code{emacs} as many times as necessary to
1935 visit all of the files listed in the file @file{todo}.
1940 You can pass a list of files produced by @code{find} to a file archiving
1941 program. GNU @code{tar} and @code{cpio} can both read lists of file
1942 names from the standard input---either delimited by nulls (the safe way)
1943 or by blanks (the lazy, risky default way). To use null-delimited
1944 names, give them the @samp{--null} option. You can store a file archive
1945 in a file, write it on a tape, or send it over a network to extract on
1948 One common use of @code{find} to archive files is to send a list of the
1949 files in a directory tree to @code{cpio}. Use @samp{-depth} so if a
1950 directory does not have write permission for its owner, its contents can
1951 still be restored from the archive since the directory's permissions are
1952 restored after its contents. Here is an example of doing this using
1953 @code{cpio}; you could use a more complex @code{find} expression to
1954 archive only certain files.
1957 find . -depth -print0 |
1958 cpio --create --null --format=crc --file=/dev/nrst0
1961 You could restore that archive using this command:
1964 cpio --extract --null --make-dir --unconditional \
1965 --preserve --file=/dev/nrst0
1968 Here are the commands to do the same things using @code{tar}:
1971 find . -depth -print0 |
1972 tar --create --null --files-from=- --file=/dev/nrst0
1974 tar --extract --null --preserve-perm --same-owner \
1978 @c Idea from Rick Sladkey.
1979 Here is an example of copying a directory from one machine to another:
1982 find . -depth -print0 | cpio -0o -Hnewc |
1983 rsh @var{other-machine} "cd `pwd` && cpio -i0dum"
1987 @section Cleaning Up
1989 @c Idea from Jim Meyering.
1990 This section gives examples of removing unwanted files in various situations.
1991 Here is a command to remove the CVS backup files created when an update
1995 find . -name '.#*' -print0 | xargs -0r rm -f
1998 The command above works, but the following is safer:
2001 find . -name '.#*' -depth -delete
2004 @c Idea from Franc,ois Pinard.
2005 You can run this command to clean out your clutter in @file{/tmp}. You
2006 might place it in the file your shell runs when you log out
2007 (@file{.bash_logout}, @file{.logout}, or @file{.zlogout}, depending on
2008 which shell you use).
2011 find /tmp -depth -user "$LOGNAME" -type f -delete
2014 If your @code{find} command removes directories, you may find that
2015 you get a spurious error message when @code{find} tries to recurse
2016 into a directory that has now been removed. Using the @samp{-depth}
2017 option will normally resolve this problem.
2019 @c Idea from Noah Friedman.
2020 To remove old Emacs backup and auto-save files, you can use a command
2021 like the following. It is especially important in this case to use
2022 null-terminated file names because Emacs packages like the VM mailer
2023 often create temporary file names with spaces in them, like @file{#reply
2024 to David J. MacKenzie<1>#}.
2027 find ~ \( -name '*~' -o -name '#*#' \) -print0 |
2028 xargs --no-run-if-empty --null rm -vf
2031 Removing old files from @file{/tmp} is commonly done from @code{cron}:
2033 @c Idea from Kaveh Ghazi.
2035 find /tmp /var/tmp -not -type d -mtime +3 -delete
2036 find /tmp /var/tmp -depth -mindepth 1 -type d -empty -delete
2039 The second @code{find} command above uses @samp{-depth} so it cleans out
2040 empty directories depth-first, hoping that the parents become empty and
2041 can be removed too. It uses @samp{-mindepth} to avoid removing
2042 @file{/tmp} itself if it becomes totally empty.
2044 @node Strange File Names
2045 @section Strange File Names
2048 @c From: tmatimar@isgtec.com (Ted Timar)
2049 @c Newsgroups: comp.unix.questions,comp.unix.shell,comp.answers,news.answers
2050 @c Subject: Unix - Frequently Asked Questions (2/7) [Frequent posting]
2051 @c Subject: How do I remove a file with funny characters in the filename ?
2052 @c Date: Thu Mar 18 17:16:55 EST 1993
2053 @code{find} can help you remove or rename a file with strange characters
2054 in its name. People are sometimes stymied by files whose names contain
2055 characters such as spaces, tabs, control characters, or characters with
2056 the high bit set. The simplest way to remove such files is:
2059 rm -i @var{some*pattern*that*matches*the*problem*file}
2062 @code{rm} asks you whether to remove each file matching the given
2063 pattern. If you are using an old shell, this approach might not work if
2064 the file name contains a character with the high bit set; the shell may
2065 strip it off. A more reliable way is:
2068 find . -maxdepth 1 @var{tests} -ok rm '@{@}' \;
2072 where @var{tests} uniquely identify the file. The @samp{-maxdepth 1}
2073 option prevents @code{find} from wasting time searching for the file in
2074 any subdirectories; if there are no subdirectories, you may omit it. A
2075 good way to uniquely identify the problem file is to figure out its
2082 Suppose you have a file whose name contains control characters, and you
2083 have found that its inode number is 12345. This command prompts you for
2084 whether to remove it:
2087 find . -maxdepth 1 -inum 12345 -ok rm -f '@{@}' \;
2090 If you don't want to be asked, perhaps because the file name may contain
2091 a strange character sequence that will mess up your screen when printed,
2092 then use @samp{-exec} instead of @samp{-ok}.
2094 If you want to rename the file instead, you can use @code{mv} instead of
2098 find . -maxdepth 1 -inum 12345 -ok mv '@{@}' @var{new-file-name} \;
2101 @node Fixing Permissions
2102 @section Fixing Permissions
2104 Suppose you want to make sure that everyone can write to the directories in a
2105 certain directory tree. Here is a way to find directories lacking either
2106 user or group write permission (or both), and fix their permissions:
2109 find . -type d -not -perm -ug=w | xargs chmod ug+w
2113 You could also reverse the operations, if you want to make sure that
2114 directories do @emph{not} have world write permission.
2116 @node Classifying Files
2117 @section Classifying Files
2120 @c From: martin@mwtech.UUCP (Martin Weitzel)
2121 @c Newsgroups: comp.unix.wizards,comp.unix.questions
2122 @c Subject: Advanced usage of 'find' (Re: Unix security automating script)
2123 @c Date: 22 Mar 90 15:05:19 GMT
2124 If you want to classify a set of files into several groups based on
2125 different criteria, you can use the comma operator to perform multiple
2126 independent tests on the files. Here is an example:
2129 find / -type d \( -perm -o=w -fprint allwrite , \
2130 -perm -o=x -fprint allexec \)
2132 echo "Directories that can be written to by everyone:"
2135 echo "Directories with search permissions for everyone:"
2139 @code{find} has only to make one scan through the directory tree (which
2140 is one of the most time consuming parts of its work).
2142 @node Databases, File Permissions, Common Tasks, Top
2143 @chapter File Name Databases
2145 The file name databases used by @code{locate} contain lists of files
2146 that were in particular directory trees when the databases were last
2147 updated. The file name of the default database is determined when
2148 @code{locate} and @code{updatedb} are configured and installed. The
2149 frequency with which the databases are updated and the directories for
2150 which they contain entries depend on how often @code{updatedb} is run,
2151 and with which arguments.
2154 * Database Locations::
2155 * Database Formats::
2156 * Newline Handling::
2160 @node Database Locations
2161 @section Database Locations
2163 There can be multiple file name databases. Users can select which
2164 databases @code{locate} searches using an environment variable or a
2165 command line option. The system administrator can choose the file name
2166 of the default database, the frequency with which the databases are
2167 updated, and the directories for which they contain entries. File name
2168 databases are updated by running the @code{updatedb} program, typically
2171 In networked environments, it often makes sense to build a database at
2172 the root of each filesystem, containing the entries for that filesystem.
2173 @code{updatedb} is then run for each filesystem on the fileserver where
2174 that filesystem is on a local disk, to prevent thrashing the network.
2175 Here are the options to @code{updatedb} to select which directories each
2176 database contains entries for:
2179 @item --findoptions='@var{OPTION}@dots{}'
2180 Global options to pass on to @code{find}.
2181 The environment variable @code{FINDOPTIONS} also sets this value.
2184 @item --localpaths='@var{path}@dots{}'
2185 Non-network directories to put in the database.
2186 Default is @file{/}.
2188 @item --netpaths='@var{path}@dots{}'
2189 Network (NFS, AFS, RFS, etc.) directories to put in the database.
2190 The environment variable @code{NETPATHS} also sets this value.
2194 @item --prunepaths='@var{path}@dots{}'
2195 Directories to not put in the database, which would otherwise be.
2196 The environment variable @code{PRUNEPATHS} also sets this value.
2197 Default is @file{/tmp /usr/tmp /var/tmp /afs}.
2199 @item --prunefs='@var{path}@dots{}'
2200 File systems to not put in the database, which would otherwise be.
2201 Note that files are pruned when a file system is reached;
2202 Any file system mounted under an undesired file system will be
2204 The environment variable @code{PRUNEFS} also sets this value.
2205 Default is @file{nfs NFS proc}.
2207 @item --output=@var{dbfile}
2208 The database file to build.
2209 Default is system-dependent, but typically @file{/usr/local/var/locatedb}.
2211 @item --localuser=@var{user}
2212 The user to search the non-network directories as, using @code{su}.
2213 Default is to search the non-network directories as the current user.
2214 You can also use the environment variable @code{LOCALUSER} to set this user.
2216 @item --netuser=@var{user}
2217 The user to search network directories as, using @code{su}.
2218 Default is @code{daemon}.
2219 You can also use the environment variable @code{NETUSER} to set this user.
2222 @node Database Formats
2223 @section Database Formats
2225 The file name databases contain lists of files that were in particular
2226 directory trees when the databases were last updated. The file name
2227 database format changed starting with GNU @code{locate} version 4.0 to
2228 allow machines with different byte orderings to share the databases. The
2229 new GNU @code{locate} can read both the old and new database formats.
2230 However, old versions of @code{locate} and @code{find} produce incorrect
2231 results if given a new-format database.
2234 * New Database Format::
2236 * Old Database Format::
2239 @node New Database Format
2240 @subsection New Database Format
2242 @code{updatedb} runs a program called @code{frcode} to
2243 @dfn{front-compress} the list of file names, which reduces the database
2244 size by a factor of 4 to 5. Front-compression (also known as
2245 incremental encoding) works as follows.
2247 The database entries are a sorted list (case-insensitively, for users'
2248 convenience). Since the list is sorted, each entry is likely to share a
2249 prefix (initial string) with the previous entry. Each database entry
2250 begins with an offset-differential count byte, which is the additional
2251 number of characters of prefix of the preceding entry to use beyond the
2252 number that the preceding entry is using of its predecessor. (The
2253 counts can be negative.) Following the count is a null-terminated ASCII
2254 remainder---the part of the name that follows the shared prefix.
2256 If the offset-differential count is larger than can be stored in a byte
2257 (+/-127), the byte has the value 0x80 and the count follows in a 2-byte
2258 word, with the high byte first (network byte order).
2260 Every database begins with a dummy entry for a file called
2261 @file{LOCATE02}, which @code{locate} checks for to ensure that the
2262 database file has the correct format; it ignores the entry in doing the
2265 Databases can not be concatenated together, even if the first (dummy)
2266 entry is trimmed from all but the first database. This is because the
2267 offset-differential count in the first entry of the second and following
2268 databases will be wrong.
2270 @node Sample Database
2271 @subsection Sample Database
2273 Sample input to @code{frcode}:
2274 @c with nulls changed to newlines:
2278 /usr/src/cmd/aardvark.c
2279 /usr/src/cmd/armadillo.c
2283 Length of the longest prefix of the preceding entry to share:
2292 Output from @code{frcode}, with trailing nulls changed to newlines
2293 and count bytes made printable:
2303 (6 = 14 - 8, and -9 = 5 - 14)
2305 @node Old Database Format
2306 @subsection Old Database Format
2308 The old database format is used by Unix @code{locate} and @code{find}
2309 programs and earlier releases of the GNU ones. @code{updatedb} produces
2310 this format if given the @samp{--old-format} option.
2312 @code{updatedb} runs programs called @code{bigram} and @code{code} to
2313 produce old-format databases. The old format differs from the new one
2314 in the following ways. Instead of each entry starting with an
2315 offset-differential count byte and ending with a null, byte values from
2316 0 through 28 indicate offset-differential counts from -14 through 14.
2317 The byte value indicating that a long offset-differential count follows
2318 is 0x1e (30), not 0x80. The long counts are stored in host byte order,
2319 which is not necessarily network byte order, and host integer word size,
2320 which is usually 4 bytes. They also represent a count 14 less than
2321 their value. The database lines have no termination byte; the start of
2322 the next line is indicated by its first byte having a value <= 30.
2324 In addition, instead of starting with a dummy entry, the old database
2325 format starts with a 256 byte table containing the 128 most common
2326 bigrams in the file list. A bigram is a pair of adjacent bytes. Bytes
2327 in the database that have the high bit set are indexes (with the high
2328 bit cleared) into the bigram table. The bigram and offset-differential
2329 count coding makes these databases 20-25% smaller than the new format,
2330 but makes them not 8-bit clean. Any byte in a file name that is in the
2331 ranges used for the special codes is replaced in the database by a
2332 question mark, which not coincidentally is the shell wildcard to match a
2335 The old format therefore can not faithfully store entries with non-ASCII
2336 characters. It therefore should not be used in internationalized
2339 @node Newline Handling
2340 @section Newline Handling
2342 Within the database, filenames are terminated with a null character.
2343 This is the case for both the old and the new format.
2345 When the new database format is being used, the compression technique
2346 used to generate the database though relies on the ability to sort the
2347 list of files before they are presented to @code{frcode}.
2349 If the system's sort command allows its input list of files to be
2350 separated with null characters via the @samp{-z} option, this option
2351 is used and therefore @code{updatedb} and @code{locate} will both
2352 correctly handle filenames containing newlines. If the @code{sort}
2353 command lacks support for this, the list of files is delimited with
2354 the newline character, meaning that parts of filenames containing
2355 newlines will be incorrectly sorted. This can result in both
2356 incorrect matches and incorrect failures to match.
2358 On the other hand, if you are using the old database format, filenames
2359 with embedded newlines are not correctly handled. There is no
2360 technical limitation which enforces this, it's just that the
2361 @code{bigram} program has no been updated to support lists of
2362 filenames separated by nulls.
2364 So, if you are using the new database format (this is the default) and
2365 your system uses GNU @code{find}, newlines will be correctly handled
2366 at all times. Otherwise, newlines may not be correctly handled.
2368 @node File Permissions, Reference, Databases, Top
2369 @chapter File Permissions
2373 @node Reference, Error Messages, File Permissions, Top
2376 Below are summaries of the command line syntax for the programs
2377 discussed in this manual.
2382 * Invoking updatedb::
2386 @node Invoking find, Invoking locate, , Reference
2387 @section Invoking @code{find}
2390 find @r{[-H] [-L] [-P]} @r{[}@var{file}@dots{}@r{]} @r{[}@var{expression}@r{]}
2393 @code{find} searches the directory tree rooted at each file name
2394 @var{file} by evaluating the @var{expression} on each file it finds in
2397 The options @samp{-H}, @samp{-L} or @samp{-P} may be specified at the
2398 start of the command line (if none of these is specified, @samp{-P} is
2399 assumed). The arguments after these are a list of files or
2400 directories that should be searched.
2402 This list of files to search is followed by a list of expressions
2403 describing the files we wish to search for. The first part of the
2404 expression is recognised by the fact that it begins with @samp{-},
2405 @samp{(}, @samp{)}, @samp{,}, or @samp{!}. Any arguments after it are
2406 the rest of the expression. If no paths are given, the current
2407 directory is used. If no expression is given, the expression
2408 @samp{-print} is used.
2410 @code{find} exits with status 0 if all files are processed successfully,
2411 greater than 0 if errors occur.
2413 Three options can precede the list of path names. They determine the
2414 way that symbolic links are handled.
2418 Never follow symbolic links (this is the default), except in the case
2419 of the @samp{-xtype} predicate.
2421 Always follow symbolic links, except in the case of the @samp{-xtype}
2424 Follow symbolic links specified in the list of paths to search, or
2425 which are otherwise specified on the command line.
2428 If @code{find} would follow a symbolic link, but cannot for any reason
2429 (for example, because it has insufficient permissions or the link is
2430 broken), it falls back on using the properties of the symbolic link
2431 itself. @ref{Symbolic Links} for a more complete description of how
2432 symbolic links are handled.
2434 @xref{Primary Index}, for a summary of all of the tests, actions, and
2435 options that the expression can contain. If the expression is
2436 missing, @samp{-print} is assumed.
2440 @code{find} also recognizes two options for administrative use:
2444 Print a summary of the command-line argument format and exit.
2446 Print the version number of @code{find} and exit.
2451 * Warning Messages::
2455 @node Warning Messages,,, Invoking find
2456 @subsection Warning Messages
2458 If there is an error on the @code{find} command line, an error message
2459 is normally issued. However, there are some usages that are
2460 inadvisable but which @code{find} should still accept. Under these
2461 circumstances, @code{find} may issue a warning message. By default,
2462 warnings are enabled only if @code{find} is being run interactively
2463 (specifically, if the standard input is a terminal). Warning messages
2464 can be controlled explicitly by the use of options on the command
2469 Issue warning messages where appropriate.
2471 Do not issue warning messages.
2474 These options take effect at the point on the command line where they
2475 are specified. Therefore if you specify @samp{-nowarn} at the end of
2476 the command line, you will not see warning messages for any problems
2477 occurring before that. The warning messages affected by the above
2478 options are triggered by:
2482 Use of the @samp{-d} option which is deprecated; please use
2483 @samp{-depth} instead, since the latter is POSIX-compliant.
2485 Use of the @samp{-ipath} option which is deprecated; please use
2486 @samp{-iwholename} instead.
2488 Specifying an option (for example @samp{-mindepth}) after a non-option
2489 (for example @samp{-type} or @samp{-print}) on the command line.
2493 The default behaviour above is designed to work in that way so that
2494 existing shell scripts which use such constructs don't generate
2495 spurious errors, but people will be made aware of the problem.
2497 Some warning messages are issued for less common or more serious
2498 problems, and so cannot be turned off:
2502 Use of an unrecognised backslash escape sequence with @samp{-fprintf}
2504 Use of an unrecognised formatting directive with @samp{-fprintf}
2507 @node Invoking locate, Invoking updatedb, Invoking find, Reference
2508 @section Invoking @code{locate}
2511 locate @r{[}@var{option}@dots{}@r{]} @var{pattern}@dots{}
2515 @item --database=@var{path}
2516 @itemx -d @var{path}
2517 Instead of searching the default file name database, search the file
2518 name databases in @var{path}, which is a colon-separated list of
2519 database file names. You can also use the environment variable
2520 @code{LOCATE_PATH} to set the list of database files to search. The
2521 option overrides the environment variable if both are used.
2525 Only print out such names which currently exist (instead of such names
2526 which existed when the database was created).
2527 Note that this may slow down the program a lot, if there are many matches
2532 Ignore case distinctions in both the pattern and the file names.
2536 Results are separated with the ASCII NUL character rather than the
2541 Just print the number of results, not the results themselves.
2545 Limit the number of results printed to N. If you use the
2546 @samp{--count} option, the value printed will never be larger than
2550 The specified pattern is matched against the whole name of the file in
2551 the locate database. If the pattern contains metacharacters, it must
2552 match exactly. If not, it must match part of the whole file name.
2553 This is the default behaviour.
2556 The specified pattern is matched against just the last component of
2557 the name of the file in the locate database. This last component is
2558 also called the ``base name''. For example, the base name of
2559 @file{/tmp/mystuff/foo.old.c} is @file{foo.old.c}. If the pattern
2560 contains metacharacters, it must match exactly. If not, it must match
2561 part of the whole file name.
2564 Print a summary of the options to @code{locate} and exit.
2567 Print the version number of @code{locate} and exit.
2570 @node Invoking updatedb, Invoking xargs, Invoking locate, Reference
2571 @section Invoking @code{updatedb}
2574 updatedb @r{[}@var{option}@dots{}@r{]}
2578 @item --findoptions='@var{OPTION}@dots{}'
2579 Global options to pass on to @code{find}.
2580 The environment variable @code{FINDOPTIONS} also sets this value.
2583 @item --localpaths='@var{path}@dots{}'
2584 Non-network directories to put in the database.
2585 Default is @file{/}.
2587 @item --netpaths='@var{path}@dots{}'
2588 Network (NFS, AFS, RFS, etc.) directories to put in the database.
2589 The environment variable @code{NETPATHS} also sets this value.
2592 @item --prunepaths='@var{path}@dots{}'
2593 Directories to not put in the database, which would otherwise be.
2594 The environment variable @code{PRUNEPATHS} also sets this value.
2595 Default is @file{/tmp /usr/tmp /var/tmp /afs}.
2597 @item --prunefs='@var{path}@dots{}'
2598 File systems to omit from the database, which would otherwise be included.
2599 Note that files are pruned when a file system is reached;
2600 Any file system mounted under an undesired file system will be
2602 The environment variable @code{PRUNEFS} also sets this value.
2603 Default is @file{nfs NFS proc}.
2605 @item --output=@var{dbfile}
2606 The database file to build.
2607 Default is system-dependent, but typically @file{/usr/local/var/locatedb}.
2609 @item --localuser=@var{user}
2610 The user to search the non-network directories as, using @code{su}.
2611 Default is to search the non-network directories as the current user.
2612 You can also use the environment variable @code{LOCALUSER} to set this user.
2614 @item --netuser=@var{user}
2615 The user to search network directories as, using @code{su}(1).
2616 Default is @code{daemon}.
2617 You can also use the environment variable @code{NETUSER} to set this user.
2620 @node Invoking xargs, , Invoking updatedb, Reference
2621 @section Invoking @code{xargs}
2624 xargs @r{[}@var{option}@dots{}@r{]} @r{[}@var{command} @r{[}@var{initial-arguments}@r{]}@r{]}
2627 @code{xargs} exits with the following status:
2633 if any invocation of the command exited with status 1-125
2635 if the command exited with status 255
2637 if the command is killed by a signal
2639 if the command cannot be run
2641 if the command is not found
2643 if some other error occurred.
2647 @item --arg-file@r{=@var{inputfile}}
2648 @itemx -a @r{=@var{inputfile}}
2649 Read names from the file @var{inputfile} instead of standard input.
2653 Input filenames are terminated by a null character instead of by
2654 whitespace, and the quotes and backslash are not special (every
2655 character is taken literally). Disables the end of file string, which
2656 is treated like any other argument.
2658 @item --eof@r{[}=@var{eof-str}@r{]}
2659 @itemx -e@r{[}@var{eof-str}@r{]}
2660 Set the end of file string to @var{eof-str}. If the end of file string
2661 occurs as a line of input, the rest of the input is ignored. If
2662 @var{eof-str} is omitted, there is no end of file string. If this
2663 option is not given, the end of file string defaults to @samp{_}.
2666 Print a summary of the options to @code{xargs} and exit.
2668 @item --replace@r{[}=@var{replace-str}@r{]}
2669 @itemx -i@r{[}@var{replace-str}@r{]}
2670 Replace occurrences of @var{replace-str} in the initial arguments with
2671 names read from standard input. Also, unquoted blanks do not
2672 terminate arguments; instead, the input is split at newlines only.
2673 If @var{replace-str} is omitted, it defaults to @samp{@{@}}
2674 (like for @samp{find -exec}). Implies @samp{-x} and @samp{-l 1}.
2676 @item --max-lines@r{[}=@var{max-lines}@r{]}
2677 @itemx -l@r{[}@var{max-lines}@r{]}
2678 Use at most @var{max-lines} nonblank input lines per command line;
2679 @var{max-lines} defaults to 1 if omitted. Trailing blanks cause an
2680 input line to be logically continued on the next input line, for the
2681 purpose of counting the lines. Implies @samp{-x}.
2683 @item --max-args=@var{max-args}
2684 @itemx -n @var{max-args}
2685 Use at most @var{max-args} arguments per command line. Fewer than
2686 @var{max-args} arguments will be used if the size (see the @samp{-s}
2687 option) is exceeded, unless the @samp{-x} option is given, in which case
2688 @code{xargs} will exit.
2692 Prompt the user about whether to run each command line and read a line
2693 from the terminal. Only run the command line if the response starts
2694 with @samp{y} or @samp{Y}. Implies @samp{-t}.
2696 @item --no-run-if-empty
2698 If the standard input does not contain any nonblanks, do not run the
2699 command. By default, the command is run once even if there is no input.
2701 @item --max-chars=@var{max-chars}
2702 @itemx -s @var{max-chars}
2703 Use at most @var{max-chars} characters per command line, including the
2704 command and initial arguments and the terminating nulls at the ends of
2705 the argument strings.
2709 Print the command line on the standard error output before executing
2713 Print the version number of @code{xargs} and exit.
2717 Exit if the size (see the @samp{-s} option) is exceeded.
2720 @item --max-procs=@var{max-procs}
2721 @itemx -P @var{max-procs}
2722 Run up to @var{max-procs} processes at a time; the default is 1. If
2723 @var{max-procs} is 0, @code{xargs} will run as many processes as
2727 @node Error Messages, Primary Index, Reference, Top
2728 @chapter Error Messages
2730 This section describes some of the error messages you might get from
2731 @code{find}, @code{xargs}, or @code{locate}, explains them and in some
2732 cases provides advice as to what you should do about this.
2734 This manual is written in English. The GNU findutils software
2735 features translated error messages for many languages. For this
2736 reason where possible we try to make the error messages produced by
2737 the programs self-explanatory. This approach avoids asking people to
2738 figure out which English-language error message the test they actually
2739 saw might correspond to. Error messages which are self-explanatory
2740 will not normally be described or discussed in this document. For
2741 those messages which are discussed in this document, only the
2742 English-language version of the message will be listed.
2745 * Error Messages From find::
2746 * Error Messages From xargs::
2747 * Error Messages From locate::
2748 * Error Messages From updatedb::
2751 @node Error Messages From find, Error Messages From xargs, , Error Messages
2752 @section Error Messages From find
2755 @item invalid predicate `-foo'
2756 This means that the @code{find} command line included something that
2757 started with a dash or other special character. The @code{find}
2758 program tried to interpret this as a test, action or option, but
2759 didn't recognise it. If you intended it to be a test, check what you
2760 specified against the documentation. If, on the other hand, the
2761 string is the name of a file which has been expanded from a wildcard
2762 (for example because you have a @samp{*} on the command line),
2763 consider using @samp{./*} or just @samp{.} instead.
2765 @item unexpected extra predicate
2766 This usually happens if you have an extra bracket on the command line
2767 (for example @samp{find . -print \)}).
2769 @item Warning: filesystem /path/foo has recently been mounted
2770 @itemx Warning: filesystem /path/foo has recently been unmounted
2771 These messages might appear when @code{find} moves into a directory
2772 and finds that the device number and inode are different to what it
2773 expected them to be. If the directory @code{find} has moved into is
2774 on an NFS filesystem, it will not issue this message, because
2775 @code{automount} frequently mounts new filesystems on directories as
2776 you move into them (that is how it knows you want to use the
2777 filesystem). So, if you do see this message, be wary --
2778 @code{automount} may not have been responsible. Consider the
2779 possibility that someone else is manipulating the filesystem while
2780 @code{find} is running. Some people might do this in order to mislead
2781 @code{find} or persuade it to look at one set of files when it thought
2782 it was looking at another set.
2784 @item /path/foo changed during execution of find (old device number 12345, new device number 6789, filesystem type is <whatever>) [ref XXX]
2785 This message is issued when @code{find} changes directory and ends up
2786 somewhere it didn't expect to be. This happens in one of two
2787 circumstances. Firstly this happens when ``automount'' does its thing
2788 on a system where @code{find} doesn't know how to determine what the
2789 current set of mounted filesystems is@footnote{To do this, @code{find}
2790 requires to be able to use @code{getmntent()} - check the
2791 @file{config.h} file for HAVE_GETMNTENT, which should be #defined to
2792 1. If HAVE_GETMNTENT is not set, consider submitting a problem report
2793 to @email{bug-findutils@@gnu.org}, because @code{find} needs to be
2794 able to figure out how to enumerate the mounted devices on your
2797 Secondly, this can happen when the device number of a directory
2798 appears to change during a change of current directory, but
2799 @code{find} is moving up the filesystem hierarchy rather than down it.
2800 In order to prevent @code{find} wandering off into some unexpected
2801 part of the filesystem, we stop it at this point.
2803 @item Don't know how to use getmntent() to read `/etc/mtab'. This is a bug.
2804 This message is issued when a problem similar to the above occurs on a
2805 system where @code{find} doesn't know how to figure out the current
2806 list of mount points. Ask for help on @email{bug-findutils@@gnu.org}.
2808 @item /path/foo/bar changed during execution of find (old inode number 12345, new inode number 67893, filesystem type is <whatever>) [ref XXX]"),
2809 This message is issued when @code{find} changes directory and
2810 discovers that the inode number of that directory once it's got there
2811 is different to the inode number that it obtained when it examined the
2812 directory some time previously. This normally means that while
2813 @code{find} has been deep in a directory hierarchy doing something
2814 time consuming, somebody has moved the one of the parent directories
2815 to another location in the same filesystem. This may have been done
2816 maliciously, or may not. In any case, @code{find} stops at this point
2817 in order to avoid traversing parts of the filesystem that it wasn't
2818 intended to. You can use @code{ls -li} or @code{find /path -inum
2819 12345 -o -inum 67893} to find out more about what has happened.
2821 @item sanity check of the fnmatch() library function failed.
2822 Please submit a bug report. You may well be asked questions about
2823 your system, and if you compiled the @code{findutils} code yourself,
2824 you should keep your copy of the build tree around. The likely
2825 explanation is that your system has a buggy implementation of
2826 @code{fnmatch} that looks enough like the GNU version to fool
2827 @code{configure}, but which doesn't work properly.
2830 This normally happens if you use the @code{-exec} action or a
2831 something similar (@code{-ok} and so forth) but the system has run out
2832 of free process slots. This is either because the system is very busy
2833 and the system has reached its maximum process limit, or because you
2834 have a resource limit in place and you've reached it. Check the
2835 system for runaway processes (if @code{ps} still works). Some process
2836 slots are normally reserved for use by @samp{root}.
2838 @item some-program terminated by signal 99
2839 Some program which was launched via @code{-exec} or similar was killed
2840 with a fatal signal. This is just an advisory message.
2845 @node Error Messages From xargs, Error Messages From locate, Error Messages From find, Error Messages
2846 @section Error Messages From xargs
2849 @item environment is too large for exec
2850 This message means that you have so many environment variables set
2851 (or such large values for them) that there is no room within the
2852 system-imposed limits on program command-line argument length to
2853 invoke any program. I'm sure you did this deliberately. Please try
2854 unsetting some environment variables, or exiting the current shell.
2856 @item can not fit single argument within argument list size limit
2857 You are using the @samp{-i} option and @code{xargs} doesn't have
2858 enough space to build a command line because it has read in a really
2859 large item and it doesn't fit. You can probably work around this
2860 problem with the @samp{-s} option, but the default size is pretty
2861 large. You must be trying pretty hard to break @code{xargs}.
2864 See the description of the similar message for @code{find}.
2866 @item <program>: exited with status 255; aborting
2867 When a command run by @code{xargs} exits with status 255, @code{xargs}
2868 is supposed to stop. If this is not what you intended, wrap the
2869 program you are trying to invoke in a shell script which doesn't
2872 @item <program>: terminated by signal 99
2873 See the description of the similar message for @code{find}.
2876 @node Error Messages From locate, Error Messages From updatedb, Error Messages From xargs, Error Messages
2877 @section Error Messages From locate
2880 @item warning: database `/usr/local/var/locatedb' is more than 8 days old
2881 The @code{locate} program relies on a database which is periodically
2882 built by the @code{updatedb} program. That hasn't happened in a long
2883 time. To fix this problem, run @code{updatedb} manually. This can
2884 often happen on systems that are generally not left on, so the periodic
2885 ``cron'' task which normally does this doesn't get a chance to run.
2887 @item locate database `/usr/local/var/locatedb' is corrupt or invalid
2888 This should not happen. Re-run @code{updatedb}. If that works, but
2889 @code{locate} still produces this error, run @code{locate --version}
2890 and @code{updatedb --version}. These should produce the same output.
2891 If not, you are using a mixed toolset; check your @samp{$PATH}
2892 environment variable and your shell aliases (if you have any). If
2893 both programs claim to be GNU versions, this is a bug; all versions of
2894 these programs should interoperate without problem. Ask for help on
2895 @email{bug-findutils@@gnu.org}.
2899 @node Error Messages From updatedb, , Error Messages From locate, Error Messages
2900 @section Error Messages From updatedb
2902 The @code{updatedb} program (and the programs it invokes) do issue
2903 error messages, but none of them seem to me to be candidates for
2904 guidance. If you are having a problem understanding one of these, ask
2905 for help on @email{bug-findutils@@gnu.org}.
2907 @node Primary Index, , Error Messages, Top
2908 @unnumbered @code{find} Primary Index
2910 This is a list of all of the primaries (tests, actions, and options)
2911 that make up @code{find} expressions for selecting files. @xref{find
2912 Expressions}, for more information on expressions.
2919 @comment texi related words used by Emacs' spell checker ispell.el
2921 @comment LocalWords: texinfo setfilename settitle setchapternewpage
2922 @comment LocalWords: iftex finalout ifinfo DIR titlepage vskip pt
2923 @comment LocalWords: filll dir samp dfn noindent xref pxref
2924 @comment LocalWords: var deffn texi deffnx itemx emph asis
2925 @comment LocalWords: findex smallexample subsubsection cindex
2927 @comment other words used by Emacs' spell checker ispell.el
2928 @comment LocalWords: README fred updatedb xargs Plett Rendell akefile
2929 @comment LocalWords: args grep Filesystems fo foo fOo wildcards iname
2930 @comment LocalWords: ipath regex iregex expr fubar regexps
2931 @comment LocalWords: metacharacters macs sr sc inode lname ilname
2932 @comment LocalWords: sysdep noleaf ls inum xdev filesystems usr atime
2933 @comment LocalWords: ctime mtime amin cmin mmin al daystart Sladkey rm
2934 @comment LocalWords: anewer cnewer bckw rf xtype uname gname uid gid
2935 @comment LocalWords: nouser nogroup chown chgrp perm ch maxdepth
2936 @comment LocalWords: mindepth cpio src CD AFS statted stat fstype ufs
2937 @comment LocalWords: nfs tmp mfs printf fprint dils rw djm Nov lwall
2938 @comment LocalWords: POSIXLY fls fprintf strftime locale's EDT GMT AP
2939 @comment LocalWords: EST diff perl backquotes sprintf Falstad Oct cron
2940 @comment LocalWords: eg vmunix mkdir afs allexec allwrite ARG bigram
2941 @comment LocalWords: bigrams cd chmod comp crc CVS dbfile dum eof
2942 @comment LocalWords: fileserver filesystem fn frcode Ghazi Hnewc iXX
2943 @comment LocalWords: joeuser Kaveh localpaths localuser LOGNAME
2944 @comment LocalWords: Meyering mv netpaths netuser nonblank nonblanks
2945 @comment LocalWords: ois ok Pinard printindex proc procs prunefs
2946 @comment LocalWords: prunepaths pwd RFS rmadillo rmdir rsh sbins str
2947 @comment LocalWords: su Timar ubins ug unstripped vf VM Weitzel
2948 @comment LocalWords: wildcard zlogout