mdfind

mdfind is the built in command line tool that utilises the spotlight search engine on macOS. Every Spotlight query is an AND search by default. It is surprisingly powerful.

* Mastering Spotlight - superuser.com

The mdfind command consults the central metadata store and returns a list of files that match the given metadata query. The query can be a string or a query expression.

The following useful options are available: * -onlyin dir (search the directory specified) * -name fileName (matching file names)

Passing a simple string searches all metadata fields. So:

mdfind image

returns all files with any metadata attribute value matching the string "image"

mdfind "kMDItemAuthors == '*MyFavoriteAuthor*'"

returns all files that contain "MyFavoriteAuthor" in the kMDItemAuthor metadata attribute:

Here are some useful links: * The Power of mdfind - macdevcenter.com * Spotlight syntax, mdfind examples, and metadata attributes - osxnotes.net

Spotlight/mdfind uses the following frequently uses search syntax:

name:file.txt kind:"jpeg image" date:today date:"this week" modified:12/31/11 created:12/1/11-12/31/11 kind:video AND size:<100000

All words passed in a query string to mdfind are implicitly ANDed together. That is, "invoice apress" means both words must appear. Spotlight allows other Boolean operators as well:

# Searching for named attributes

If I'm only interested in certain attributes, I can use the -name option:

mdls -name kMDItemComposer "11 Space Truckin'.m4a"

mdfind -name javascript -onlyin ~/Books

and you can use wildcards:

mdfind 'kMDItemFSName=*.pdf'

If you want to only list folders (in spotlight kind:folders):

mdfind 'kMDItemKind=folders&&kMDItemFSName=folderName''

You can create complex searches by building up complex strings:

mdfind 'kMDItemFSName=convert&&kMDItemContentType=public.unix-executable'

# Getting precise

Now that I know some attribute names, I can get very precise in how I search. Say I want to find songs composed by Roger Waters. I need to search the kMDItemComposer attribute for "Waters". I'll put the string I'm searching for in double quotes, and then the entire search expression in single quotes.

mdfind 'kMDItemComposer = "Waters"'

I know that I have more than three songs written by Roger Waters, so I'll rerun the search with wildcards, with an asterisk to mean "any string."

mdfind 'kMDItemComposer = "*Waters*"'

If I want a case-insensitive search, I can put the letter c outside the double quotes, as in this search to find all forms of "McCartney", regardless of the capitalization.

mdfind 'kMDItemComposer = "*mccartney*"c'

# More shell examples You can find many more examples over at osxnotes.net

# See also

Spotlight is a system-wide desktop search feature of Apple's OS X and iOS operating systems - wikipedia

When you type in your search terms, Spotlight looks for even a partial match at the beginning of a word. So, when you search for phon you’ll find phone, phony, and phonatory, but not symphony.

Here are some commonly used spotlight attributes that you can use with mdfind:

All words passed in a query string to mdfind are implicitly ANDed together. That is, "invoice apress" means both words must appear. Spotlight allows other Boolean operators as well: