Finding Files with find
Contents:
The find Command Is Great; The Problem Is Finding How to Use It
Delving Through a Deep Directory Tree
Don't Forget -print
Looking for Files with Particular Names
Searching for Old Files
Be an Expert on find Search Operators
The Times that find Finds
Exact File Time Comparisons
Problems with -newer
Running Commands on What You Find
Using -exec to Create Custom Tests
Finding Many Things with One Command
Searching for Files by Type
Searching for Files by Size
Searching for Files by Permission
Searching by Owner and Group
Duplicating a Directory Tree (No Pathnames with find {} Operator)
Using "Fast find"
Finding Files (Much) Faster with a find Database
grepping a Directory Tree (and a Gotcha)
lookfor: Which File Has that Word?
Finding the Links to a File
Finding Files with -prune
Skipping Some Parts of a Tree in find (A More Selective -prune)
Keeping find From Searching Networked Filesystems
The find Command Is Great; The Problem Is Finding How to Use It
find is one of UNIX's most useful and important utilities. It finds files that match a given set of parameters, ranging from the file's name to its modification date. Now, we'll be looking at many of the things it can do. As an introduction, here's a quick summary of its features and operators:
%find
path operators
where path
is the directory in which find will begin to search and operators
(or, in more customary jargon, options) tell find which files you're interested in. The operators
are:
-name
filename
- Find files with the given
filename
. This is the most commonly used operator.filename
may include wildcards (), but if it does, it must be quoted to prevent the shell from interpreting the wildcards. See article . -perm
mode
- Find files with the given access
mode
(). You must give the access mode in octal (). See articles and . -type
c
- The files of the given type, specified by
c
.c
is a one-digit code; for example,f
for a plain file,b
for a block special file,l
for a symbolic link, etc. See article . -user
name
- Find files belonging to user
name
.name
may also be a user ID number (). See article . -group
name
- Find files belonging to group
name
.name
may also be a group ID number (). See article . -size
n
- Find files that are
n
blocks long. A block equals 512 bytes. The notation+
n
says "find files that are overn
blocks long." The notationn
c
says "find files that aren
characters long." Can you guess what+
n
c
means? See article . -inum
n
- Find files with the inode number ()
n
. See article . -atime
n
- Find files that were accessed
n
days ago.+
n
means "find files that were accessed over n days ago" (i.e., not accessed in the last n days).-
n
means "find files that were accessed less than n days ago" (i.e., accessed in the last n days). See articles and . -mtime
n
- Similar to atime, except that it checks the time the file's contents were modified. See articles and .
-ctime
n
- Similar to atime, except that it checks the time the inode () was last changed. "Changed" means that the file was modified or that one of its attributes (for example, its owner) was changed. See articles and .
-newer
file
- Find files that have been modified more recently than the given
file
. See articles and .
Of course, you often want to take some action on files that match several criteria. So we need some way to combine several operators:
operator1
-a
operator2
- Find files that match both
operator1
andoperator2
. The-a
isn't necessary; when two search parameters are juxtaposed, find assumes you want files that match both of them. See article . operator1
-o
operator2
- Find files that match either
operator1
oroperator2
. See article . !
operator
- Find all files that do not match the given
operator
. The!
performs a logical NOT operation. See article . (
expression
\)
- Logical precedence; in a complex expression, evaluate this part of the
expression
before the rest. See article .
Another group of operators tells find what action to take when it locates a file:
-print
- Print the file's name on standard output. See articles and .
-exec
command
- Execute
command
. To include the pathname of the file that's just been found incommand
, use the special symbol{}
.command
must end with a backslash followed by a semicolon (;
). For example:
%
find -name "*.o" -exec rm -f {} \;
tells find to delete any files whose names end in o. See article .
-ok
command
- Same as -exec, except that find prompts you for permission before executing
command
. This is a useful way to test find commands. See article .
A last word: find is one of the tools that vendors frequently fiddle with, adding (or deleting) a few operators that they like (or dislike). The operators listed above should be valid on virtually any system. If you check your system's manual page, you may find a few others.
- ML