How do I add a directory to be indexed by locate.updatedb
on macOS / BSD so that I can find files in that directory with the locate
command?
I read the man page of locate(1)
on Mac, but didn't find anything useful.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityThe locate(1)
man page tells you how to use the locate
command to look stuff up. As the indexing is done by locate.updatedb
, you should read the locate.updatedb(8)
man page instead.
But on a macOS Big Sur at least, the information relevant to your question is rather limited. Basically it just says:
The contents of the newly built database can be controlled by the
/etc/locate.rc
file.
By default, all the settings in /etc/locate.rc
seem to be commented out. The relevant lines for your question are:
# directories to be put in the database
#SEARCHPATHS="/"
# directories unwanted in output
#PRUNEPATHS="/tmp /var/tmp"
# filesystems allowed. Beware: a non-listed filesystem will be pruned
# and if the SEARCHPATHS starts in such a filesystem locate will build
# an empty database.
#
# be careful if you add 'nfs'
#FILESYSTEMS="hfs ufs apfs"
Assuming the file follows the convention of presenting the default settings as commented-out examples, it seems that everything located on HFS, UFS or APFS filesystems will be indexed by default, except the contents of /tmp
and /var/tmp
.
If the directory you wish to add is located on one of those filesystem types, and is not under /tmp
or /var/tmp
, the directory should automatically get indexed the next time locate.updatedb
runs.
Note that many variants of locate
will check for access permissions when displaying the results, and will only show you files you would have permission to see.
locate
can find /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask/Casks/google-chrome.rb
, but not /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask-versions/Casks/google-chrome-canary.rb
, and it can't seem to find contents in ~/my-custom-dir
.
locate.updatedb(8)
man page, the database is updated only once a week by default - did you run the update task manually for testing?
locate.updatedb
before the tests. But these brew tap dirs and ~/my-custom-dir
has been on my mac for months, so they should be indexed, right?
mdfind
command that utilizes the Spotlight search engine and its databases, which might be a better option than relying on the very basic locate
.
locate
is just something that was inherited from macOS's BSD origins and probably hasn't received much attention, I think that's a good assumption to make.