locate Command in Linux: Find Files Quickly

By 

•

Updated on

•

11 min read

Terminal showing locate command search results for files in Linux

You know a file is called nginx.conf or docker-compose.yml, but you do not remember where it lives. Running find from the root directory works, but it walks the entire file system and can take a while on a large disk.

The locate command answers the same question almost instantly. It does not scan the disk. It searches a prebuilt database of file names instead. On most current distributions, locate is provided by plocate, a faster rewrite of the older mlocate.

This guide explains how to install locate, keep its database up to date, and use its options to narrow down the results.

When a name lookup is not enough, our find command builder assembles a find command from size, time, and permission filters.

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 plocate: no pattern to search for specified. Otherwise, you will see locate: command not found. On Ubuntu, the shell also tells you which package to install:

output
Command 'locate' not found, but can be installed with:
sudo apt install plocate

If locate is not installed, you can 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 that do not ship plocate, install mlocate instead:

Terminal
sudo apt update
sudo apt install mlocate

Install locate on Fedora, RHEL, and Derivatives

On Fedora and RHEL 10 (including Rocky Linux and AlmaLinux 10), the package is plocate:

Terminal
sudo dnf install plocate

RHEL 9 and its derivatives still ship mlocate:

Terminal
sudo dnf install mlocate

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 daily. On systems using plocate with systemd, this is a systemd timer (plocate-updatedb.timer). On Debian and Ubuntu systems without systemd, the package provides a fallback cron job in /etc/cron.daily/plocate.

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 whose parent directories the user can read and traverse.

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
/snap/core24/2125/etc/bash.bashrc
/snap/core24/2125/etc/skel/.bashrc
/snap/core24/2125/usr/share/dot.bashrc
/snap/core24/2125/usr/share/base-files/dot.bashrc
/usr/share/dot.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.

When the pattern does contain a globbing character, locate no longer adds the implicit wildcards. The pattern has to match the whole path, from the leading / to the end of the file name.

Search in a Specific Directory

There is no option for limiting the search to one directory. Instead, include the directory path in the pattern. For example, to find all .conf files under /etc/ssh, you would type:

Terminal
locate '/etc/ssh/*.conf'
output
/etc/ssh/ssh_config.d/20-systemd-ssh-proxy.conf
/etc/ssh/sshd_config.d/60-cloudimg-settings.conf

Both files live in subdirectories of /etc/ssh. Unlike in the shell, the * wildcard in a locate pattern also matches the / character. The search covers the whole directory tree below /etc/ssh.

Limit the Number of Results

To limit the search results, use the -l (--limit) 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 -l 10 '*.py'

The -n option does the same thing. It is still accepted for compatibility with older locate implementations.

By default, locate performs case-sensitive searches. The -i (--ignore-case) option tells locate to run a case-insensitive search. For example, phones and cameras often save videos with an uppercase .MP4 extension:

Terminal
locate -i '*.mp4'
output
/home/linuxize/Videos/Family Trip.mp4
/home/linuxize/Videos/clip.mp4
/home/linuxize/Videos/holiday.MP4

Without -i, the holiday.MP4 file would not be included in the results.

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
11

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

With -b, the directory part of each path is ignored during the match. The pattern is still treated as a substring, so the output includes names that only contain config.yaml:

output
/home/linuxize/projects/app/config.yaml
/home/linuxize/projects/app/config.yaml.bak
/home/linuxize/projects/app/config.yaml.d
/home/linuxize/projects/app/old-config.yaml
/home/linuxize/projects/web/config.yaml

Search for an Exact File Name

To find only the files named exactly config.yaml, add a globbing character to the pattern. With a wildcard in the pattern, locate matches the entire path:

Terminal
locate '*/config.yaml'
output
/home/linuxize/projects/app/config.yaml
/home/linuxize/projects/web/config.yaml

The */ prefix matches any directory. The name after the last slash has to match exactly.

