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:
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'
Mac OS X Hints
http://hints.macworld.com/article.php?story=20111007011922833