The locate is a command line utility for finding files by name in Linux, just like find command. However, it works more efficiently compared to its counterpart; it uses one or more databases populated by the updatedb program and prints file names matching at least one of the patterns (a user provides) to standard output.
Locate package is provided by the GNU findutils or mlocate packages. These packages are known to provide the same implementation of the program. On most CentOS/RHEL systems, findutils comes pre-installed, however, if you try to run a locate command, you may encounter the error:
-bash: locate: command not found
In this article, we will show you how to install mlocate package which provides the locate and updatedb commands to find files in Linux systems.
Below is a sample output showing the above error and querying findutils package.
$ locate bash_completion.sh $ rpm -qa | grep findutils
$ sudo yum install mlocate [On CentOS/RHEL] $ sudo apt install mlocate [On Debian/Ubuntu]
After installing mlocate, you need to update the updatedb, which is used by locate command as root user with the sudo command, otherwise you will get an error. The default database storage location is /var/lib/mlocate/mlocate.db.
$ sudo updatedb
Once the database is updated, now try to run the locate command, which should work this time around.
$ locate bash_completion.sh
To find an exact match according to pattern you enter, use this
-b option and the
\ globbing option as in the following syntax.
$ locate -b '\bash_completion.sh'
Note: You can use the LOCATE_PATH environmental variable to set a path to extra databases, which are read after the default database or any databases listed using the –database flag on the command line.
That’s all! In this guide, we showed you how to install mlocate package which offers the locate and updatedb commands on a Linux system. Share your views with us through the feedback form below.