If you interact often with Linux based operating systems, you’re probably familiar with some of the tools we can use to search for files on Linux systems and attached storage devices. Here is an introduction to a few useful ones:
which
which is used to find binary files in the system. It is commonly used in scripts to show the full path of a bin file being executed. For example, when we use the common network tool ping it actually launches /bin/ping.
whereis
whereis is similar in many ways to which but it is used to search for the full path/location of a binary tool and also associated manual (man) pages. It can be used as in the above example to show the full path location of ping and also list the location of its associated man page.
Find
find is used to search for files on the system or attached storage devices. It can be used for pretty much any type of file and has a comprehensive list of input arguments, which allow us to search by modification date, which user interacted with the file etc. It is usually the best tool to use in order to locate files on the system and is certainly the most sophisticated in terms of flexibility and features. However, the main draw-back of find is that it can be very slow: it runs in real time and searches the entire drive or directory (depending on syntax) for the file in questions, this can take time and thus it is not always the best tool to use. Check out the man page to see what criteria you can search by.
Locate
locate is a newer tool which is used in a similar way to find. It lacks many of the more sophisticated features of find but operates much more quickly: thus it can sometimes be the better choice. The reason for the improved speed is that it uses a previously created database which indexes the entire system (excluding network drives, removable media and encrypted directories by default). The increased search speed of locate makes it a great choice if you need to locate a file quickly and have a large amount of files and folders on the system. However, there is a drawback. Due to the fact that the database is usually only updated once per day (by default), it fails to index newly created files and therefore, can’t find them.
The user can manually update the database but the better solution is usually to use the find tool instead, when searching for new files. You’ll see form the example below that locate fails to discover the new file I create, “file123”, until I update the database manually. One final caveat with locate, due to the fact it uses a potentially outdated database, it can also list files that no longer exist.