How to Install and use locate command on linux

Install and use locate command on Linux

The locate command is used to find files by their filename. The locate command is lightning fast because there is a background process that runs on your system that continuously finds new files and stores them in a database. This video covers the usage of locating command on Linux

Create a file by using the following command

root@linuxhelp:~# touch file.txt
root@linuxhelp:~# touch File.txt

Use the locate command to find the file.txt file, until update the DB the locate cant able to find the file

root@linuxhelp:~# locate file.txt
/usr/share/doc/alsa-base/driver/Procfile.txt.gz

Now update the DB by using the following command

root@linuxhelp:~# updatedb

Now find file.txt using locate command

root@linuxhelp:~# locate file.txt
/root/file.txt
/usr/share/doc/alsa-base/driver/Procfile.txt.gz

Use the -i option to find Case sensitivity

root@linuxhelp:~# locate -i file.txt
/root/File.txt
/root/file.txt
/usr/share/doc/alsa-base/driver/Procfile.txt.gz

Use the following command to list the particular directory using limit

root@linuxhelp:~# locate -l 5 passwd
/etc/passwd
/etc/passwd-
/etc/cron.daily/passwd
/etc/pam.d/chpasswd
/etc/pam.d/passwd

List out the file

root@linuxhelp:~# ls -la
total 55144
drwx------  4 root root     4096 Jan 12 00:22 .
drwxr-xr-x 24 root root     4096 Dec 18 00:28 ..
-rw-------  1 root root     1115 Jan 12 00:21 .bash_history
-rw-r--r--  1 root root     3106 Apr  9  2018 .bashrc
drwx------  2 root root     4096 Aug  6 00:34 .cache
-rw-r--r--  1 root root        0 Jan 12 00:22 file.txt
-rw-r--r--  1 root root        0 Jan 12 00:22 File.txt
-rw-r--r--  1 root root      148 Aug 17  2015 .profile
-r--r--r--  1 root root 56435756 Dec 18 00:59 VMwareTools-10.3.10-12406962.tar.gz
drwxr-xr-x  9 root root     4096 Feb 20  2019 vmware-tools-distrib

Remove the file.txt

root@linuxhelp:~# rm -rf file.txt

Now find the file.txt using locate command until we update the db it will show the file.txt while we use the locate command

root@linuxhelp:~# locate file.txt
/root/file.txt
/usr/share/doc/alsa-base/driver/Procfile.txt.gz

Use the following command to find the file present in the system

root@linuxhelp:~# locate -e file.txt
/usr/share/doc/alsa-base/driver/Procfile.txt.gz

This is the method to install and use locate command on Linux comes to end

FAQ
Q
Whether the "locate" command is the default package in Linux or not?
A
No. The "locate" command is not a default package in Linux. If you want to use the "locate" functionality, you should install it manually as per your Linux distro.
Q
How to print a number of found entries while executing the "locate" command?
A
You can use the option of "-c" with the "locate" command to print the number of found entries while executing the "locate" command. For eg: "locate -c file"
Q
How to display the information about the locate DB using the "locate" command?
A
You can use the option of "-S" with the "locate" command to display the information about the locate DB. For Syntax: "locate -S"
Q
How to set a limit for search queries to a Specific Number of any file?
A
You can use the option of "-n" with the "locate" command to set a limit for search queries to a Specific Number of any file. For Ex: "locate "*.html" -n 20"
Q
Can I execute the "locate" command to find any file (or) directory anywhere in the Linux system?
A
You can execute the "locate" command to find any file (or) directory anywhere in the Linux system.