The locate command in Linux

The locate command is the fastest way to find files, directories, and scripts. It maintains a database of all file paths. It accepts a file name or text string as an argument. It searches the given file name or text string in its database and lists the results.

The locate command syntax

The locate command uses the following syntax.

#locate [options] [text-string or file-name]

The locate command options

The following table lists the most commonly used options.

Short optionLong option Description Example
-A --all List only the files that match all the specified patternslocate -A testfile
-b --basename Matches only base name locate -b testfile
-c --count List the number of matching entries instead of the file names locate -c testfile
-d --database Replaces the default database file with specified database file name locate -d [DBPATH]
-e --existing List only existing entires locate -e testfile
-L --follow Omit broken symbolic links locate -L testfile
-i --ignore Ignores case sensitivity locate -i testfile
-h --help Displays the help with a list of all available options locate -h
-n--number Stops after finding the given number of files locate -n 10 testfile
-p --ignore-spaces Ignore spaces when matching patterns locate -p test file
-l --limit Limit the number of entries in the output locate -l 5 testfile
-S --statistics Provide statistics about the database locate -S
-q --quiet suppresses error messages locate -q testfile
-r --regexp Use regex pattern locate -r testfile[1-3]

You can check the man page of the locate command to see a list of all supported options.

The locate command database

The updatedb utility updates the database the locate command uses. It periodically runs as a cron. On most systems, it runs once a day. Since the database is not updated continuously, the locate command does not list the recently added files in the search result. You can fix this issue by manually updating the database. The following command updates the database.


Open a terminal, create a new file, and use the locate command to find it. The locate command will not search the new file until the database is updated. Update the database and search the file again. This time, the locate command will find the file.

The following image shows the above exercise.


The locate command examples

You can use the locate command in many ways. This section provides common uses of the locate command.

Searching a file name

The following command searches all files containing the msql keyword in their name or path.

#locate msql

The following image shows the output of the above command.

search file

Piping the output

If the output contains more results than the terminal screen can display, the terminal will scroll the results. You will see only limited results. Instead of sending the output on the screen, you can redirect it to the less command. The less command allows us to scroll the output.

The following command searches for the keyword ftp and sends the output to the less command.

#locate ftp | less

The following image shows the output of the above command.

locate database

Viewing the database location and statistics

The following command displays the database location and statistics.

#locate -S


Run the above command, note down the total number of files and directory paths, create a file and directory, run the locate command with the S option again, and compare the result with the previous output.

The following image shows the above exercise.

update database

Avoiding case-sensitive search

By default, the locate command performs a case-sensitive search. It means MyFile and myfile are two different files for the locate command. If you want to ignore the letters' cases in search, use the i option.


Create a test file, use mix cases in the file name, update the database, use the locate command without any option to search the file, use only lowercase letters in the search string, now use the locate command again with the i option to search the file and compare the result of both commands. When we use the i option, it ignores the letter's case in the search string.

The following image shows the above exercise.

ignore case search

Counting the matching files

Instead of viewing the matching entries, if we want to count them, we can use the c option. The following command searches and counts all file paths having the keyword html.

#locate -c html

The following image shows the output of this command.

counting result

Searching only existing files

As we know, the locate command depends on its database for search, and a cron job updates its database. Because of this, any change in the file system does not reflect in the database immediately. When we delete a file, the database does not remove the file's entry until the next update runs.


Use the locate command to search a test file created in the previous exercise, delete the test file and search again, update the database, and search again for the deleted file.

The following image shows the above exercise.

searching only existing files

Limiting the search result

By default, it displays all results. The n option limits the results. The following command lists only the top five search results.

#locate html -n 5

The following image shows the output of the above command.

limiting search results

Omitting errors in results

The q option omits errors in the result.


The d option allows us to use a custom database. Use the d option to search a text string in a database that does not exist. It will give an error. Repeat the command with the q option. Compare the output of both commands.

The following image shows the above exercise.

omitting errors

Using regular expression

The locate command supports regular expression. The r option allows us to use regular expressions. The following command uses a regexp.

#locate -r named.conf$

The following image shows the output of the above command.

using regular expression

Advantages and disadvantages of the locate command

On the positive side, the locate command is faster than other native commands available on Linux for the search operation. On the negative side, its results are not always accurate. If the database has incorrect information, the locate command will display the wrong information. For example, you create a new file. The locate command will not find this file until its database is updated. Similarly, you delete a file. The locate command will display the deleted file in the search result until its data is updated.

ComputerNetworkingNotes Linux Tutorials The locate command in Linux