
locate Command in Linux
locate is a Linux command that allows you to quickly find files and directories by their names. It performs much faster compared to find command because it uses a database that is regularly updated with the file paths on the system.
When you type a search term, the locate scans this database to return results almost instantly. This means you can find files without knowing their exact path or name, even handling partial matches.
Table of Contents
Here is a comprehensive guide to the options available with the locate command −
- How to Install locate Command in Linux?
- Syntax of locate Command
- locate Command Options
- Examples of locate Command in Linux
How to Install locate Command in Linux?
The locate command is not installed by default on most Linux systems. Here's how you can install it on different Linux distributions −
Debian-based Systems (like Ubuntu) −
sudo apt install plocate
Red Hat-based Systems (like Fedora) −
sudo yum install plocate
Arch Linux −
sudo pacman -S plocate
OpenSUSE −
sudo zypper install plocate
After installation, you need to update the locate database using the following command to make it functional −
sudo updatedb
Syntax of locate Command
The basic syntax to use the locate command on Linux is as follows −
locate [options] [pattern]
Where,
- [options] are different flags that are used to refine the search.
- pattern is the filename or partial filename you're searching for. Wildcards (*) and regular expressions are allowed.
locate Command Options
The following table summarizes different options available for the locate command on Linux system −
Options | Description |
---|---|
-b | Search only for the basename (filename without path). |
-c | Count the number of matching entries. |
-d | Use a specific database file. |
-i | Ignore case distinctions. |
-l | Limit the number of results. |
-0 | Output a null character after each result. |
-N | Specify the maximum number of results. |
-r | Use regular expressions for searching. |
-w | Match the whole path name exactly. |
Examples of locate Command in Linux
Let's explore a few practical examples of Linux locate command −
- Find a Specific File by Name
- Ignore Case Sensitivity
- Limit the Number of Search Results
- Search Using Regular Expressions
- Count the Number of Matches
Find a Specific File by Name
To find a file named example.txt, simply use the locate command followed by the filename. This will list all paths where example.txt is found −
locate example.txt

Ignore Case Sensitivity
If you want to find example.txt without worrying about the case, use the -i option. The command provided below will locate the file regardless of whether it's written in uppercase or lowercase −
locate -i example.txt

Limit the Number of Search Results
In case you want to limit the number of results, you can use the -n option followed by the number of results you want. Follow the below-given command to return only the first 5 matches found −
locate -n 5 example.txt

Search Using Regular Expressions
For more complex searches, you can use the -r option to include regular expressions. The following example searches for example.txt within the /usr directory and its subdirectories −
locate -r '^/usr.*bin/example.txt$'
Count the Number of Matches
If you only want to know the number of matching entries, simply use the -c option. The following command will display the total count of files named example.txt −
locate -c example.txt

These detailed examples should help you master the locate command and make your file searches much more efficient on Linux.
Conclusion
The locate command is a powerful tool in Linux for quickly finding files and directories by their names. It significantly outperforms the find command in speed, thanks to its use of a pre-built, regularly updated database.
In this tutorial, we covered the installation process, detailed the syntax, explored various options, and provided practical examples. With these insights, you can efficiently utilize the locate command to make your file searches on Linux swift and effective.