Use Spotlight from the Command Line with mdfind

Dec 5, 2006 - 9 Comments

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

Related articles:

  • Six useful Spotlight keystrokes
  • Reindex Spotlight from the Command Line
  • How to completely disable Spotlight
  • Hold down Command key to open containing folder in Spotlight
Posted by: OSXDaily in Command Line, Mac OS X

9 Comments

» Comments RSS Feed

  1. anonymous says:
    December 5, 2006 at 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:

    www.macgeekery.com/gspot/2006-12/a_simple_backup_script

    Reply
  2. Franco says:
    December 6, 2006 at 1:47 am

    Good stuff

    Reply
  3. Chris says:
    March 4, 2008 at 10:34 am

    Do you know any way to set spotlight comments from the command line?

    Reply
  4. DerkaDerk says:
    May 27, 2008 at 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.

    Reply
  5. D says:
    May 27, 2008 at 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..

    Reply
  6. links for 2009-07-25 « LinkStream says:
    July 25, 2009 at 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) [...]

    Reply
  7. links for 2009-07-25 « weblinkstream says:
    July 25, 2009 at 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) [...]

    Reply
  8. how to gain muscle says:
    October 28, 2009 at 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..

    Reply
  9. Roman Gaufman says:
    July 1, 2011 at 6:51 pm

    Here’s a nice trick. Add this to your .bashrc:

    dropboxf () { mdfind -onlyin ~/Dropbox “$1″ | sed “s/\/Users\/$USER\/Dropbox\///”;}

    And you can do something like this to search your dropbox:

    $ dropboxf 168.99
    fusetech/Accounts/2000-00 to 2007-09/20070927 168.99 (Roman) VAT CPC – Parts for Leonid.pdf

    Very useful to quickly search your dropbox.

    Reply

Leave a Reply

Click here to cancel reply.

Previous: Beautiful Dorena – Paint Fun from the Maker of Kid Pix
Next: Gizmo – A Free Phone For Your Macintosh

 

Shop for Apple & Mac Deals on Amazon.comspacer

Subscribe to OSXDaily

spacer spacer spacer spacer
Follow @OSXDaily

Shared on Facebook

Shop at Amazon

Ad

OSXDaily on Facebook

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.