๐ locate Command Cheat Sheet
๐ What is locate?
The locate command is used to quickly search for files and directories on your system by querying a prebuilt database (updated with updatedb).
๐ ๏ธ 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 filename
Example:
locate python
๐ฏ 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/'
โ 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 .sh$ |
Use regex to match .log files |
locate --regex '\.log$' |
๐ Update the Database
sudo updatedb
Use this after adding/removing files to make sure locate returns fresh results.
โ Tips
locateis faster thanfind, but can be outdated until the nextupdatedbrun.- For real-time search, use
find.
๐ 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.) |
Written by A.M. Rinas