locate Command Explained

locate Command Explained

locate finds files by name, and it does so almost instantly, because it is not actually looking at your disk when you run it. It is looking at an index built ahead of time, the same way a book’s index lets you find a topic without reading every page.

Basic usage

locate nginx.conf
# /etc/nginx/nginx.conf
# /etc/nginx/sites-available/nginx.conf.bak

locate readme
# matches any path containing "readme" anywhere in it,
# not just files literally named exactly "readme"

By default, locate matches your search term anywhere within the full path, not just the filename itself, and it is a substring match rather than requiring an exact name.

Why it is so fast: searching an index, not the disk

time locate nginx.conf
# real    0m0.012s

time find / -name "nginx.conf" 2>/dev/null
# real    0m4.328s

The dramatic speed difference comes down to what each tool is actually doing. find walks the live filesystem tree in real time: opening directories, reading their contents, checking each entry, recursing into subdirectories, over and over, all while you wait. locate instead queries a database that was already built in advance, which is a fundamentally faster operation regardless of how many files actually exist on disk.

The database behind locate: updatedb

sudo updatedb

locate’s speed depends entirely on this prebuilt database (commonly stored at /var/lib/mlocate/mlocate.db), which is created and refreshed by a separate program called updatedb. On most distributions, updatedb runs automatically once a day, triggered by a cron job or a systemd timer, so the index stays reasonably current without any manual intervention.

# Check when the database was last updated
ls -l /var/lib/mlocate/mlocate.db

# Force an immediate refresh
sudo updatedb

Why results can be stale

touch /home/colton/brand-new-file.txt
locate brand-new-file.txt
# (nothing found, if updatedb has not run since the file was created)

sudo updatedb
locate brand-new-file.txt
# /home/colton/brand-new-file.txt

This is the fundamental tradeoff for locate’s speed: it reflects the state of the filesystem as of the last updatedb run, not the current live state. A file created moments ago, or deleted moments ago, may not be accurately reflected until the next scheduled update runs, or until you trigger one manually.

Installing locate if it is missing

# Debian / Ubuntu
sudo apt install mlocate

# Fedora / RHEL
sudo dnf install mlocate

# After installing, build the initial database
sudo updatedb

locate returns no results at all on a freshly installed system until a database actually exists, which is why running updatedb once manually right after installation is a necessary first step.

locate -i readme
# matches README, readme, ReadMe, and any other case variation

By default, locate is case-sensitive. -i makes it match regardless of letter case, which is useful since filename capitalization conventions vary widely across different pieces of software and operating systems.

Filtering by count and existence

locate -c nginx.conf
# just a count of matches, not the full list of paths

locate -e nginx.conf
# only shows results for files that STILL actually exist right
# now (locate double-checks the filesystem for each match,
# which is slightly slower but filters out stale entries)

-e (existing) adds a live existence check on top of the database lookup, filtering out any result that the database still lists but which has actually been deleted since the last updatedb run, trading away a small amount of speed for more accurate, current results.

Excluding certain paths from the database

cat /etc/updatedb.conf
# PRUNEPATHS="/tmp /var/tmp /mnt /media"

/etc/updatedb.conf controls what updatedb includes when building its index. Directories listed in PRUNEPATHS are skipped entirely, which is useful for excluding fast-changing, low-value locations like /tmp from ever showing up in locate results, and keeps the database itself smaller and faster to build.

Frequently Asked Questions

What does the locate command do?

locate searches for files by name almost instantly, by querying a prebuilt database (index) of the filesystem instead of scanning the actual disk in real time. Given part of a filename, it returns every path in its database that contains that text, generally within a fraction of a second even on a filesystem with millions of files.

Why is locate so much faster than find?

find walks the actual, live filesystem structure at the moment you run it, checking directory after directory in real time. locate instead searches an index that was already built in advance, similar to searching a book’s index rather than reading every page. Querying a prebuilt index is dramatically faster than a live filesystem walk, especially on systems with a very large number of files, though it comes at the cost of the results potentially being slightly out of date.

What is updatedb and why does locate need it?

updatedb is the program that builds and refreshes the database locate searches against. It typically runs automatically once a day via a scheduled cron job or systemd timer. Without ever running updatedb, locate’s database would never reflect any files created after it was first built, so the periodic refresh is what keeps locate results reasonably current over time.

Why does locate show a file that I just deleted, or not show a file I just created?

This happens because locate searches a cached snapshot of the filesystem rather than checking live disk state. If updatedb has not run since you made the change, the database still reflects how things looked at the time of its last update. Running sudo updatedb manually forces an immediate refresh, after which locate results will reflect your recent changes.

How do I install locate if it is not available on my system?

On Debian and Ubuntu, install the mlocate package with sudo apt install mlocate. On Fedora and RHEL-based systems, it is often already present or installable via sudo dnf install mlocate. After installing, run sudo updatedb once manually to build the initial database, since locate returns no results at all until a database exists.

Can locate search case-insensitively or with more advanced patterns?

Yes, the -i flag makes the search case-insensitive, such as locate -i readme, which matches README, readme, and ReadMe alike. locate also supports basic wildcard-style patterns with -A (all patterns must match) and -0 for null-separated output suited for piping into other tools, though its pattern matching is considerably simpler than find’s more extensive expression syntax.