Older guides use locate -b '\config.yaml' for this. That trick works with mlocate, where the backslash counts as a globbing character. plocate does not treat it that way and returns the same partial matches as a plain search.

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, locate can treat the pattern as a regular expression. There are two options for this:

  • -r (--regexp) - Interpret the pattern as a basic regular expression (BRE).
  • --regex - Interpret the pattern as an extended regular expression (ERE). The |, +, and () characters work without backslashes.

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)$'
output
/home/linuxize/Videos/Family Trip.mp4
/home/linuxize/Videos/clip.mp4
/home/linuxize/Videos/holiday.MP4
/home/linuxize/Videos/talk.avi

The $ anchor matches the end of the path, so a file such as vnd.avi.xml is not included. If you run the same pattern with -r, locate prints nothing. In a basic regular expression, the parentheses and the | character are literal.

Both regex options make locate scan the entire database, so they are slower than regular pattern searches.

Pass Results to xargs

Each match is printed on its own line. That works for reading, but it breaks when you pass the results to another command with xargs . By default, xargs splits its input on whitespace, and a path with a space in it turns into two arguments:

Terminal
locate -i '*.mp4' | xargs ls -l
output
ls: cannot access '/home/linuxize/Videos/Family': No such file or directory
ls: cannot access 'Trip.mp4': No such file or directory
-rw-rw-r-- 1 linuxize linuxize 0 Oct 23 19:29 /home/linuxize/Videos/clip.mp4
-rw-rw-r-- 1 linuxize linuxize 0 Oct 23 19:29 /home/linuxize/Videos/holiday.MP4

The -0 (--null) option separates the matches with a null character. Pair it with xargs -0 so each path arrives as a single argument:

Terminal
locate -0 -i '*.mp4' | xargs -0 ls -l

This time ls receives Family Trip.mp4 as one argument and lists all three files.

Excluding Directories from the Database

The updatedb command reads its settings from the /etc/updatedb.conf file. The following variables control what is left out of the database:

  • PRUNEPATHS - Directory paths that are not indexed.
  • PRUNENAMES - Directory names (without a path) that are not indexed, wherever they appear.
  • PRUNEFS - File system types that are not indexed, such as proc, tmpfs, and network file systems.
  • PRUNE_BIND_MOUNTS - When set to yes, bind mounts are not indexed.

To keep a directory out of the results, for example a backup directory full of old copies, open the file with sudo and append its path to the PRUNEPATHS line:

/etc/updatedb.confconf
PRUNEPATHS="/tmp /var/spool /media /var/lib/os-prober /var/lib/ceph /home/.ecryptfs /var/lib/schroot /srv/backup"

On Ubuntu, the PRUNENAMES line is commented out by default. Uncomment it to skip version control directories, and add any other directory names you want to ignore, such as node_modules:

/etc/updatedb.confconf
PRUNENAMES=".git .bzr .hg .svn node_modules"

The changes take effect the next time the database is rebuilt. To apply them right away, run:

Terminal
sudo updatedb

The pruned directory itself stays in the database, but the files inside it do not. A search for backup still returns /srv/backup. The files stored in it no longer appear.

Quick Reference

CommandDescription
locate patternSearch for files matching pattern
locate -i patternCase-insensitive search
locate -b patternMatch the file name only, not the full path
locate '*/filename'Match an exact file name
locate '/etc/*.conf'Search only below a directory
locate -l 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 basic regular expression
locate --regex -i '\.(mp4|avi)$'Extended regex search, case-insensitive
locate -0 pattern | xargs -0 commandPass results safely to xargs
sudo updatedbManually update the file database

Troubleshooting

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

/var/lib/plocate/plocate.db: No such file or directory
The database has not been created yet. Run sudo updatedb once to build it.

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, '*/filename' for an exact file name, or narrow output with -l and regex patterns.

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

Excluded directory still shows its files
Check the path in PRUNEPATHS for a trailing slash. The entry /srv/backup/ does not match anything. Write it as /srv/backup, then run sudo updatedb.

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?
Add a globbing character so the pattern has to match the whole path: locate '*/filename'. The locate -b '\filename' form only works with mlocate.

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 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page