
The basic syntax of the
find
command is simple:
find [where-to-look] [search-criteria] [what-to-do]
All parameters are optional (that's what those square brackets mean),
and the defaults are:
find . (current directory),
blank (show all files) and
-print
(display to the screen), so the command
find
on its own will display the pathnames of all files in the current
directory and all its subdirectories.
Basic searches
find -name myfile
Case-sensitive search for all files named "myfile" in the current
directory or any of its subdirectories.
find -iname myfile
Case-insensitive search for all files named "myfile" in the current
directory or any of its subdirectories. (This will also pick up files
with names like "Myfile", "MYFILE" and "MyFiLe".)
find ~ -name my*
Find all files in the user's /home directory beginning with "my".
find ~ -name *file
Find all files in the user's /home directory ending in "file".
find -name "[0-9]*"
Find all files and directories starting with a number.
find
-type d -name "[0-9]*"
Find only directories starting with a number.
find
-type d -name "[a-c]*"
Find only directories beginning with the letters a, b or c (lowercase).
Controlling depth
By default find searches all subdirectories too. But you can control
that with
-maxdepth.
find
. -maxdepth 1 -name myfile
Only search the current directory for files named "myfile".
Property searches
Find doesn't just work on file names!
find
~ -user fred
Find all files in /home directory owned by user fred.
find
~ -size +750k
Find all files in the /home directory larger then 7500k.
find
~ -size +500k -size -750k
Find all files in the /home directory larger then 500k but less than
750k.
find
~ -size +100M
Find all files in the /home directory greater than 100M.
Finding by time
-mtime is a measure of
24-hour periods, so ...
find
-mtime 0
Finds all files that were modified in the last 24 hours.
find
-mtime -2
Find all files that are 2 days old or newer.
find
-mtime +30
Find all files that were modified more than 30 days ago.
-mmin is a measure of
minutes, so ...
find
-mmin -10
Finds all files that were modified less than 10 minutes ago.
find
-mmin +5 -mmin -10
Find all files modified between 6 and 9 minutes ago.
Executing commands
If find returns a match,
-exec command will execute
command.
find
-iname my* -type f -exec ls -lah
'{}' \;
Find and list all files beginning with "my" (case insensitive).
find
-size +500k -size -1000k -exec
ls -lah '{}' \;
Find and list all files larger then 500k but less than 1,000k.
find
-iname my* -exec file '{}' \;
Find all files beginning with "my" and display their file types.
find
-iname my* -exec md5sum {} \;
Find all files beginning with "my" and display their md5sums.
You can even pipe output to other commands after the
-exec ...
find
. -type f -exec ls -sah {} \; |
sort -n -r | head -10
Find, sort and list the 10 biggest files in the current directory (and
its subdirectories).
Permission, owners and groups
You can also search based on permissions, owners or groups.
find
-type f -user geoff
Find all files owned by user "geoff".
find
-type f -group root
Find all files belonging to group "root".
find
. -perm -o=w
Find all files in the current directory that are writeable by others
find
. -perm /u=w,g=w
Find all files in the current directory that are writable by either
their owner or their group.
find
. -perm -g+w,u+w
Find all files in the current directory that are writable by both their
owner and their group.
find
. -perm -a+r -perm /a+w ! -perm
/a+x
Find all files in the current directory that are readable by everybody
(-perm -a+r), have at least one write bit set (-perm /a+w) but are not
executable by anybody (-perm /a+x).
Tips & Tricks
If you're searching the entire system as an ordinary user, you'll get
lots of "permission denied" messages from directories for which you
don't have read permission. To avoid these, redirect the error messages
to a null device and you won't see them:
find
/ -name myfile 2>/dev/null
Of course there's a great deal more to
find -- check out
man find
for full details -- but it's only one tool in the command line arsenal.
To my mind, if you're just seaching for files by name, you can't beat
the
updatedb/locate combination.
Got a useful
find command?
Leave a comment!

<--Previous
Hidden Linux
Next Hidden
Linux -->