An extremely useful command for finding particular groups of files (numerous examples follow this description). find
descends the directory tree beginning at each pathname
and locates files that meet the specified conditions
. At least one pathname
and one condition
must be specified. The most useful conditions include -print
(which must be explicitly given to display any output), -name
and -type
(for general use), -exec
and -size
(for advanced users), and -mtime
and -user
(for administrators). On Solaris (and other recent Unix systems), -print
is the default condition if none are provided.
Conditions may be grouped by enclosing them in \( \)
(escaped parentheses), negated with !
(use \!
in the C shell), given as alternatives by separating them with -o
, or repeated (adding restrictions to the match; usually only for -name
, -type
, and -perm
).
The find
command can often be combined with the xargs
command when there are too many files for naming on the command line. (See xargs
.)
-atime
+
n
| -
n
| n
Find files that were last accessed more than n
(+
n
), less than n
(-
n
), or exactly n
days ago. Note that find
will change the access time of directories supplied as pathnames
.
-cpio
dev
Take matching files and write them on device dev
, using cpio
. Obsolete.
-ctime
+
n
| -
n
| n
Find files that were changed more than n
(+
n
), less than n
(-
n
), or exactly n
days ago. Change refers to modification, permission or ownership changes, etc.; therefore, -ctime
is more inclusive than -atime
or -mtime
.
-depth
Descend the directory tree, skipping directories and working on actual files first (and then
the parent directories). Useful when files reside in unwritable directories (e.g., when using find
with cpio
).
-exec
command
{} \;
Run the Unix command
on each file matched by find
, provided command
executes successfully on that file; i.e., returns a 0 exit status. When command
runs, the argument {}
substitutes the current file. Follow the entire sequence with an escaped semicolon (\;
).
-follow
Follow symbolic links and track the directories visited (don't use this with -type l
).
-fstype
type
Find files that reside on filesystems of type type
.
-group
gname
Find files belonging to group gname
. gname
can be a group name or a group ID number.
-inum
n
Find files whose inode number is n
.
-links
n
Find files having n
links.
-local
Find files that physically reside on the local system.
-ls
Display matching files with associated statistics (as if run through ls -lids
).
-mount
Search for files that reside only on the same filesystem as pathname
.
-mtime
+
n
| -
n
| n
Find files that were last modified more than n
(+
n
), less than n
(-
n
), or exactly n
days ago.
-name
pattern
Find files whose names match pattern
. Filename metacharacters may be used, but should be escaped or quoted.
-ncpio
dev
Take matching files and write them on device dev
, using cpio -c
. Obsolete.
-newer
file
Find files that have been modified more recently than file
; similar to -mtime
.
-nogroup
Find files belonging to a group not
in /etc/group
.
-nouser
Find files owned by a user not
in /etc/passwd
.
-ok
command
{} \;
Same as -exec
, but user must respond (with a y
) before command
is executed.
-perm
nnn
Find files whose permission settings (e.g., rwx
) match octal number nnn
exactly (e.g., 664 matches -rw-rw-r--
). Use a minus sign to make a wildcard match of any specified bit (e.g., -perm -600
matches -rw******
, where * can be any mode). Some systems also allow +
nnn
for this purpose.
Solaris allows nnn
to be a symbolic mode in the same form as allowed by chmod
.
-print
Print the matching files and directories, using their full pathnames. On Solaris, this is the default.
-prune
"Prune" the directory tree of unwanted directory searches; that is, skip the directory most recently matched.
-size
n
[c
]
Find files containing n
blocks, or, if c
is specified, files that are n
characters (bytes) long. (One block = 512 bytes).
-type
c
Find files whose type is c
. c
can be:
-user
user
Find files belonging to a user
name or ID.
-xdev
Same as -mount
. Solaris (and some BSD systems) only.
List all files (and subdirectories) in your home directory:
find $HOME -print
List all files named chapter1
underneath the /work
directory:
find /work -name chapter1 -print
List "memo" files owned by ann
(note the use of multiple starting paths):
find /work /usr -name 'memo*' -user ann -print
Search the filesystem (begin at root) for manpage directories:
find / -type d -name 'man*' -print
Search the current directory, look for filenames that don't begin with a capital letter, and send them to the printer:
find . \! -name '[A-Z]*' -exec lp {} \;
Find and compress files whose names don't end with .Z
:
compress `find . -type f \! -name '*.Z' -print`
Remove all empty files on the system (prompting first):
find / -size 0 -ok rm {} \;
Skip RCS directories, but list remaining read-only files:
find . -name RCS -prune -o -perm 444 -print
Search the system for files that were modified within the last two days (good candidates for backing up):
find / -mtime -2 -print
Recursively grep
for a pattern down a directory tree:
find /book -print | xargs grep '[Nn]utshell'