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

Click here to return to the 'A much nicer version' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
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" ]
        echo "$FUNCNAME: $DB does not exist. Initializing..." > /dev/stderr
        sudo launchctl load -w /System/Library/LaunchDaemons/ > /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.

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

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

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

[ Reply to This | # ]