Get Started Using the ‘find’ Command in Linux

This brief but detailed guide will help get you started using the “find” command in Linux and provide helpful real-world examples.

As hard drives have become even less expensive 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” that’ll help you do just that. 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”

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”

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”

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

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”

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 find manpage for pointers to other features of “find”. Or go even farther with “find and replace“.

If this article helped you, I'd be grateful if you could share it on your preferred social network - it helps me a lot. If you're feeling particularly generous, you could buy me a coffee and I'd be super grateful :)

buy a coffee for

Home » Linux » Get Started Using the ‘find’ Command in Linux

Leave a Comment

Your email address will not be published.