Introduction
In the day-to-day of a system administrator or developer, locating a specific file among thousands of directories can become a tedious task if one relies on commands like find without proper optimization. Fortunately, Linux provides a tool designed precisely to offer almost instantaneous responses: locate. This command bases its speed on a pre-built database that indexes the system’s paths, allowing searches in fractions of a second.
What is locate?
Locate is a utility that is part of the mlocate package (or slocate in some distributions). Instead of traversing the directory tree in real time, it queries a database previously created by the updatedb daemon. That database contains a list of all file and directory paths accessible to the user who generated it, making the search extremely fast, albeit at the cost that the information may be outdated until the next update.
How it works
The workflow of locate is divided into two main phases:
- Database generation: the
updatedbcommand (usually run via cron) walks the filesystem and stores each found path in a binary file, normally located at/var/lib/mlocate/mlocate.db. - Query: when the user invokes
locate pattern, the program searches for matches within that database using simple expressions or regular expressions, returning the paths that contain the specified pattern.
Because the database is static between runs of updatedb, locate is ideal for searching files that do not change frequently, such as binaries, libraries, documentation, or configuration files.
Installation and configuration
In most modern distributions, locate comes pre-installed. If it is not present, it can be easily installed from the package manager:
- Debian/Ubuntu:
sudo apt-get install mlocate - Red Hat/CentOS:
sudo yum install mlocateorsudo dnf install mlocate - Arch Linux:
sudo pacman -S mlocate
After installation, the updatedb service is configured to run automatically each day via a cron task located at /etc/cron.daily/mlocate. However, users can manually trigger the update with sudo updatedb when they need to immediately reflect recent changes.
Basic usage
The simplest syntax for locate is:
locate [options] pattern
For example, to find all files that contain the word nginx in their name:
locate nginx
This will return a list of paths such as /etc/nginx/nginx.conf, /usr/sbin/nginx, and any other file whose name includes nginx. If one wishes to limit the search to exact paths, the -r modifier can be added to use regular expressions:
locate -r '^/var/log/.*\.log$'
The above command will show only the .log files inside /var/log and its subdirectories.
Useful options
Locate includes several flags that refine the search:
-i: ignores case.-lor--limit: limits the number of results (useful to avoid excessive output).-S: shows statistics about the database (number of files, size, etc.).-e: removes from the output those files that do not exist on the filesystem (useful after massive deletions).-p: shows only the paths that belong to a specific mount point, specifying-p /mnt/datosfor example.
Updating the database
As mentioned, the database is updated automatically, but there are scenarios where
This post is also available in ESPAÑOL.