The locate Command in Linux

locate command

locate is considered low-impacting in terms of system resource consumption, because it does not search the file system in real time. Instead, locate searches through a database which is updated at idle hours (usually at night). This approach provides very fast searching, but results may not reflect recently added or deleted files.

The basic syntax of the locate command is:

# locate [options] search_string

The following example shows how to use locate to find a specific file by filename:

$ locate file1
/var/tmp/file1.txt

There are multiple implementations of the locate command. All offer similar functionality, but have implemented different features.

  • locate: This is the original locate that is currently maintained by the GNU foundation. The locate command is included in the mlocate package available from the GNU foundation.
  • slocate: Secure locate (slocate) is secure in that it stores files’ permissions in its database, and therefore will not locate files for users who do not have the right to see them.

Building the locate Database

On a newly installed system that hasn’t run through the night, you’ll need to login as root and run the following command to create the database:

# /etc/cron.daily/mlocate.cron

locate Packages

In order to use the locate command the proper RPM package must be installed. To find which package provides the locate command use the command below:

# yum whatprovides locate
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.lga7.us.voxel.net
 * epel: mirror.cogentco.com
 * extras: mirror.genesisadaptive.com
 * nux-dextop: li.nux.ro
 * updates: mirrors.gigenet.com
epel/x86_64/filelists_db                        | 9.9 MB  00:00:00     
mlocate-0.26-6.el7.x86_64 : An utility for finding files by name
Repo        : base
Matched from:
Filename    : /usr/bin/locate

As shown above mlocate package provides the locate command.

Related Post