We create several files in the Linux system, and it is not possible for us to remember the locations of all the files. In such a case, you can use the “locate” or “find” command to help you find the file.
The locate command is used to locate files and directories on your system. It can search for files and directories based on their name, content, or location.
I’m following this guide on a minimal version of Debian 11, and I believe this step will work on all major Linux distributions:
Table of Contents
The prerequisite is simple enough; you just need to have the mlocate package installed on your system, but how do I know if it is available or not?
Simple, just run the locate command in your terminal window, and if it prints the following output, then you can skip the installation step and follow the usage of the locate command in Linux.
locate: pattern argument expected Try 'locate --help' for more information.
And for those who get “bash: locate: command not found” need to follow the below steps:
Install Locate on Linux
The locate command can be installed through the official repository in all Linux distributions. You just need to run the following command as per your respective distributions to install the mlocate package to access the locate command:
Install locate on Ubuntu/Debian
sudo apt update sudo apt install mlocate // Ubuntu/Debian based distributions
Install locate on RockyLinux/RHEL
sudo dnf update sudo dnf install mlocate // AlamaLinux/RockyLinux/RHEL based distributions
Install locate on ManjaroLinux/Endeavour/Arch Linux
sudo pacman -S mlocate // Manjaro Linux/Endeavour OS/Arch based distributions
Once the installation is over, you can run the following examples to learn more about the usage of the locate command.
8+ Examples of how to use locate command on Linux
1. Basic Usage of the locate command
The use of locate is very intuitive; you just need to invoke the locate command and pass the file name as arguments to locate the file from the disk storage.
The basic syntax of the locate command is as follows:
For example, I need to search all files that has a history as the file name or path under the current directory and its sub-directories,
I can use the following command:
$ locate "history" /usr/share/man/man1/docker-history.1.gz /usr/share/man/man1/docker-image-history.1.gz ...... /var/log/apt/history.log /var/log/apt/history.log.1.gz
2. Format locate command with less
The list can be long when the locate command finds multiple file names consisting of respective search arguments, and you will need some kind of pager tool to scroll back and forth.
For better readability, you can pipe the locate command with less command.
locate [SEARCH-QUERY] | less
For example, I had run the above command on the TTY interface and faced a problem with scrolling, but once I piped the output with less, the problem was resolved.
Here is the command I used:
$ locate "history" | less
3. Restrict the number of outputs
From the above example, you have seen that we get a number of results for the specific search query. It would be best if we could limit the output results to a specific number.
locate [SEARCH-QUERY] -n [NO.OF OUTPUTS]
For example, if we want to limit the number of results to 5, we can use the -n option with a search query.
$ locate history -n 5 /home/ankit/.bash_history /home/jane/.local/share/klipper/history2.lst /home/qbtuser/.bash_history /home/trendoceans/.bash_history /home/trendoceans/.zsh_history
4. Display or count the number of search queries
When you use the above command to limit the search, you are bound to miss several results. How do I proceed if I want to know the number of entries for specific queries or search patterns?
$ locate -c [SEARCH-QUERY]
For example, I want to find the number of files with the “history” as the file name or path.
$ locate -c "history" 466
Omg! I have 466 files which include history has a file name.
5. Ignore case sensitive
The output that you get from the locate command on-screen is case-sensitive, which means when you search for “history,” it will only find a word that has “history,” and it will ignore cases like “History” or “HISTORY.”
If you want to search for a case-insensitive word, you need to use the -i option.
locate -i [SEARCH-QUERY]
For example, if you want to search for all the files that have a history in their name or path, you need to use the -i option as shown in the following command:
$ locate -i HISTORY /usr/share/perl/5.32.1/CPAN/Meta/History /usr/share/perl/5.32.1/CPAN/Meta/History.pm .... /var/log/apt/history.log /var/log/apt/history.log.1.gz
6. Search for the exact filename
The above command is good when you are unsure about the file name and location of the specified query. But when you are damn sure about the file name, then you can clear out the clutter and print the exact file path .
To find the exact filename path, you can use -r or –regexp options.
locate -r [SEARCH-QUERY]
For example, I want to find out where the apt command history log is located. To find that, you can run the following command:
$ locate -r history.log /var/log/apt/history.log /var/log/apt/history.log.1.gz
7. Show only available files
No, doubt you will be amazed by the speed of locate command to find out the absolute path of the specific search query. But sometimes, you may find the path of a file is no longer available on your system and still locate command is displaying the path.
That happens because the updatedb command updates the database file every 24 hours via cron job. Any changes that occur after the update cycle will not be recorded until the next cycle or manual update.
You can use -e or –existing options to show only existing files.
locate -e [SEARCH-QUERY]
For example, Let me remove the file name “sample.txt” and execute the command without -e option and with -e option to find out the difference.
$ rm sample.txt $ locate sample.txt /home/trendoceans/sample.txt $ locate -e sample.txt -c 0
8. Stats of /var/lib/mlocate/mlocate.db
As you know, the locate command fetches data from /var/lib/mlocate/mlocate.db. So, you can find out how many records are stored in the database using the following command, which includes the location of the database, no. of directories, total no. of files, including the size of the database and file name:
$ locate -S Database /var/lib/mlocate/mlocate.db: 93,135 directories 8,94,890 files 7,96,93,484 bytes in file names 2,81,82,516 bytes used to store database
9. Use different mlocate Database
Sometimes you may have multiple mlocate files available to your system, and if you want to read that mlocate file, then you need to use the -d option and specify the path of the database with the search pattern.
For example, if you have two mlocate databases available, you can use the following command to search for a file from the second mlocate database:
locate -d [NEW DATABASE PATH] [SEARCH-QUERY]
10. Manually update locate database
The /var/lib/mlocate/mlocate.db database file gets updated every 24 hours via a cron job, and new changes will be reflected in the file system after 24 hours.
And if you made any changes and want to update the database right now, then you need to run the below command in your terminal to update the database.
$ sudo updatedb
Now you can run the locate command to find updated details.
That’s all for now.
If you want to share some locate commands, I missed or any tricks that can be useful for Linux users, please share them with us.
We will be very happy to have them.
A man with a tech effusive who has explored some of the amazing technology stuff and is exploring more. While moving towards, I had a chance to work on Android development, Linux, AWS, and DevOps with several open-source tools.
This Post Has 3 Comments
I wrote a small layer on top of mlocate, which builds a locate.db for each drive I have (many offlines). A script blocate (b for backup) would then loop over all the locate.db’s using locate and report what locate would report but then for all my way too many old HDD’s. It’s saved my life more than I can count. I should add this to my github repo where I keep all my dotfiles. Somebody should remind me if they’re interested in this.
We would be glad to have it.
I clearly forgot about this. In the mean time I’m now running into mlocate vs. plocate. The latter is the new version, and defaults comes with ubuntu22, whereas ubuntu20 is the older mlocate format.