The “locate” is a command-line utility for searching files by name in Linux. It is like the “find” command. However, it works more efficiently than “find” command. It uses databases populated by the “updatedb” program to search in linux and print file names matching. Unless the “find” command, the locate command does not search the entire Linux file system, but it looks through the regularly updated file database. As a result, the search results come faster. Using “locate” command to search for a file is very easy and convenient.
The syntax for the locate command is:
locate [options] [pattern]
“Locate” command is provided by the “mlocate” package. On most CentOS/RHEL-based systems, “find” command comes pre-installed; so, if you try to run “locate” command, you may encounter the error: “command not found“
We need to install “mlocate” package, which will provide the “locate” and “updatedb” commands to find files in Linux systems. To install “mlocate”, we can use the YUM package or apt command, depending on our operating system.
Install Locate on CentOS/RHEL-based Linux systems:
yum install mlocate
For Debian or Ubuntu-based Linux Systems:
apt install mlocate
After installing “mlocate” package, we need to update the database with “updatedb” command. The default database storage location is /var/lib/mlocate/mlocate.db.
updatedb
Once the database is updated, we can run the locate command to search in Linux.
locate <filename>
If we want to get the count number of the matching files, we can use “locate” command with option “c”. For example, this command will count the files which are ending with “.html”.
locate -c .html
That’s everything! We just learned how to install “locate” by “mlocate” package on AlmaLinux/CentOS/RHEL-based Linux system. You can now practice the “locate” command to search on your Linux system. Don’t forget to maintain your Digital-Wellbeing.