find pathname-list expression
Find recursively descends the directory hierarchy for each
pathname in the pathname-list (i.e., one or more pathnames) seeking
files that match a boolean expression written in the primaries given
below. In the descriptions, the argument n is used as a decimal
integer where +n means more than n, -n means less than
n and n means exactly n.
- -name filename
- True if the filename argument matches the current file name. Normal
Shell argument syntax as described in glob(7) may be used if
escaped (watch out for `[', `?' and `*'). The internationalization
constructs `[[:class:]]', `[[=e=]]', and `[[.cs.]]' are understood with
/usr/5bin/s42/find, /usr/5bin/posix/find, and
/usr/5bin/posix2001/find, but not with /usr/5bin/find.
- -perm mode
- True if the file permission flags exactly match the octal number or
symbolic mode (see chmod(1)). If mode is prefixed by
a minus sign, the flags are compared: (flags&mode)==mode.
- -type c
- True if the type of the file is c, where c is
b |
block special file; |
c |
character special file; |
d |
directory; |
D |
Solaris door; |
f |
plain file; |
l |
symbolic link; |
n |
HP-UX network special file; |
p |
named pipe; |
s |
socket. |
- -follow
- Always true; causes find to follow symbolic links. The `-type l'
condition never occurs in this case.
- -links n
- True if the file has n links.
- -user uname
- True if the file belongs to the user uname (login name or numeric
user ID).
- -group gname
- True if the file belongs to group gname (group name or numeric
group ID).
- -size
n[c]
- True if the file is n blocks long (512 bytes per block), or, with
c, n bytes long.
- -inum n
- True if the file has inode number n.
- -atime n
- True if the file has been accessed in n days.
- -mtime n
- True if the file has been modified in n days.
- -ctime n
- True if the file inode has been changed in n days.
- -exec command ...
;
- True if the executed command returns a zero value as exit status. The end
of the command must be punctuated by an (escaped) semicolon. A command
argument `{}' is replaced by the current pathname.
- -exec command ...
{} +
- Always true. The {} argument is replaced by a set of aggregated
pathnames. Each pathname is passed to the command as a single argument.
Every time a limit of arguments is reached by the pathnames found so far,
the command is executed, and aggregating starts using a new set beginning
with the next pathname. If any invocation of command returns a non-zero
exit status, find will return a non-zero exit status when its processing
is done.
- -ok command ...
;
- Like -exec except that the generated command is written on the
standard output, then the standard input is read and the command executed
only upon response y.
- -print
- Always true; causes the current pathname to be printed. If no expression
is given, -print is used per default (as a change introduced by
POSIX.2).
- -newer file
- True if the current file has been modified more recently than the argument
file.
- -anewer
file
- True if the current file has been accessed more recently than the argument
file was modified. This primary is an extension.
- -cnewer
file
- True if a status change has occurred on the current file more recently
than the argument file was modified. This primary is an
extension.
- -depth
- Always true; causes the contents of each directory to be examined before
the directory itselves.
- -fstype
type
- True if the current file resides on a file system of the given type.
- -local
- True if the file is on a local file system. Are file system types except
for nfs and smbfs are currently considered local.
- -mount
- Always true; restricts the search to directories that have the same device
id as the currently examined path operand.
- -xdev
- The same as -mount. This primary has been introduced by POSIX.
- -nouser
- True if the file is owned by a user that has no login name.
- -nogroup
- True if the file is owned by a group that lacks a group name.
- -prune
- Always true. Causes the search for the current path to be stopped once the
primary is evaluated. When combined with -depth, -prune has
no effect.
- -cpio device
- Always true. Writes the file on the named device in binary cpio format
(5120-byte records). Implies -depth.
- -ncpio
device
- Always true. Writes the file on the named device in SVR4 ASCII cpio format
(5120-byte records). Implies -depth.
The primaries may be combined using the following operators (in
order of decreasing precedence):
- 1)
- A parenthesized group of primaries and operators (parentheses are special
to the Shell and must be escaped).
- 2)
- The negation of a primary (`!' is the unary not operator).
- 3)
- Concatenation of primaries (the and operation is implied by the
juxtaposition of two primaries or by an explicit -a operator).
- 4)
- Alternation of primaries (`-o' is the or operator).
Options have been introduced by POSIX.1-2001 in addition to the
expression operators. They must preceed the pathname-list one the
command line and have no effect on boolean expression processing.
- -H
- Follow symbolic links given on the command line, but do not follow
symbolic links encountered during directory traversal.
- -L
- Follow all symbolic links found, like the -follow primary.
With the -follow primary or the -L option, hierarchy
loops caused by symbolic links are detected, but only
/usr/5bin/posix2001/find prints an error message. The offending link
is not followed, and operation continues with the next directory entry
found.
To remove all files named `a.out' or `*.o' that have not been
accessed for a week:
-
- find / \( -name a.out -o -name ´*.o´ \) -atime +7 -exec rm
{} \;
The rm command is executed once for each file. The form
-
- find / \( -name a.out -o -name ´*.o´ \) -atime +7 -exec rm
{} +
is faster since the rm command is executed with a set of
pathnames.
To find all files below the directory `documents' that contain the
regular expression `string':
-
- find documents -type f -exec grep string {} +
To find all files in the directory `home', not descending into its
subdirectories:
-
- find home ! -name home -prune
To check whether the file `diary' has been updated within the last
two days; the name of the file is printed if true, and is not printed
otherwise:
-
- find diary -prune -mtime -2
- LANG,
LC_ALL
- See locale(7).
- LC_COLLATE
- Affects the collation order for range expressions, equivalence classes,
and collation symbols in patterns with /usr/5bin/s42/find,
/usr/5bin/posix/find, and /usr/5bin/posix2001/find.
- LC_CTYPE
- Determines the mapping of bytes to characters and character class
expressions in patterns.
- SYSV3
- Causes the text of some diagnostic messages to be changed; causes
-ncpio to create traditional ASCII cpio format archives.
chmod(1), cpio(1), pax(1), sh(1), xargs(1), stat(2), glob(7),
locale(7)
Undesired effects can result if file names printed by find
contain newline characters, as shown by the following command sequence:
$ mkdir -p ´dummy
> /etc´
$ touch ´dummy
> /etc/passwd´
$ find . -print
.
./dummy
./dummy
/etc
./dummy
/etc/passwd
$
Shell scripts or utilities unaware of this problem will operate on
/etc/passwd (or other arbitrary file names) when reading such output; a
malicious user might create such files to read or overwrite privileged
information. To circumvent this problem, one of the following proposals should
be taken unless the file hierarchy traversed by the find command is
definitively known not to contain such file names:
- –
- If the output is read by the xargs utility to gain faster execution
by aggregating command arguments as in
find . -print | xargs command
a safe and equally fast substitute is the
find . -exec command {} +
operand of find; it is not portably accepted by find
implementations, though.
- –
- A universal solution for submitting file names to the xargs utility
is given in the NOTES section of xargs(1).
- –
- The method employed by this script can be generalized as follows: If the
script or utility reading the output of find provides the necessary
parsing capabilities, special path prefixes can be given to the
find command, such as
find /.//. -print
for absolute path names or
find .//. -print
for relative path names. Since adjacent slash characters never appear in
relative file names found during directory traversal, they can be taken
as delimiters; a line starts a new path name only if the delimiter
appears.
- –
- The -name operand can be used to exclude all path names that
contain newline characters, as in
$ find . -name ´*
> *´ -prune -o ! -name ´*
> *´ -print
Note that certain other implementations of find require a leading
period in the pattern to match file names with a leading period; it may be
necessary to exclude such patterns as well.
- –
- The -depth operand cannot be combined with the -prune
operand used in the previous example. When the directory name must be
printed after file names below that directory, as with the cpio
command, file names that leave the specified path hierarchy should be
filtered out:
find . -depth | egrep ´^\./´ | cpio -oc -O
/dev/rmt/c0s0
(note the escaped regular expression meta-character).
- –
- The -cpio and -ncpio operands will automatically exclude
file names that contain newline characters with this find
implementation.
The -print0 operand supported by some other implementations
is considered a very limited work-around since it does not allow the output
to be processed by utilities unaware of NUL characters; it has therefore not
been included here.