Name

locate — stdin  stdout  - file  -- opt  --help  --version

Synopsis

locate [options]

The locate command searches an index (database) of file locations to locate a given file. 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.

You can set up OS X to index the entire filesystem on a regular basis (e.g., once a day), meaning you can simply run locate and it will work. To do this, run:

sudo launchctl load -w \
  /System/Library/LaunchDaemons/com.apple.locate.plist

This starts generating the index, which may take a while to complete.[14] Then you can locate files by name with:

locate myfile

At this point, you might wonder why locate is necessary, since every Finder window has a Search box for locating files. In fact, this Finder feature does not locate system files that are normally hidden by the Finder. Try searching with the Finder for who, for instance, and it will not locate /usr/bin/who.[15]

Useful options

-i

Case-insensitive search.

-l N

Display only the first N files.

[14] The launchctl command is covered in Scheduling Jobs.

[15] You can make the Finder search for system files with a bit of work. Perform a search, then click the + button and look for the Kind dropdown. Change it to Other, then select System Files, click OK, and then change “aren’t included” to “are included.” Now you can search for system files in the Finder, but ...

Get Macintosh Terminal Pocket Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.