find scans directories and all their subdirectories for files which match a specified search criterion. Files located in this way can be listed and/or processed with a command.
find locates not only ordinary files, but also directories, special files and FIFOs.
Syntax
find directory expression |
directory
Name of the directory that is to be scanned. find will search through this directory and recursively through its subdirectories, provided you have read and execute permission (r and x bit) for them. More than one directory may be named.
expression
expression may be given as one or more primary expressions (primaries) of the kind listed below or as a combination of these primaries.
find applies expression to each file in the specified directories or subdirectories by evaluating primaries from left to right. Each primary is considered to have a "true" or "false" value for a file. The truth value of each expression and the next relational operator determine whether or not find will process the next primary.
Most primaries define conditions, i.e. they return a value of true if the current file satisfies the condition.
The expressions:
-print
-exec command \;
-ok command \;
are not conditions. They cause actions to be executed, e.g. output of the file name or deletion of the file. When find processes one of these expressions, the associated action is always performed, regardless of whether the expression returns a value of true or false. If none of the above action primaries are specified, find exits with an error message. Shell metacharacters that appear in expression must be escaped to ensure that they are passed to find and not interpreted by the shell.
Primaries as conditions
The following section describes primaries; the method of combining primaries is shown thereafter. If you use options to find files with specific attributes (e.g. the files modified within a given period), the relevant option must come before the -print option (see below), as otherwise all files will be listed. In the following discussion, n represents a decimal integer; +n means "more than n", -n means "less than n", and n means "exactly n".
True if file matches the name of the current file.
True if the file permission flags exactly match mode (see chmod). mode can also be an octal number. If mode is prefixed by a minus sign (-), only the bits that are set in mode are compared with the file permission flags, and the expression evaluates true if they match.
True if the current file is of type character, where character can be:
True if the file system to which the current file belongs is of type name, where name can be ufs or some special file systems such as proc or fdfs.
True if there are (more/less than) n links to the current file.
True if the current file has an inode number greater than (+), less than (-), or equal to n. The expression -inum +n -inum -m can be used to find all inode numbers between n and m.
True if the current file belongs to the user login_name. This option is only of use for users with uid=0 (command id).
True if the current file belongs to a login name which is not present. This option is only of use for users with uid=0 (command id).
True if the current file belongs to the group group_name.
True if the current file belongs to a group that is not listed in the /etc/group file.
c not specified: c specified:
(access) True if the current file was last accessed (more / less than / equal) (n-1) to n*24 hours ago. The access time of directories searched by find is changed by find itself.
(modification time) True if the current file was last modified (more / less than / equal) (n-1) to n*24 hours ago.
True if the inode of the current file was last modified (more / less than / equal) (n-1) to n*24 hours ago.
True if the current file is "newer" than the specified file, i.e. if it was created or modified at a later point in time.
True if the current file physically resides on the local system.
Always true. If this expression is processed no search is conducted in files or directories which lie below the current level in the file hierarchy.
Always true. If this expression is processed no search is conducted in files or directories which lie below directories which have a different device number (st_dev, see the stat() function [4]). If -xdev is set then this applies to the entire expression even if -xdev is not normally processed. |
Action primaries
Always true. If this expression is present, it causes find to print the path name of the current file on standard output.
command is executed as soon as this expression is processed. You must terminate command with a blank and an escaped semicolon (\;). A set of braces {} can be used to represent the name of the current file. The expression -exec command \; is true if the executed command returns an exit status of 0. The truth value is significant when this expression is combined with other expressions.
Like -exec, except that find asks if the command is to be executed each time the expression is processed. The command is only executed if you respond with the character or string signifying "y[es]" in the current locale (see environment variable LANG). |
Expressions that affect the behavior of find
Always true. If present, this expression causes find to act upon entries in a directory before the directory itself.
Always true. This expression causes symbolic links to be followed. It should not be combined with the -type l expression.
Always true. If this expression is present, it causes find to restrict its search to the file system containing the directory specified. |
Combining expressions
The expressions described above can be combined with one another as shown below. Please note the blanks used before and after the operators!
Parentheses group expressions together. The parentheses themselves must be escaped with a backslash, since they have a special meaning for the shell.
Negation, i.e. true if the expression evaluates to false.
Logical AND, i.e. true if both expressions are true. If the first expression is false, find does not process the second expression. Example If the second expression is
Logical OR, i.e. true when either of the expressions is true. If the first expression is true, Example If the second expression is |
Error
Error messages without command termination (exit status 0)
Error messages with command termination (non-zero exit status)
You have either combined primaries incorrectly in the find call or specified more than one argument in a primary.
You have not specified an action (print, exec, ok).
You have forgotten the escaped semicolon \; which is required to terminate a command in an -exec or -ok expression.
You have specified too few arguments.
You have failed to specify the pathname list for directory. |
File
/etc/group File containing group names |
Locale
The following environment variables affect the execution of find: LANG Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined. LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_COLLATE Determine the locale for the behavior of ranges, equivalence classes and multicharacter collating elements used in pattern matching notation for the -n option. In file name generation patterns in square brackets (such as find . -name ’[[=a=]]*’ -print), the LC_COLLATE environment variable governs the scope of character ranges, equivalence classes and collating elements. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other. LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. |
Example 1
A simple example to illustrate how find works: Display the path names of all files that
In this case, expression comprises two primaries that are linked by a logical AND operator. find first checks the -name tmp primary for each file. If this expression is true (i.e. the current file is named tmp), find will also process the expression -print, which outputs the path names. Otherwise, find does not process the -print expression and remains silent. Please note the sequence. If you entered:
instead, find would process the -print expression for each file, i.e. would output the path names of all files. |
Example 2
A more complex example: Display the path names of all files that
Please note the use of blanks between the arguments! find begins by processing the parenthesized specifications for each file. These comprise two primaries linked by a logical OR operator. find first tests the -name tmp primary. If this expression is true (i.e. if the current file is named tmp), find does not process the expression -name ’*.xx’ at all; this is because the result of the expression in parentheses will be true in any case. If -name tmp evaluates to false, find tests the second primary, i.e. -name ’*.xx’. If this expression is true (i.e. the file name ends in .xx), the parenthesized expression will also evaluate to true; otherwise, the result of the expression in parentheses is false. If the parenthesized expression is true, find then processes the -print expression, i.e. outputs the file names (AND operator; see Example 1). If false, find does not process the -print expression, i.e. remains silent. |
Example 3
Search all entries in the /usr/santaclaus directory and its subdirectories and print the files not owned by santaclaus.
|
Example 4
Delete all files that,
Confirmation is to be requested before the removal of each file.
|
Example 5
Recursively print all file names in the current directory and below, but skipping all SCCS directories:
|
See also
chmod, ln, test |