Productive, Not Busy - Work smarter and get more done.

How to search from the Mac OS X command line with Spotlight

20 May 2009

Mac OS X's Spotlight feature automatically indexes files and their metadata on your hard drive and any external drives while you use your Mac.

A simple Spotlight search can be performed by clicking the Spotlight icon in the top-right corner of your Mac's screen while a more advanced search (based on kind, date, name, contents, device make, album, artist, bit rate, city, codec, EXIF data, language, height, width, etc.) can be performed by using the Spotlight search box in the top-right corner of a Finder window.

These simple or advanced searches can also be performed on the command line by using the mdfind utility.

Searches

To perform a simple search, enter a command like this:

mdfind geekology

To perform a search in a specific directory, enter a command like this to search only the "Documents" folder in your "Home" folder:

mdfind -onlyin ~/Documents geekology

To see a count of matches instead of the actual matches, add the "-count" argument to the command:

mdfind -count -onlyin ~/Documents geekology

To see a live (continually updated) list (or count) of results, use a command like this:

mdfind -live -onlyin ~/Documents geekology

To search for results based on a specific metatag (e.g. "kMDItemTitle"), use a command like this:

mdfind -onlyin ~/Documents "kMDItemTitle == '*geekology*'"

For more information on mdfind's arguments, see it's man page:

man mdfind

Metatags

To see a specific file's metatags, use the mdls utility:

mdls geekology.png

…and to see all possible metatags use this command:

mdimport -X
Do you like this? Share it:

Copyright © Geekology 2012. All Rights Reserved.