Syntax locate pattern
locate searches a database for all pathnames which match the specified pattern. The database is recomputed periodically, (about once a week) and contains the path-names of all files which are publicly accessible.
Shell globbing and quoting characters (`*', `?', ``\'', `[' and `]') can be used in pattern, although they will have to be escaped from the shell.
Preceding any character with a backslash (`\') eliminates any special meaning which it can have. The matching differs in that no characters must be matched explicitly, including slashes (`/').
As a special case, a pattern containing no globbing characters (`foo') is matched as though it were `*foo*'..
To force a build/update of the database:
$ cd /
$ sudo /usr/libexec/locate.updatedb
The update can alternatively be called via a plist which simply executes /usr/libexec/locate.updatedb
$ sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
Search the database for files with names containing "foo",
or in folders with names containing "foo"
$ locate foo
Search the database for files (but not in folders) with names ending in ".jpg"
$ locate '*.jpg'
Script to update database
On modern hardware it is often faster to create and search an uncompressed index file of all filenames than to use the compressed locate database, source: Julia Evans [x]
sudo find / > database.txt
grep Alanis database.txt
[I do not] carry such information in my mind since it is readily available in books. ...The value of a college education is not the learning of many facts but the training of the mind to think ~ Albert Einstein
Related macOS commands:
find - Search for files that meet a desired criteria
grep - Search file(s) for lines that match a given pattern
ln - Make links between files (hard links, symbolic links)
ls - List information about file(s)
mdfind - Spotlight search
whereis - Locate a command.
which - Locate a program file in the user's path