How to Use the ‘mdfind’ Command for High-Speed Spotlight Searching via Terminal

The Sluggishness of standard ‘find’

When searching for a misplaced file via the macOS Terminal, most users reach for the classic UNIX find command (e.g., find / -name "budget.pdf"). The problem with the find command is that it physically crawls through every single folder on your hard drive, one by one. On a massive 2TB hard drive, a deep search can take several minutes to complete.

macOS has a much better, native solution: Spotlight. Spotlight constantly runs in the background, maintaining a highly optimized, indexed database of every file on your computer. When you click the magnifying glass in the graphical menu bar, you are querying this index, which is why the results appear instantly.

You can tap directly into this lightning-fast database from the command line using the mdfind (Metadata Find) command.

Basic Searching

To find a file by its name instantly, you don’t need to specify a directory path. You simply query the database:

mdfind -name "budget.pdf"

The terminal will instantly output the absolute path to any file matching that name.

You can also search for the contents of files, exactly as you do in the graphical Spotlight window. If you remember writing a document that contained the phrase “Q4 Projections,” but you forgot what you named the file, type:

mdfind "Q4 Projections"

Advanced Metadata Queries

The true power of mdfind is its ability to query the deep metadata that macOS attaches to files.

macOS tracks hundreds of invisible metadata tags for every file (e.g., the author of a document, the focal length of a photograph, the duration of an MP4). You can see a file’s metadata by running the mdls (Metadata List) command on it.

Once you know the metadata keys, you can build incredibly complex, high-speed queries using the mdfind command.

Example 1: Finding Specific File Types

To find every single Adobe Photoshop document (PSD) on your entire hard drive instantly:

mdfind "kMDItemKind == 'Adobe Photoshop document'"

Example 2: Finding Files by Date

Suppose you want to find all files that were created after January 1, 2023.

mdfind "kMDItemFSCreationDate > $time.iso(2023-01-01T00:00:00Z)"

Example 3: Complex Boolean Logic

You can combine attributes using standard Boolean operators (&& for AND, || for OR).

Suppose you want to find all MP3 audio files on your computer where the “Author” (Artist) metadata tag specifically contains “Beatles”:

mdfind "kMDItemKind == 'MP3 audio' && kMDItemAuthors == '*Beatles*'"

(The asterisks act as wildcards, ensuring it matches whether the tag says “The Beatles” or just “Beatles”).

Restricting the Search Directory

By default, mdfind searches the entire global index. If you only want to search within a specific folder (like your sprawling Documents folder) to reduce the noise of the output, you can use the -onlyin flag.

mdfind -onlyin ~/Documents -name "invoice"

Conclusion

While traditional UNIX tools like find and grep are powerful, they are wildly inefficient on modern macOS architectures. By leveraging the mdfind command, developers and power users can query Apple’s native Spotlight database, reducing search times from minutes to milliseconds.

RELATED POSTS

  • How to Use the Linux time Command to Measure Script Execution Duration
  • How to Use the ‘diskutil’ Command to Securely Erase Hard Drives in macOS
  • How to Use the Linux ionice Command to Prioritize Disk I/O Operations
  • How to Manage and Reclaim Storage Space on macOS
  • How to Change the Name of Your Mac Computer (Hostname)
  • Get the best tech tips delivered straight to your inbox.

    Join thousands of readers mastering Apple, Google, Microsoft, and Linux.