LinuxCommandLibrary

locate

Find files by name quickly

TLDR

Look for pattern in the database. Note: The database is recomputed periodically (usually weekly or daily)

$ locate [pattern]
copy

Look for a file by its exact filename (a pattern containing no globbing characters is interpreted as *pattern*)
$ locate '*/[filename]'
copy

Recompute the database. You need to do it if you want to find recently added files
$ sudo updatedb
copy

SYNOPSIS

locate [OPTION]... PATTERN...

PARAMETERS

-b, --basename
    Match only the base name of path names.

-c, --existing
    Only list entries that currently exist on the filesystem. This performs a stat() check for each database entry.

-i, --ignore-case
    Ignore case distinctions when matching patterns.

-n NUM, --limit=NUM
    Exit after finding NUM successful matches.

-r, --regex
    Interpret patterns as basic regular expressions (BREs).

-0, --null
    Separate entries with a null character, useful for scripting.

-q, --quiet
    Suppress error messages.

-V, --version
    Display version information and exit.

-h, --help
    Display help information and exit.

DESCRIPTION

The locate command is a utility on Unix-like operating systems used for quickly finding files by name. Unlike the find command, which searches the filesystem in real-time, locate operates by querying a pre-built database of all files on the system. This database is typically created and updated by the updatedb command, often run periodically via a cron job (e.g., daily).

The primary advantage of locate is its speed. Because it doesn't need to traverse the filesystem, it can return results almost instantly, even on systems with a vast number of files. This makes it ideal for quick checks when you know part of a filename. However, its main limitation is that the results are only as current as the last time the database was updated. If files have been created, deleted, or renamed since then, locate will not reflect those changes. Additionally, since it reads from a global database, it can show paths to files that the current user might not have permission to access, which can be a privacy concern for some.

CAVEATS

Database Freshness: The results from locate are only as up-to-date as the last time the updatedb command was run. Newly created, deleted, or renamed files will not be reflected until the database is updated.

Security and Privacy: locate reads from a global database, which typically contains paths to all files on the system, regardless of the current user's permissions. This means it can list files or directories that the user might not actually be able to access, potentially revealing sensitive file paths.

Permissions during indexing: If updatedb was run with restricted permissions, the database might not contain all files on the system.

DATABASE LOCATION

The default location for the locate database is commonly /var/lib/mlocate/mlocate.db or similar paths like /var/lib/locate/locate.db. The exact path can vary depending on the system and locate implementation.

DATABASE UPDATES

The database is typically updated by the updatedb command, usually executed nightly via a system cron job (e.g., within /etc/cron.daily/ or a similar directory). Users can also run sudo updatedb manually to force an immediate update.

HISTORY

The locate command has been a staple utility in Unix-like operating systems for decades, providing a fast alternative to real-time filesystem searches. Its development is closely tied to the findutils package (or mlocate for newer, more efficient versions), which aims to provide fundamental file search utilities. The efficiency of locate, relying on a pre-built index, was particularly advantageous in older computing environments where disk I/O was a significant bottleneck. While its core functionality remains consistent, implementations like mlocate have introduced optimizations for larger databases and modern file systems.

SEE ALSO

find(1), updatedb(8), which(1), whereis(1)

Copied to clipboard