locate – find files by name quickly

locate finds files by name using a pre-built database, making it much faster than find.
Synopsis
locate [OPTIONS] PATTERN
Common Options
| Option | Description |
|---|---|
-i | Case-insensitive |
-n N | Limit to N results |
-c | Count matches only |
-e | Only show existing files |
-r | Use regex pattern |
Examples
Basic search
$ locate nginx.conf
/etc/nginx/nginx.conf
/etc/nginx/nginx.conf.bak
Case-insensitive
$ locate -i readme
/home/greys/README.md
/usr/share/doc/bash/README
Limit results
$ locate -n 5 "*.log"
Count matches
$ locate -c "*.py"
1234
Pattern matching
$ locate "*/bin/python*"
/usr/bin/python3
/usr/bin/python3.10
Update the Database
The database must be updated to find new files:
$ sudo updatedb
Typically runs automatically via cron, but run manually after adding files.
locate vs find
| Feature | locate | find |
|---|---|---|
| Speed | Very fast | Slow (scans filesystem) |
| Freshness | May be outdated | Always current |
| Features | Name only | Name, size, time, perms |
| New files | Need updatedb | Finds immediately |
# locate is faster for name searches
$ locate nginx.conf # Instant
# find is needed for complex queries
$ find / -name "*.log" -mtime -1 # Modified today
Configuration
Database location: /var/lib/mlocate/mlocate.db
Config: /etc/updatedb.conf
Tips
- Run updatedb after installing: New files won’t appear until then
- Use find for recent files: locate database may be hours old
- Install if missing:
apt install mlocateorplocate - Faster alternative:
plocateis newer and faster






