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

OptionMeaning
-iCase-insensitive search
-cPrint only the number of matches
-n NLimit output to the first N matches
-bMatch only the basename, not the full path
--regexTreat the pattern as a regular expression
-eOnly 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

 
 

 
 

Common Use Cases

Use Case Command
Find all Python binaries locate python
Find all .conf files locate .conf
Find files in /etc directory locate /etc
Find VS Code files locate code
Show only .sh scripts locate --regex '\.sh
Use regex to match .log files locate --regex '\.log
Case-insensitive search for a config locate -i nginx.conf
Count how many matches exist locate -c python

Update the Database

sudo updatedb

Use this after adding/removing files to make sure locate returns fresh results. The daily systemd timer or cron job does this automatically, but there is always a delay between a file appearing and it being indexed.


Common Problems

Problem 1: "locate: command not found"

Cause: mlocate/plocate is not installed.

Solution: sudo apt install plocate (modern default) or mlocate, then sudo updatedb.

Problem 2: newly created files are not found

Cause: the database has not been refreshed since the file was created. This is the most common "locate is broken" moment — it is working as designed.

Solution: sudo updatedb, then retry. For files that must be findable immediately, use find.

Problem 3: results include files that no longer exist

Cause: the database lists paths from before they were deleted.

Solution: rerun sudo updatedb, or use locate -e to filter to existing paths only.

Problem 4: "updatedb: command not found"

Cause: on Debian-based systems updatedb is provided by mlocate or plocate, which may not be installed.

Solution: install the package first: sudo apt install plocate.


Tips


locate vs find

Feature locate find
Speed Very fast Slower (scans in real time)
Up-to-date No (needs updatedb) Yes
Supports filters Needs grep or --regex Yes (-name, -type, etc.)
Filters by type/size/date No Yes
Needs permissions Index is readable by all users Respects filesystem permissions

Conclusion

Use locate when you know part of a name and want the answer instantly; use find when you need current results or complex filters. The two are complementary, and remembering to run sudo updatedb after creating files keeps locate trustworthy.


A.M. Rinas

Written by A.M. Rinas

Full-Stack Software Engineer working on web applications, DevOps automation, and databases. More about me on the about page, or say hi through the contact page and LinkedIn.

← Back to all articles