Next: , Previous: Full Name Patterns, Up: Name


2.1.3 Fast Full Name Search

To search for files by name without having to actually scan the directories on the disk (which can be slow), you can use the locate program. For each shell pattern you give it, locate searches one or more databases of file names and displays the file names that contain the pattern. See Shell Pattern Matching, for details about shell patterns.

If a pattern is a plain string – it contains no metacharacters – locate displays all file names in the database that contain that string. If a pattern contains metacharacters, locate only displays file names that match the pattern exactly. As a result, patterns that contain metacharacters should usually begin with a ‘*’, and will most often end with one as well. The exceptions are patterns that are intended to explicitly match the beginning or end of a file name.

If you only want locate to match against the last component of the file names (the “base name” of the files) you can use the ‘--basename’ option. The opposite behaviour is the default, but can be selected explicitly by using the option ‘--wholename’.

The command

     locate pattern

is almost equivalent to

     find directories -name pattern

where directories are the directories for which the file name databases contain information. The differences are that the locate information might be out of date, and that locate handles wildcards in the pattern slightly differently than find (see Shell Pattern Matching).

The file name databases contain lists of files that were on the system when the databases were last updated. The system administrator can choose the file name of the default database, the frequency with which the databases are updated, and the directories for which they contain entries.

Here is how to select which file name databases locate searches. The default is system-dependent. At the time this document was generated, the default was /usr/local/var/locatedb.

--database=path
-d path
Instead of searching the default file name database, search the file name databases in path, which is a colon-separated list of database file names. You can also use the environment variable LOCATE_PATH to set the list of database files to search. The option overrides the environment variable if both are used.

GNU locate can read file name databases generated by the slocate package. However, these generally contain a list of all the files on the system, and so when using this database, locate will produce output only for files which are accessible to you. See Invoking locate, for a description of the ‘--existing’ option which is used to do this.

The updatedb program can also generate database in a format compatible with slocate. See Invoking updatedb, for a description of its ‘--dbformat’ and ‘--output’ options.