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.
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”.
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.
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:
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.
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.
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
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.
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.