Beginning with version 10.4 (“Tiger”), Mac OS X has had a powerful indexing and search engine built in. This can be used from the graphical interface, which is how most mac users interact with it. However, a command-line interface allows for some powerful searching and scripting.
This article by Andy Lester goes through the basic usage of the mdfind
and mdls
utilities, and gives examples of how to use them with UNIX pipes and in shell scripts.
Just as an example, to find text in any file on the system with find
and grep
, you’d have to do something like:
find / | xargs grep "my search string"
This would run grep
on every file in the system, which is horribly inefficient and will take forever. With mdfind
, you’d simply type:
mdfind "my search string"
This will use the existing search database to find the appropriate documents.
(N.B. – Spotlight cannot actually do a “phrase” search like the find/grep example above. See this story for more detail and some work-arounds.)
I’m glad you found the article useful. I wish more people knew about the slick stuff you can do with mdfind. There’s even more in Leopard.
Shameless plug, but I wrote a script to make mdfind a little more interactive at the command line. I find it’s helpful when I SSH into my home computer.
It’s on my blog: http://blog.iharder.net/2011/04/05/spotlight-searching-at-the-command-line/trackback/
-Rob