Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Oneechan69

macrumors regular
Original poster
Mar 29, 2022
216
29
US
mdfind and locate are both CLIs for searching files on Mac. mdfind uses the Mac's spotlight index for searching while locate uses its own. To manually update locate's index you need to run /usr/libexec/locate.updatedb. If you have used both, how do they compare, is one better than the other?
 

zevrix

macrumors regular
Oct 10, 2012
224
133
mdfind and locate are both CLIs for searching files on Mac. mdfind uses the Mac's spotlight index for searching while locate uses its own. To manually update locate's index you need to run /usr/libexec/locate.updatedb. If you have used both, how do they compare, is one better than the other?

I never used it but if you read the BUGS section of the man page for locate then it doesn't look like it's a reliable way to look for files. By default this database doesn't even exist on my Mac - has to be generated first:

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.
 

Brian33

macrumors 65816
Apr 30, 2008
1,427
354
USA (Virginia)
I haven't used locate. But from what I can tell, it only searches a database of pathnames. It doesn't index anything within a file, as far as I can tell.

The Spotlight database indexes the contents of files (and I guess the file and pathnames, also).

So that's a pretty big difference.
 
  • Like
Reactions: gilby101

gilby101

macrumors 68030
Mar 17, 2010
2,550
1,368
Tasmania
I have always assumed (but can't find a reference) that locate is present in macOS just for POSIX compliance. As such it is mainly of historical interest.

mdfind and locate are both CLIs for searching files on Mac
You might be better comparing mdfind with find. Both are actively used, particularly find, in command scripts.
 
  • Like
Reactions: zevrix and frou

frou

macrumors 65816
Mar 14, 2009
1,308
1,811
I have always assumed (but can't find a reference) that locate is present in macOS just for POSIX compliance.
I don't think it's actually required by POSIX but indeed it's probably there as an old Unix thing that users at some point in the past had the expectation of being able to use.
 
Last edited:
  • Like
Reactions: gilby101
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.