10 locate command examples in Linux [Cheat Sheet]

Written By - Rohan Timalsina

Introduction to locate command

There are multiple tools in Linux that allows you to search files and directories in the system. locate is one of the most used tools for searching files in Unix-based operating systems. It locates the files by their names. It searches for files whose names match a specified pattern in the command.


locate command vs. find command

find is another popular command-line tool for searching files and directories in the Linux system. Both locate and find commands have similar functionality, but they work in different ways. locate searches files in the database whereas find searches files in the entire file system. Due to this, locate is faster than the find command in searching files. find has advanced options to search files based on different conditions, making it more powerful than the locate command.


How to install locate command

locate command is not installed by default in Linux systems. You can install locate command using the package manager tool of your Linux distros.

Install locate on Ubuntu, Debian, and Linux Mint:

$ sudo apt install plocate

Install locate on Fedora, CentOS, AlmaLinux, and Red Hat:

$ sudo dnf install plocate

plocate is a replacement for the older mlocate package. If your system is unable to locate plocate package, use mlocate instead. When installing the plocate package, the plocate database is also initialized on the system.


How to use locate command

The syntax for using locate command is as follows:

$ locate [OPTION] [PATTERN]


Different examples to use locate command

1. locate files having a specific name

You can specify a file name to the locate command and search for all files that match the specified pattern. The following command locates all files having name test1.txt.

$ locate test1.txt

Sample Output:

locate command to locate files having specific name


2. Update a database file

If you just created new files, you have to update the database to show them in the locate command output. Also, if you get the deleted files in the output, then the database has not been updated yet.

You can manually update the database using the command below.

$ sudo updatedb

Sample Output:

After updating the database, you will get the updated file results.

update locate package database manually

The database is regularly updated every 24 hours automatically.


3. Print only existing files at the time locate is run

When you have not updated the database, the deleted files still appear in the result when locate command is used. To get only the names of files that exist at the time locate command is run, you can use -e or --existing flag.

$ locate -e test1.txt


$ locate --existing test1.txt

Sample Output:

locate command to print only existing files at the time locate is run

4. Count the number of matching entries

With -c or --count option, locate command displays the number of matching entries instead of printing their full path. The following command counts the number of all .txt files on the computer.

$ locate -c *.txt


$ locate --count *.txt

Sample Output:

locate command to count the number of files


5. Limit the number of output

By default, locate command displays all files that match the specified pattern. You can use the -l, --limit, or -n option to limit the number of files displayed on the output.

The following commands print the name of the first 15 .txt files.

$ locate -l 15 *.txt


$ locate --limit 15 *.txt


$ locate -n 15 *.txt

Sample Output:

limit the number of results in locate command output


6. Ignore cases when matching patterns

You can use -i or --ignore-case option to ignore case-sensitive files when searching for files. File names with both uppercase and lowercase characters are displayed in the output.

$ locate -i pattern


$ locate --ignore-count pattern

Sample Output:

locate command to ignore cases when matching patterns


7. Display the names of files with an exact name

By default, locate command searches for files whose name contains the specified pattern. For example, searching for a pattern test displays all files or directories that have a string test in their names.

To display results having an exact match only, you can use the -r or --regex option.

$ locate -r /test$


$ locate --regex /test$

Sample Output:

locate command to search files having exact names


8. Display statistics about the locate database

You can view the current statistics of your mlocate database using the -S or --statistics option.

$ locate -S


$ locate --statistics

Sample Output:

locate command to display statistics of database


9. Use a different database instead of the default

The default database used to search files is /var/lib/mlocate/mlocate.db. You can specify a different database with -d or --database option.

$ locate -d DBPATH pattern


$ locate --database DBPATH pattern

Sample Output:

The following example searches the test2.txt pattern in the database /test/database.db in the home directory.

locate command to use a different database instead of default


10. Hide the error message in the output

Sometimes, locate command displays the error message while searching the file. The -q or --quiet option tells locate to suppress the error output. It can be used to hide errors like permission denied.

$ locate -q 


$ locate --quiet

Sample Output:

locate command to hide the error output



In this tutorial, we have discussed different examples of using locate commands in Linux. locate command is helpful for searching files quickly. The mlocate database needs to be updated whenever you create or remove files on your system. If you have any questions, please let us know in the comment section.


What's Next

25+ most used find commands in Linux [Cheat Sheet]
How to find and remove duplicate files using shell script in Linux


Further Reading

man page for locate command


Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment