Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Automatically update locate db on-demand UNIX
This hint provides a method to automatically insure that the locate db is not too old (but only) whenever you use the locate command (in Terminal or a script).

The hint:
  • a) uses sudo to update the db as root,
  • b) assumes you're using bash as your shell interpreter,
  • c) sets an alias so as to be transparent,
  • d) adds "-i" to your locate command, making it case-insensitive,
  • e) considers the db outdated if it's older than an hour, (actually 3600 seconds, which you can change of course.)
Using your favorite text editor add the following line to the file called ~/.bash_profile. It's inside your Home directory as indicated but hidden. If it doesn't already exist you can create a new text file and save it to that path and name.
alias locate='if [ $((`date +%s`-`eval $(stat -s /var/db/locate.database); echo $st_mtime`)) -gt 3600 ]; then echo "locate: db is too old!">/dev/stderr; sudo /usr/libexec/locate.updatedb; fi; locate -i'

[crarko adds: Note that in creating the command alias you slightly change the meaning of the locate command; you can use a different alias if you want to keep both ways around and update manually. You lose some of the value of the hint if you do that.]
    •    
  • Currently 2.40 / 5
  You rated: 2 / 5 (5 votes cast)
 
[11,363 views]  

Automatically update locate db on-demand | 5 comments | Create New Account
Click here to return to the 'Automatically update locate db on-demand' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Automatically update locate db on-demand
Authored by: wallybear on Oct 07, '11 10:18:03AM

I think that considering 'old' a locatedb older than 1 hour is a little overkill, keep in mind that periodic maintenance tasks update it only weekly.
Another mean to be sure locate db (and other maintenance tasks) are regularly run is using the free PseudoAnacron (available on http://www.jaw.it/pages/en/x_misc.html page).



[ Reply to This | # ]
Automatically update locate db on-demand
Authored by: baltwo on Oct 07, '11 02:19:16PM

just a couple of comments.

1. Rebuilding the database is no longer part of the weekly maintenance script. The launchd trigger, stored in /System/Library/LaunchDaemons/ as com.apple.locate.plist and was supposed to launch at 3:15 AM on Saturdays, has been disabled. So, if you want to rebuild the database, you have to do it manually.

2. The locate.updatedb executable is crippled, excluding some system-level items. To rebuild it so nothing's missing, you need to replace the current locate.updatedb executable with the uncrippled one from Leopard, located in its /usr/libexec/ folder, assuming that you still have an early Leopard installation or installer. I believe it was crippled in 10.5.5 (one dated 12/12/08 or earlier will work uncrippled).

Supposedly, Apple crippled it as a security measure, but since I'm on a single-user machine, there's no sound reason to preclude searching everywhere. That makes it better than Spotlight for searching for named items anywhere.



[ Reply to This | # ]
Use Finder
Authored by: kipkoan on Oct 08, '11 07:43:10AM
I used to try to use locate, but found out that the Finder database is accessible from the terminal. Now I just alias locate to mdfind -name.

[ Reply to This | # ]
Automatically update locate db on-demand
Authored by: Hal Itosis on Oct 09, '11 06:08:02PM
I can't speak for Lion, but some of these comments about Leopard & Snowy don't seem right...
Rebuilding the database is no longer part of the weekly maintenance script. The launchd trigger, stored in /System/Library/LaunchDaemons/ as com.apple.locate.plist and was supposed to launch at 3:15 AM on Saturdays, has been disabled. So, if you want to rebuild the database, you have to do it manually.
...or, simply re-enable the automatic task. In fact, when a user attempts to use locate on a virgin Snow Leopard machine, they are presented with this message in Terminal:
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.
So, simply follow those instructions (as i have), and Snow Leopard will do the whole weekly "Unix maintenance" task routine, just as Leopard did.


The locate.updatedb executable is crippled, excluding some system-level items. To rebuild it so nothing's missing, you need to replace the current locate.updatedb executable with the uncrippled one from Leopard, located in its /usr/libexec/ folder, assuming that you still have an early Leopard installation or installer. I believe it was crippled in 10.5.5 (one dated 12/12/08 or earlier will work uncrippled).
Again, it all works fine in Snow Leopard. And locate.updatedb is not "crippled" (at least not on my 2010 Core i7 MBP running 10.6.8):
$ ls -l /usr/libexec/locate.updatedb
-r-xr-xr-x  1 root  wheel  3449 May 18  2009 /usr/libexec/locate.updatedb
As can be seen, the mod date there is 2009... and as i say, it all works fine.


Supposedly, Apple crippled it as a security measure,
I hadn't heard that (what/where is your source?). If i had to guess, Apple probably disabled it so that non-Unix folks (who don't use locate on the command line) wouldn't worry about the occasional mysterious background activity, or wonder what's loading down the CPU, etc. For geeks who care, that message we see when attempting to run locate instructs us how to re-enable the weekly rebuild (as i mentioned above), and so they can it get up and running in seconds.

-HI-
Edited on Oct 09, '11 06:10:46PM by Hal Itosis


[ Reply to This | # ]
A much nicer version
Authored by: GaelicWizard on Oct 22, '11 05:17:30PM
Below is a much nicer version of the original hint. I've re-written it to be both readable and to ask launchd to asynchronously update the database. This way, I don't have to wait for my entire file system to be traversed before getting (stale) results. I've also added a check that the database doesn't exist at all, in which case the appropriate launchd task will be loaded.

alias locate="locate -i" # case insensitive
function locate ()
{
    local MAXAGE=$(( 24 * 60*60 )) # twenty-four hours, in seconds.
    local NOW="$(date +%s)"
    local DB="/var/db/locate.database"

    if [ ! -e "$DB" ]
    then
        echo "$FUNCNAME: $DB does not exist. Initializing..." > /dev/stderr
        sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist > /dev/stderr
        echo "$FUNCNAME: Please try again in a few minutes." > /dev/stderr
        return -1 # stop and fail, there's nothing we can do until the database has been created.
    fi

    local DBCREATED="$(eval $(stat -s $DB); echo $st_mtime )"
    local DBAGE="$(( $NOW - $MAXAGE ))"

    if [ $DBAGE -gt $NOW ]
    then
        echo "$FUNCNAME: $DB is more than $(( $MAXAGE / (60*60) )) hours old. Updating..." > /dev/stderr
        sudo launchctl start com.apple.locate > /dev/stderr
        echo "$FUNCNAME: stale results: " > /dev/stderr
    fi

    command locate "$@"
}
Edited on Oct 22, '11 05:23:40PM by GaelicWizard


[ Reply to This | # ]