This is the personal blog of Neil Ang. Simple and interesting technology articles written by a developer for developers. Feel free to comment on posts or link to this site. Constructive feedback is always welcomed.

Updating the locate database in Mac OS X

Posted: 6 November 2007

One of my favorite commands in Unix is "locate". It's useful to help find files through terminal - fast! After freshly installing Leopard, I realised that the database for the command had not been built.

`/var/db/locate.database': No such file or directory

No problem. To fix this simply run the command:

sudo /usr/libexec/locate.updatedb

After a few minutes the database will be created, and you can continue using terminal efficiently.

Your comments (subscribe)

Gravatar

James Pamplin 7 Nov 07 at 8:56am

Thanks Neil, worked great!

Gravatar

David 10 Nov 07 at 8:17am

Thanks! This was exactly what I was looking for.

Gravatar

Tony Arnold 14 Nov 07 at 0:48am

Alternately, place the following in your .bash_login:

function locate { mdfind "kMDItemDisplayName == '$@'wc"; }

This will make the command-line locate command use Spotlight's index to locate items on your system.

Gravatar

Tony Arnold 14 Nov 07 at 0:49am

The only caveat with the method I list above is that it won't search locations that are not included in the Spotlight index. Unfortunately for UNIX geeks like us, Neil, this means it skips directories such as /usr, /bin and /opt. Bleh.

Gravatar

f1Sh 22 Nov 07 at 1:32am

Very nice hint! Thanks for the update, it made me crazy to look for the reason!

Gravatar

Shlomo 27 Nov 07 at 5:12am

I get the following warning:

shlomo$ sudo /usr/libexec/locate.updatedb Password: ******** >>> WARNING >>> Executing updatedb as root. This WILL reveal all filenames >>> on your machine to all login users, which is a security risk.

When I leave off the "sudo" then, after about ten minutes, I get:

/usr/libexec/locate.updatedb: line 89: /var/db/locate.database: Permission denied

So how do I index only what my user can see but still write to the db file?

Gravatar

Neil Ang 27 Nov 07 at 10:06pm

Shlomo – I'm the only user on my computer, so I didn't consider this as a problem.

Off the top of my head, there are 2 options that come to mind:

  1. The comment by Tony Arnold (above) explains how you can associate the locate database with spotlight. This might give you the desired effect.
  2. Your error occurs on line 89, which is when it trys to write to the file /var/db/locate.database. You could try and chmod this file so your user account has write permission, and try again.

N.B. I've never tested these options, and don't know that the outcome will be. Only try them if you are confident you won't break anything. Please let me know if these do work out for you, thanks.

Gravatar

Jay 7 Dec 07 at 1:31am

I would highly recommend chmod'ing your locate binary and database so that only root has access to perform locate searches. As it says in Leopard when you perform the initial locate database creation: "This WILL reveal all filenames on your machine to all login users, which is a security risk."

Gravatar

dan 11 Dec 07 at 3:00am

Thanks, didn't want to wait for Saturday at 4:30AM!

Gravatar

Si 16 Dec 07 at 7:31am

Hi Neil. Thanks for the post, it worked for me. Now I just have to cron the command. I really love your blog's theme, by the way!

Gravatar

xenon 28 May 08 at 9:39pm

thanks man!

Gravatar

Batuhan 23 Jun 08 at 0:55am

Thanks!

Gravatar

JeffRichards 9 Jul 08 at 4:08am

This is the reason to love the internet, You can read through the man pages (which does tell you how to do this), or just do a simple search and BANGO, you have your answer.

thanks!

Gravatar

Telmo Dias 2 Nov 08 at 6:43am

Thank you very much!

Post a comment

Comment Guidelines

  • You can subscribe to the comments on this entry via RSS.
  • Have no more than 2 links, otherwise your comment will be flagged as spam.
  • Links are automagically generated.
  • <em>text</em> to make text italic.
  • <strong>text</strong> to make text bold.

JavaScript needs to be enabled to comment.