locate — quick reference
Basic search
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):
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 Search for a filename substring
Use locate when you know part of a name and want every indexed path that contains it — much faster than find / on large disks.
Run the command:
locate -l 5 passwdSample output:
/etc/passwd
/etc/passwd-
/etc/pam.d/chpasswd
/etc/pam.d/passwd
/etc/security/opasswdRaise or remove -l when you need more rows. Paths come from the last updatedb run, not a live disk walk.
Essential Ignore letter case with -i
When you are unsure about capitalization (HOSTS vs hosts), -i matches both.
Run the command:
locate -i -l 3 HOSTSSample output:
/etc/ghostscript
/etc/hosts
/etc/hosts.allowThe 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:
locate -l 3 hostsSample output:
/etc/ghostscript
/etc/hosts
/etc/hosts.allowCombine 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:
locate -c passwdSample output:
218The 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:
locate -b -l 5 passwdSample output:
/etc/passwd
/etc/passwd-
/etc/pam.d/chpasswd
/etc/pam.d/passwd
/etc/security/opasswdWithout -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:
sudo updatedbSample 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 Anchor a regex to exact path endings
-r uses basic regex; --regex uses extended regex. Anchors reduce noise when many paths share a substring.
Run the command:
locate -r -l 3 '/bin/bash$'Sample output:
/snap/core22/2133/usr/bin/bash
/snap/core22/2411/usr/bin/bash
/snap/core24/1643/usr/bin/bashRegex search scans the database differently from plain substring mode and can be slower on huge indexes.
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:
locate -l 2 -0 passwd | od -cSample output:
0000000 / e t c / p a s s w d \0 / e t c
0000020 / p a s s w d - \0
0000031Feed to xargs -0 or while IFS= read -r -d '' path loops without breaking on spaces.
locate — when to use / when not
| Use locate when | Use something else when |
|---|---|
|
|
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.
Related commands
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:
readlink -f "$(which locate)"Sample output on Ubuntu 25.04:
/usr/bin/plocateA 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:
locate -l 1 myfile | xargs -r ls -dOr 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:
locate -l 5 .confA 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:
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 |
