Search

Top Posts

Comments

  • Dan: You’d definitely need a microphone, mic stand and usb audio interface for a start!
  • Sepp: Mac OS X comes with an even more powerful Firewall than the one in the system preferences. Open up the Terminal...
  • jeffcard: Nice guide, thanks for sharing, here is a step by step guide regarding how to convert DVD, video and audio,...
  • James: Well, it hasn’t crashed on me yet, and I’ve been using it for at least 2 months now…
  • Dasher: Any experience creating a RAM disk, specifically for helping iTunes run faster? I am on a 2006 Imac 21″...

Categories

Recent Posts


Use Spotlight from the Command Line with mdfind

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.

Posted by: Editor

Share

Save big on Mac Deals from Amazon.com

Comments:

Comments: 9

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..

Pingback from links for 2009-07-25 « LinkStream
Time: July 25, 2009, 10:06 pm

[...] Use Spotlight from the Command Line with mdfind – OS X Daily (tags: Useful_hints2 osx commandline macosx shell apple unix howto search terminal spotlight) [...]

Pingback from links for 2009-07-25 « weblinkstream
Time: July 25, 2009, 10:31 pm

[...] Use Spotlight from the Command Line with mdfind – OS X Daily (tags: Useful_hints2 osx commandline macosx shell apple unix howto search terminal spotlight) [...]

Comment from how to gain muscle
Time: October 28, 2009, 1:28 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..

Write a comment






December 5th, 2006