Setup locate command on Mac OS X

Posted by Mike on February 28, 2009

One of the commands that I immediately missed when I started using OS X for my development environment was the locate command. It is very helpful to be at a command prompt and type:

locate mysql

and have it return a list of every file and directory with mysql in them. It is very fast and is great for chaining with other shell commands. In order for it to work it needs a local file database and it needs to update the database periodically to keep up with the changes made on the system. So my goal with these instructions is a quick way to get locate to work and have it update once a day to keep up with everything.

1. Open a Terminal
2. type: sudo /usr/libexec/locate.updatedb

Once it’s finished running, the locate command will work. That same command needs to be run anytime you want the file database updated. I schedule this to run daily with cron.

1. type: sudo crontab -e
2. add this line to the file, save, and exit

30 20 * * * /usr/libexec/locate.updatedb

What this says is run this command everyday at 8:30 pm. You can adjust this time to what ever is most convenient. It’s best if this is a time when you expect the machine to be on but not in heavy use, as it will make it slow for a few minutes while it runs.

Now the locate command should be working and automatically updating itself for file changes everyday. If you decide to change the time that it runs, just follow the second set of steps again and it will overwrite the existing settings. If you’d like to dig deeper into the crontab file for more advanced scheduling options type:

man 5 crontab