Terminal 101: Search Through Metadata
Posted 09/24/2012 at 9:07am
| by Cory Bohon
Every Monday, we'll show you how to do something new and simple with Apple's built-in command line application. You don't need any fancy software, or a knowledge of coding to do any of these. All you need is a keyboard to type 'em out!
In a previous Terminal 101, we showed you how to use the Find command to search for items and certain file types on your Mac, but the results are anything but easy to read. The power of Spotlight’s search is the ability to search through metadata in files, and that functionality can be duplicated in Terminal with the mdfind command. The “md” stands for metadata because this tool can search through the files and pick out the metadata based on pre-defiend keywords.
Basic Search
Let’s say that we have a file on our Mac, somewhere on the drive, and we need to find it. To do this, the basic mdfind syntax is like this:
mdfind keyword
In this basic syntax, mdfind will use the one keyword specified after the command to search for files containing that keyword in the file, or in the metadata. While this basic type of search works, it is notorious for producing lots files because you’re only searching using one keyword.
Output

While the search is being conducted, the mdfind command will output both the directory path and the name of the file to the screen.
Using Multiple Keywords in Search

To use multiple keywords, you will use the following syntax:
mdfind "keyword keyword keyword"
This will search through your files, narrowing them down with only files that match the keywords that you’ve given in quotation marks.
Next week, we’ll show you how to use mdfind to perform more advanced searches, including how to limit search results using boolean operators to narrow down the search results even more, so that you can find exactly the file that you are looking for.
Cory Bohon is a freelance technology writer, indie Mac and iOS developer, and amateur photographer. Follow this article's author, Cory Bohon on Twitter.