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?
- 4,317
5 Answers
It's locate.updatedb on Mac.
sudo /usr/libexec/locate.updatedb
For more information see the locate.updatedb man page.
-
10That
manpage also claims: It is typically run once a week by the /System/Library/LaunchDaemons/com.apple.locate.plist job. (Andman locatetells one about that script: /usr/libexec/locate.updatedb Script to update the locate database) – Arjan Feb 16 '10 at 15:29 -
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 Feb 16 '10 at 15:36
-
On linux I usually run
sudo ionice -c3 updatedbwhich tellsupdatedbto share the i/o controller nicely (io nice) but I don't think this command is available on mac. I also miss having the-rregular 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 Jun 16 '12 at 12:41 -
3Example macosx alternative to "locate (-r)" and "updatedb" that uses spotlight ("-i" optional of course): mdfind -name "mp4" | egrep -i "^/Users.Downloads/.Stuff" – michael Jan 30 '13 at 19:35
-
1Also, if you don't find files which you expect to, note this relevant caveat from the the BUGS section of the manpage:
– Ashutosh Jindal Dec 09 '15 at 13:18The 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 -
f you get
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission deniedcdto your home directory, i.e.cd ~– Michael Durrant Aug 29 '16 at 12:15 -
On MacOS Sierra if you get the permission denied error try going to root
cd /and then executing thesudo /usr/libexec/locate.updatedb. – yossile Jun 26 '17 at 11:20 -
@michael thank you for pointing out that
mdfindis an alternative to usinglocateon Mac OS. – Noah Sussman Aug 22 '17 at 19:19 -
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 Jan 21 '20 at 20:19
You can do sudo ln -s /usr/libexec/locate.updatedb /usr/local/bin/updatedb to make the updatedb command available.
- 1,061
-
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 Jul 02 '15 at 16:51
-
8
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"
- 411
-
3Brew-installing findutils on OS X Mavericks gave me a
gupdatedbcommand, not anupdatedbone. Unfortunately this command gave me an error described here (where your SO answer is referenced). Ultimately I've aliasedupdatedbtoLC_ALL=’C’ sudo updatedbas a workaround, but I don't know if this is a long-term solution. – David Rivers Dec 13 '13 at 19:18 -
3Add
/opt/local/libexec/gnubinat the start of your path, if you want thecoreutilsandfindutilsinstalled bymacportsto be available with their original names (and not theirg-prefixed versions). – 0 _ Sep 18 '14 at 22:01 -
I've recently installed the Linux version of
locatefrom MacPorts. Unfortunately, theupdatedbport is broken, which of course renders the newer Linux version oflocateas useless. – Seamus Jun 26 '22 at 18:42
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
Please be aware that the database can take some time to generate; once
the database has been created, this message will no longer appear.
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
- 181
-
or to avoid the name collision with the macos ootb
locateinstall without the--with-default-namesswitch and then invoke viaglocate– ccpizza Feb 15 '24 at 12:28
locateon an OS X box it tells you to runsudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist. – viam0Zah Feb 17 '10 at 15:24locatefor, 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 Feb 17 '10 at 16:09man locate:) The answer has been inman locatesince at least August 17, 2006. – Seamus Jun 26 '22 at 19:06