Linux locate Command: File Search 100x Faster Than find#

When searching for files on Linux servers, most people reach for find first. But if you’ve ever run find / -name "nginx.conf" on a system with millions of files, you know the pain of waiting. Let’s talk about locate — an underrated file search tool that’s dramatically faster.

Why is locate So Fast?#

find traverses the filesystem in real-time, checking every directory and file. locate works differently: it maintains a database of file paths. Searching is just a database lookup, essentially a string match operation.

find / -name "*.conf"     # Real-time traversal, could take minutes
locate "*.conf"           # Database lookup, done in 0.1 seconds

The speed difference can be 100x or more.

Where Does the Database Come From?#

The locate database is maintained by updatedb. Most Linux distributions update it automatically via cron, usually once daily:

# Check database update time
locate -S
# Sample output:
# Database /var/lib/mlocate/mlocate.db:
#   1,234,567 files
#   56,789 directories
#   78,901,234 bytes in file names
#   12,345,678 bytes used to store database
#   Last updated: 2026-05-06 02:00:00

To manually update the database (after installing new software, for example):

sudo updatedb

The update typically takes seconds to tens of seconds, depending on filesystem size.

Basic Usage#

Search by Filename#

locate nginx.conf
# Output:
# /etc/nginx/nginx.conf
# /etc/nginx/sites-available/nginx.conf
# /home/user/projects/nginx.conf
locate -i README
# Matches README, readme, ReadMe, etc.

Limit Result Count#

When there are too many results, use -n:

locate -n 5 "*.log"
# Only shows the first 5 results

Use Regular Expressions#

The -r flag enables regex:

locate -r "\.sh$"
# Find all files ending with .sh

locate -r "^/home.*\.conf$"
# Find all .conf files in /home directory

Count Matches#

When you only want the count, not the paths:

locate -c "*.py"
# Output: 15234

Advanced Techniques#

Match Basename Only (Not Full Path)#

By default, locate matches the full path. For example, locate conf would match /etc/nginx/conf.d/. To match only the filename:

locate -b conf
# Only matches files with "conf" in their name, not directory names in the path

Show Only Existing Files#

The database isn’t real-time and may contain deleted files. Use -e to filter:

locate -e nginx.conf
# Only outputs files that still exist in the filesystem

This option makes locate slightly slower since it verifies file existence, but still much faster than find.

View Database Statistics#

locate -S

This shows:

  • Number of files and directories in the database
  • Database size
  • Last update time

Specify Database File#

If you have multiple databases (for different partitions, for example):

locate -d /path/to/db.db pattern

locate vs find: When to Use Which?#

Scenario Recommended Command Reason
Quick config file lookup locate Fast, config locations are stable
Find recently modified files find -mtime locate database has lag
Filter by permission, size, time find locate only matches paths
Find just-created files find Database hasn’t updated yet
Search by file content grep -r Neither supports content search

Simple rule:

  • Search by filename/path only → locate
  • Filter by attributes (time, size, permissions) → find
  • Search by content → grep

Configuration Tuning#

locate config is typically at /etc/updatedb.conf:

PRUNE_BIND_MOUNTS="yes"
PRUNEPATHS="/tmp /var/spool /media /var/cache /run"
PRUNEFS="NFS nfs nfs4 rpc_pipefs autofs"
  • PRUNEPATHS: Directories to exclude from indexing
  • PRUNEFS: Filesystem types to exclude

If your project directory is at /home/projects and you keep getting noise from /tmp and /var/cache, you could adjust PRUNEPATHS (though frequent modifications aren’t recommended).

Common Issues#

Can’t Find Recently Created Files#

The database hasn’t updated yet. Run:

sudo updatedb

Permission Issues#

The locate database typically only contains files accessible to the current user. Regular users running locate might miss root-owned files. Use sudo locate for full access.

Database Corruption#

Rarely, the database can get corrupted. Symptoms include search failures or abnormal results. Fix it:

sudo rm /var/lib/mlocate/mlocate.db
sudo updatedb

Practical Examples#

Find All Java Versions on the System#

locate -r "/bin/java$" | xargs -I {} {} -version

Count Files by Type#

for ext in py js ts go java; do
  echo -n "$ext: "
  locate -c ".$ext$"
done

Find Config File and View Content#

locate -n 1 nginx.conf | xargs cat

Summary#

locate’s core advantage is speed, especially significant on large filesystems. The downside is it’s not real-time — recently created files might not be found.

Recommended usage:

  1. Quick config/log file lookup → locate
  2. Need precise filtering → find
  3. Search code content → grep -r or ripgrep

Add locate to your toolkit. Next time you need to find a file, you’ll save precious time.


Related: Linux find Command Guide | Linux grep Command Guide | Linux which Command Guide