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 traceroute Command: Trace Routers on the Way to a Host

Linux traceroute Command: Trace Routers on the Way to a Host

Learn how to trace Routers on the Way to a Host with the Linux traceroute command, including practical examples, key options, and important precautions.

Linux stat Command: Display Detailed File Metadata

Linux stat Command: Display Detailed File Metadata

Learn how to use the Linux stat command to inspect file size, permissions, ownership, timestamps, inode, and filesystem metadata, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux Tutorial / Understand systemd Services, Boot, and Logs

Linux Tutorial / Understand systemd Services, Boot, and Logs

Understand Linux boot stages and systemd units, distinguish active services from boot enablement, and inspect current-boot logs with systemctl and journalctl.

Linux nslookup Command: Query DNS Names and Records

Linux nslookup Command: Query DNS Names and Records

Learn how to query DNS Names and Records with the Linux nslookup command, including practical examples, key options, and important precautions.

Linux last Command: Review Login and Reboot History

Linux last Command: Review Login and Reboot History

Learn how to use the Linux last command to review user login sessions, system reboots, and shutdown history, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux machine-id: How to Check and Reset It for Cloned Systems

Linux machine-id: How to Check and Reset It for Cloned Systems

Learn what the Linux machine-id identifies, how to check it, and how to prepare cloned system images for unique IDs. Understand the difference between an empty file and first-boot initialization.

Linux sed Command: Search and Replace Text Streams

Linux sed Command: Search and Replace Text Streams

Learn how to search and Replace Text Streams with the Linux sed command, including practical examples, key options, and important precautions.

Linux gzip Command: Compress a File in gzip Format

Linux gzip Command: Compress a File in gzip Format

Learn how to compress a File in gzip Format with the Linux gzip command, including practical examples, key options, and important precautions.

Linux apropos Command: Search Commands by Description

Linux apropos Command: Search Commands by Description

Learn how to use the Linux apropos command to find relevant manual pages by searching description keywords, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux more Command: View Text One Screen at a Time

Linux more Command: View Text One Screen at a Time

Learn how to use Linux more to read text one screen at a time, move and search interactively, start at a line or pattern, and choose between more and less.