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.

Introduction

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 updatedb

Syntax

locate [OPTIONS] PATTERN

The 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

OptionDescription
-iCase-insensitive search
-cCount the number of matches instead of listing them
-l nLimit output to n results
-rUse a regular expression as the pattern
-eOnly return files that currently exist on disk
-bMatch only the basename (not the full path)
-wMatch only whole path components
--databaseUse 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.conf

Common 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).

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