locate Command Guide for Fast Filename Lookups
This guide helps you understand and use the locate command, which finds files by name almost instantly by searching a prebuilt database instead of walking the disk.
What is locate, and Why Use It?
The locate command (short for “list contents of directories”) prints the paths of files whose names match your pattern. It reads from a database that is rebuilt periodically, which makes it dramatically faster than find for simple name searches.
Why Learn locate?
- Speed: Results appear instantly, even on a machine with millions of files.
- Simple syntax: One argument and you are done — no flags, no patterns to quote.
- Partial matches: It matches substrings by default, so
locate nginxfindsnginx.confanywhere. - Complements
find: Uselocatefor quick lookups andfindwhen you need real-time accuracy or complex rules.
locateis not installed by default everywhere. On Debian/Ubuntu it ships with theplocateormlocatepackage (sudo apt install plocate); on Fedora/RHEL it issudo dnf install plocate. On macOS it is not available — usemdfindorfindinstead.
1. Search by Name Fragment
locate nginx
- Prints every path containing the substring
nginx.
Example:
locate hosts
/etc/hosts
/private/etc/hosts
/usr/local/etc/hosts
2. Require Exact Words or Whole Names
locate -b nginx.conf
-b(basename) matches only the file name, not directory components.
locate -r 'nginx\.conf$'
-rtreats the argument as a regular expression, anchored with$for an exact end-of-name match.
3. Limit the Number of Results
locate -n 10 config
-n 10stops after ten matches — essential when a fragment likelibmatches thousands of paths.
locate -3 .conf
- Short form of
-n 3.
4. Show Only Files You Can Actually Access
locate -A .bashrc
-Arestricts results to paths that exist on the current machine and are accessible to you. Without it, the database may list files that were deleted or belong to other users.
5. Restrict to Specific File Types
locate -e "*.jpg"
-echecks that each listed path still exists on disk. Combine with wildcards for “images that actually exist”.
6. Update the Database
sudo updatedb
- Rebuilds the index so
locatesees files created since the last rebuild. On systems usingplocatethis runs automatically via a timer; on older systems you may need to run it manually after installing software.
Common Pitfalls
- Stale results. Files created after the last
updatedbrun are invisible until the index refreshes. Usefindwhen you need certainty. - Not installed. On minimal servers
locatemay be missing; installplocatefirst. - Database permission errors. If
updatedbcomplains, run it withsudoor check that the database path is writable. - macOS has no
locate. Usemdfind(Spotlight) orfindinstead. - Too many results. A fragment like
logcan match tens of thousands of paths; add-nor a more specific pattern.
Related Commands
| Command | Description |
|---|---|
find | Real-time filesystem search with complex criteria |
whereis | Locate a command’s binary, source, and man page |
which | Show the path of an executable in $PATH |
ls | List directory contents |
locate Cheatsheet
| Command | Description |
|---|---|
locate nginx | Find paths containing the substring |
locate -b nginx.conf | Match the file name only |
locate -r 'pattern$' | Regular expression match |
locate -n 10 pattern | Stop after 10 results |
locate -A pattern | Only show accessible, existing paths |
locate -e pattern | Only show paths that exist on disk |
sudo updatedb | Rebuild the search database |