<code>find</code> is a shell command that locates files based on search criteria and performs actions on the matching files such as printing the file system path to the standard output. It starts searching at a directory in a hierarchical structure and recursively traverses the tree although can be limited to a maximum number of levels. Commonly used search criteria include file name pattern matching, file type matching, and time range matching for last modification or last access. With no arguments, the command outputs the path of each file in the tree rooted at the working directory. The command can search through different file systems of partitions belonging to one or more storage devices mounted under the starting directory. The command is available on most Unix-like systems.
The command first appeared in Version 5 Unix as part of the Programmer's Workbench project, and was written by Dick Haight alongside cpio, which were designed to be used together.
The GNU implementation was originally written by Eric Decker. It was later enhanced by David MacKenzie, Jay Plett, and Tim Wood. The GNU implementation provides features beyond the POSIX specification.
The command was ported to the IBM i operating system.
The BusyBox computer program provides the command and many other commands in a single executable file.
The syntax of the command can be described as:
$ find [-H|-L] path... [expression]
Traditionally, at least one path must precede the expression, but newer versions allow for no path argument, defaulting to the working directory.
The expression can specify various aspects including match criteria and actions to perform on matched files. Expression elements are separated by whitespace and are evaluated left-to-right. The command can match via wildcard characters but wildcard match text must be quoted to prevent shell globbing. If the expression is omitted, then all files are matched.
The expression specifies the behavior of the command including what files to select (sometimes called predicate) and what to do with each matching file (sometimes called action). The elements of an expression commonly include:
Operators are used to combine expression elements. Operators are listed in order of decreasing precedence:
In light of the fact that a file system can contain looped structures via hard and soft links, POSIX requires that the command detect infinite loops, i.e. entering a previously visited directory that is an ancestor of the last file encountered. When it detects an infinite loop, the command must write a diagnostic message to standard error plus either recover its position in the hierarchy or terminate.
The and options, specified in the POSIX standard, control how the command handles symbolic links. The default behavior is to not follow symbolic links. The option causes the command to follow symbolic links. The option causes the command to follow symbolic links while processing the command line arguments. A common extension is the option, for explicitly disabling symlink following.
The following command searches the current working directory tree for files named starting with "my". The single quotes avoid the shell expansion. Without them, the shell would replace "my*" with the list of files whose names begin with "my" in the current working directory which is not necessarily the same as the files matching in subdirectories.
The following command includes to limit results to regular files, excluding other file system items such as directories and symbolic links.
The following command includes the action option to include detailed file information like from command .
The following command searches every directory except the subdirectory tree as specified by the <code>-prune</code> option, for a regular file named .
The following command searches the and subdirectory trees for files named .
This following command finds files with a name ending with either "jsp" or "java" by combining two options with an option. The sub-expression () is enclosed in parentheses which are each escaped with a backslash to prevent them from being interpreted as special shell characters.
The following command changes the permissions of all files with names matching "*.mp3" in the directory tree "/var/ftp/mp3". The action is specified by <code>-exec chmod 644 {} \;</code>. For every matching file, the command <code>chmod 644 {}</code> is executed by <code>{}</code> replaced with the name of the file. The semicolon (with backslash prefix to avoid the shell interpreting it as a command separator) indicates the end of the command. In some shells, the <code>{}</code> must be quoted. The trailing is often escaped with a leading , but can be enclosed in single quotes instead.
The following command deletes empty files and print the names. The <code>-delete</code> option is an extension, originally added in some BSD-family operating systems, and present in FreeBSD 2.2.1 and later, NetBSD 5.0 and later, OpenBSD 6.1 and later, DragonFly BSD GNU find, and macOS.
The following command searches file owned by user 123.
The following command matches file names ignoring case. The <code>-iname</code> option is not POSIX required.
If the <code>-iname</code> switch is not supported on your system then workaround techniques may be possible such as:
The following command searches for files sized between 100 kilobytes and 500 kilobytes.
The following command searches for files in the document folder modified in the last week.
The following command finds files last edited in February 2017.
The following command lists files edited more recently than "document.txt".