find prints to the standard output the names of the
files that match the given criteria. See section Actions, for how to get more
information about the matching files.
Here are ways to search for files whose name matches a certain pattern. See section Shell Pattern Matching, for a description of the pattern arguments to these tests.
Each of these tests has a case-sensitive version and a case-insensitive version, whose name begins with `i'. In a case-insensitive comparison, the patterns `fo*' and `F??' match the file names `Foo', `FOO', `foo', `fOo', etc.
find /usr/local/doc -name '*.texi'
To search for files by name without having to actually scan the
directories on the disk (which can be slow), you can use the
locate program. For each shell pattern you give it,
locate searches one or more databases of file names and displays
the file names that contain the pattern. See section Shell Pattern Matching,
for details about shell patterns.
If a pattern is a plain string--it contains no
locate displays all file names in the database
that contain that string. If a pattern contains
locate only displays file names that match the
pattern exactly. As a result, patterns that contain metacharacters
should usually begin with a `*', and will most often end with one
as well. The exceptions are patterns that are intended to explicitly
match the beginning or end of a file name.
is almost equivalent to
find directories -name pattern
where directories are the directories for which the file name
databases contain information. The differences are that the
locate information might be out of date, and that
handles wildcards in the pattern slightly differently than
(see section Shell Pattern Matching).
The file name databases contain lists of files that were on the system when the databases were last updated. The system administrator can choose the file name of the default database, the frequency with which the databases are updated, and the directories for which they contain entries.
Here is how to select which file name databases
The default is system-dependent.
LOCATE_PATHto set the list of database files to search. The option overrides the environment variable if both are used.
locate can compare file names, or parts of file
names, to shell patterns. A shell pattern is a string that may
contain the following special characters, which are known as
wildcards or metacharacters.
You must quote patterns that contain metacharacters to prevent the shell from expanding them itself. Double and single quotes both work; so does escaping with a backslash.
find tests that do shell pattern matching (`-name',
`-path', etc.), wildcards in the pattern do not match a `.'
at the beginning of a file name. This is not the case for
locate. Thus, `find -name '*macs'' does not match a file
named `.emacs', but `locate '*macs'' does.
Slash characters have no special significance in the shell pattern
locate do, unlike in the shell, in
which wildcards do not match them. Therefore, a pattern `foo*bar'
can match a file name `foo3/bar', and a pattern `./sr*sc' can
match a file name `./src/misc'.
There are two ways that files can be linked together. Symbolic links are a special type of file whose contents are a portion of the name of another file. Hard links are multiple directory entries for one file; the file names all have the same index node (inode) number on the disk.
find . -lname '*sysdep.c'
findfollows symbolic links to directories when searching directory trees.
To find hard links, first get the inode number of the file whose links you want to find. You can learn a file's inode number and the number of links to it by running `ls -i' or `find -ls'. If the file has more than one link, you can search for the other links by passing that inode number to `-inum'. Add the `-xdev' option if you are starting the search at a directory that has other filesystems mounted on it, such as `/usr' on many systems. Doing this saves needless searching, since hard links to a file must be on the same filesystem. See section Filesystems.
You can also search for files that have a certain number of links, with `-links'. Directories normally have at least two hard links; their `.' entry is the second one. If they have subdirectories, each of those also has a hard link called `..' to its parent directory.
Each file has three time stamps, which record the last time that certain operations were performed on the file:
You can search for files whose time stamps are within a certain age range, or compare them to other time stamps.
These tests are mainly useful with ranges (`+n' and `-n').
find /u/bill -amin +2 -amin -6
find ~ -daystart -type f -mtime 1
As an alternative to comparing timestamps to the current time, you can
compare them to another file's timestamp. That file's timestamp could
be updated by another program when some event occurs. Or you could set
it to a particular fixed date using the
touch command. For
example, to list files in `/usr' modified after February 1 of the
touch -t 02010000 /tmp/stamp$$ find /usr -newer /tmp/stamp$$ rm -f /tmp/stamp$$
find . -newer /bin/sh
The size does not count indirect blocks, but it does count blocks in sparse files that are not actually allocated.
See section File Permissions, for information on how file permissions are structured and how to specify them.
To search for files based on their contents, you can use the
program. For example, to find out which C source files in the current
directory contain the string `thing', you can do:
grep -l thing *.[ch]
If you also want to search for the string in files in subdirectories,
you can combine
find . -name '*.[ch]' | xargs grep -l thing
The `-l' option causes
grep to print only the names of files
that contain the string, rather than the lines that contain it. The
string argument (`thing') is actually a regular expression, so it
can contain metacharacters. This method can be refined a little by
using the `-r' option to make
xargs not run
find produces no output, and using the
`-print0' and the
xargs option `-0' to avoid
misinterpreting files whose names contain spaces:
find . -name '*.[ch]' -print0 | xargs -r -0 grep -l thing
For a fuller treatment of finding files whose contents match a pattern,
see the manual page for
Here is how to control which directories
find searches, and how
it searches them. These two options allow you to process a horizontal
slice of a directory tree.
tar. If a directory does not have write permission for its owner, its contents can still be restored from the archive since the directory's permissions are restored after its contents.
For example, to skip the directory `src/emacs' and all files and directories under it, and print the names of the other files found:
find . -path './src/emacs' -prune -o -print
findis examining a directory, after it has statted 2 fewer subdirectories than the directory's link count, it knows that the rest of the entries in the directory are non-directories (leaf files in the directory tree). If only the files' names need to be examined, there is no need to stat them; this gives a significant increase in search speed.
A filesystem is a section of a disk, either on the local host or
mounted from a remote host over a network. Searching network
filesystems can be slow, so it is common to make
find avoid them.
There are two ways to avoid searching certain filesystems. One way is
find to only search one filesystem:
The other way is to check the type of filesystem each file is on, and not descend directories that are on undesirable filesystem types:
ufs 4.2 4.3 nfs tmp mfs S51K S52K
You can use `-printf' with the `%F' directive to see the types of your filesystems. See section Print File Information. `-fstype' is usually used with `-prune' to avoid searching remote filesystems (see section Directories).
Operators build a complex expression from tests and actions. The operators are, in order of decreasing precedence:
( expr )
expr1 -a expr2
expr1 -and expr2
expr1 -o expr2
expr1 -or expr2
expr1 , expr2
find searches the directory tree rooted at each file name by
evaluating the expression from left to right, according to the rules of
precedence, until the outcome is known (the left hand side is false for
`-and', true for `-or'), at which point
find moves on
to the next file name.
There are two other tests that can be useful in complex expressions: