One of the most common Linux operations is locating a particular file by its name. In Linux, the 'locate' command serves the purpose as it is used to search a file or a directory by name.
It is similar to the 'find' command, which locates the file in the filesystem. But here, it searches the file in the database, both being the most popular ones. The locate command runs in the background and is much faster when compared to the find command.
In this article, we'll demonstrate how to use the locate command.
Installation
Before we begin, let's check if the 'locate' package already exists in your system. It may or may not be pre-installed in your Linux system as it depends on how the system was provisioned and the Linux distribution.
To check, we have to open the terminal, type 'locate' and press Enter. If the system already has the locate package installed, it will return the following - locate: no pattern to search for specified. Otherwise, it will display - locate command not found.
Now, in order to install locate, we can simply use the package manager of the distro.
For Ubuntu & Debian
$ sudo apt install mlocate
For CentOs & Fedora
$ sudo yum install mlocate
How the locate command works?
The locate command searches for a specific pattern through a database file usually generated by the updatedb command. During the installation of the mlocate package, a cron job is created, which executes the updatedb command every 24 hours (customizable) to ensure time-to-time updation of data in the database.
For further information about the cron job, we can inspect the /etc/cron.daily/mlocate file.
The database can be manually updated using the updatedb command as root or any user with sufficient privileges.
Depending on the number of files and directories and the system performance, the update might take a while. Any files created after the update will not reflect in the database.
Syntax
locate [OPTION] PATTERN…
The locate command, if used without any options, is expected to return or display the absolute path of all files and directories that matches the searched pattern and also for which the user has read access.
Below are a few common examples of the locate command.
1. Locate command without Options
To search for a file called '.bash_history', we would type:
locate .bash_history
The output will contain results of all files that have the string '.bash_history' in it. Since we have only one specific file, we get the following output:
/home/test/.bash_history
2. Limit search queries to a specific number
This is done to reduce redundancy. To limit the search queries to a specific number, we can use the '-n' command.
$ locate "*.html" -n 5
Sample output
/home/test/.vscode-server/bin/695af097c7bd098fbf017ce3ac85e09bbc5dda06/extensions/github authentication/media/index.html /home/test/.vscode-server/bin/695af097c7bd098fbf017ce3ac85e09bbc5dda06/extensions/micros oft-authentication/media/index.html /home/test/env/lib/python3.10/site-packages/django/contrib/admin/templates/admin/404.html /home/test/env/lib/python3.10/site-packages/django/contrib/admin/templates/admin/500.html /home/test/env/lib/python3.10/site-packages/django/contrib/admin/templates/admin/actions.html
The results will show the first 5 files that end with .html.
3. List the number of matching entries
To count the number of occurrences of a file name or search pattern using the locate command, we can invoke the '-c' option as shown.
$ locate -c [txt]*
Sample output
$ locate -c “*.txt*” 1241
In the above example, the output shows there are 1241 .txt files in the database.
4. Refresh mlocate Database
The locate command is completely dependent on the database called mlocate. Hence, it is vital to frequently update the database for proper functioning of the utility.
In order to update the mlocate database, we use the 'updatedb' command. It has to be executed by the root user or any user with privileges of the root user.
$ sudo updatedb
5. List only files present in the system
One of the major drawbacks of using the locate command is that it still returns the results of files whose physical copies are erased or deleted from the system, even when we have an updated mlocate database.
In order to avoid this problem, we can use the '-e' option with the locate command. The process searches the system to confirm the existence of the file that has been requested if it is still present in the mlocate database.
$ locate -i -e *test.txt*
Sample output
/home/test/test.txt
6. Display locate outputs ignoring case sensitive elements
The locate command is configured in such a manner that it will return two different outputs for "test.txt" and "TEST.txt", respectively.
To ignore the case sensitivity and show outputs for both "test.txt" and "TEST.txt", we can use the '-i' option with the locate command.
$ locate -i “test.txt”
Sample output
/home/test/TEST.txt /home/test/test.txt
7. Check the status of the locate database
If we ever want to check the current status of the mlocate.db (mlocate database), we can view the analytics by using the '-S' command.
$ locate -S
Sample output
Database /var/lib/mlocate/mlocate.db: 31,234 directories 3,23,345 files 1,32,456 files 2,34,23,656 bytes in file names 1,08,23,213 bytes used to store database
8. Locate outputs in a single line
By default, locate separator is the newline (\\n) character. We also have options like using the ASCII NUL, which can be achieved by using the '-0' option with the locate command.
$ locate -i -0 *test.txt”
Sample output
/home/test/TEST.txt/home/test/test.txt
9. Ignore error messages in locate
Sometimes, when we try to access the mlocate.db, it returns unnecessary error messages creating more confusion and stating that we do not have the required permissions for direct access to the database as we're not the root user.
To suppress these error messages, we can use the '-q' option with the locate command.
$ locate “\*.dat” -q*
10. Choose a different mlocate location
Imagine a scenario where we're inputting queries looking for results not present in the mlocate database. In this case, we'd like to choose the right database present somewhere in the same system. This can be achieved using the '-d' option with the locate command.
$ locate -d <new_db_path> <filename>
Locate command is probably one of those utility commands that covers almost everything we ask it to perform. To keep the process efficient, the mlocate database, i.e., mlocate.db needs to be updated frequently.
11. Format the output - less option
When there's a long list of files in the output, we can use the locate command with 'less' option for better readability.
$ locate mysql | less
Sample output
/etc/mysql /etc/php/8.2/mods-available/mysqli.ini /etc/php/8.2/mods-available/mysqlnd.ini /etc/php/8.2/mods-available/pdo_mysql.ini /etc/rc0.d/K01mysql /etc/rc1.d/K01mysql /etc/rc2.d/S01mysql /etc/rc3.d/S01mysql /etc/rc4.d/S01mysql /etc/rc5.d/S01mysql /etc/rc6.d/K01mysql /etc/systemd/system/multi-user.target.wants/mysql.service /home/hsf/.mysql_history /snap/core20/1822/etc/apparmor.d/abstractions/mysql /snap/core20/1822/usr/share/bash-completion/completions/mysql /snap/core20/1822/usr/share/bash-completion/completions/mysqladmin :
12. Format the output - more option
If the output is long, we can consider piping the output to more commands for easier scrolling.
$ locate mysql | more
And we get the entire list of mysql files.
Conclusion
Locate command has its own database. It is easy and quick enough to find a particular file location on the system. In this article, we've learnt the concepts of locate command in Linux with appropriate examples, each having a different use case.