Linux locate command with Examples

Written by: Bobbin Zachariah   |   Last updated: November 16, 2022

A Linux system has a hierarchical file system which starts at the root. File systems in Linux distributions can be a bit complicated for a user, especially a beginner if he is trying to find files.

In this tutorial, we learn how to use locate command to find your files.

locate command

Locate command in Linux is a command line tool used to list/find files by name or some patterns provided by the user. It is super easy to use the locate command and its output entries super fast than any other tool because it searches the files from a database of files.


The standard syntax to be followed is:

locate [options] [pattern]

Linux locate vs find

There are many tools present in Linux for locating files in the system. Like, find, which, locate, etc which works differently and outputs the data in independent ways. Out of which find and locate are mostly used.

By using the find command you can search for files by owner, name, group, permissions, type, size, time modified, date and various other patterns as well. But the find searches the filesystem recursively which consumes a lot of time and therefore makes it inefficient.

On the other hand, locate command searches for your files from an existing database of files rather than going through the whole filesystem which makes it fast as well as efficient.

Install Linux locate

Locate command comes preinstalled with some Linux Distributions. If you getting locate command not found error, install using the package manager.

For Ubuntu / Debian-based operating systems:

sudo apt install mlocate

For CentOS Stream/Redhat OS:

sudo yum install mlocate

For Fedora Linux:

sudo dnf install mlocate

And for Arch Linux-based operating systems:

sudo pacman -S mlocate

How locate command works

As discussed earlier, locate command uses a database file to search for files in the system. This file should be updated in order to output only the latest entries of files. Therefore a cron job is created for running the command "sudo updatedb" after every 24 hours. This updates the database file with the latest files created.

Files created after this update will not be displayed in the output screen if you searched for that specific files. Therefore it's better to update the database manually with the following command -

sudo updatedb

This will take some time depending on how much your system is loaded with files. This doesn't really give any output and just updates the database files -

locate updatedb

How to use locate command in Linux

Let us see some useful examples of locate command with its parameters and patterns.

Example 1: Locate a file

To locate the files with the pattern "mysql" we will just run the following command:

locate mysql
locate a file using locate command

And it will display all the files with full paths in the format of a list.

Example 2: Limit the search query

As the output entries are too many you can limit entries with the help of the -n parameter like this:

locate *.txt -n 10
using wildcard to search a pattern

It displays all the txt files with any names but as the limit is set to 10, only that amount of entries will be displayed.

Example 3: Display number of matching entries

Display the number of files that exist with the matching pattern with the -c option.

locate -c apache2
the number of matching entries only

As the image shows, eg: 735 file names exist with the pattern "apache2", 35 of "mysql", etc.

Example 4: Ignore case when matching patterns

By default locate command searches with case-sensitive files. To override this the -i option can be used to ignore the case of the patterns -

locate -i "sample."
ignore case

As in the output entries, there are all the files with small and capital alphabets present.

Example 5: Format the Output

The output of locate command line can be a very long list and its better to format the output with -

locate mysql | less
format the output of locate

Which makes scrolling a lot easier and clean as we know the start and end of all the entries on the terminal screen.

Example 6: Replace the default database

By default, the mlocate database is located at /var/lib/plocate/plocate.db. But you can use your own database.

locate -d /path/to/your/database.db <file_name>
change the default database

For eg: You have created a backup of the database before the updatedb command. Then you can search queries in the old backup database.

Example 7: Search for a File with an Exact Name

You can only search for files that contain the search term using a simple syntax. Use the -r option and add the dollar sign ($) to the end of your search term to get the file with the exact name, for instance:

locate -r mysql$
search exact filename

Example 8: Output without New Line

The output by default will be in the form of a list. But you can put a NULL instead of a newline to separate matches.

locate -i -0 **

Example 9: Show Existing Files

If you haven't updated the database, the Linux locate command may even display a deleted file, as we've already mentioned. Thankfully, you can avoid this issue by using the -e option, as shown here:

locate -e
Display only entries that refer to files existing

locate command options

Find useful options of locate command.

-0For the separator instead of using a new line, use ASCII NULL.
-AInstead of names matching one or more non-option arguments, only names matching all non-option arguments will be printed.
-bIf the pattern matches the last part of the name of a file listed in the database, the results are deemed to match. The 'base name' is the term used to describe this last element.
-cIf print (-p) is also present, print the total number of matches we found rather than the names of the matched files.
-d pathSearch the file name databases in the path, which is a separate list of database file names, as opposed to the default file name.
-eOnly print names that are in use right now.
-EPrint only those names that are currently unavailable.
--helpPrint a list of the ways to find and leave.
-iCase distinctions in the names and the pattern should be ignored.
-l NOnly allow N matches in total.
-LBroken symbolic links should be treated as non-existent when checking for the existence of files. This is the standard.
--max-database-age DBy default, locate searches a database that is more than 8 days old and a warning message will be displayed. This option alters that value from 8 to a different number.
-mAccepted but does nothing, it is for BSD locate compatibility.
-P, -HTreat broken symbolic links as if they were real files when testing for file existence (using the -e or -E options). The -H version of this option is only included for find compatibility; -P is preferred over -H.
-pBecause of the presence of --statistics (-S) or --count, print search results when they normally wouldn't
-rFilenames that match the given regular expression in their full paths are displayed.
--regextype RR dialect for regular expressions There are a number of dialects that are supported, including "findutils-default," "posix-awk," "posix-basic," "posix-egrep," "posix-extended," "posix-minimal-basic," "awk," "ed," "egrep," "emacs," "gnu-awk," "grep," and "sed."
-sAccepted but does nothing for BSD locate compatibility.
-SUnless non-option arguments are provided, print various statistics about each locate database and then exit without conducting a search. For BSD compatibility, -S is accepted as an alternative to --statistics.
--versionPrint the locate and exit version number.
-wCompare against the entire file name that is listed in the database. This is the standard.


Linux locate command is very useful to find files existing files in your system. Especially in Linux servers where you have to communicate with a terminal. Locate command is fast and efficient with only one disadvantage i.e. you will need to update the database from time to time. Locate command offers many options for users to filter out file search queries according to their needs.

Try out more options by going through the manual with man command.

Navigate all-in-one place of Linux Commands for more learning.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master's degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification.



Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment