This article explains the operation of the “locate” command in Linux. The speed of “locate” is one of its main benefits. It does not do a real-time file system scan. Rather, it indexes the file paths on your system using a pre-built database. Almost immediate results are obtained by Locate because this database is updated often (typically once a day via a cron job).
Note: Newly created files may not show up immediately. To include recent changes, you’ll need to manually update the database using “updatedb.”
Follow the steps:
- Installing “locate” on Your System
Depending on your Linux distribution, locate may not be installed by default. You can install it using your system’s package manager:- On Debian/Ubuntu systems:
sudo apt install locate
- On RHEL/CentOS systems:
sudo yum install locate
- Once installed, ensure the database is created or updated:
sudo updatedb
- If you don’t update the database, you may encounter an error like:
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory
Running updatedb resolves this by creating the necessary index.
- On Debian/Ubuntu systems:
- Performing a Search with “locate”
- To perform a basic search, simply run:
locate filename
- For example, to find all files with a .txt extension:
locate *.txt
You may find the output too broad. That’s where additional options help refine your search.
- To perform a basic search, simply run:
- Useful “locate” Options
Here are some helpful options to use with locate:- -i – Make the search case-insensitive.
locate -i report.txt
- -r – Use regular expressions to define more complex search patterns.
locate -r '^/home/user.*\.log$'
- -c – Show only the count of matching entries, instead of listing them.
locate -c backup
- -i – Make the search case-insensitive.
We can conclude that the locate command is a fast and efficient way to find files on a Linux system using a pre-indexed database. With just a few commands and options, you can significantly reduce the time spent manually searching directories.
If you’re managing a large system or need frequent access to scattered files, learning locate can greatly enhance your command-line productivity.
Need to track down large files? Learn How to locate large files in cPanel using the terminal