Find files fast with locate

Spotlight search, even when accessed through the Finder’s Find command, doesn’t cover all your startup volume, not by a long way. When you want to locate a command tool, for example, it will draw a complete blank. If you want to search more of the accessible folders on your startup volume, then you’ll be best using a tool like locate instead.

locate is another of those powerful Unix tools which has crossed over to macOS, but which is greatly underused. Unlike other search tools, such as find, it is very simple to use, and the database which it works from is tiny by modern standards.

Before you can use it, assuming that you have never used it before, you’ll need to build or rebuild its database. This is kept at /var/db/locate.database, and if it isn’t there, any attempt to use the locate command will prompt you to create it. That’s easily done by loading its property list, which has sat there unused all this time. In Terminal, enter
sudo launchctl load -w /System/Library/LaunchDaemons/
and authenticate with your admin password.

locate and its helpers will then build the database in the background. Here, that took just a couple of minutes, at the end of which it saved the file /var/db/locate.database and quit. You can watch its progress in Activity Monitor if you wish.

By comparison with Spotlight’s database, locate.database is tiny. For my million plus files in over 1 TB, it required just 36 MB.

If you already have a locate.database, but suspect that it might be out of date, you can force an update using
sudo /usr/libexec/locate.updatedb

In normal use, the locate command is simple. The only option that you’re likely to use much is -i for case insensitivity. Use standard ‘globbing’ terms such as *, ?, and […], and \ as its escape character. In case you’re in any doubt:

  • * matches any character, including none
  • ? matches any single character
  • [abc] matches any one of the characters a, b, c
  • [a-z] matches any one character from the range a to z
  • [!abc] matches any one character except a, b, or c
  • [!a-z] matches any one character except those in the range a to z.

Ends of a string which lack a globbing term are assumed to have * present. So enter
locate locate
and you’ll be given a list of every file whose path includes the characters locate within it. Use
locate .lo[!g]
and you’ll see the full pathnames to all the files with extensions such as .loc, except those ending in .log.

locate has some neat twists too. If you have a bootable external drive, restart from that and build locate‘s database there as well. When you want to search both drives, you can specify the two databases in a command of the form
locate -d /Volumes/External/var/db/locate.database: searchstring
with the -d option to specify which database to use and the colon : appended to include the default database on your current startup volume. Unless you go out of your way to build and use a database on your Time Machine backup, that will be excluded from such searches.

By default, locate‘s database is only updated every week. For most of the files that I need to locate, that should be fine, although you can always force a refresh using
sudo /usr/libexec/locate.updatedb

locate builds its database as the user nobody in the group nobody, which gives excellent coverage outside users’ Home folders, but very little inside them. This is complementary to Spotlight coverage. When looking for macOS components and installed software, locate is usually far superior to Spotlight. When looking for documents and content, locate is usually of very limited value.