Find

With hard drives becoming real cheap over the past few years we’ve grown accustomed to having many gigabytes or sometimes even terabytes of data on our computers. This is when being able to find information on our computers becomes important. Linux has a very handy command line tool very aptly called “find”. I’ll share with you some basic features of the “find” command to get you started. I’m quite certain that you will find it pretty useful.

The syntax for using “find” is formed like this:

# find [where to search] [criteria]

A simple example of its usage is:

# find -name “*.txt”
./Documents/articles/doctor_dolittle.txt
./Documents/articles/Installing_Redhat_Linux.txt
./Desktop/todo_list.txt

The above command will search the current directory and all its subdirectories for files with the “txt” extension. One thing to note here is that all the parameters for find are completely optional. This means that even if you just enter the “find” command and hit the Enter key you will get a result – a list of all the in the current directory and the subdirectories. Try it out.

If you want to search multiple directories for text files you can list the directories to search in one after another:

# find /home/hobbes/ /home/calvin/ -name “*.txt”
/home/calvin/Documents/articles/doctor_dolittle.txt
/home/calvin/Desktop/todo_list.txt
/home/hobbes/Desktop/articles.txt
/home/hobbes/data_logging.txt

You might get a few errors when you run some searches. This would mostly be related to the lack of permissions. A nice trick to make the output a lot cleaner is to send the errors to “/dev/null”:

# find /home/hobbes/ /home/calvin/ -name “*.txt” 2>/dev/null

Another problem you might encounter is with the fact that by default these searches are case sensitive. So searching for “picasso” will not yield the result “Picasso”. You can circumvent that by using the “-iname” option:

# find /home/calvin/ -iname “picasso”
./Documents/images/picasso.png
./Documents/articles/The_artist_Picasso.doc

There are many cool “hacks” for the “find” command. Here are a few. To find files that were modified in the last 45 mins you can do a time-based search:

# find /home/calvin/ -mmin -45
./hello.txt
./artwork.jpg

Similarly you can search for files that have been modified between 15 and 25 minutes ago:

# find -mmin +15 -mmin -25

You can also control the depth that “find” goes into when searching subdirectories. The following command:

# find /home/calvin/ -maxdepth 2 -name “*.txt”

will only go to the top two levels of the specified directory and look for files with the “txt” file extension. Optionally you can also tell “find” to search only two levels lower:

# find /home/calvin/ -mindepth 2 -name “*.txt”
./artexpo.txt
./Documents/project.txt

As with most commands you can append the output to a file. So if you want to log a search you can do the following:

# find /home/calvin/ -name “*.txt” > /tmp/search.log

The “find” command can be extremely useful once you get the hang of it. A book can probably be written on. What I’ve shared with you here is just a taste of the possibilities. Take a look at the command’s manpage for pointers to other features of “find”.

g33kadmin

I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.