locate Command Cheat Sheet
locate is the fastest way to find a file by name on Linux — it searches a prebuilt index
instead of scanning the disk, so results come back in milliseconds even on huge filesystems. The trade-off
is freshness: the index is only as current as the last updatedb run. This cheat sheet covers
installation, everyday usage, regex patterns, and the problems that make people think locate is broken.
What is locate?
The locate command queries a database of file paths (usually /var/lib/mlocate/mlocate.db) that is rebuilt regularly by updatedb — on most systems a cron job or systemd timer runs it daily.
Installation (if not already installed)
sudo apt update
sudo apt install mlocate -y
sudo updatedb # Manually update the database
Basic Usage
Search for a File or Directory
locate matches the pattern against any part of the full path — no wildcards needed:
locate filename
Example:
locate python
Typical output (every path containing "python"):
/usr/bin/python3
/usr/bin/python3.12
/usr/lib/python3.12/...
/home/rinas/projects/python-app/main.py
Common Options
| Option | Meaning |
|---|---|
-i | Case-insensitive search |
-c | Print only the number of matches |
-n N | Limit output to the first N matches |
-b | Match only the basename, not the full path |
--regex | Treat the pattern as a regular expression |
-e | Only show paths that still exist |
Pattern Matching & Filtering
Match Full or Partial File Names
locate myfile.txt
locate myfile
locate /etc/hosts
Use Wildcards (with --regex or pipe to grep)
locate by default supports substring matching.
For more powerful matching, use:
locate --regex 'pattern'
Or filter with grep:
locate python | grep '/bin/'
For example, to find every .log file whose path ends with app.log, a regex keeps
the result list short:
locate --regex 'app\.log