In this article, I will take you through the steps to install locate command in Linux. locate command is used to search files in Linux based system just like find command in Linux. As you might aware find is another popular tool used in Linux system to search files. You can check 40 Best Examples of Find Command in Linux to check more about find command examples.
locate [OPTION]… PATTERN…
Install locate command in Linux
Step 1: Prerequisites
a)You need to have a running RedHat/CentOS 7/8 System.
b)You should have
yum command installed in your System.
c)You should have
sudo access to run all privileged commands.
to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo access to User.
Step 2: Update Your System
Before going through the steps to install locate command in Linux you need to first update your System with latest version using
yum update -y command as shown below. As you can see from below output, in this case system is already updated with the latest version so it is not detecting any more update.
[root@localhost ~]# yum update -y Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.piconets.webwerks.in * epel: mirrors.piconets.webwerks.in * extras: mirrors.piconets.webwerks.in * updates: mirrors.piconets.webwerks.in No packages marked for update
Step 3: Install mlocate package
Now that when we know system is already updated with latest version it is time to install locate command using
yum install -y mlocate command as shown below.
[root@localhost ~]# yum install -y mlocate Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.piconets.webwerks.in * epel: mirrors.piconets.webwerks.in * extras: mirrors.piconets.webwerks.in * updates: mirrors.piconets.webwerks.in Resolving Dependencies --> Running transaction check ---> Package mlocate.x86_64 0:0.26-8.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================================================================================================== Package Arch Version Repository Size ======================================================================================================================================================================== Installing: mlocate x86_64 0.26-8.el7 base 113 k Transaction Summary ======================================================================================================================================================================== Install 1 Package Total download size: 113 k Installed size: 379 k Downloading packages: mlocate-0.26-8.el7.x86_64.rpm | 113 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mlocate-0.26-8.el7.x86_64 1/1 Verifying : mlocate-0.26-8.el7.x86_64 1/1 Installed: mlocate.x86_64 0:0.26-8.el7 Complete!
Step 4: Update Your DB
After successful installation of locate command in Linux you need to update the DB by running
updatedb command as shown below. This command will update the mlocate database.
[root@localhost ~]# updatedb
"locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory".
Step 5: Test locate command
Now that mlocate DB is updated you can quickly test the functionality of locate command by running locate
file.txt command as shown below. This command will search for a
file.txt anywhere available in the System with the current logged in user access.
[root@localhost ~]# locate file.txt /home/centos/file.txt /root/file.txt
"Permission Denied"error while running locate command from non-privileged user id like you get in find command search. It will simply not look into those directories where it is not allowed.
You can check other options that can be used with locate command using
--help as shown below.
[root@localhost ~]# locate --help Usage: locate [OPTION]... [PATTERN]... Search for entries in a mlocate database. -A, --all only print entries that match all patterns -b, --basename match only the base name of path names -c, --count only print number of found entries -d, --database DBPATH use DBPATH instead of default database (which is /var/lib/mlocate/mlocate.db) install locate -e, --existing only print entries for currently existing files -L, --follow follow trailing symbolic links when checking file existence (default) install locate -h, --help print this help -i, --ignore-case ignore case distinctions when matching patterns -l, --limit, -n LIMIT limit output (or counting) to LIMIT entries -m, --mmap ignored, for backward compatibility -P, --nofollow, -H don't follow trailing symbolic links when checking file existence install locate