Use Spotlight from the Command Line with mdfind
December 5th, 2006 - Command Line, Mac OS X
Spotlight is one of my favorite features of Mac OS X, I probably use it more than even the Dock. Being able to hit command-space to quickly locate documents, emails, and launch Applications is endlessly useful. For some of us, it’s necessary to be in the terminal, and wouldn’t it be cool to have this ultra seach feature in the command line? Well it’s there, open up your Terminal (or iTerm) and explore mdfind, the command line version of Spotlight.
Even if you’re new or oblivious to the Unix underpinnings of Mac OS X, you can try this out. Open your terminal and type mdfind, you’ll get some directions thrown back at you because you didn’t specify a search query:
$ mdfind
mdfind: no query specified.
Usage: mdfind [-live] [-onlyin directory] query
list the files matching the query
query can be an expression or a sequence of words
-live Query should stay active
-onlyin dir Search only within given directory
-0 Use NUL (“\0”) as a path separator, for use with xargs -0.
example: mdfind image
example: mdfind “kMDItemAuthor == ‘*MyFavoriteAuthor*’”
example: mdfind -live MyFavoriteAuthor
If this looks confusing, it’s not. Say you want to find a file named ‘recipe’ and you know it’s in your documents folder, you’d type:
$ mdfind recipes -onlyin ~/Documents/
If you have a ton of recipes, it’s probably a good idea to pipe the output into more, as shown below:
$ mdfind recipes -onlyin ~/Documents/ | more
By piping the results through more, it allows you to view a screenful at a time. You can exit more by hitting control-c.
There’s much more to be done with the mdfind command, but we’ll keep it simple for now. Beyond curiosity, the mdfind command is probably more useful to sysadmins, unix geeks, shell scripters, and programmers than the standard Mac user. For the rest of us, command-space is where it’s at, and we’re not complaining.
Mac Deals from the Apple Store:
Comments:
Comments: 6
Comment from anonymous
Time: December 5, 2006, 6:55 pm
i love mdfind. I use it as much as I use spotlight, and it truly has changed the way I use a computer, much like gmail’s philosophy (search don’t sort) has changed the way I use email. For the not-so-faint of heart, check out this tip:
http://www.macgeekery.com/gspot/2006-12/a_simple_backup_script
Comment from Franco
Time: December 6, 2006, 1:47 am
Good stuff
Comment from Chris
Time: March 4, 2008, 10:34 am
Do you know any way to set spotlight comments from the command line?
Comment from Frosting
Time: April 5, 2008, 10:40 pm
I’ve been using mdfind for quite a while, but i guess it was only for basic operations, i did not even know you could use it with | more. Very nice article btw, i think i’ll go read a bit about the command, seems even more useful than what i though. Might be the recipe to success to make Mac OS X even more perfect
Comment from DerkaDerk
Time: May 27, 2008, 1:08 pm
By piping the results through more, it allows you to view a screenful at a time. You can exit more by hitting control-c.
Comment from D
Time: May 27, 2008, 1:11 pm
..There’s much more to be done with the mdfind command, but we’ll keep it simple for now. Beyond curiosity, the mdfind command is probably more useful to sysadmins, unix geeks, shell scripters, and programmers than the standard Mac user. For the rest of us, command-space is where it’s at, and we’re not complaining..
Social bookmarks:
- | del.icio.us | Digg it | Furl | Netscape | reddit | StumbleUpon | Yahoo MyWeb |
December 5th, 2006

Write a comment