The Linux Locate Command: Quick File Search

Introduction

In the day-to-day of a system manager or developer, locating a specific file between thousands of directories can become a tedious task if you use commands likefindwithout proper optimization. Fortunately, Linux has a tool designed precisely to provide almost instant responses:locate. This command bases its speed on a pre-constructed database that indexes the system routes, allowing searches in second fractions.

What's crazy?

Locate is a utility that is part of the packagemlocate(orslocatein some distributions). Instead of going through the directories tree in real time, consult a database previously created by the demonupdatedb. This database contains a list of all file routes and directories accessible to the user who generated it, making the search extremely fast, although at the cost of the information being outdated until the next update.

How it works

The locate workflow is divided into two main phases:

  • Database generation: the commandupdatedb(usually run by cron) travel the file system and save every route found in a binary file, usually located in/var/lib/mlocate/mlocate.db.
  • Consultation: when the user invokeslocate patrón, the program seeks coincidences within that database using simple expressions or regular expressions, returning the routes containing the specified pattern.

Because the database is static between executions ofupdatedb, locate is ideal for file searches that do not change very often, such as binaries, libraries, documentation or configuration files.

Installation and configuration

In most modern distributions, locate is already pre-installed. If not present, it can be easily installed from the package manager:

  • Debian / Ubuntu:sudo apt-get install mlocate
  • Hat / CentOS Network:sudo yum install mlocateorsudo dnf install mlocate
  • Arch Linux:sudo pacman -S mlocate

After installation, the serviceupdatedbis set to run automatically every day by a cron task located in/etc/cron.daily/mlocate. However, users can launch the update manually withsudo updatedbwhen they need to reflect recent changes immediately.

Basic use

The simplest location syntax is:

locate [options] pattern

For example, to find all files containing the wordnginxon his behalf:

locate nginx

This will return a list of routes like/etc/nginx/nginx.conf, /usr/sbin/nginxand any other file whose name includesnginx. If you want to limit the search to exact routes, you can add the modifier-rto use regular expressions:

locate -r '^ / var / log /.*. log$'

The previous command will display only the files.logwithin/var/logand their subdirectories.

Useful options

Locate includes several hors that tune the search:

  • -i: ignores capital and lower capital.
  • -lor--limit: limits the number of results (useful to avoid excessive output).
  • -S: shows statistics on the database (number of files, size, etc.).
  • -e: removes from output those files that do not exist in the file system (useful after mass erasing).
  • -p: shows only routes belonging to a particular assembly point, specifying-p /mnt/datosFor example.

Update the database

As mentioned, the database is automatically updated, but there are scenarios where an immediate update is required:

  • After installing a lot of software.
  • After moving or removing large volumes of data.
  • On servers where accuracy is critical and cannot be expected to be daily.

Runsudo updatedbcan take several minutes on systems with millions of files, as you must go through the entire directory tree. During this process, the consumption of I / O and CPU is increasing, so it is recommended to schedule it in low-load hours.

Limitations and considerations

Although located extremely fast, it has some limitations that should be taken into account:

  • No files created after the latest database update.
  • Depends on the permissions of the user who ranupdatedb; if it is run as root, it will include all accessible files, but if it is run as a standard user, it will omit those to whom it does not have a reading permit.
  • It does not support search for file attributes (size, date, permissions) as it doesfind.
  • In systems with mounted network file systems (NFS, CIFS), inclusion or exclusion depends on the options ofupdatedbconfigured in/etc/updatedb.conf.

For searches that require more complex criteria or that need to reflect the exact state of the system in real time,findremains the most appropriate tool, the slowest albeit.

Conclusion

The commandlocateis an elegant and fast solution to locate files on Linux systems when a slight gap is tolerated at present. Your indexed database allows you to obtain results in fractions of second, making it an essential ally for routine maintenance, administration and development tasks. Combined with appropriate use ofupdatedband your filtering options, locate can significantly reduce the time dedicated to file search, leaving more space for productive work.

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

EnglishenEnglishEnglish