Mac OS X Quick Tip: Using Spotlight to search from the command line

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

 

Related posts:

  1. Mac OS X Quick Tip – Reset Spotlight’s data cache and re-index your hard drive
  2. Firefox Quick Tip: Custom search keywords
  3. Reusing commands with different arguments on a Bash command line
  4. Bulk convert HTML, RTF, etc. documents to PDF using the Mac OS X command line or an AppleScript
  5. Displaying a text-based file and folder tree on the command line
Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!

Afrigator