Quick take: locate filename searches a prebuilt database and returns results nearly instantly. Run sudo updatedb to refresh the database when files have been added or removed since the last daily update.
What Is the locate Command in Linux?
The locate command finds files by searching a prebuilt index of the filesystem rather than scanning directories in real time. This makes it dramatically faster than find for full-filesystem searches — a search that would take find minutes returns with locate in under a second.
The trade-off is currency: the database is rebuilt by updatedb, which runs automatically daily on most systems. Files created or deleted since the last update will be missing or still listed.
Install locate
# Ubuntu / Debian (classic)
sudo apt install mlocate
# Ubuntu 22.04+ (faster plocate, recommended)
sudo apt install plocate
# RHEL / CentOS / Rocky
sudo dnf install mlocate
# Build the initial database after install
sudo updatedbSyntax
locate [OPTIONS] PATTERNThe pattern is matched as a substring anywhere in the full file path. locate nginx matches /etc/nginx/nginx.conf, /usr/sbin/nginx, and /var/log/nginx/.
Common Options
| Option | Description |
|---|---|
| -i | Case-insensitive search |
| -c | Count the number of matches instead of listing them |
| -l n | Limit output to n results |
| -r | Use a regular expression as the pattern |
| -e | Only return files that currently exist on disk |
| -b | Match only the basename (not the full path) |
| -w | Match only whole path components |
| --database | Use a custom database file instead of the default |
Practical Examples
# Find all files containing 'nginx' in their path
locate nginx
# Case-insensitive search
locate -i SSH_CONFIG
# Find only files named exactly 'nginx.conf' (basename match)
locate -b '\nginx.conf'
# Count how many files match
locate -c '*.conf'
# Limit to first 10 results
locate -l 10 python
# Only show results that still exist on disk (removes stale entries)
locate -e '*.log'
# Use regex pattern
locate -r '\.py$'
# Find PHP files under /var/www
locate /var/www | grep '\.php$'The backslash in '\nginx.conf' is a plocate/mlocate extension that disables substring matching on the basename — it matches only the exact basename nginx.conf.
updatedb — Refresh the Database
updatedb rebuilds the file path database that locate reads. It scans the filesystem and writes a compressed index.
# Rebuild the database (requires root)
sudo updatedb
# Check when the database was last updated
stat /var/lib/mlocate/mlocate.db
# Exclude certain paths from indexing (edit the config)
# /etc/updatedb.conf — PRUNEPATHS and PRUNEFS control exclusions
cat /etc/updatedb.confCommon directories excluded by default: /proc, /sys, /tmp, network mounts. Edit /etc/updatedb.conf to add more exclusions if your server has large scratch directories you never need to search.
locate vs find
Use locate when: you need fast results, you are searching the whole system for a file you know exists, and freshness is not critical.
Use find when: you need current results (files created minutes ago), you need to filter by size, permissions, or modification time, or you need to execute actions on the matched files.
A practical pattern: use locate for discovery (find the path), then use find for precision (filter to the exact file).
plocate vs mlocate — Which Should You Use?
Ubuntu 22.04+ ships plocate as the default, replacing the older mlocate. plocate uses a compressed trigram index that is 10–20x faster than mlocate's linear scan for substring searches, and the database is significantly smaller.
| Feature | mlocate | plocate |
|---|---|---|
| Index type | Sorted path list | Trigram index |
| Typical search time | 100–500ms | 5–30ms |
| Database size | Larger | 30–50% smaller |
| updatedb speed | Slower | Faster |
| Ubuntu default | Up to 20.04 | 22.04+ |
| Command compatibility | Full POSIX locate | Full mlocate compatibility |
Both use the same command syntax and the same updatedb configuration at /etc/updatedb.conf. If you have an existing script using locate, it works without changes after switching to plocate.
Configuring updatedb.conf — Excluding Directories
The /etc/updatedb.conf file controls what updatedb indexes. Large directories full of generated files (build artifacts, container layers, node_modules) should be excluded to keep the database small and updatedb fast.
cat /etc/updatedb.conf
PRUNE_BIND_MOUNTS="yes"
PRUNEPATHS="/tmp /var/spool /media /var/lib/os-prober /var/lib/ceph"
PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs shfs sysfs cifs lustre tmpfs usbfs udf fuse.glusterfs fuse.sshfs curlftpfs ecryptfs fusesmb devtmpfs"
# Add custom exclusions (edit the file)
sudo nano /etc/updatedb.conf
# Add to PRUNEPATHS (space-separated):
# PRUNEPATHS="/tmp /var/spool ... /var/lib/docker /home/user/node_modules"
# Rebuild after changing config
sudo updatedb
Using locate in Shell Scripts
#!/bin/bash
# Find all .conf files for a service and check if they exist on disk
SERVICE="nginx"
echo "Config files for $SERVICE:"
locate -e "$SERVICE" | grep "\.conf$" | while read -r file; do
if [ -r "$file" ]; then
echo " [readable] $file"
else
echo " [no read] $file"
fi
done
# Count total indexed paths
TOTAL=$(locate -c "")
echo "Total indexed paths: $TOTAL"
Common Errors and Fixes
| Error | Cause | Fix |
|---|---|---|
| "database not found" or no output after install | updatedb has not run yet | Run sudo updatedb manually |
| Recently created files not found | Database is stale (updated daily) | Run sudo updatedb to refresh |
| Permission denied on locate results | File exists but you cannot read it | Use sudo locate or run locate -e to verify existence |
| locate: command not found | Neither mlocate nor plocate installed | sudo apt install plocate |
Reference: locate man page — man7.org. Tested on Ubuntu 22.04 LTS with plocate 1.1.15.
Final Thoughts
locate is the fastest way to find a file when you know part of its name and do not need real-time accuracy. Add sudo updatedb to your workflow after major file operations so searches remain useful. For time-sensitive or attribute-based searches, find is the right tool.
FAQ: locate Command in Linux
How do I install locate on Ubuntu?+
Run sudo apt install mlocate. After installation, run sudo updatedb to build the initial database. On modern Ubuntu, plocate is the newer faster alternative: sudo apt install plocate.
Why does locate not find files I just created?+
locate reads from a prebuilt database that is only updated by updatedb. Files created after the last database update will not appear. Run sudo updatedb manually to refresh.
What is the difference between locate and find?+
locate searches a prebuilt database and returns results instantly, but the database may be stale. find scans the filesystem in real-time and is always current, but is much slower for broad searches.
How do I search case-insensitively with locate?+
Use locate -i pattern. This matches regardless of uppercase or lowercase, so locate -i nginx.conf finds nginx.conf, NGINX.CONF, Nginx.Conf, and any other capitalisation.
How often does updatedb run automatically?+
On most Linux distros, updatedb runs daily via a cron job or systemd timer (usually at night). You can check with systemctl list-timers | grep updatedb or check /etc/cron.daily/.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support