Linux locate Command: Search Indexed File Paths Quickly

The locate command searches a prebuilt database of file paths. It is much faster than walking the filesystem for every query, but recent creations, moves, and deletions may not appear until the database is updated.

This guide explains the basic syntax, key options, practical examples, related commands, common cautions, and frequently asked questions for locate.

What Is the locate Command?

Depending on the distribution, the command may be provided by plocate, mlocate, or GNU locate. A scheduled service or updatedb creates the index. Database locations, permission filtering, pattern rules, and available options therefore vary by implementation.

Basic Syntax

locate [OPTION]... PATTERN...

Options and edge cases can vary by implementation and version. Check locate --help and man locate on the target system before relying on implementation-specific behavior.

Basic locate Usage

Search for part of a path

A plain string usually matches anywhere in an indexed full path.

locate nginx.conf

Ignore letter case

Use -i on implementations that support case-insensitive matching.

locate -i readme.md

Require existing paths

The -e or --existing option checks that an indexed result still exists.

locate -e '*.service'

Why fast search results do not guarantee the latest files

locate searches a prebuilt path index instead of scanning the disk directly. On a system whose index includes the file, the following query can find it:

locate /etc/passwd

Example output:

/etc/passwd

New files may not appear until the index is updated, and files that have already been deleted may still appear. If there are no results, check the index status and the search pattern, and if the current existence of files is important, recheck with find.

Key Options

Option Purpose
-i Ignore case while matching.
-e, --existing Print only paths that currently exist.
-b, --basename Match only the final filename component.
-c, --count Print the number of matches instead of the paths.
-l N, --limit N Stop after at most N results.
-r REGEXP Match a regular expression in supported implementations.
-d DBPATH Search a specified database or database list.

Practical Examples

Match a basename more precisely

Basename mode reduces matches caused only by directory components.

locate -b '\hosts'

Count or limit results

Check the result volume before printing a very large path list.

locate -c cache
locate -l 20 cache

Update the system index

A full update can require elevated privileges and significant I/O. Check the distribution's scheduled update policy first.

sudo updatedb

locate and Related Commands

Command Primary role Best fit
locate Indexed path-name search Fast path discovery
find Live filesystem traversal Current state and rich conditions
which PATH executable lookup Command resolution
whereis Known program-related locations Binaries, source, and manuals

Important Cautions

The index can be stale

New files may be missing and deleted paths may remain until the next database update.

Path visibility follows the implementation's policy

Database creation and permission filtering determine which indexed names each user may see.

Check the installed implementation

plocate, mlocate, and GNU locate share many concepts but differ in options and exact pattern semantics.

FAQ

Why is locate not installed?

Some distributions do not install it by default; the package may be named plocate or mlocate.

Why can it not find a file I just created?

The database may not have been updated. Use find for an immediate live search.

Does updatedb always require sudo?

A system-wide database usually requires elevated access, although some implementations support user databases.

Why does locate show a deleted file?

It is a stale index entry. Use -e or update the database.

Related Resources