locate Command Cheat Sheet
locate finds files by name. It is much faster than find because it searches a pre-built database (mlocate.db) instead of crawling the disk.
Synopsis
locate [OPTION]... [PATTERN]...
Basic Usage
Find a File
locate myfile.txt
Case Insensitive (-i)
locate -i MyFile.txt
Advanced Search
Limit Output (-n)
Show only the first 5 results.
locate -n 5 *.conf
Regular Expressions (-r)
Use regex for complex patterns.
locate -r '^/usr/.*\.jpg$'
Exact Match
By default locate matches substring (e.g., bin matches /bin, /usr/bin, binary).
To match exact filename base:
locate -b '\filename'
Database Management
Update Database
If you just created a file, locate won't find it until the DB updates.
sudo updatedb
Check Existing Files (-e)
Since locate reads a DB, it might list files that were deleted. Use -e to check if file actually exists right now.
locate -e deleted_file
Statistics
See how many files are indexed.
locate -S
Notes
- Security:
updatedbusually runs as root butlocaterespects permissions (won't show files you can't read) if configured correctly, but purelyslocate/mlocateimplementations vary. - Config:
/etc/updatedb.confcontrols what paths are pruned (ignored), like/tmpor/mnt.