Beginners Guide for locate Command on Linux

Linux TLDR
Last Updated:
Reading time: 4 minutes

The locate command is used to quickly search for files or directories within the indexes in the database file known as updatedb.

It is identical to the find command, except it will search in the database file, which is much faster than find’s method of searching rigorously throughout the entire system.

The updatedb database is set to automatically update in 24 hours using cron jobs when you install this program, but if you make any changes, it will not be able to locate them until cron jobs or you manually request the database update.

Tutorial Details

Descriptionlocate
Difficulty LevelLow
Root or Sudo PrivilegesYes
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisiteslocate, updatedb
Internet RequiredYes (for installation)

How to Install locate in Linux

If the command is missing from your distribution, execute one of the following commands to install it.

$ sudo apt install mlocate -y                                       #For Ubuntu, Pop!_OS, etc
$ sudo yum install mlocate -y                                      #For Fedora, AlmaLinux, etc

Syntax of the locate Command

The locate command takes two arguments, one being the option and the other being the pattern.

$ locate [OPTION] [PATTERN]

The following is the list of valid options:

OptionsDescription
-0, --nullIt will use ASCII NUL as a separator instead of newline.
-A, --allIt will only print the names that match the entire pattern.
-b, --basenameIt will look for the final component (also known as the basename) for a match.
-c, --countIt will print the total number of matches instead of their names.
-d, --database [DBPATH]Instead of using the default database path to search, assign your custom database path to search for names.
-e, --existingOnly print out names that currently exist (instead of names that existed when the database was created).
-E, --non-existingOnly print out names that currently do not exist (instead of names that existed when the database was created).
-i, --ignore-caseIgnore case sensitivity for both the pattern and the file names.
-l N, --limit=NLimit the number of searches assigned to this flag.
-L, --followIf you are searching for files with “the -e or -E options”, it will ignore the broken symbolic links.
--max-database-age DA database that has not been updated for more than 8 days will show you a warning message, but you can replace the X days with a custom value using this flag.
-P, -H, --nofollowThe opposite of -L, --follow options.
-r, --regex [REGEXP]Used for searching a basic regular expression [REGEXP].
-S, --statisticsPrint various statistics about each database to standard output instead of searching for files.
-w, --wholenameMatch the whole name of the file as listed in the database.
-h, –helpDisplay the help section.

Manually Updating the Database

The updatedb database is set to automatically update within 24 hours using cron jobs, but you can manually update the database after every file change using the following command.

$ sudo updatedb

Searching for a Filename

The easiest way to search for any file is to assign its name as an argument to the locate command to search in the database.

$ locate nginx

The above command will print the absolute path with the Nginx pattern in the filename.

searching files related to nginx using locate command

Searching for the Exact Filename

The following command will print the absolute path ending with the Nginx pattern.

$ locate */nginx

Output:

Searching absolute path of nginx using locate command

Limiting the Number of Searches

By default, the locate command will print the absolute path for all the matched filenames, but you can limit the search result to a specific number using the “-l” or “--limit” flag.

$ locate -l 5 nginx

Output:

filter results in locate command

Display the Number of File Occurrences

Use the “-c” or “--count” flag to print the total number of matches instead of their name.

$ locate -c nginx

Output:

Counting total number of results

The following command will print the total number of matches for the exact matched filename.

$ locate -c */nginx

Output:

Counting total number of absolute results

The following command will print the total number of matches for the specific file type (ex: “txt” or “jpg“).

$ locate -c "*.txt"
$ locate -c "*.jpg"

Output:

Counting total number of text and jpg files in Linux system using locate command

Ignore Case Sensitivity

By default, the locate command will search for the file using case-sensitivity, meaning “file.txt” and “File.txt” are not the same.

However, you can ignore the case-sensitivity using the “-i” or “--ignore-case” flag, which will include “file.txt” and “File.txt” while searching.

For example, I’ve created multiple “.txt” files with different case-sensitivity in my home directory and issued “sudo updatedb” to update database, as shown.

$ ls -l *.txt

Output:

Sample text file in home directory

If you search for these files using “file.txt” as an argument without any flags, it will only output one result with the exact matching pattern.

$ locate ~/*file.txt*

Output:

Locate command only listing case sensitive files

But if you use the “-i” or “--ignore-case” flag, it will print all the files without considering the case-sensitivity.

$ locate -i ~/*FILE.txt*

Output:

Ignoring case sensitivity in locate command

Searching Only for Existing Files

If you have deleted a file between the automatic database update set in cron jobs (ex: “24 hours”), your files might still appear in the output because the database still has the file record.

For example, I’ve updated the updatedb database after creating the “file.txt” file as shown.

$ ls file.txt 
$ locate ~/file.txt 

Output:

Checking and locating file using locate command

For instance, if the file is deleted without refreshing the updatedb database, it will still appear in the output when searched.

$ rm file.txt 
$ locate ~/file.txt 

Output:

Deleted file appearing in locate command results

To ignore the deleted files that come up in the search, use the “-e” or “--existing” flag.

$ locate -e ~/file.txt

Output:

Ignoring deleted files in locate command results without updating database

Choose a Different Updatedb Location

By default, the locate command will use the default database located at the “/var/cache/locate/locatedb” path.

However, you can specify a custom database path using the “LOCATE_PATHenvironmental variable or by using the “-d” or “--database” flag with the database path.

$ locate -d ~/database.db ~/file.txt

And here comes the end of this article.

If you have any questions or queries, feel free to ask them in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.