Beginners Guide for locate Command in Linux

Linux TLDR
Last Updated:
Reading time: 5 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
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Prerequisiteslocate, updatedb
Internet RequiredYes (for installation)
Discussed Tools in this Article

How to Install locate in Linux

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

Installing locate on Debian-based distros like Ubuntu, Pop!_OS, etc

$ sudo apt install mlocate -y

Installing locate on RHEL-based distros like Fedora, AlmaLinux, etc

$ sudo yum install mlocate -y

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
/etc/default/nginx
/etc/init.d/nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/modules-available
/etc/nginx/modules-enabled

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

Also Read: Beginners Guide for Whereis Command in Linux

Searching for the Exact Filename

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

$ locate */nginx
/etc/default/nginx
/etc/init.d/nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/ufw/applications.d/nginx
/opt/kasm/1.11.0/conf/nginx
/opt/kasm/1.11.0/log/nginx
/usr/lib/nginx
/usr/sbin/nginx
/usr/share/doc/nginx
/usr/share/nginx

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
/etc/default/nginx
/etc/init.d/nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d

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
352

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

$ locate -c */nginx
37

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

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

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, as shown.

$ ls -l *.txt
-rw-rw-r-- 1 linuxtldr linuxtldr 0 Nov 28 11:43 file.txt
-rw-rw-r-- 1 linuxtldr linuxtldr 0 Nov 28 11:44 File.txt
-rw-rw-r-- 1 linuxtldr linuxtldr 0 Nov 28 11:44 FILE.txt

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*
/home/linuxtldr/FILE.txt

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*
/home/linuxtldr/FILE.txt
/home/linuxtldr/File.txt
/home/linuxtldr/file.txt

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 
file.txt
$ locate ~/file.txt 
/home/linuxtldr/file.txt

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 
/home/linuxtldr/file.txt

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

$ locate -e ~/file.txt

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
/home/linuxtldr/file.txt

Also Read: Beginners Guide for Which Command in Linux

Suppressing the Errors While Searching

If you assign a custom database that does not exist or anything else that prints the error as shown.

$ locate -d ~/database.db ~/file.txt
locate: ‘/home/linuxtldr/database.db’: No such file or directory

To suppress such error messages from the output, use the “-q” flag.

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

While executing the above command, you may get the following error.

locate: invalid option -- 'q'
Try 'locate --help' for more information.

Then, instead of using the “-q” flag, redirect the stderr(2) to /dev/null, as shown.

$ locate -d ~/database.db ~/file.txt 2>/dev/null

Separating Output Entries with ASCII NUL

When you search for files using the locate command, it will print the entries separated by a newline “\n” character.

$ locate -l 5 nginx
/etc/default/nginx
/etc/init.d/nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d

Use the “-0” or “--null” flag to separate the entries with an ASCII NUL instead of a newline character.

$ locate -l 5 -0 nginx
/etc/default/nginx/etc/init.d/nginx/etc/logrotate.d/nginx/etc/nginx/etc/nginx/conf.d$ 

Printing locate Database Statistics

Use the “-S” or “--statistics” flag to print the database statistics information, like database path, format, modification date, size, and many more.

$ locate -S
Database /var/cache/locate/locatedb is in the GNU LOCATE02 format.
Database was last modified at 2022:11:28 11:48:00.826577517 +0530
Locate database size: 12376729 bytes
All Filenames: 947555
File names have a cumulative length of 109838787 bytes.
Of those file names,

	303 contain whitespace, 
	0 contain newline characters, 
	and 20 contain characters with the high bit set.
Compression ratio 88.73% (higher is better)

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.