Finding Linux Server Files with “locate”

For the next couple of days, we will focus on searching. There are a few ways to search for files on a Linux server. Some are more in-depth than others. The tool called locate relies on a database backend for fast searching. The database must index the filesystem, and then the user can search for file names that contain the specified search terms.

In order to setup locate, the server administrator (root) needs to run another command called updatedb. This will index all files in the locate database. This command can put stress on the CPU, especially if there are a lot of files to index, so it is best to run updatedb during slow access periods (i.e. after midnight).

Many Linux distributions will have updatedb setup to run automatically using cron. Often, you will find a link to a script executing it in cron.daily. If you do not want to use the CPU cycles or want to configure your own schedule, you can safely delete the script.

Once the database has been updated, you can search by entering the command followed by the search term:

locate support

The search will return results like “support.html”, “tech-support.htm”, and “supported-help.php”. For more advanced searches, locate can use regular expressions and other advanced features. To learn more, type “man locate” from the command line.

An alternative to locate is slocate, a secure version of the same tool that does not allow the user to search above his/her own permission level. In other words, a normal web hosting account user cannot search and find files owned by root. Both locate and slocate are available in most Linux distribution package repositories.