locate Command in Linux: Find Files Quickly

By 

Updated on

7 min read

Terminal showing locate command search results for files in Linux

One of the most common operations when working on Linux is to search for files and directories. There are several commands on Linux systems that allow you to search for files, with find and locate being the most used ones.

The locate command is the quickest and simplest way to search for files and directories by their names.

In this article, we will explain how to use the locate command.

Installing locate

Depending on the distribution and how the system was provisioned, the locate package may or may not be pre-installed on your Linux system.

To check whether the locate utility is installed, open up your terminal, type locate and press Enter. If the package is installed, the system will display locate: no pattern to search for specified. Otherwise, you will see something like locate command not found.

If locate is not installed, you can easily install it using the package manager of your distro.

Install locate on Ubuntu, Debian, and Derivatives

On Ubuntu 22.04+ and Debian 12+, the package is plocate (a faster, modern replacement for mlocate):

Terminal
sudo apt update
sudo apt install plocate

On older releases, install mlocate instead:

Terminal
sudo apt update
sudo apt install mlocate

Install locate on Fedora, RHEL, and Derivatives

Terminal
sudo dnf install plocate

How Does locate Work

The locate command searches for a given pattern through a database file that is generated by the updatedb command. The found results are displayed on the screen, one per line.

During the installation of the package, a scheduled task is created that runs the updatedb command every 24 hours. On systems using plocate, this is a systemd timer (plocate-updatedb.timer). On older systems with mlocate, it is a cron job in /etc/cron.daily/mlocate.

The database can be manually updated by running updatedb as root or a user with sudo privileges:

Terminal
sudo updatedb

The update process will take some time, depending on the number of files and directories and the speed of your system.

Files created after the database update will not be shown in the locate results.

Compared to the more powerful find command that searches the file system in real time, locate operates much faster but lacks many features and can search only by the file name.

How to Use the locate Command

The syntax for the locate command is as follows:

txt
locate [OPTION] PATTERN...

In its most basic form, when used without any options, the locate command will print the absolute path of all files and directories that match the search pattern and for which the user has read permission.

For example, to search for a file named .bashrc you would type:

Terminal
locate .bashrc

The output will include the names of all files containing the string .bashrc in their names:

output
/etc/bash.bashrc
/etc/skel/.bashrc
/home/linuxize/.bashrc
/usr/share/base-files/dot.bashrc
/usr/share/doc/adduser/examples/adduser.local.conf.examples/bash.bashrc
/usr/share/doc/adduser/examples/adduser.local.conf.examples/skel/dot.bashrc

The /root/.bashrc file will not be shown because we ran the command as a normal user that does not have access permissions to the /root directory.

If the result list is long, for better readability, you can pipe the output to the less command:

Terminal
locate .bashrc | less

Globbing and Pattern Matching

The locate command also accepts patterns containing globbing characters such as the wildcard character *. When the pattern contains no globbing characters, the command searches for *PATTERN*. That is why in the previous example, all files containing the search pattern in their names were displayed.

The wildcard is a symbol used to represent zero, one, or more characters. For example, to search for all .md files on the system, you would type:

Terminal
locate '*.md'

Quoting the pattern prevents the shell from expanding it before locate receives it.

Limit the Number of Results

To limit the search results, use the -n option followed by the number of results you want to be displayed. The following command will search for all .py files and display only 10 results:

Terminal
locate -n 10 '*.py'

By default, locate performs case-sensitive searches. The -i (--ignore-case) option tells locate to run a case-insensitive search:

Terminal
locate -i readme.md
output
/home/linuxize/p1/readme.md
/home/linuxize/p2/README.md
/home/linuxize/p3/ReadMe.md

Count Matching Entries

To display the count of all matching entries, use the -c (--count) option. The following command would return the number of all files containing .bashrc in their names:

Terminal
locate -c .bashrc
output
6

Search by Basename Only

By default, locate matches the pattern against the full path. To search only the filename (the last component of the path), use the -b (--basename) option:

Terminal
locate -b '\config.yaml'

The leading backslash tells locate to match the exact filename rather than treating it as a substring.

Check for Existing Files

By default, locate does not check whether the found files still exist on the file system. If you deleted a file after the latest database update, and if the file matches the search pattern, it will still be included in the search results.

To display only the names of the files that exist at the time locate is run, use the -e (--existing) option. For example, the following would return only the existing .json files:

Terminal
locate -e '*.json'

Search with Regular Expressions

If you need to run a more complex search, use the -r (--regexp) option, which allows you to search using a basic regexp instead of patterns. This option can be specified multiple times. For example, to search for all .mp4 and .avi files on your system and ignore case, you would run:

Terminal
locate --regex -i "(\.mp4|\.avi)"

Quick Reference

CommandDescription
locate patternSearch for files matching pattern
locate -i patternCase-insensitive search
locate -b '\filename'Match basename only (exact filename)
locate -n 10 patternShow only the first 10 results
locate -c patternCount matching entries
locate -e patternShow only files that still exist
locate -r 'regex'Search using a regular expression
locate --regex -i '(\.mp4|\.avi)'Regex search, case-insensitive
sudo updatedbManually update the file database

Troubleshooting

locate: command not found
The locate package is not installed. Install plocate (or mlocate on older releases) using your distribution package manager.

locate does not find a file you just created
The index database is stale. Run sudo updatedb and search again.

Search returns deleted files
The database still contains old entries. Use locate -e pattern to show only existing files, then refresh with sudo updatedb.

Pattern returns too many results
Use -b for basename-only matching or narrow output with -n and -r patterns.

Wildcard pattern behaves unexpectedly
Quote patterns like *.log as '*.log' so the shell does not expand them before passing them to locate.

FAQ

What is the difference between locate and find? The locate command searches a pre-built database, which makes it much faster than find . However, find searches the file system in real time and supports searching by file size, permissions, modification time, and many other criteria. Use locate for quick name-based lookups and find for complex queries.

Why does locate not find a file I just created? The locate database is updated periodically (usually once a day). Files created after the last update will not appear in the results. Run sudo updatedb to refresh the database manually.

What is the difference between mlocate and plocate? plocate is a modern replacement for mlocate. It builds a smaller database, uses less memory, and returns results faster. On recent distributions such as Ubuntu 22.04+ and Debian 12+, plocate is the default.

How do I search for an exact filename instead of a substring? Use the -b option with a leading backslash: locate -b '\filename'. Without -b, locate matches the pattern against the full path.

Conclusion

The locate command searches a pre-built database for files and directories whose name matches a given pattern. It is extremely fast for simple name-based lookups. For more complex searches based on file attributes, use the find command.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page