Skip to main content

Locate Cheatsheet

Syntax

locate [OPTIONS] PATTERN [PATTERN...]

Core Flags

FlagPurpose
-iCase insensitive
-bBasename only (not full path)
-eVerify each result exists right now
-cCount results only (no listing)
-n NShow only first N results
-0Null-byte separated output (safe for pipes)
-r / --regexUse regex instead of substring
-SShow database statistics
-d FILEUse a specific database file

Pattern Tricks

GoalPattern
Substring (default)locate nginx
Exact basenamelocate -b "\nginx.conf"
Regexlocate --regex "\.conf$"
Case insensitivelocate -i readme
Multiple ANDlocate 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.