locate Command in Linux: Syntax, Options & Practical Examples (plocate)

locate finds file paths by name using a prebuilt index instead of walking the disk. On Ubuntu 25.04 the locate name is a symlink to plocate; searches are fast but you need updatedb when files were added or removed recently.

Published

Updated

Read time 8 min read

Reviewed byDeepak Prasad

locate Command in Linux: Syntax, Options & Practical Examples (plocate)
About locate finds file paths by name using a prebuilt index instead of walking the disk. On Ubuntu 25.04 the locate name is a symlink to plocate; searches are fast but you need updatedb when files were added or removed recently.
Tested on Ubuntu 25.04 (Plucky Puffin); plocate 1.1.19 (locate symlink); kernel 7.0.0-27-generic
Package plocate (apt/deb) · plocate (dnf/rpm)
Man page locate(1)
Privilege none for search; root / sudo for updatedb
Distros

Ubuntu 22.04+ and Fedora often ship plocate (/usr/bin/locateplocate).

Real-time tree walk with filters: find.

locate — quick reference

locate on Ubuntu 25.04 runs plocate under the hood. Patterns match anywhere in the path unless you use -b.

When to use Command
Find paths whose names contain a substring locate passwd
Search the full path (default; same as -w) locate -w hosts
Match only the filename portion of each path locate -b passwd
Case-insensitive pattern locate -i PATTERN

Output control

Trim long result lists or feed paths to other tools.

When to use Command
Stop after N matches locate -l 10 PATTERN
Print match count instead of paths locate -c PATTERN
Separate paths with NUL (safe for xargs -0) locate -0 PATTERN
Print paths literally (no extra quoting on a tty) locate -N PATTERN

Pattern types

Substring search is the default. Regex modes are slower but more precise.

When to use Command
Basic substring (default) locate nginx
Basic regular expression locate -r '\.conf$'
Extended regular expression locate --regex 'hosts$'

Database

Results come from /var/lib/plocate/plocate.db on Ubuntu. Refresh after bulk installs or deletes.

When to use Command
Rebuild the index (needs root) sudo updatedb
Search an alternate database file locate -d /path/to/db PATTERN

Help and version

When to use Command
Show built-in usage locate --help
Show plocate version locate --version

locate — command syntax

Synopsis from locate --help on Ubuntu 25.04 (plocate 1.1.19, invoked as locate):

text
plocate [OPTION]... PATTERN...

locate is a symlink to plocate. The index lives under /var/lib/plocate/; sudo updatedb rebuilds it. Searching does not need root; updating the database does.


locate — command examples

Essential Ignore letter case with -i

When you are unsure about capitalization (HOSTS vs hosts), -i matches both.

Run the command:

bash
locate -i -l 3 HOSTS

Sample output:

text
/etc/ghostscript
/etc/hosts
/etc/hosts.allow

The pattern matches any case variant in the indexed paths.

Common Cap how many paths are printed

Long patterns like conf can return thousands of lines. -l stops after N matches.

Run the command:

bash
locate -l 3 hosts

Sample output:

text
/etc/ghostscript
/etc/hosts
/etc/hosts.allow

Combine with grep or a tighter pattern when you still get too many hits.

Common Count matches without listing them

-c is useful in scripts that only need to know whether something is indexed or how common a name is.

Run the command:

bash
locate -c passwd

Sample output:

text
218

The number reflects the database snapshot, not a live file count on disk.

Common Match the filename, not the full path

-b compares the pattern to the basename — handy when a short name appears in many directory paths.

Run the command:

bash
locate -b -l 5 passwd

Sample output:

text
/etc/passwd
/etc/passwd-
/etc/pam.d/chpasswd
/etc/pam.d/passwd
/etc/security/opasswd

Without -b, the same pattern also matches paths where passwd appears only in a parent directory name.

Common Refresh the index with updatedb

Run this after installing packages or deleting trees so locate sees current paths. Ubuntu often schedules updatedb via cron; manual runs help right after big changes.

Run the command:

bash
sudo updatedb

Sample output:

updatedb typically prints nothing on success. On Ubuntu, plocate cron jobs often rebuild the index daily; run it by hand after large package installs or tree deletes so the next locate search matches disk reality.

Advanced Emit NUL-separated paths for xargs

-0 prints each path followed by ASCII NUL instead of newline — safe when filenames contain spaces.

Run the command:

bash
locate -l 2 -0 passwd | od -c

Sample output:

text
0000000   /   e   t   c   /   p   a   s   s   w   d  \0   /   e   t   c
0000020   /   p   a   s   s   w   d   -  \0
0000031

