For a certain reason, some Linux users prefer to use command line instead of GUI to perform some tasks, including finding files. Basically, the current Linux is completely different than it was few years ago. Most of current Linux distributions comes with full of GUI desktop environment to ease new comers to operate them, just like Windows and Mac.
But again, those who have been using Linux from the start keep using command line because they feel more comfortable from it. For example, they prefer to use find command to search for a file rather than typing a certain term in the search box of file explorer. If you want to looks geeky, you can also start familiarize yourself to command line too.
There are three common commands in Linux that you can use to find your files.
This is the most common command to search for a file Linux. You can use this command to search for general files on your machine like documents, photos, audios and lots more. This command works by looking for a string in the directories you’ve set according to parameters that you’ve included.
Find has a number of parameters that you can use for narrowing the search results. If you want to be a master in find you can read the manual by typing man find in terminal. For now, let’s take a look at the example below to make everything clearer. In the example below I will be looking for all PDF files in the Documents folder. So, I should type the following command.
find /home/cap/Documents/ -name "*.pdf"
- name means you are looking for files with certain name.
If I want to narrow the search results to show only PDF files that were last modified less than 60 days ago I just need to add the “mtime” option.
find /home/cap/Documents/ -name "*.pdf" -mtime -60 -print
- -mtime -60 means you are looking for a file modified less than 60 days.
Like you can see from the example above that find supports wildcards. Once again, you can read manual page of find by typing man find in terminal.
Unlike find, locate depends on updatedb to work. Updatedb itself is an utility which creates a database of your files and periodically updates it via cron scheduling (you can find out about updatedb using whatis command). Locate can search for files by name. It also supports wildcards for a bulk search. Locate is a great tool to search for a certain configuration file within your system.
Locate also has some parameters to enable you search for a specific file. Anytime you need a help you can read its manual by typing man locate in terminal. A good part about locate is that it can help you find the files you are looking for even if you don’t know the full name of the file. Just type a part of the filename you are looking for, and locate will display all files with the word in their name.
locate -ei grub
- e means you are looking for all existing files on your system.
- i means you’re performing a case-insensitive
The example above is meant to look for all files that contain the term of “grub” in their filenames by ignoring the uppercase and lowercase.
The third common tools that you can use to find a file in Linux via command line is whereis. Unlike the two above, whereis has a very specific purpose. It only locates the binary, source and manual files for the specified command names so you may don’t want to use this tool too often.
For instance, you may wondering where the location of binary files of your favorite applications actually is. Whereis has no much options. You can run whereis without any options to get a list of all files.
The command above will show the following result.