Linux Command Reference ยท Viewing and Searching Files
Linux locate command
Reference for the Linux locate command and updatedb: search a prebuilt filename database instantly, limit and filter results, and know when it goes stale.
locate finds files by name in a prebuilt database instead of walking the disk. That makes it far faster than find on big filesystems: a search over millions of paths takes a fraction of a second. The cost is that its answer is only as fresh as the last database update, usually a nightly job running updatedb.
It matches a substring (or pattern) against the whole path, not just the file name.
locate [OPTION]... PATTERN... sudo updatedb
Try it
locate is not installed in our sandbox, and the database needs root to build, so this block is read-only:
$ locate nginx.conf /etc/nginx/nginx.conf /usr/share/doc/nginx/examples/nginx.conf $ locate -c '*.service' 312 $ locate -i readme | head -3 /usr/share/doc/bash/README.gz /usr/share/doc/coreutils/README.Debian /home/ann/project/Readme.md
What locate replaces can be shown with find, which is installed:
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
locate [-i] [-b] [-c] [-e] [-l N] [-r REGEX] [-S] PATTERN... updatedb [OPTIONS]
| Option | Description |
|---|---|
-i, --ignore-case |
Ignore case. |
-b, --basename |
Match the base name only. |
-c, --count |
Print the number of matches instead of the names. |
-e, --existing |
Print only entries that still exist (checks each path). |
-l N, --limit=N |
Stop after N results. |
-r REGEX, --regexp=REGEX |
Treat the argument as a basic regular expression. |
--regex |
Treat patterns as extended regular expressions. |
-A, --all |
Require all patterns to match, not any. |
-S, --statistics |
Show database statistics. |
-d PATH |
Use another database file. |
Returns: 0 if at least one match was found, 1 if none were found.
Usage notes
- Staleness. A file created a minute ago will not be found until
updatedbruns (sudo updatedb). A deleted file may still show up; use-e. - Globs. A pattern without
*,?or[is treated as*pattern*. With glob characters, the pattern must match the whole path, so use'*.conf'rather than'.conf'. - Quoting. Quote globs so the shell does not expand them in the current directory.
- Permissions. On modern systems (plocate, mlocate) each user only sees paths they may read, based on directory permissions at the time of the database build.
- Implementations.
mlocateandplocateare the common ones.plocateis the default on recent Debian and Ubuntu and is faster; options above work in both. - Excluded places.
updatedbskips pseudo filesystems and paths listed in/etc/updatedb.conf(PRUNEPATHS), such as/tmpand/proc. - No other attributes. You cannot filter by size, age or type. For that, use
find. - Scripts should not depend on
locate: it may be absent, empty or stale. Usefindorcommand -v.
Related
find,which,whereis,grep- Lessons: Finding Files