How to use Spotlight to search for files from a Mac OS X Terminal window

If you ever want to use the Spotlight search functionality from a Mac OS X terminal window, you can access the same Spotlight information using the mdfind command. Here are a few examples of how to use mdfind to find files and directories on your Mac OS X system.

To find files with the word "alexander" in them, from a Terminal window you can use mdfind like this:

mdfind alexander

Now, if you just want to find a file using a portion of the filename, use this mdfindcommand:

mdfind "kMDItemFSName = Java"

You can also tell mdfind to look in only certain directories (like MyPicturesDirectory), using syntax like this:

mdfind -onlyin MyPicturesDirectory

Here's the output from the mdfind help command:

$ mdfind --help

Usage: mdfind [-live] [-count] [-onlyin directory] query
list the files matching the query
query can be an expression or a sequence of words

-live                Query should stay active
-count               Query only reports matching items count
-onlyin <dir>  Search only within given directory
-s <name>      Show contents of smart folder <name>
-0                   Use NUL (``\0'') as a path separator, 
                     for use with xargs -0.

example:  mdfind image
example:  mdfind "kMDItemAuthor == '*MyFavoriteAuthor*'"
example:  mdfind -live MyFavoriteAuthor


This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.