locate
Find files by name using a prebuilt index, fast but can be stale
By CMD Script Team · 4 min read · Last updated
locate [OPTIONS] PATTERNOptions
| Flag | Description |
|---|---|
-i | Case-insensitive match |
-c | Print only a count of matching entries instead of the entries themselves |
-r | Interpret PATTERN as a basic regular expression instead of a glob-like pattern |
-n | Limit output to at most N results, e.g. locate -n 20 pattern |
-e | Only print entries that still exist on disk at the time of the search |
-A | Only print entries matching all given patterns (AND instead of OR) |
Distribution compatibility
- Ubuntu (mlocate/plocate)
- Debian (mlocate/plocate)
- Fedora (mlocate)
- Arch (plocate/mlocate)
- macOS (locate, database not built by default)
What it does
locate finds files and directories by name almost instantly by searching a prebuilt
index (maintained by mlocate or the newer plocate) instead of walking the live
filesystem the way find does. The tradeoff for that speed is freshness: the index is
only as current as the last time updatedb ran (typically once a day via a cron job or
systemd timer), so very recently created or deleted files may not show up — or may show
up as still existing — until the database is refreshed.
Beginner examples
locate nginx.conf— find any path containing "nginx.conf" anywhere on the indexed filesystemlocate -i readme— case-insensitive search for "readme"locate '*.pdf'— find all indexed paths ending in.pdf(quote the pattern so the shell doesn't expand it first)locate -c error— print just the number of matches instead of listing them
locate httpd.conf
Advanced examples
- Limit the number of results returned:
locate -n 20 backup - Only show results for files that still actually exist on disk right now:
locate -e old-report.csv - Require multiple patterns to all match the same path:
locate -A nginx conf - Use a full regular expression instead of the default glob-like matching:
locate -r '\.log$' - Force a fresh database before searching for a file created moments ago:
sudo updatedb && locate newly-created-file.txt
locate -i -n 15 '*.service'
Common mistakes
- Searching for a file that was just created and assuming
locateis broken when it returns nothing — the database simply hasn't been refreshed yet; runsudo updatedbor usefindfor a live search. - Forgetting to quote glob patterns like
*.log, letting the shell expand them against the current directory before locate ever sees the pattern. - Assuming
locatesees every file on the system regardless of permissions —mlocatefilters results based on what the calling user is allowed to read, and some paths may be excluded from indexing entirely (e.g. viaPRUNEPATHSinupdatedb.conf). - Relying on
locatefor security-sensitive existence checks — since the database can be stale, a missing or present result isn't a guarantee about the current state of the filesystem.
Tips
- Run
sudo updatedbmanually right before alocatesearch if you know the file you need was created very recently. - Use
locate -iliberally — filename casing is often inconsistent, and case- insensitive search avoids missing an otherwise-correct match. - For directories intentionally excluded from indexing (like
/tmpor removable media in many default configs), you'll needfindinstead, sincelocatewill never see files there.
Best practices
- Reach for
locatefirst when you need a fast name-based search of a whole system and can tolerate the index being up to a day stale; fall back tofindwhen you need guaranteed real-time accuracy or content/attribute-based filtering (size, mtime, permissions). - If
locateseems to be missing recent files regularly, check that theupdatedbtimer/cron job is actually enabled and running on schedule. - Don't rely on
locatealone for security auditing of file existence; combine it withfindor direct checks when correctness matters more than speed.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Quickly finding the full path of a configuration file you know the name of but not
the location, e.g.
locate httpd.conf. - Auditing a system for leftover files matching a pattern (like old log or backup
files) across the entire filesystem in a fraction of the time
find /would take. - Locating all instances of a particular library or binary across a large filesystem during troubleshooting.
Common interview questions
- Why might locate fail to find a file that clearly exists? Because
locatesearches a prebuilt database that's only updated periodically (viaupdatedb), so a very recently created file won't appear until the next database refresh. - How does locate achieve its speed advantage over find? It performs a lookup
against a precomputed, indexed database of paths, avoiding the filesystem traversal
that
findperforms live at query time. - How do you force the locate database to reflect the current filesystem state?
Run
sudo updatedbto rebuild the index immediately instead of waiting for its scheduled run.
Frequently Asked Questions
Why does locate return nothing for a file I know exists?
locate searches a prebuilt database (usually maintained by mlocate or plocate), not the live filesystem. If the file was created after the last database update, it won't show up until updatedb runs again, either via its daily cron/systemd timer or manually.
How do I update the locate database manually?
Run sudo updatedb — this rescans the filesystem (respecting its configured exclusions) and rebuilds the database that locate searches against.
Why is locate so much faster than find for name searches?
find walks the live filesystem tree at the time you run it, checking every directory; locate just looks up matches in a precomputed, indexed database, which is dramatically faster but reflects the filesystem as of the last updatedb run rather than right now.
Why doesn't locate show files in my home directory that I expect it to find?
Most locate configurations (mlocate/plocate) run updatedb as a specific system user and may skip or restrict some directories for privacy/performance, and they also only show files that the invoking user has permission to see (mlocate specifically checks file readability).
Cheat sheet
Download a quick-reference cheat sheet for locate.