O'Reilly logo

Linux Pocket Guide, 2nd Edition by Daniel J. Barrett

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required