Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 nginx finds nginx.conf anywhere.
  • Complements find: Use locate for quick lookups and find when you need real-time accuracy or complex rules.

locate is not installed by default everywhere. On Debian/Ubuntu it ships with the plocate or mlocate package (sudo apt install plocate); on Fedora/RHEL it is sudo dnf install plocate. On macOS it is not available — use mdfind or find instead.


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$'
  • -r treats 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 10 stops after ten matches — essential when a fragment like lib matches thousands of paths.
locate -3 .conf
  • Short form of -n 3.

4. Show Only Files You Can Actually Access

locate -A .bashrc
  • -A restricts 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"
  • -e checks 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 locate sees files created since the last rebuild. On systems using plocate this 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 updatedb run are invisible until the index refreshes. Use find when you need certainty.
  • Not installed. On minimal servers locate may be missing; install plocate first.
  • Database permission errors. If updatedb complains, run it with sudo or check that the database path is writable.
  • macOS has no locate. Use mdfind (Spotlight) or find instead.
  • Too many results. A fragment like log can match tens of thousands of paths; add -n or a more specific pattern.

CommandDescription
findReal-time filesystem search with complex criteria
whereisLocate a command’s binary, source, and man page
whichShow the path of an executable in $PATH
lsList directory contents

locate Cheatsheet

CommandDescription
locate nginxFind paths containing the substring
locate -b nginx.confMatch the file name only
locate -r 'pattern$'Regular expression match
locate -n 10 patternStop after 10 results
locate -A patternOnly show accessible, existing paths
locate -e patternOnly show paths that exist on disk
sudo updatedbRebuild the search database