find - Find files

Synopsis

find [-H | -L | -P] [-Xdx] [-f file] file ... [expression]

Description

The find utility recursively descends the directory tree for each file listed, evaluating an expression (composed of the "primaries" and "operands" listed below) in terms of each file in the tree.

Options

-H
Symbolic links on the command line are followed. Symbolic links encountered in the tree traversal are not followed. The file information and file type returned for each symbolic link specified on the command line is for the file referenced by the link. If the referenced file does not exist, the file information and type will be for the link itself.
-L
Both symbolic links on the command line and symbolic links encountered in the tree traversal are followed. The file information and file type returned for each symbolic link is for the file referenced by the link. If the referenced file does not exist, the file information and type will be for the link itself.
-P
No symbolic links are followed. The file information and file type returned for each symbolic link are for the link itself.
-X
A modification to permit find to be safely used in conjunction with xargs. If a file name contains any of the delimiting characters used by xargs, a diagnostic message is displayed on standard error, and the file is skipped. The delimiting characters include single (') and double (") quotation marks, backslash (\), space, tab and newline characters.
-d
find performs a depth-first traversal. The directories are visited in post-order and all entries in a directory will be acted on before the directory itself. By default, find visits directories in pre-order, or before their contents. Note, the default is not a breadth-first traversal.
-f
Specify a file hierarchy for find to traverse. File hierarchies may also be specified as the operands immediately following the options.
-x
Prevent find from descending into directories that have a device number different than that of the file from which the descent began.

Primaries

-atime n
True if the difference between the file last access time and the time find was started, rounded up to the next full 24-hour period, is n 24-hour periods.
-ctime n
True if the difference between the time of last change of file status information and the time find was started, rounded up to the next full 24-hour period, is n 24-hour periods.
-exec utility [argument ...] ;
True if the program named utility returns a zero value as its exit status. Optional arguments may be passed to the utility. The expression must be terminated by a semicolon (;). If the string "{}" appears anywhere in the utility name or the arguments it is replaced by the path name of the current file. The utility is run from the directory from which find was run. Since the semicolon is also a special character for the shell, you may need to escape the semicolon so it is passed as an argument to find.
-group gname
True if the file belongs to the group gname. If gname is numeric and there is no such group name, then gname is treated as a group identifier.
-inum n
True if the file has inode number n.
-links n
True if the file has n links.
-ls
This primary always evaluates to true. The following information for the current file is written to standard output:
  • inode number
  • size in kilobytes
  • file permissions
  • number of hard links
  • owner
  • group
  • size in bytes
  • last modification time
  • path name

If the file is a block or character special file, the major and minor numbers will be displayed instead of the size in bytes. If the file is a symbolic link, the path name of the linked-to file will be displayed preceded by `->'.

-mtime n
True if the difference between the file last modification time and the time find was started, rounded up to the next full 24-hour period, is n 24-hour periods.
-ok utility [argument...] ;
The -ok primary is identical to the -exec primary with the exception that find requests user affirmation for running the utility by printing a message to standard error and reading a response. If the response is other than the first character of the YES response in the current locale, the utility is not run and the value of the ok expression is false.
-name pattern
True if the last component of the path name being examined matches pattern. Special shell pattern matching characters ([, ], *, and ?) may be used as part of pattern. These characters may be matched explicitly by escaping them with a backslash (\).
-newer file
True if the current file has a more recent last modification time than file.
-nouser
True if the file belongs to an unknown user.
-nogroup
True if the file belongs to an unknown group.
-path pattern
True if the path name being examined matches pattern. Special shell pattern matching characters ([, ], *, and ?) may be used as part of pattern. These characters may be matched explicitly by escaping them with a backslash (\). Slashes (/) are treated as normal characters and do not need to be matched explicitly.
-perm [-]mode
The mode can be either symbolic or an octal number in the formats supported by the chmod command. If the mode is symbolic, a starting value of zero is assumed and the mode sets or clears permissions without regard to the process file mode creation mask. If the mode is octal, only bits 00777 (S_IRWXU | S_IRWXG | S_IRWXO) of the file's mode bits participate in the comparison. If the mode is preceded by a dash (-), this primary evaluates to true if at least all of the bits in the mode are set in the file's mode bits. If the mode is not preceded by a dash, this primary evaluates to true if the bits in the mode exactly match the file's mode bits. Note, the first character of a symbolic mode may not be a dash (-).
-print
This primary always evaluates to true. It prints the path name of the current file to standard output. The expression is appended to the user specified expression if neither -exec, -ls nor -ok is specified.
-prune
This primary always evaluates to true. It causes find to not descend into the current file. Note, the -prune primary has no effect if the -d option was specified.
-size n[c]
True if the file's size, rounded up, in 512-byte blocks is n. If n is followed by c, then the primary is true if the file's size is n bytes.
-type t
True if the file is of the specified type. Possible file types are as follows:
  • b for block special
  • c for character special
  • d for directory
  • f for regular file
  • l for symbolic link
  • p for FIFO
  • s for socket
-user uname
True if the file belongs to the user uname. If uname is numeric and there is no such user name, then uname is treated as a user identifier.

All primaries which take a numeric argument allow the number to be preceded by a plus sign (+) or a minus sign (-). A preceding plus sign means "more than n", a preceding minus sign means "less than n" and neither means "exactly n".

Operators

The primaries may be combined using the following operators. The operators are listed in order of decreasing precedence.

(expression)
This evaluates to true if the parenthesized expression evaluates to true.
!expression
This is the unary NOT operator. It evaluates to true if the expression is false.
expression -and expression
The -and operator is the logical AND operator. As it is implied by the juxtaposition of two expressions it does not need to be specified. The expression evaluates to true if both expressions are true. The second expression is not evaluated if the first expression is false.
expression -or expression
The -or operator is the logical OR operator. The expression evaluates to true if either the first or the second expression is true. The second expression is not evaluated if the first expression is true.

All operands and primaries must be separate arguments to the find utility. Primaries which themselves take arguments expect each argument to be a separate argument to find. Notes

The special characters used by find are also special characters to many shell programs. In particular, the characters *, [, ], ?, (, ), !, and ; may need to be escaped from the shell.

Exit status

  • 0 on success
  • >0 if an error occurs

Examples

  1. Find all *.class files starting at the directory "/project/java/class".
    find /project/java/class -name '*.class'
  2. Find all the *.java files that have the "import java.awt;" string in them starting at the directory, "/project/java/code".
    find /project/java/code -name '*.java' -exec grep 'import java.awt;' {} \;
  3. Find all the *.class files starting at the directory "/project/java/class" and remove the files.
    find /project/java/class -name '*.class' -exec rm {} \;
  4. Find all the files that belong to the user "abbey" starting at the directory, "/project".
    find /project -user abbey