Locate Cheatsheet
Syntax
locate [OPTIONS] PATTERN [PATTERN...]
Core Flags
| Flag | Purpose |
|---|---|
-i | Case insensitive |
-b | Basename only (not full path) |
-e | Verify each result exists right now |
-c | Count results only (no listing) |
-n N | Show only first N results |
-0 | Null-byte separated output (safe for pipes) |
-r / --regex | Use regex instead of substring |
-S | Show database statistics |
-d FILE | Use a specific database file |
Pattern Tricks
| Goal | Pattern |
|---|---|
| Substring (default) | locate nginx |
| Exact basename | locate -b "\nginx.conf" |
| Regex | locate --regex "\.conf$" |
| Case insensitive | locate -i readme |
| Multiple AND | locate nginx conf |
Quick Reference Recipes
# Find php.ini anywhere
locate php.ini
# Only show files that actually exist
locate -e nginx.conf
# Exact file name (not substring)
locate -b "\php.ini"
# Case insensitive
locate -i "README"
# Count results
locate -c "*.log"
# Limit output
locate -n 10 "config"
# Regex: files ending in .yml or .yaml
locate --regex "\.(yml|yaml)$"
# After installing: refresh database
sudo updatedb
# Safe pipeline (handles spaces in paths)
locate -0 -e "*.conf" | xargs -0 grep "listen"
# Find where a binary lives
locate -b "\ffmpeg" | grep "/bin/"
# Database stats
locate -S
The Freshness Rule
Always remember
If you just created a file and locate can't find it, run sudo updatedb first. If you need 100% real-time accuracy, use find or fd instead.