Mac OS X Quick Tip: Using Spotlight to search from the command line
Author: willem In: apple, bash scripting, coding, productivity, tips & tricks, tools, unix, web developmentMac 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 mdfindMetatags:
To see a specific file’s metatags, use the mdls utility:
mdls geekology.png
…and to see all possible metatags use this command:
mdimport -XRelated posts:
- Mac OS X Quick Tip - Taking screenshots from the command line
- Editing, Validating and Querying XML with the XMLStarlet command line utility
- Mac OS X Quick Tip - Reset Spotlight’s data cache and re-index your hard drive
- Reusing commands with different arguments on a Bash command line
- Creating shortened URLs from the command line
Like this post? Subscribe to the Geekology RSS 2.0 feed!














Leave a reply