๐Ÿ” 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


๐Ÿ†š 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