1 This is Info file find.info, produced by Makeinfo version 1.68 from the
5 * Finding Files: (find). Listing and operating on files
6 that match certain criteria.
9 This file documents the GNU utilities for finding files that match
10 certain criteria and performing various operations on them.
12 Copyright (C) 1994 Free Software Foundation, Inc.
14 Permission is granted to make and distribute verbatim copies of this
15 manual provided the copyright notice and this permission notice are
16 preserved on all copies.
18 Permission is granted to copy and distribute modified versions of
19 this manual under the conditions for verbatim copying, provided that
20 the entire resulting derived work is distributed under the terms of a
21 permission notice identical to this one.
23 Permission is granted to copy and distribute translations of this
24 manual into another language, under the above conditions for modified
25 versions, except that this permission notice may be stated in a
26 translation approved by the Foundation.
29 File: find.info, Node: Top, Next: Introduction, Up: (dir)
31 This file documents the GNU utilities for finding files that match
32 certain criteria and performing various actions on them. This is
33 edition 1.1, for `find' version 4.1.
37 * Introduction:: Summary of the tasks this manual describes.
38 * Finding Files:: Finding files that match certain criteria.
39 * Actions:: Doing things to files you have found.
40 * Common Tasks:: Solutions to common real-world problems.
41 * Databases:: Maintaining file name databases.
42 * File Permissions:: How to control access to files.
43 * Reference:: Summary of how to invoke the programs.
44 * Primary Index:: The components of `find' expressions.
47 File: find.info, Node: Introduction, Next: Finding Files, Prev: Top, Up: Top
52 This manual shows how to find files that meet criteria you specify,
53 and how to perform various actions on the files that you find. The
54 principal programs that you use to perform these tasks are `find',
55 `locate', and `xargs'. Some of the examples in this manual use
56 capabilities specific to the GNU versions of those programs.
58 GNU `find' was originally written by Eric Decker, with enhancements
59 by David MacKenzie, Jay Plett, and Tim Wood. GNU `xargs' was
60 originally written by Mike Rendell, with enhancements by David
61 MacKenzie. GNU `locate' and its associated utilities were originally
62 written by James Woods, with enhancements by David MacKenzie. The idea
63 for `find -print0' and `xargs -0' came from Dan Bernstein. Many other
64 people have contributed bug fixes, small improvements, and helpful
67 Mail suggestions and bug reports for these programs to
68 `bug-gnu-utils@prep.ai.mit.edu'. Please include the version number,
69 which you can get by running `find --version'.
78 File: find.info, Node: Scope, Next: Overview, Up: Introduction
83 For brevity, the word "file" in this manual means a regular file, a
84 directory, a symbolic link, or any other kind of node that has a
85 directory entry. A directory entry is also called a "file name". A
86 file name may contain some, all, or none of the directories in a path
87 that leads to the file. These are all examples of what this manual
94 /usr/local/include/termcap.h
96 A "directory tree" is a directory and the files it contains, all of
97 its subdirectories and the files they contain, etc. It can also be a
98 single non-directory file.
100 These programs enable you to find the files in one or more directory
103 * have names that contain certain text or match a certain pattern;
105 * are links to certain files;
107 * were last used during a certain period of time;
109 * are within a certain size range;
111 * are of a certain type (regular file, directory, symbolic link,
114 * are owned by a certain user or group;
116 * have certain access permissions;
118 * contain text that matches a certain pattern;
120 * are within a certain depth in the directory tree;
122 * or some combination of the above.
124 Once you have found the files you're looking for (or files that are
125 potentially the ones you're looking for), you can do more to them than
126 simply list their names. You can get any combination of the files'
127 attributes, or process the files in many ways, either individually or in
128 groups of various sizes. Actions that you might want to perform on the
129 files you have found include, but are not limited to:
133 * store in an archive
137 * change access permissions
139 * classify into groups
141 This manual describes how to perform each of those tasks, and more.
144 File: find.info, Node: Overview, Next: find Expressions, Prev: Scope, Up: Introduction
149 The principal programs used for making lists of files that match
150 given criteria and running commands on them are `find', `locate', and
151 `xargs'. An additional command, `updatedb', is used by system
152 administrators to create databases for `locate' to use.
154 `find' searches for files in a directory hierarchy and prints
155 information about the files it found. It is run like this:
157 find [FILE...] [EXPRESSION]
159 Here is a typical use of `find'. This example prints the names of all
160 files in the directory tree rooted in `/usr/src' whose name ends with
161 `.c' and that are larger than 100 Kilobytes.
162 find /usr/src -name '*.c' -size +100k -print
164 `locate' searches special file name databases for file names that
165 match patterns. The system administrator runs the `updatedb' program
166 to create the databases. `locate' is run like this:
168 locate [OPTION...] PATTERN...
170 This example prints the names of all files in the default file name
171 database whose name ends with `Makefile' or `makefile'. Which file
172 names are stored in the database depends on how the system
173 administrator ran `updatedb'.
174 locate '*[Mm]akefile'
176 The name `xargs', pronounced EX-args, means "combine arguments."
177 `xargs' builds and executes command lines by gathering together
178 arguments it reads on the standard input. Most often, these arguments
179 are lists of file names generated by `find'. `xargs' is run like this:
181 xargs [OPTION...] [COMMAND [INITIAL-ARGUMENTS]]
183 The following command searches the files listed in the file `file-list'
184 and prints all of the lines in them that contain the word `typedef'.
185 xargs grep typedef < file-list
188 File: find.info, Node: find Expressions, Prev: Overview, Up: Introduction
193 The expression that `find' uses to select files consists of one or
194 more "primaries", each of which is a separate command line argument to
195 `find'. `find' evaluates the expression each time it processes a file.
196 An expression can contain any of the following types of primaries:
199 affect overall operation rather than the processing of a specific
203 return a true or false value, depending on the file's attributes;
206 have side effects and return a true or false value; and
209 connect the other arguments and affect when and whether they are
212 You can omit the operator between two primaries; it defaults to
213 `-and'. *Note Combining Primaries With Operators::, for ways to
214 connect primaries into more complex expressions. If the expression
215 contains no actions other than `-prune', `-print' is performed on all
216 files for which the entire expression is true (*note Print File
219 Options take effect immediately, rather than being evaluated for each
220 file when their place in the expression is reached. Therefore, for
221 clarity, it is best to place them at the beginning of the expression.
223 Many of the primaries take arguments, which immediately follow them
224 in the next command line argument to `find'. Some arguments are file
225 names, patterns, or other strings; others are numbers. Numeric
226 arguments can be specified as
238 File: find.info, Node: Finding Files, Next: Actions, Prev: Introduction, Up: Top
243 By default, `find' prints to the standard output the names of the
244 files that match the given criteria. *Note Actions::, for how to get
245 more information about the matching files.
259 * Combining Primaries With Operators::
262 File: find.info, Node: Name, Next: Links, Up: Finding Files
267 Here are ways to search for files whose name matches a certain
268 pattern. *Note Shell Pattern Matching::, for a description of the
269 PATTERN arguments to these tests.
271 Each of these tests has a case-sensitive version and a
272 case-insensitive version, whose name begins with `i'. In a
273 case-insensitive comparison, the patterns `fo*' and `F??' match the
274 file names `Foo', `FOO', `foo', `fOo', etc.
278 * Base Name Patterns::
279 * Full Name Patterns::
280 * Fast Full Name Search::
281 * Shell Pattern Matching:: Wildcards used by these programs.
284 File: find.info, Node: Base Name Patterns, Next: Full Name Patterns, Up: Name
289 - Test: -name PATTERN
290 - Test: -iname PATTERN
291 True if the base of the file name (the path with the leading
292 directories removed) matches shell pattern PATTERN. For `-iname',
293 the match is case-insensitive. To ignore a whole directory tree,
294 use `-prune' (*note Directories::.). As an example, to find
295 Texinfo source files in `/usr/local/doc':
297 find /usr/local/doc -name '*.texi'
300 File: find.info, Node: Full Name Patterns, Next: Fast Full Name Search, Prev: Base Name Patterns, Up: Name
305 - Test: -path PATTERN
306 - Test: -ipath PATTERN
307 True if the entire file name, starting with the command line
308 argument under which the file was found, matches shell pattern
309 PATTERN. For `-ipath', the match is case-insensitive. To ignore
310 a whole directory tree, use `-prune' rather than checking every
311 file in the tree (*note Directories::.).
315 True if the entire file name matches regular expression EXPR.
316 This is a match on the whole path, not a search. For example, to
317 match a file named `./fubar3', you can use the regular expression
318 `.*bar.' or `.*b.*3', but not `b.*r3'. *Note Syntax of Regular
319 Expressions: (emacs)Regexps, for a description of the syntax of
320 regular expressions. For `-iregex', the match is case-insensitive.
323 File: find.info, Node: Fast Full Name Search, Next: Shell Pattern Matching, Prev: Full Name Patterns, Up: Name
325 Fast Full Name Search
326 ---------------------
328 To search for files by name without having to actually scan the
329 directories on the disk (which can be slow), you can use the `locate'
330 program. For each shell pattern you give it, `locate' searches one or
331 more databases of file names and displays the file names that contain
332 the pattern. *Note Shell Pattern Matching::, for details about shell
335 If a pattern is a plain string--it contains no
336 metacharacters--`locate' displays all file names in the database that
337 contain that string. If a pattern contains metacharacters, `locate'
338 only displays file names that match the pattern exactly. As a result,
339 patterns that contain metacharacters should usually begin with a `*',
340 and will most often end with one as well. The exceptions are patterns
341 that are intended to explicitly match the beginning or end of a file
347 is almost equivalent to
348 find DIRECTORIES -name PATTERN
350 where DIRECTORIES are the directories for which the file name
351 databases contain information. The differences are that the `locate'
352 information might be out of date, and that `locate' handles wildcards
353 in the pattern slightly differently than `find' (*note Shell Pattern
356 The file name databases contain lists of files that were on the
357 system when the databases were last updated. The system administrator
358 can choose the file name of the default database, the frequency with
359 which the databases are updated, and the directories for which they
362 Here is how to select which file name databases `locate' searches.
363 The default is system-dependent.
367 Instead of searching the default file name database, search the
368 file name databases in PATH, which is a colon-separated list of
369 database file names. You can also use the environment variable
370 `LOCATE_PATH' to set the list of database files to search. The
371 option overrides the environment variable if both are used.
374 File: find.info, Node: Shell Pattern Matching, Prev: Fast Full Name Search, Up: Name
376 Shell Pattern Matching
377 ----------------------
379 `find' and `locate' can compare file names, or parts of file names,
380 to shell patterns. A "shell pattern" is a string that may contain the
381 following special characters, which are known as "wildcards" or
384 You must quote patterns that contain metacharacters to prevent the
385 shell from expanding them itself. Double and single quotes both work;
386 so does escaping with a backslash.
389 Matches any zero or more characters.
392 Matches any one character.
395 Matches exactly one character that is a member of the string
396 STRING. This is called a "character class". As a shorthand,
397 STRING may contain ranges, which consist of two characters with a
398 dash between them. For example, the class `[a-z0-9_]' matches a
399 lowercase letter, a number, or an underscore. You can negate a
400 class by placing a `!' or `^' immediately after the opening
401 bracket. Thus, `[^A-Z@]' matches any character except an
402 uppercase letter or an at sign.
405 Removes the special meaning of the character that follows it. This
406 works even in character classes.
408 In the `find' tests that do shell pattern matching (`-name',
409 `-path', etc.), wildcards in the pattern do not match a `.' at the
410 beginning of a file name. This is not the case for `locate'. Thus,
411 `find -name '*macs'' does not match a file named `.emacs', but `locate
414 Slash characters have no special significance in the shell pattern
415 matching that `find' and `locate' do, unlike in the shell, in which
416 wildcards do not match them. Therefore, a pattern `foo*bar' can match
417 a file name `foo3/bar', and a pattern `./sr*sc' can match a file name
421 File: find.info, Node: Links, Next: Time, Prev: Name, Up: Finding Files
426 There are two ways that files can be linked together. "Symbolic
427 links" are a special type of file whose contents are a portion of the
428 name of another file. "Hard links" are multiple directory entries for
429 one file; the file names all have the same index node ("inode") number
438 File: find.info, Node: Symbolic Links, Next: Hard Links, Up: Links
443 - Test: -lname PATTERN
444 - Test: -ilname PATTERN
445 True if the file is a symbolic link whose contents match shell
446 pattern PATTERN. For `-ilname', the match is case-insensitive.
447 *Note Shell Pattern Matching::, for details about the PATTERN
448 argument. So, to list any symbolic links to `sysdep.c' in the
449 current directory and its subdirectories, you can do:
451 find . -lname '*sysdep.c'
454 Dereference symbolic links. The following differences in behavior
455 occur when this option is given:
457 * `find' follows symbolic links to directories when searching
460 * `-lname' and `-ilname' always return false.
462 * `-type' reports the types of the files that symbolic links
465 * Implies `-noleaf' (*note Directories::.).
468 File: find.info, Node: Hard Links, Prev: Symbolic Links, Up: Links
473 To find hard links, first get the inode number of the file whose
474 links you want to find. You can learn a file's inode number and the
475 number of links to it by running `ls -i' or `find -ls'. If the file has
476 more than one link, you can search for the other links by passing that
477 inode number to `-inum'. Add the `-xdev' option if you are starting
478 the search at a directory that has other filesystems mounted on it,
479 such as `/usr' on many systems. Doing this saves needless searching,
480 since hard links to a file must be on the same filesystem. *Note
484 File has inode number N.
486 You can also search for files that have a certain number of links,
487 with `-links'. Directories normally have at least two hard links; their
488 `.' entry is the second one. If they have subdirectories, each of
489 those also has a hard link called `..' to its parent directory.
492 File has N hard links.
495 File: find.info, Node: Time, Next: Size, Prev: Links, Up: Finding Files
500 Each file has three time stamps, which record the last time that
501 certain operations were performed on the file:
503 1. access (read the file's contents)
505 2. change the status (modify the file or its attributes)
507 3. modify (change the file's contents)
509 You can search for files whose time stamps are within a certain age
510 range, or compare them to other time stamps.
515 * Comparing Timestamps::
518 File: find.info, Node: Age Ranges, Next: Comparing Timestamps, Up: Time
523 These tests are mainly useful with ranges (`+N' and `-N').
528 True if the file was last accessed (or its status changed, or it
529 was modified) N*24 hours ago.
534 True if the file was last accessed (or its status changed, or it
535 was modified) N minutes ago. These tests provide finer
536 granularity of measurement than `-atime' et al. For example, to
537 list files in `/u/bill' that were last read from 2 to 6 minutes
540 find /u/bill -amin +2 -amin -6
543 Measure times from the beginning of today rather than from 24
544 hours ago. So, to list the regular files in your home directory
545 that were modified yesterday, do
547 find ~ -daystart -type f -mtime 1
550 File: find.info, Node: Comparing Timestamps, Prev: Age Ranges, Up: Time
555 As an alternative to comparing timestamps to the current time, you
556 can compare them to another file's timestamp. That file's timestamp
557 could be updated by another program when some event occurs. Or you
558 could set it to a particular fixed date using the `touch' command. For
559 example, to list files in `/usr' modified after February 1 of the
562 touch -t 02010000 /tmp/stamp$$
563 find /usr -newer /tmp/stamp$$
569 True if the file was last accessed (or its status changed, or it
570 was modified) more recently than FILE was modified. These tests
571 are affected by `-follow' only if `-follow' comes before them on
572 the command line. *Note Symbolic Links::, for more information on
573 `-follow'. As an example, to list any files modified since
574 `/bin/sh' was last modified:
576 find . -newer /bin/sh
579 True if the file was last accessed N days after its status was
580 last changed. Useful for finding files that are not being used,
581 and could perhaps be archived or removed to save disk space.
584 File: find.info, Node: Size, Next: Type, Prev: Time, Up: Finding Files
589 - Test: -size N[BCKW]
590 True if the file uses N units of space, rounding up. The units
591 are 512-byte blocks by default, but they can be changed by adding a
592 one-character suffix to N:
601 kilobytes (1024 bytes)
606 The size does not count indirect blocks, but it does count blocks
607 in sparse files that are not actually allocated.
610 True if the file is empty and is either a regular file or a
611 directory. This might make it a good candidate for deletion.
612 This test is useful with `-depth' (*note Directories::.) and
613 `-exec rm -rf '{}' ';'' (*note Single File::.).
616 File: find.info, Node: Type, Next: Owner, Prev: Size, Up: Finding Files
622 True if the file is of type C:
625 block (buffered) special
628 character (unbuffered) special
646 The same as `-type' unless the file is a symbolic link. For
647 symbolic links: if `-follow' has not been given, true if the file
648 is a link to a file of type C; if `-follow' has been given, true
649 if C is `l'. In other words, for symbolic links, `-xtype' checks
650 the type of the file that `-type' does not check. *Note Symbolic
651 Links::, for more information on `-follow'.
654 File: find.info, Node: Owner, Next: Permissions, Prev: Type, Up: Finding Files
661 True if the file is owned by user UNAME (belongs to group GNAME).
662 A numeric ID is allowed.
666 True if the file's numeric user ID (group ID) is N. These tests
667 support ranges (`+N' and `-N'), unlike `-user' and `-group'.
671 True if no user corresponds to the file's numeric user ID (no group
672 corresponds to the numeric group ID). These cases usually mean
673 that the files belonged to users who have since been removed from
674 the system. You probably should change the ownership of such
675 files to an existing user or group, using the `chown' or `chgrp'
679 File: find.info, Node: Permissions, Next: Contents, Prev: Owner, Up: Finding Files
684 *Note File Permissions::, for information on how file permissions are
685 structured and how to specify them.
688 True if the file's permissions are exactly MODE (which can be
689 numeric or symbolic). Symbolic modes use mode 0 as a point of
690 departure. If MODE starts with `-', true if *all* of the
691 permissions set in MODE are set for the file; permissions not set
692 in MODE are ignored. If MODE starts with `+', true if *any* of
693 the permissions set in MODE are set for the file; permissions not
694 set in MODE are ignored.
697 File: find.info, Node: Contents, Next: Directories, Prev: Permissions, Up: Finding Files
702 To search for files based on their contents, you can use the `grep'
703 program. For example, to find out which C source files in the current
704 directory contain the string `thing', you can do:
708 If you also want to search for the string in files in subdirectories,
709 you can combine `grep' with `find' and `xargs', like this:
711 find . -name '*.[ch]' | xargs grep -l thing
713 The `-l' option causes `grep' to print only the names of files that
714 contain the string, rather than the lines that contain it. The string
715 argument (`thing') is actually a regular expression, so it can contain
716 metacharacters. This method can be refined a little by using the `-r'
717 option to make `xargs' not run `grep' if `find' produces no output, and
718 using the `find' action `-print0' and the `xargs' option `-0' to avoid
719 misinterpreting files whose names contain spaces:
721 find . -name '*.[ch]' -print0 | xargs -r -0 grep -l thing
723 For a fuller treatment of finding files whose contents match a
724 pattern, see the manual page for `grep'.
727 File: find.info, Node: Directories, Next: Filesystems, Prev: Contents, Up: Finding Files
732 Here is how to control which directories `find' searches, and how it
733 searches them. These two options allow you to process a horizontal
734 slice of a directory tree.
736 - Option: -maxdepth LEVELS
737 Descend at most LEVELS (a non-negative integer) levels of
738 directories below the command line arguments. `-maxdepth 0' means
739 only apply the tests and actions to the command line arguments.
741 - Option: -mindepth LEVELS
742 Do not apply any tests or actions at levels less than LEVELS (a
743 non-negative integer). `-mindepth 1' means process all files
744 except the command line arguments.
747 Process each directory's contents before the directory itself.
748 Doing this is a good idea when producing lists of files to archive
749 with `cpio' or `tar'. If a directory does not have write
750 permission for its owner, its contents can still be restored from
751 the archive since the directory's permissions are restored after
755 If `-depth' is not given, true; do not descend the current
756 directory. If `-depth' is given, false; no effect. `-prune' only
757 affects tests and actions that come after it in the expression, not
758 those that come before.
760 For example, to skip the directory `src/emacs' and all files and
761 directories under it, and print the names of the other files found:
763 find . -path './src/emacs' -prune -o -print
766 Do not optimize by assuming that directories contain 2 fewer
767 subdirectories than their hard link count. This option is needed
768 when searching filesystems that do not follow the Unix
769 directory-link convention, such as CD-ROM or MS-DOS filesystems or
770 AFS volume mount points. Each directory on a normal Unix
771 filesystem has at least 2 hard links: its name and its `.' entry.
772 Additionally, its subdirectories (if any) each have a `..' entry
773 linked to that directory. When `find' is examining a directory,
774 after it has statted 2 fewer subdirectories than the directory's
775 link count, it knows that the rest of the entries in the directory
776 are non-directories ("leaf" files in the directory tree). If only
777 the files' names need to be examined, there is no need to stat
778 them; this gives a significant increase in search speed.
781 File: find.info, Node: Filesystems, Next: Combining Primaries With Operators, Prev: Directories, Up: Finding Files
786 A "filesystem" is a section of a disk, either on the local host or
787 mounted from a remote host over a network. Searching network
788 filesystems can be slow, so it is common to make `find' avoid them.
790 There are two ways to avoid searching certain filesystems. One way
791 is to tell `find' to only search one filesystem:
795 Don't descend directories on other filesystems. These options are
798 The other way is to check the type of filesystem each file is on, and
799 not descend directories that are on undesirable filesystem types:
802 True if the file is on a filesystem of type TYPE. The valid
803 filesystem types vary among different versions of Unix; an
804 incomplete list of filesystem types that are accepted on some
805 version of Unix or another is:
806 ufs 4.2 4.3 nfs tmp mfs S51K S52K
807 You can use `-printf' with the `%F' directive to see the types of
808 your filesystems. *Note Print File Information::. `-fstype' is
809 usually used with `-prune' to avoid searching remote filesystems
810 (*note Directories::.).
813 File: find.info, Node: Combining Primaries With Operators, Prev: Filesystems, Up: Finding Files
815 Combining Primaries With Operators
816 ==================================
818 Operators build a complex expression from tests and actions. The
819 operators are, in order of decreasing precedence:
822 Force precedence. True if EXPR is true.
826 True if EXPR is false.
831 And; EXPR2 is not evaluated if EXPR1 is false.
835 Or; EXPR2 is not evaluated if EXPR1 is true.
838 List; both EXPR1 and EXPR2 are always evaluated. True if EXPR2 is
839 true. The value of EXPR1 is discarded. This operator lets you do
840 multiple independent operations on one traversal, without
841 depending on whether other operations succeeded.
843 `find' searches the directory tree rooted at each file name by
844 evaluating the expression from left to right, according to the rules of
845 precedence, until the outcome is known (the left hand side is false for
846 `-and', true for `-or'), at which point `find' moves on to the next
849 There are two other tests that can be useful in complex expressions:
858 File: find.info, Node: Actions, Next: Common Tasks, Prev: Finding Files, Up: Top
863 There are several ways you can print information about the files that
864 match the criteria you gave in the `find' expression. You can print
865 the information either to the standard output or to a file that you
866 name. You can also execute commands that have the file names as
867 arguments. You can use those commands as further filters to select
873 * Print File Information::
878 File: find.info, Node: Print File Name, Next: Print File Information, Up: Actions
884 True; print the full file name on the standard output, followed by
887 - Action: -fprint FILE
888 True; print the full file name into file FILE, followed by a
889 newline. If FILE does not exist when `find' is run, it is
890 created; if it does exist, it is truncated to 0 bytes. The file
891 names `/dev/stdout' and `/dev/stderr' are handled specially; they
892 refer to the standard output and standard error output,
896 File: find.info, Node: Print File Information, Next: Run Commands, Prev: Print File Name, Up: Actions
898 Print File Information
899 ======================
902 True; list the current file in `ls -dils' format on the standard
903 output. The output looks like this:
905 204744 17 -rw-r--r-- 1 djm staff 17337 Nov 2 1992 ./lwall-quotes
909 1. The inode number of the file. *Note Hard Links::, for how to
910 find files based on their inode number.
912 2. the number of blocks in the file. The block counts are of 1K
913 blocks, unless the environment variable `POSIXLY_CORRECT' is
914 set, in which case 512-byte blocks are used. *Note Size::,
915 for how to find files based on their size.
917 3. The file's type and permissions. The type is shown as a dash
918 for a regular file; for other file types, a letter like for
919 `-type' is used (*note Type::.). The permissions are read,
920 write, and execute for the file's owner, its group, and other
921 users, respectively; a dash means the permission is not
922 granted. *Note File Permissions::, for more details about
923 file permissions. *Note Permissions::, for how to find files
924 based on their permissions.
926 4. The number of hard links to the file.
928 5. The user who owns the file.
932 7. The file's size in bytes.
934 8. The date the file was last modified.
936 9. The file's name. `-ls' quotes non-printable characters in
937 the file names using C-like backslash escapes.
940 True; like `-ls' but write to FILE like `-fprint' (*note Print
943 - Action: -printf FORMAT
944 True; print FORMAT on the standard output, interpreting `\'
945 escapes and `%' directives. Field widths and precisions can be
946 specified as with the `printf' C function. Unlike `-print',
947 `-printf' does not add a newline at the end of the string.
949 - Action: -fprintf FILE FORMAT
950 True; like `-printf' but write to FILE like `-fprint' (*note Print
956 * Format Directives::
960 File: find.info, Node: Escapes, Next: Format Directives, Up: Print File Information
965 The escapes that `-printf' and `-fprintf' recognize are:
974 Stop printing from this format immediately and flush the output.
992 A literal backslash (`\').
994 A `\' character followed by any other character is treated as an
995 ordinary character, so they both are printed, and a warning message is
996 printed to the standard error output (because it was probably a typo).
999 File: find.info, Node: Format Directives, Next: Time Formats, Prev: Escapes, Up: Print File Information
1004 `-printf' and `-fprintf' support the following format directives to
1005 print information about the file being processed. Unlike the C
1006 `printf' function, they do not support field width specifiers.
1008 `%%' is a literal percent sign. A `%' character followed by any
1009 other character is discarded (but the other character is printed), and
1010 a warning message is printed to the standard error output (because it
1011 was probably a typo).
1016 * Ownership Directives::
1018 * Location Directives::
1022 File: find.info, Node: Name Directives, Next: Ownership Directives, Up: Format Directives
1031 File's name with any leading directories removed (only the last
1035 Leading directories of file's name (all but the last element and
1036 the slash before it).
1039 File's name with the name of the command line argument under which
1040 it was found removed from the beginning.
1043 Command line argument under which file was found.
1046 File: find.info, Node: Ownership Directives, Next: Size Directives, Prev: Name Directives, Up: Format Directives
1048 Ownership Directives
1049 ....................
1052 File's group name, or numeric group ID if the group has no name.
1055 File's numeric group ID.
1058 File's user name, or numeric user ID if the user has no name.
1061 File's numeric user ID.
1064 File's permissions (in octal).
1067 File: find.info, Node: Size Directives, Next: Location Directives, Prev: Ownership Directives, Up: Format Directives
1073 File's size in 1K blocks (rounded up).
1076 File's size in 512-byte blocks (rounded up).
1079 File's size in bytes.
1082 File: find.info, Node: Location Directives, Next: Time Directives, Prev: Size Directives, Up: Format Directives
1088 File's depth in the directory tree; files named on the command line
1092 Type of the filesystem the file is on; this value can be used for
1093 `-fstype' (*note Directories::.).
1096 Object of symbolic link (empty string if file is not a symbolic
1100 File's inode number (in decimal).
1103 Number of hard links to file.
1106 File: find.info, Node: Time Directives, Prev: Location Directives, Up: Format Directives
1111 Some of these directives use the C `ctime' function. Its output
1112 depends on the current locale, but it typically looks like
1114 Wed Nov 2 00:42:36 1994
1117 File's last access time in the format returned by the C `ctime'
1121 File's last access time in the format specified by K (*note Time
1125 File's last status change time in the format returned by the C
1129 File's last status change time in the format specified by K (*note
1133 File's last modification time in the format returned by the C
1137 File's last modification time in the format specified by K (*note
1141 File: find.info, Node: Time Formats, Prev: Format Directives, Up: Print File Information
1146 Below are the formats for the directives `%A', `%C', and `%T', which
1147 print the file's timestamps. Some of these formats might not be
1148 available on all systems, due to differences in the C `strftime'
1149 function between systems.
1155 * Combined Time Formats::
1158 File: find.info, Node: Time Components, Next: Date Components, Up: Time Formats
1163 The following format directives print single components of the time.
1181 time zone (e.g., EDT), or nothing if no time zone is determinable
1190 seconds since Jan. 1, 1970, 00:00 GMT.
1193 File: find.info, Node: Date Components, Next: Combined Time Formats, Prev: Time Components, Up: Time Formats
1198 The following format directives print single components of the date.
1201 locale's abbreviated weekday name (Sun..Sat)
1204 locale's full weekday name, variable length (Sunday..Saturday)
1208 locale's abbreviated month name (Jan..Dec)
1211 locale's full month name, variable length (January..December)
1217 day of month (01..31)
1223 day of year (001..366)
1226 week number of year with Sunday as first day of week (00..53)
1229 week number of year with Monday as first day of week (00..53)
1235 last two digits of year (00..99)
1238 File: find.info, Node: Combined Time Formats, Prev: Date Components, Up: Time Formats
1240 Combined Time Formats
1241 .....................
1243 The following format directives print combinations of time and date
1247 time, 12-hour (hh:mm:ss [AP]M)
1250 time, 24-hour (hh:mm:ss)
1253 locale's time representation (H:M:S)
1256 locale's date and time (Sat Nov 04 12:02:33 EST 1989)
1262 locale's date representation (mm/dd/yy)
1265 File: find.info, Node: Run Commands, Next: Adding Tests, Prev: Print File Information, Up: Actions
1270 You can use the list of file names created by `find' or `locate' as
1271 arguments to other commands. In this way you can perform arbitrary
1272 actions on the files.
1281 File: find.info, Node: Single File, Next: Multiple Files, Up: Run Commands
1286 Here is how to run a command on one file at a time.
1288 - Action: -exec COMMAND ;
1289 Execute COMMAND; true if 0 status is returned. `find' takes all
1290 arguments after `-exec' to be part of the command until an
1291 argument consisting of `;' is reached. It replaces the string
1292 `{}' by the current file name being processed everywhere it occurs
1293 in the command. Both of these constructions need to be escaped
1294 (with a `\') or quoted to protect them from expansion by the shell.
1295 The command is executed in the directory in which `find' was run.
1297 For example, to compare each C header file in the current
1298 directory with the file `/tmp/master':
1300 find . -name '*.h' -exec diff -u '{}' /tmp/master ';'
1303 File: find.info, Node: Multiple Files, Next: Querying, Prev: Single File, Up: Run Commands
1308 Sometimes you need to process files alone. But when you don't, it
1309 is faster to run a command on as many files as possible at a time,
1310 rather than once per file. Doing this saves on the time it takes to
1311 start up the command each time.
1313 To run a command on more than one file at once, use the `xargs'
1314 command, which is invoked like this:
1316 xargs [OPTION...] [COMMAND [INITIAL-ARGUMENTS]]
1318 `xargs' reads arguments from the standard input, delimited by blanks
1319 (which can be protected with double or single quotes or a backslash) or
1320 newlines. It executes the COMMAND (default is `/bin/echo') one or more
1321 times with any INITIAL-ARGUMENTS followed by arguments read from
1322 standard input. Blank lines on the standard input are ignored.
1324 Instead of blank-delimited names, it is safer to use `find -print0'
1325 or `find -fprint0' and process the output by giving the `-0' or
1326 `--null' option to GNU `xargs', GNU `tar', GNU `cpio', or `perl'.
1328 You can use shell command substitution (backquotes) to process a
1329 list of arguments, like this:
1331 grep -l sprintf `find $HOME -name '*.c' -print`
1333 However, that method produces an error if the length of the `.c'
1334 file names exceeds the operating system's command-line length limit.
1335 `xargs' avoids that problem by running the command as many times as
1336 necessary without exceeding the limit:
1338 find $HOME -name '*.c' -print | grep -l sprintf
1340 However, if the command needs to have its standard input be a
1341 terminal (`less', for example), you have to use the shell command
1342 substitution method.
1346 * Unsafe File Name Handling::
1347 * Safe File Name Handling::
1348 * Limiting Command Size::
1349 * Interspersing File Names::
1352 File: find.info, Node: Unsafe File Name Handling, Next: Safe File Name Handling, Up: Multiple Files
1354 Unsafe File Name Handling
1355 .........................
1357 Because file names can contain quotes, backslashes, blank characters,
1358 and even newlines, it is not safe to process them using `xargs' in its
1359 default mode of operation. But since most files' names do not contain
1360 blanks, this problem occurs only infrequently. If you are only
1361 searching through files that you know have safe names, then you need not
1362 be concerned about it.
1364 In many applications, if `xargs' botches processing a file because
1365 its name contains special characters, some data might be lost. The
1366 importance of this problem depends on the importance of the data and
1367 whether anyone notices the loss soon enough to correct it. However,
1368 here is an extreme example of the problems that using blank-delimited
1369 names can cause. If the following command is run daily from `cron',
1370 then any user can remove any file on the system:
1372 find / -name '#*' -atime +7 -print | xargs rm
1374 For example, you could do something like this:
1379 and then `cron' would delete `/vmunix', if it ran `xargs' with `/' as
1380 its current directory.
1382 To delete other files, for example `/u/joeuser/.plan', you could do
1389 eg$ mkdir u u/joeuser u/joeuser/.plan'
1391 eg$ echo > u/joeuser/.plan'
1394 eg$ find . -name '#*' -print | xargs echo
1395 ./# ./# /u/joeuser/.plan /#foo
1398 File: find.info, Node: Safe File Name Handling, Next: Limiting Command Size, Prev: Unsafe File Name Handling, Up: Multiple Files
1400 Safe File Name Handling
1401 .......................
1403 Here is how to make `find' output file names so that they can be
1404 used by other programs without being mangled or misinterpreted. You can
1405 process file names generated this way by giving the `-0' or `--null'
1406 option to GNU `xargs', GNU `tar', GNU `cpio', or `perl'.
1409 True; print the full file name on the standard output, followed by
1412 - Action: -fprint0 FILE
1413 True; like `-print0' but write to FILE like `-fprint' (*note Print
1417 File: find.info, Node: Limiting Command Size, Next: Interspersing File Names, Prev: Safe File Name Handling, Up: Multiple Files
1419 Limiting Command Size
1420 .....................
1422 `xargs' gives you control over how many arguments it passes to the
1423 command each time it executes it. By default, it uses up to `ARG_MAX'
1424 - 2k, or 20k, whichever is smaller, characters per command. It uses as
1425 many lines and arguments as fit within that limit. The following
1426 options modify those values.
1430 If the standard input does not contain any nonblanks, do not run
1431 the command. By default, the command is run once even if there is
1434 `--max-lines[=MAX-LINES]'
1436 Use at most MAX-LINES nonblank input lines per command line;
1437 MAX-LINES defaults to 1 if omitted. Trailing blanks cause an
1438 input line to be logically continued on the next input line, for
1439 the purpose of counting the lines. Implies `-x'.
1441 `--max-args=MAX-ARGS'
1443 Use at most MAX-ARGS arguments per command line. Fewer than
1444 MAX-ARGS arguments will be used if the size (see the `-s' option)
1445 is exceeded, unless the `-x' option is given, in which case
1448 `--max-chars=MAX-CHARS'
1450 Use at most MAX-CHARS characters per command line, including the
1451 command and initial arguments and the terminating nulls at the
1452 ends of the argument strings.
1454 `--max-procs=MAX-PROCS'
1456 Run up to MAX-PROCS processes at a time; the default is 1. If
1457 MAX-PROCS is 0, `xargs' will run as many processes as possible at
1458 a time. Use the `-n', `-s', or `-l' option with `-P'; otherwise
1459 chances are that the command will be run only once.
1462 File: find.info, Node: Interspersing File Names, Prev: Limiting Command Size, Up: Multiple Files
1464 Interspersing File Names
1465 ........................
1467 `xargs' can insert the name of the file it is processing between
1468 arguments you give for the command. Unless you also give options to
1469 limit the command size (*note Limiting Command Size::.), this mode of
1470 operation is equivalent to `find -exec' (*note Single File::.).
1472 `--replace[=REPLACE-STR]'
1474 Replace occurences of REPLACE-STR in the initial arguments with
1475 names read from standard input. Also, unquoted blanks do not
1476 terminate arguments. If REPLACE-STR is omitted, it defaults to
1477 `{}' (like for `find -exec'). Implies `-x' and `-l 1'. As an
1478 example, to sort each file the `bills' directory, leaving the
1479 output in that file name with `.sorted' appended, you could do:
1481 find bills -type f | xargs -iXX sort -o XX.sorted XX
1483 The equivalent command using `find -exec' is:
1485 find bills -type f -exec sort -o '{}.sorted' '{}' ';'
1488 File: find.info, Node: Querying, Prev: Multiple Files, Up: Run Commands
1493 To ask the user whether to execute a command on a single file, you
1494 can use the `find' primary `-ok' instead of `-exec':
1496 - Action: -ok COMMAND ;
1497 Like `-exec' (*note Single File::.), but ask the user first (on
1498 the standard input); if the response does not start with `y' or
1499 `Y', do not run the command, and return false.
1501 When processing multiple files with a single command, to query the
1502 user you give `xargs' the following option. When using this option, you
1503 might find it useful to control the number of files processed per
1504 invocation of the command (*note Limiting Command Size::.).
1508 Prompt the user about whether to run each command line and read a
1509 line from the terminal. Only run the command line if the response
1510 starts with `y' or `Y'. Implies `-t'.
1513 File: find.info, Node: Adding Tests, Prev: Run Commands, Up: Actions
1518 You can test for file attributes that none of the `find' builtin
1519 tests check. To do this, use `xargs' to run a program that filters a
1520 list of files printed by `find'. If possible, use `find' builtin tests
1521 to pare down the list, so the program run by `xargs' has less work to
1522 do. The tests builtin to `find' will likely run faster than tests that
1523 other programs perform.
1525 For example, here is a way to print the names of all of the
1526 unstripped binaries in the `/usr/local' directory tree. Builtin tests
1527 avoid running `file' on files that are not regular files or are not
1530 find /usr/local -type f -perm +a=x | xargs file |
1531 grep 'not stripped' | cut -d: -f1
1533 The `cut' program removes everything after the file name from the
1536 If you want to place a special test somewhere in the middle of a
1537 `find' expression, you can use `-exec' to run a program that performs
1538 the test. Because `-exec' evaluates to the exit status of the executed
1539 program, you can write a program (which can be a shell script) that
1540 tests for a special attribute and make it exit with a true (zero) or
1541 false (non-zero) status. It is a good idea to place such a special
1542 test *after* the builtin tests, because it starts a new process which
1543 could be avoided if a builtin test evaluates to false. Use this method
1544 only when `xargs' is not flexible enough, because starting one or more
1545 new processes to test each file is slower than using `xargs' to start
1546 one process that tests many files.
1548 Here is a shell script called `unstripped' that checks whether its
1549 argument is an unstripped binary file:
1552 file $1 | grep 'not stripped' > /dev/null
1554 This script relies on the fact that the shell exits with the status
1555 of the last program it executed, in this case `grep'. `grep' exits
1556 with a true status if it found any matches, false if not. Here is an
1557 example of using the script (assuming it is in your search path). It
1558 lists the stripped executables in the file `sbins' and the unstripped
1561 find /usr/local -type f -perm +a=x \
1562 \( -exec unstripped '{}' \; -fprint ubins -o -fprint sbins \)
1565 File: find.info, Node: Common Tasks, Next: Databases, Prev: Actions, Up: Top
1570 The sections that follow contain some extended examples that both
1571 give a good idea of the power of these programs, and show you how to
1572 solve common real-world problems.
1576 * Viewing And Editing::
1579 * Strange File Names::
1580 * Fixing Permissions::
1581 * Classifying Files::