locate

find filenames quickly

bsd versionlinux Version
locate [-0Scims] [-l limit] [-d database] filename-pattern

Searches a database ( built periodically (usually weekly ) )     for all pathnames which match filename-pattern.

Shell globbing and quoting characters (*, ?, \, [ and ]) used in filename-pattern must be escaped by preceding them with a backslash (\)
Matching differs from normal regex in that no characters must be matched explicitly, including slashes (/).

A filename-pattern containing no globbing characters is matched if the name contains the pattern.

-0̸ output pathnames separated by a NUL character (character code 00) instead of default (newline, character code 0x0A).
-S output statistics about the database (unfortunately not including the creation time) and exit.
Database: /var/db/locate.database
Compression: Front: 11.69%, Bigram: 56.00%, Total: 8.12%
Filenames: 1186598, Characters: 152669453, Database size: 12389534
Bigram characters: 5451142, Integers: 102656, 8-Bit characters: 398
-c count of matching file names only
-d database [ : … Search a colon-separated list of databases. A single colon is a reference to the default.

If - is given as the database name, standard input will be read instead. For example, you can compress your database and use:

$ zcat database.gz | locate -d - pattern
This might be useful on machines with a fast CPU and little RAM and slow I/O. Note: you can only use one pattern for stdin.
-i Ignore case in the pattern and database.
-l n Limit output to n file names
-m Use mmap(2) instead of the stdio. default usually faster
-s Use the stdio(3) library instead of mmap(2).

Filenames contain any character except newline (\n) and NUL (00).

Examples

First search in $HOME/lib/myLocatedb and then in the default system database:
$ locate -d $HOME/lib/myLocatedb: foo

First search $HOME/lib/myLocatedb then the default, then /var/db/locate.database then

$ locate -d $HOME/lib/mydb::/cdrom/locate.database foo
/cdrom/locate.database.
$ locate -d db1 -d db2 -d db3 pattern
is the same as
$ locate -d db1:db2:db3 pattern
or
$ locate -d db1:db2 -d db3 pattern

Environment

$LOCATE_PATH database, ignored if -d was specified.

Files

/var/db/locate.database system default locate database
/usr/libexec/locate.updatedb Script to update the locate database
/System/Library/LaunchDaemons/com.apple.locate.plist Job that starts the database rebuild

See

find, whereis, which, fnmatch, locate.updatedb(8)

BUGS

NON-bug: May fail to list some files that are present, or may list files that have been removed from the system, because locate reports files that are present in the database, /System/Library/LaunchDaemons/com.apple.locate.plist job.
Use find to locate files that are of a more transitory nature.

The locate utility does not recognize multibyte characters.

/System/Library/LaunchDaemons/com.apple.locate.plist

    key= Label com.apple.locate 
    key= Disabled  false/
    key= ProgramArguments   /usr/libexec/locate.updatedb  
    key= LowPriorityIO  true/
    key= Nice  integer= 5 
    key= KeepAlive  dict=  key= PathState  dict=  key= /var/db/locate.database  false
    key= StartCalendarInterval 
        key= Hour  integer= 3 key= Minute  integer= 15 key= Weekday  integer= 6 
        key= AbandonProcessGroup  true

and is protected by SIP.

Script /usr/libexec/locate.updatedb which invokes locate.mklocatedb and locate. bigram