What is locate?
The locate command is a file‑search tool on Linux systems that works using a pre‑built database. Instead of walking the directory tree in real time, locate queries an index created beforehand, allowing near‑instant results even on systems with millions of files. This speed makes it ideal for administrators and users who need to quickly locate documents, configurations, or binaries without waiting for find to finish its traversal.
Installing locate
On most modern distributions, the package that provides locate is called mlocate or plocate. To install it on Debian/Ubuntu run:
sudo apt update && sudo apt install mlocate
On Fedora, CentOS, or RHEL use:
sudo dnf install mlocate
On Arch Linux the package is mlocate and is installed with:
sudo pacman -S mlocate
After installation, the service that updates the database runs automatically via a cron job or a systemd timer.
Updating the database
Although the database is refreshed periodically, you can force a manual update with the updatedb command. This process may take a few seconds or minutes depending on the size of the filesystem and disk speed.
sudo updatedb
It is advisable to run updatedb after installing many packages or after copying large volumes of data so that locate reflects the most recent changes.
Basic syntax
The simplest use of locate is:
locate patternWhere
patterncan be a literal string or a simple expression with wildcards*and?. For example, to find all files whose name ends in.conf:
locate *.conf
The command returns a list of full paths, one per line, that match the pattern.
Practical examples
- Search for a specific program:
locate firefoxshows all paths related to the browser. - Find Apache configuration files:
locate apache2.conf - List all shell scripts in
/usr/local/bin:locate /usr/local/bin/*.sh - Search ignoring case:
locate -i README
Useful options
-l Nor--limit=N: limits output to N results, useful when the pattern is very generic.-Sor--statistics: shows statistics about the database, such as number of indexed files and size.-ror--regex: allows using POSIX regular expressions instead of simple wildcards.-eor--existing: shows only results that actually exist on the system (discards deleted entries still present in the database).-cor--count: only shows the number of matches without listing them.
Limitations and considerations
Although locate is extremely fast, it has some restrictions to keep in mind:
- The database may be out‑of‑date between runs of
updatedb, so newly created or deleted files may not appear or may appear falsely. - It does not search inside file contents; it only examines names and paths.
- On systems with strict security policies (e.g., SELinux or AppArmor) access to certain paths may be restricted, causing some files not to be indexed.
- The database can occupy several hundred megabytes on very large systems, though it is usually manageable.
For searches that require inspecting content or need to be always up‑to‑date, combine locate with find or tools like grep -r.
Alternatives to locate
If you need an alternative that does not rely on a periodic database, consider:
find: searches in real time and supports complex expressions, although it is slower.fd: a modern, user‑friendly version offindwith colors and simplified syntax.ripgrep(rg): searches file contents at high speed, useful when the name alone is insufficient.Atlasormlocatewith custom timers to adjust the update frequency to your needs.
Conclusion
The locate command is an essential part of any Linux user’s toolkit who values speed and simplicity in file searching. By understanding how to install, update, and use its main options, you can drastically reduce the time spent locating resources on the system. Remember to keep the database up to date and know its limitations so you can effectively combine it with other tools when the situation calls for it.
This post is also available in ESPAÑOL.