The Linux Locate Command: Quick File Search

What's crazy?

The commandlocateis a file search tool in Linux systems that works through a preconstructed database. Instead of going through the directories tree in real time,locatecheck a previously created index, which allows you to get almost instant results even on systems with millions of files. This speed makes it an ideal option for administrators and users who need to quickly locate documents, configurations or binary without waiting for you tofindfinish your tour.

Location installation

In most modern distributions, the package it provideslocateIt's calledmlocateorplocate. To install it in Debian / Ubuntu runs:

sudo apt update && sudo apt install mlocate

In Fedora, CentOS or RHEL uses:

sudo dnf install mlocate

In Arch Linux the package ismlocateand is installed with:

sudo pacman -S mlocate

After installation, the service in charge of updating the database is automatically run by a cron or a systemd timer.

Updating the database

Although the database is regularly refreshed, you can force a manual update with the commandupdatedb. This process can consume a few seconds or minutes depending on the size of the file system and the speed of the disk.

sudo updatedb

It is recommended to runupdatedbafter installing many packages or after copying large volumes of data so thatlocatereflect the most recent changes.

Basic syntax

The simplest use oflocateis:

locate patrón

Wherepatróncan be a literal chain or a simple expression with comodines*and?. For example, to find all the files whose name ends in.conf:

locate *.conf

The command returns a list of complete routes, one per line, that match the pattern.

Practical examples

  • Find a specific program: locate firefoxdisplays all browser-related routes.
  • Find Apache configuration files: locate apache2.conf
  • List all shell scripts in/usr/local/bin: locate /usr/local/bin/*.sh
  • Search by ignoring capital and lower capital: locate -i README

Useful options

  • -l Nor--limit=N: limits the output to N results, useful when the pattern is very generic.
  • -Sor--statistics: shows database statistics, such as number of indexed files and size.
  • -ror--regex: allows to use regular POSIX expressions instead of simple comandines.
  • -eor--existing: shows only those results that actually exist in the system (discards deleted inputs but still present in the database).
  • -cor--count: only shows the number of unlisted coincidences.

Limitations and considerations

Althoughlocateis extremely fast, has some restrictions you must consider:

  • The database may be outdated between executions ofupdatedb, so newly created or deleted files may not appear or appear falsely.
  • It does not search within the content of the files; it only examines names and routes.
  • In systems with strict security policies (e.g., SELinux or AppArmor) access to certain routes may be restricted, which causes some files not to be indexed.
  • The database can occupy several hundred megabytes in very large systems, although it is generally manageable.

For searches that require content inspection or that need to be always up to date, combinelocatewithfindor tools likegrep -r.

Alternatives to location

If you need an alternative that does not depend on a periodic database, consider:

  • find: seeks in real time and supports complex expressions, although it is slower.
  • fd: a modern and friendly version offindwith simplified colors and syntax.
  • ripgrep (rg): search for content within high-speed files, useful when the name is not enough.
  • Atlasormlocatewith custom timers to adjust the update frequency to your needs.

Conclusion

The commandlocateis an essential piece of any Linux user's arsenal that values speed and simplicity in file search. By understanding how to install, update and use your main options, you can drastically reduce the time spent to locate resources in the system. It recalls to keep the database up to date and to know its limitations in order to effectively combine it with other tools when the situation so requires.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish