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

More in This Category
Linux hostnamectl Command: Show and Change the Host Name

Linux hostnamectl Command: Show and Change the Host Name

Learn how to show and Change the Host Name with the Linux hostnamectl command, including practical examples, key options, and important precautions.

Linux mount Command: Inspect and Mount Filesystems

Linux mount Command: Inspect and Mount Filesystems

Learn how to inspect and Mount Filesystems with the Linux mount command, including practical examples, key options, and important precautions.

Linux readlink Command: Inspect Symbolic Link Targets

Linux readlink Command: Inspect Symbolic Link Targets

Learn how to use the Linux readlink command to display symbolic link targets and optionally resolve canonical paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux dnf Command: Manage RPM Distribution Packages

Linux dnf Command: Manage RPM Distribution Packages

Learn how to manage RPM Distribution Packages with the Linux dnf command, including practical examples, key options, and important precautions.

Linux help Command: Read Help for Bash Built-in Commands

Linux help Command: Read Help for Bash Built-in Commands

Learn how to use the Linux help command to read usage information for Bash built-in commands, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux nice Command: Set a New Process's Scheduling Priority

Linux nice Command: Set a New Process's Scheduling Priority

Learn how to set a New Process's Scheduling Priority with the Linux nice command, including practical examples, key options, and important precautions.

Linux pacman Command: Manage Packages on Arch Linux

Linux pacman Command: Manage Packages on Arch Linux

Learn how to manage Packages on Arch Linux with the Linux pacman command, including practical examples, key options, and important precautions.

Linux sync Command: Flush Buffered Writes to Storage

Linux sync Command: Flush Buffered Writes to Storage

Learn how to flush Buffered Writes to Storage with the Linux sync command, including practical examples, key options, and important precautions.

Linux zip Command: Create ZIP Archives

Linux zip Command: Create ZIP Archives

Learn how to create ZIP Archives with the Linux zip command, including practical examples, key options, and important precautions.

Linux Tutorial / Search and Process Text with grep, sort, uniq, cut, and wc

Linux Tutorial / Search and Process Text with grep, sort, uniq, cut, and wc

Practice searching, sorting, grouping, extracting, and counting text with grep, sort, uniq, cut, and wc, including reproducible output and key caveats.