Enable and Use the ‘locate’ Command in the Mac OS X Terminal

Nov 2, 2011 - 12 Comments

locate command in Mac OS X

The locate command is very useful if you’re looking to track down every instance of a file, filetype, app, extension, things hidden deep in system folders, or just about anything else that Spotlight can’t manage. It’s extraordinarily useful for troubleshooting and even more mundane tasks like completely uninstalling Mac apps.

In order to use locate, you need to build the locate database, which also enables a few other helpful commands including whatis, find, and the manual keyword search ‘man -k’. OS X 10.7 is better at building this for you, but if you don’t have locate enabled yet all that is required is typing this command into the Terminal:

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

This is told to you directly by OS X the first time you attempt to run locate or any of the commands dependent on the database:

$ locate python

WARNING: The locate database (/var/db/locate.database) does not exist.
To create the database, run the following command:

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

Please be aware that the database can take some time to generate; once
the database has been created, this message will no longer appear.

How long it takes to generate the database varies, but the larger your hard disk the longer it’ll be. You can watch the progress indirectly through Activity Monitor, where the “find” process will be running at around 15-30% CPU use until the locate database is generated.

find process running to build locate db

Alternatively you can also run the following command and build the database:

sudo /usr/libexec/locate.updatedb

As with many terminal commands, locate accepts wildcards and regular expressions, helping you narrow down advanced searches. For example, you could find every possible file with a .jpg extension by using:

locate *.jpg

Some jpg files will inevitably have an uppercase extension though, and you can tell locate to ignore case sensitivity with -i:

locate -i *.jpg

There are plenty of other options you can work with, refer to ‘man locate’ for more info.

Don’t forget to check out more OS X command line tips too.

.

Related articles:

Posted by: William Pearson in Command Line, Mac OS, Tips & Tricks

12 Comments

» Comments RSS Feed

  1. topik says:

    I search about locate proxychain-ng configuration and file not found. first time i can sudo lauch ctl load and then can i locate my file install cause there is not file i search

  2. test moi says:

    This :
    locate -i *.jpg

    and this one below give the same result
    locate -i jpg

  3. saw says:

    But how do you turn that back off after you use it?

  4. apanloco says:

    mdfind — thank you so much!! I have never managed to get locate to work on my Mountain Lion installation. (It’s enabled, but locate doesn’t return files that I know are there)

    • steve says:

      Did you know what’s below from manual locate, isn’t it?.

      ”The locate database is typically built by user «nobody» and the locate.updatedb(8) utility skips directories which are not readable for user «nobody», group «nobody», or world. For example, if your HOME directory is not world-readable, none of your files are in the database”

      I didn’t know it…

  5. munjee says:

    Is there really a point to bother with locate anymore since it’s possible to use ‘mdfind’ (md=metadata) to search the Spotlight index? Mdfind also shows results from folders that the crippled piece of crap known as Spotlight (the GUI version that is) does not show.

    mdfind hosts

  6. David Arnstein says:

    Yikes! I made a mistake. Let me try again.

    In your example, it is better to escape the asterisk. Your call to “locate” will fail if there exists a .jpg file in the current working directory. It is better to type

    locate -i ‘*.jpg’

  7. Patrick says:

    Locate really is a great tool, too bad Locator X 0.7.3 (UI on top of locate) isn’t available anymore.

    I’ve been using “toucan search” (app store) and I’ve been very pleased with it. It is as fast as locate and it has a UI so it is more convenient to use (the only thing missing is the ability to search the content of files).

  8. adam says:

    The Rosetta Miss,
    It’s hard to buy every software at every new computer…
    But I use Lion, and yes is not the best OS X, but is not really bad too…

  9. Brandon says:

    Still using Snow Leopard eh? Glad to see I’m not alone, Lion has so many quirks with the over simplification it’s hard to use it in a production environment.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site