How to Use the Locate Command in Linux?
If you’re working on a Linux machine—either your own personal computer or a production server—you probably care about quickly finding files. Although the find command is an excellent utility for finding files and directories in real-time, it has the disadvantage of being slow, particularly on Ubuntu or other Linux operating systems with an incredibly large directory structure. This is where the locate command comes into play.
The locate command provides a more efficient way of searching for files without compromising your time. The locate command works as a fast indexing method. Rather than reading the file structure in real-time like the find command, the locate command searches a previously built index of your files. If you need to find files quickly and efficiently, the locate command is ideal whether you are a developer, sysadmin, or work with files in general.
In this article, you will understand how to use the locate command in Linux, how the command works behind the scenes, and possible practical applications ranging from a simple locate command to a more advanced, understanding use of commands. If you want to increase your Linux command-line knowledge or you’re just starting out on Linux, this tutorial will help you find files faster.
Why Use locate Instead of find?
The locate command is considerably quicker than find because it relies on a database that the system manages on its own to perform searches, which is why you do not have to wait for the system to scan through each and every folder and each and every file, which is what the find command does. Just remember that the database is not real time, so it might not show a file that you just created, unless you update the database yourself.
The find command, on the other hand, searches your file system in real time, which may take time but is accurate. When searching for a file, though, you might want to look for files with certain permissions, for a file of a certain size, or dated of a certain modification date.
So, find is still your best option, but for quick searching, locate is better.Use locate as your file search speedster in Linux—it is your go-to option when you want quick results. If you do not care about manually updating the database to keep current, then locate and running many searches with, is the way to go.
How the locate Database Works
The magic of locate comes from its file database. This database is typically updated automatically by a service running in the background (e.g., a cron job) or be refreshed manually if you’ve added a new file and want it indexed immediately.
To update the database manually, just run:
sudo updatedb
This command ensures locate has the most up-to-date list of files and directories to search from.
The database file is typically stored here:
/var/lib/mlocate/mlocate.db
If you want to use a different database, you can specify it with the -d option:
locate -d /path/to/database.db pattern
How to Install the locate Command
Most Linux distributions come with locate pre-installed. If not, installing it is quick and easy.
On Ubuntu or Debian:
sudo apt update sudo apt install plocate
Ubuntu and Debian now use the plocate package, a faster and more efficient version of mlocate.
On CentOS or Fedora:
sudo yum update -y sudo yum install mlocate
After installation, make sure to update the database:
sudo updatedb
Basic locate Command Syntax
The general syntax is:
locate [options] pattern
pattern – The filename (or part of it) you’re looking for.
options – Optional flags to fine-tune your search.
Set Up Test Files and Directories
To get comfortable with the locate command, create some test files:
touch cantech.txt Cantech.txt CANTECH.txt touch file.exe CANTECH.exe mkdir -p dir /tmp/demo/dir1/sub_dir
Now update the database so these new files are included:
sudo updatedb
Common locate Command Options
Here are some useful flags:
Option Description -i Case-insensitive search -c Print only the number of matches -n N Limit output to the first N results -r Use basic regular expressions --existing Show only files that still exist --basename Match only the filename (not full path) --wholename Match the entire path -d Specify a custom database file
Practical locate Command Examples
1. Basic search:
locate cantech.txt
2. Case-insensitive search:
locate -i cantech.txt
3. Count results only:
locate -c example.txt
4. Limit results:
locate -n 5 example.txt
5. Search with regex (find .exe files):
locate -r '\.exe$'
Advanced locate Command Examples
Search for a directory by name:
locate -r '/dir$'
Find .pdf files:
locate -r '\.pdf$'
Combine options (case-insensitive, limit 5, regex):
locate -ir '\.log$' -n 5
Using locate with Other Commands
Extract specific text from files:
locate logfile.txt | xargs grep -i 'error' | awk '{$1=$2=$3=""; print substr($0,4)}'
Count unique errors:
locate logfile.txt | xargs cat | grep -i 'error' | sort | uniq -c | sort -nr
Replace text inline with sed:
locate logfile.txt | xargs cat | sed 's/error/ERROR/g'
Save output to a file with tee:
locate logfile.txt | xargs cat | grep 'ERROR' | tee output.txt
Filter results using grep:
locate log | grep '/var/log'
Delete matched files:
locate cantech.txt | xargs rm -f
Combine locate with find for actions:
locate Cantech.txt | xargs -I {} find {} -type f -exec ls -l {} \;
Using locate Environment Variables
You can customize locate behavior with environment variables:
LOCATE_PATH – Sets the path to the database:
export LOCATE_PATH='/var/lib/plocate/plocate.db'
LOCATE_CONFIG – Points to a custom config file:
export LOCATE_CONFIG='/path/to/locate.conf'
These are especially useful in multi-user systems or custom setups.
Variants of locate: mlocate and slocate
mlocate (Merging Locate)
Efficiently updates only modified database sections. Use:
sudo updatedb.mlocate
locate example.txt
slocate (Secure Locate)
Adds permission checks so users can’t view files they don’t have access to:
sudo updatedb.slocate locate example.txt
Conclusion
One of the quickest and easiest solutions to searching for files in a Linux based system is by using the locate command. Employing an indexed build, it minimizes time when it comes to searching for files, allowing you to produce faster.
The locate command can also utilize regular expressions to conduct case-insensitive searches or be the basis for other utilities such as grep, awk, or xargs. This command is more than a basic file locator, but rather some function of your utility.
Learning how to use locate can save you a great deal of time, whether you are looking for config files, reviewing logs, or organizing a system. Add it to your toolbox in Linux, and it will save you time and reduce frustration in multiple ways.