« The daftness of data caps | Main | Readable web pages »

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





Follow Geoff Palmer on Twitter

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

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Subscribe
Newsletter & SubscriptionsPC World is New Zealand’s top selling computing and technology magazine.

It provides up-to-the-minute editorial, insight and buying advice for personal computing, cell phones, game consoles, digital entertainment and broadband.
SIGN UP
PCWorldUpdate
PC World's weekly round-up of tech news, gear and game reviews, software selections, and handy How Tos.