Feed to xargs -0 or while IFS= read -r -d '' path loops without breaking on spaces.


locate — when to use / when not

Use locate whenUse something else when
  • You need a fast name search across the whole indexed filesystem
  • You forgot where a package installed its config or binary
  • Exact path list is enough — size, age, or permissions do not matter
  • Files changed minutes ago and the index is stale → find or sudo updatedb first
  • Search by mtime, size, owner, or permissions → find
  • You must run an action on each match (-delete, -exec) → find
  • Path might not be in the database (new mount, custom PRUNEPATHS) → find

locate vs find

locate (plocate) find
Speed Very fast (indexed lookup) Slower (walks directories)
Freshness Depends on last updatedb Real-time
Search scope Path names in the database Name, type, size, time, owner, …
Privilege User can search; updatedb needs root Often none; some paths need root
Best for "Where is nginx.conf?" "Delete logs older than 7 days"

See the find command for live tree searches and actions on matches.


File discovery and path lookup tools that complement locate.

Command One line
locate Fast indexed name search (this page)
which Resolve a command name in PATH
grep Search inside file contents, not path names
updatedb Rebuild the locate/plocate database (sudo updatedb)

Browse the full index in our Linux commands reference.


locate — interview corner

What is the locate command in Linux?

locate prints file paths from a prebuilt database instead of scanning the disk at search time. On modern Ubuntu, /usr/bin/locate is a symlink to plocate, which stores paths in /var/lib/plocate/plocate.db.

Quick check:

bash
readlink -f "$(which locate)"

Sample output on Ubuntu 25.04:

text
/usr/bin/plocate

A strong answer is:

"locate does fast substring search on an indexed path database. On Ubuntu it's plocate under the hood. It's great for name lookups but can be stale until updatedb runs."

Why is locate faster than find?

find walks directories at request time — cost grows with tree size. locate looks up strings in an on-disk index built by updatedb, so search time stays low even on large servers.

Trade-off: a file created five minutes ago may be missing until the next index rebuild. find always sees the live tree.

A strong answer is:

"locate queries a pre-indexed database; find traverses the filesystem in real time. locate wins on speed; find wins on freshness and rich filters like -mtime or -size."

How do you update the locate database?

Run sudo updatedb as root. On Ubuntu with plocate this rebuilds /var/lib/plocate/plocate.db. Cron often runs it daily; after mass installs or deletes, run it manually.

A strong answer is:

"sudo updatedb rebuilds the plocate database. I run it after major package changes or when locate misses files I know exist."

Why does locate show deleted files or miss new ones?

The database is a snapshot. Deleted files remain until the next updatedb; new files are invisible until indexed. Older mlocate had -e to hide missing paths — plocate 1.1.19 does not offer -e.

Verify a path still exists:

bash
locate -l 1 myfile | xargs -r ls -d

Or use find for authoritative results.

A strong answer is:

"The index lags behind the filesystem. I run updatedb to refresh, or use find when I need real-time accuracy. plocate doesn't have mlocate's -e flag to filter dead paths."

How do you limit locate output?

Use -l LIMIT (plocate) to stop after N matches. -c prints only the count. Plain locate -n was an mlocate habit — on plocate 1.1.19 prefer -l (some builds still accept -n as an alias).

Example:

bash
locate -l 5 .conf

A strong answer is:

"I use -l to cap matches and -c when I only need a count. On plocate the documented flag is -l, not mlocate's -n."

When would you use locate -r or --regex?

Default locate does substring matching — locate log hits dialog and /var/log. -r (basic) or --regex (extended) let you anchor patterns, e.g. end with .conf:

bash
locate --regex 'nginx\.conf$'

Regex mode is slower on huge databases but cuts noise.

A strong answer is:

"Substring locate is quick but loose. I use -r or --regex when I need anchors or precise patterns — like files ending in .service under systemd paths."


Troubleshooting

Symptom Likely cause Fix
locate: command not found plocate package not installed sudo apt install plocate (Ubuntu/Debian)
No results for a file you just created Stale database sudo updatedb, then search again
Paths listed but file is gone Index not rebuilt after delete sudo updatedb or verify with test -e path
Too many matches Broad substring Tighten pattern, use -b, -l, or --regex
invalid option -- 'n' on some hosts Prefer -l on plocate locate -l 10 PATTERN per locate --help
tar or script cannot find updatedb plocate not fully installed sudo apt install plocate; which updatedb

References

Rohan Timalsina

is a technical writer and Linux enthusiast who writes practical guides on Linux commands and system administration. He focuses on simplifying complex topics through clear explanations.