How to search files from the terminal on Linux

While there are many ways with which we can search and locate files and directories on Linux, the easiest and quickest is probably through the terminal. However, not many Linux users know about that, which leads to unneeded frustration. Here is a quick guide that will hopefully help you locate what you're looking for in your system.

Use the Locate command

The “locate” command is the first resort that a Linux user should utilize because it's much faster than anything else out there. The reason for this unmatched speed is that the locate command isn't actually searching your local hard disks for the files or directories that you need to find, but more like reads through the mlocate.db database file which contains all file paths in your system. To use locate, open a terminal and type locate followed by the file name you are looking for.

Search for files with locate command

Locate can also tell you how many times a search keyword is matched in the database. This is achieved by including a “-c” parameter in the command, which stands for “count”.

use locate to find how many times the keyword has matched

What users need to have in mind here is that “locate” needs the specific and exact name of the file you're looking for, contrary to other more flexible tools. That means that locate is great for finding something that you're sure about its name, but not so great for when you don't exactly remember the file name.

Locate needs the exact file name

Moreover, and since “locate” reads a database file, the results may be outdated and not completely true. To mitigate this issue, you can update your file paths database by typing “sudo updatedb” on a terminal session.

If after that you're still not getting what you were looking for, or you're simply overwhelmed by the number of the results, proceed to the next step which is the “find” command.

Use the Find command

The “find” command is a much more powerful but also slower searching utility. This is because contrary to the “locate” command, “find” actually searching your disks for the files and directories that the user is after. Find is perfect for when you're trying to locate a file or a directory but you can't remember its name, because “find” can search for files that belong to a certain user or group of users, files that were modified or accessed recently, files that of a specific size range, hidden files, executables, read-only files, and files with certain permissions. The best part is that a user is free to combine multiple of the above criteria in one “find” command, essentially narrowing down the results.

Speaking of narrowing down, the first thing that you want to do when running “find” is to tell it to search on a specific directory. This will speed up the search process significantly, but always depending on the size of the directory. If you know where the file might be, open the terminal, navigate to the directory and run “find . [filename]”. That dot tells find to search on the current directory. If you want to search your Home directory instead, replace the dot with “~/”, and if you want to search your whole filesystem, use “/” instead.

As an example, I want to search for a file that contains a poster in my download directory. I know that the filename contains the word “poster” in it, but I don't exactly remember the name. For this reason, I would navigate to my download folder through the terminal with “cd Downloads”, and then enter the command:

sudo find . -name "*poster*"

Here is the result:

Use the find command on Linux

This tells me that there is a pdf file named as “billy_poster copy” in the Downloads folder. If I didn't know where it was, “find” would still let me know that it's in the Downloads folder if I searched on a parent directory.

Search for files with find by file name

Now, if I replace the “-name” parameter with “-iname”, I could get results with no regards to letter case. That is something that is not there in “locate”, so another helpful element for when you're not sure about the filename.

Use -iname option in find command

Now, let's suppose that I am still searching for that same poster file, and the only thing that I remember is that it is less than 5 MB of size. The command that I would use in that case is:

find ~/ -size -5M

If I knew that it's higher than lets say 2MB, the command would be “find ~/ -size +2M”. The best part is that find supports boolean operators to make the search even stronger. For this example, I will use a command that combines my knowledge that the file is less than 5 megabytes in size, and also more than 2. The command for this would be:

find / -size -5M -and -size +2M

Find files by size

Last, let's suggest that we remember nothing about the file but we remember that we accessed it within three minutes ago. To find it, use the find command as:

find / -amin -3

This would be “find / -amin -30” for half an hour, and “-amin -120” for two hours etc. If you accessed the file two days ago, use this instead:

find / -time -2

This will display all files accessed during the past two days in the location of the search.

Find files by timestamp

I think the above covers most of the usual use case scenarios. For more info about the powerful “find” command, open a terminal and type “man find”. Good luck finding what you're looking for.

Share this page:

6 Comment(s)

Add comment


From: Anonymous at: 2016-09-09 14:27:02

You're documenting text commands with text output...

And you thought it would be a good idea to post screenshots. WTF?


So, anyone who's visually impaired can't read any of your article's sample output.


Well done.

From: Bill Toulas at: 2016-09-12 16:09:09

Hey "Anonymous"I believe that screenshots of terminals help people see if an output is normal or expected. Maybe in this case I exagerated, but if anyhting, you can always skip them. :)

From: Apona K. at: 2016-09-16 07:01:47

No, I think he has a good point. Visually impaired people (aka blind) cannot have their text-to-speech programs read screenshots, only text. 

So having printed output as text instead of picture is actually much more sensible in general.

From: markh at: 2016-09-09 20:57:11

personally I use $find ~/ xxx | grep yyy

its super fast and I can drill down by piping to more grep commands

From: Scotsgeek at: 2016-09-11 01:06:44

If you are going to post images of the terminal, PLEASE crop the image to the terminal only!  We don't need to see the whole desktop!  Even expanging the image makes it hard to read the text displayed in the terminal.

From: Bill Toulas at: 2016-09-12 16:07:11

You are right, I will crop from now on. Thanks for your comment :)