What's the equivalent of Linux's updatedb command for the Mac?

240

63

If I want to use the locate command on a Linux machine, I usually run sudo updatedb first to update the database. I can run the locate command on OS X 10.5 but I can't find updatedb. What's the corresponding updatedb for the mac?

Thierry Lam

Posted 2010-02-16T15:23:59.240

Reputation: 3 497

2When first running locate on an OS X box it tells you to run sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist. – viam0Zah – 2010-02-17T15:24:35.380

13Depending on what you need to use locate for, you might find it more convenient to use the command line interface to Spotlight, mdfind, since the Spotlight database is nearly always up-to-date, and offers a lot more functionality than locate. man mdfind – Paul R – 2010-02-17T16:09:08.003

Well upatedb and locate are nice things but they require to run updatedb regularly (either updatedb runs regularly and this slows down your system at inconvenient times, or locate shows references to files which are not there anymore). So I think that instead of setting up locate/updatedb it would perhaps be better to get familiar with (the far more powerful) find. – amo-ej1 – 2010-02-16T16:43:26.400

9While find is useful for lots of things, it does need to go through the filesystem each time. If you can narrow down the places where you are looking, that's fine. The advantage of locate is that keeps its own database, and so doesn't need to search each time. This is especially important with large and/or remote filesystems. I think that each has its place. – KeithB – 2010-02-16T17:00:50.103

Answers

333

It's locate.updatedb on Mac.

sudo /usr/libexec/locate.updatedb

For more information see the locate.updatedb man page.

John T

Posted 2010-02-16T15:23:59.240

Reputation: 149 037

9That man page also claims: It is typically run once a week by the /System/Library/LaunchDaemons/com.apple.locate.plist job. (And man locate tells one about that script: /usr/libexec/locate.updatedb Script to update the locate database) – Arjan – 2010-02-16T15:29:39.243

5@Arjan : It should be run once a week, but the default it's disabled and the time when it should be done is 3am on Sundays (or something similar), which isn't really useful :) – Studer – 2010-02-16T15:36:13.443

Also, if you don't find files which you expect to, note this relevant caveat from the the BUGS section of the manpage:

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

– Ashutosh Jindal – 2015-12-09T13:18:03.063

f you get shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied cd to your home directory, i.e. cd ~ – Michael Durrant – 2016-08-29T12:15:26.217

On MacOS Sierra if you get the permission denied error try going to root cd / and then executing the sudo /usr/libexec/locate.updatedb. – yossile – 2017-06-26T11:20:42.163

@michael thank you for pointing out that mdfind is an alternative to using locate on Mac OS. – Noah Sussman – 2017-08-22T19:19:54.170

NOTE: patiently wait for the command to finish. There's no output when running the command, making it feel like it's not working, but it is. Just wait. – skplunkerin – 2020-01-21T20:19:18.660

On linux I usually run sudo ionice -c3 updatedb which tells updatedb to share the i/o controller nicely (io nice) but I don't think this command is available on mac. I also miss having the -r regular expression flag which can be used with the GNU locate, although I'm not sure I want to use homebrew and install the GNU locate mentioned by @Grogs – cwd – 2012-06-16T12:41:48.300

3Example macosx alternative to "locate (-r)" and "updatedb" that uses spotlight ("-i" optional of course): mdfind -name "mp4" | egrep -i "^/Users.Downloads/.Stuff" – michael – 2013-01-30T19:35:25.220

87

You can do sudo ln -s /usr/libexec/locate.updatedb /usr/local/bin/updatedb to make the updatedb command available.

Igor Vigasin

Posted 2010-02-16T15:23:59.240

Reputation: 871

Heh, I just posted this as a comment.. Then saw you said this. I think this is a nice little mod to make. :) – James T Snell – 2015-07-02T16:51:48.727

4or alias it in your .bash_profile – Jim Johnson – 2016-08-09T17:26:16.377

20

Personally, I just installed findutils (use MacPorts or Homebrew).

Then you have GNU locate and updatedb.

updatedb won't work without sudo.

Personally I prefer to have a per user locatedb though; if you sudo other users will know the names/locations of all your files.

I have a cron job to run:

updatedb --localpaths='/Users/grogs' --output='/Users/grogs/tmp/locatedb'

And in my .zshrc .bashrc/.bashprofile:

export LOCATE_PATH="~/tmp/locatedb"

Grogs

Posted 2010-02-16T15:23:59.240

Reputation: 371

2Add /opt/local/libexec/gnubin at the start of your path, if you want the coreutils and findutils installed by macports to be available with their original names (and not their g-prefixed versions). – Ioannis Filippidis – 2014-09-18T22:01:28.317

3

Brew-installing findutils on OS X Mavericks gave me a gupdatedb command, not an updatedb one. Unfortunately this command gave me an error described here (where your SO answer is referenced). Ultimately I've aliased updatedb to LC_ALL=’C’ sudo updatedb as a workaround, but I don't know if this is a long-term solution.

– David Rivers – 2013-12-13T19:18:15.340

2

If you run locate without first updating the database, you have a chance to see the OS's recommended way by its output.

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

Tankman六四

Posted 2010-02-16T15:23:59.240

Reputation: 349

0

Actually you can use the GNU locate & update in mac too.

brew install findutils --with-default-names

export PATH="$(brew --prefix findutils)/libexec/gnubin:$PATH"
export MANPATH="$(brew --prefix findutils)/libexec/gnuman:$MANPATH"

which locate

sully

Posted 2010-02-16T15:23:59.240

Reputation: 1