Name

locate — stdin  stdout  - file  -- opt  --help  --version

Synopsis

locate [options]

The locate command, with its partner updatedb, creates an index (database) of file locations that is quickly searchable.[13] If you plan to locate many files over time in a directory hierarchy that doesn’t change much, locate is a good choice. For locating a single file or performing more complex processing of found files, use find.

Some distros automatically index the entire filesystem on a regular basis (e.g., once a day), so you can simply run locate and it will work. But if you ever need to create an index yourself of a directory and all its subdirectories (say, storing it in /tmp/myindex), run:

$ updatedb -l0 -U directory -o /tmp/myindex

(Note that -l0 is a lowercase L followed by a zero, not the number 10.) Then to search for a string in the index:

$ locate -d /tmp/myindex string

locate has an interesting, optional security feature. You can create an index that, when searched, will display only files that the user is permitted to see. So if the superuser created an index of a protected directory, a non-superuser could search it but not see the protected files. This is done by omitting the -l0 option to updatedb and running it as root:

# updatedb -U directory -o /tmp/myindex

Indexing options for updatedb

-u

Create index from the root directory downward.

-U directory

Create index from directory downward.

-l (0|1)

Turn security off (0) or on (1). The default is 1.

-e directories

Exclude ...

Get Linux Pocket Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.