find
pathname
(s)condition(s)
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).
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, -perm).
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
.
dev
Take matching files and write them on device dev
, using cpio.
(SVR3 only.)
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.
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).
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 symbolic links and track the directories visited (don't use this with -type l).
type
Find files that reside on file system type
.
gname
Find files belonging to group gname
. gname
can be a group
name or a group ID number.
n
Find files whose inode number is n
.
n
Find files having n
links.
Find files that physically reside on the local system.
Search for files that reside only on the same file system as pathname
.
(Use -xdev on BSD systems.)
Display matching files with associated statistics (as if run through ls -gilds.
n
| -n
| n
Find files that were last modified more than n
(+n
), less
than n
(-n
), or exactly n
days ago.
pattern
Find files whose names match pattern
. Filename metacharacters
may be used, but should be escaped or quoted.
dev
Take matching files and write them on device dev
, using cpio -c.
file
Find files that have been modified more recently than file
;
similar to -mtime.
Find files belonging to a group not
in /etc/group.
Find files owned by a user not
in /etc/passwd.
command
{ } \;Same as -exec, but user must prompt (with a y)
before command
is executed.
nnn
Find files whose permission flags (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).
Print the matching files and directories, using their full pathnames.
"Prune" the directory tree of unwanted directory searches; that is, skip the directory most recently matched.
n
[c]Find files containing n
blocks, or if c is specified,
n
characters long.
c
Find files whose type is c
. c
can be b (block
special file), c (character special file), d
(directory), p (fifo or named pipe),
l (symbolic link), or f (plain file).
user
Find files belonging to a user
name or ID.
List all files (and subdirectories) in your home directory:
find $HOME -print
List all files named chapter1 in the /work directory:
find /work -name chapter1 -print
List "memo" files owned by ann:
find /work /usr -name 'memo*' -user ann -print
Search the file system (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 . \! -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'