Use Spotlight from the Command Line with mdfind

Dec 5, 2006 - 8 Comments

Terminal 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, this is kind of like a shortened manual page, but you can retrieve it manually with the -h flag as well, like most other command line tools.

$ mdfind
mdfind: no query specified.

This is because you need to supply mdfind with some data to look for, for exampel:

mdfind [search directive]

But let’s proceed with the full instruction set that comes from returning the help section, let’s review this for a moment:

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

Let’s 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.

If you don’t know where something is, you could be much more vague, and only use the name qualifier:

mdfind -name Pumpkin

This will search the entire mac for everything with “Pumpkin” in the name, which will then all be reported back.

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.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS X

8 Comments

» Comments RSS Feed

  1. anonymous says:

    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

  2. Franco says:

    Good stuff

  3. Chris says:

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

  4. DerkaDerk says:

    By piping the results through more, it allows you to view a screenful at a time. You can exit more by hitting control-c.

  5. D says:

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

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

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

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

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates