Hidden Linux : Example commands - find
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 myfileCase-sensitive search for all files named "myfile" in the current directory or any of its subdirectories.
find -iname myfileCase-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 *fileFind 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 myfileOnly search the current directory for files named "myfile".
Property searches
Find doesn't just work on file names!find
~ -user fredFind all files in /home directory owned by user fred.
find
~ -size +750kFind all files in the /home directory larger then 7500k.
find
~ -size +500k -size -750kFind all files in the /home directory larger then 500k but less than 750k.
find
~ -size +100MFind all files in the /home directory greater than 100M.
Finding by time
-mtime is a measure of
24-hour periods, so ...find
-mtime 0Finds 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 +30Find all files that were modified more than 30 days ago.
-mmin is a measure of minutes, so ...
find
-mmin -10Finds all files that were modified less than 10 minutes ago.
find
-mmin +5 -mmin -10Find 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 -10Find, 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 geoffFind all files owned by user "geoff".
find
-type f -group rootFind all files belonging to group "root".
find
. -perm -o=wFind all files in the current directory that are writeable by others
find
. -perm /u=w,g=wFind all files in the current directory that are writable by either their owner or their group.
find
. -perm -g+w,u+wFind 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+xFind 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/nullOf 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!
![]() |
![]() |



PC World is New Zealand’s top selling computing and technology magazine.
Comments
I am so used to google, that I would like to find files like this
FIND summer 2009
to get all files with word "summer" and "2009" within their filename.
So I wrote such script:
======================
#!/bin/bash
if [ "$1" == "" ]
then
exit 0
fi
QUERY="find -size +0c"
for A in $@
do
QUERY="$QUERY -and -iname \*${A}\*"
done
eval $QUERY
Posted by: macias | February 28, 2010 10:26 PM