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.

apple-finder-spotlight-search

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
Share this article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Twitter
  • GatorPeeps
  • Digg
  • Reddit
  • muti.co.za
  • DZone
  • del.icio.us
  • StumbleUpon
  • Technorati
  • Ma.gnolia
  • Slashdot

Related posts:

  1. Mac OS X Quick Tip - Taking screenshots from the command line
  2. Editing, Validating and Querying XML with the XMLStarlet command line utility
  3. Mac OS X Quick Tip - Reset Spotlight’s data cache and re-index your hard drive
  4. Reusing commands with different arguments on a Bash command line
  5. Creating shortened URLs from the command line