locate – find files by name quickly

locate command
locate command

locate finds files by name using a pre-built database, making it much faster than find.

Synopsis

locate [OPTIONS] PATTERN

Common Options

OptionDescription
-iCase-insensitive
-n NLimit to N results
-cCount matches only
-eOnly show existing files
-rUse regex pattern

Examples

$ 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

Featurelocatefind
SpeedVery fastSlow (scans filesystem)
FreshnessMay be outdatedAlways current
FeaturesName onlyName, size, time, perms
New filesNeed updatedbFinds 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 mlocate or plocate
  • Faster alternative: plocate is newer and faster

See Also

  • find — Search filesystem directly
  • updatedb — Update locate database

Tutorials