mdfind — Spotlight Search from the Command Line
Practical guide to mdfind: macOS Spotlight search in the terminal — fast file and metadata queries over the Spotlight index, plus mdls and mdutil.
mdfind searches the Spotlight index straight from the command line – which makes it noticeably faster than find, because it queries a ready-made database instead of walking through directories. You can search not only by filename, but by content and metadata too: images above a certain resolution, files from a particular download, anything changed since yesterday. Using kMDItem attributes, you can build surprisingly precise queries. This guide walks you through the commands you reach for daily – rounded out with mdls for reading a file's metadata and mdutil for managing the index itself.
mdfind — Basic Search
mdfind '<query>' — Search for files matching a query (like Spotlight).
mdfind 'invoice 2026'mdfind -name '<filename>' — Search by filename only.
mdfind -name 'docker-compose.yml'mdfind -onlyin <dir> '<query>' — Limit search to a specific directory.
mdfind -onlyin ~/Projects 'TODO'mdfind -count '<query>' — Show only the count of matching files.
mdfind -count -name '*.pdf'mdfind -live '<query>' — Continuously watch for new matches (live query).
mdfind -live 'screenshot'mdfind — Metadata Queries
mdfind 'kMDItemContentType == "<type>"' — Find files by content type (UTI).
mdfind 'kMDItemContentType == "public.png-image"'mdfind 'kMDItemKind == "<kind>"' — Find files by kind.
mdfind 'kMDItemKind == "PDF Document"'mdfind 'kMDItemFSSize > <bytes>' — Find files larger than a specific size.
mdfind 'kMDItemFSSize > 100000000'mdfind 'kMDItemContentModificationDate > $time.today(-7)' — Find files modified in the last 7 days.
mdfind 'kMDItemContentModificationDate > $time.today(-7)'mdfind 'kMDItemWhereFroms == "*example.com*"' — Find files downloaded from a specific domain.
mdfind 'kMDItemWhereFroms == "*github.com*"'mdfind 'kMDItemPixelWidth > 3000 && kMDItemContentType == "public.jpeg"' — Find high-resolution JPEG images.
mdfind 'kMDItemPixelWidth > 3000 && kMDItemContentType == "public.jpeg"'mdls — File Metadata
mdls <file> — Show all Spotlight metadata attributes of a file.
mdls photo.jpgmdls -name <attribute> <file> — Show a specific metadata attribute.
mdls -name kMDItemContentType document.pdfmdls -name kMDItemPixelWidth -name kMDItemPixelHeight <file> — Show multiple specific attributes.
mdls -name kMDItemPixelWidth -name kMDItemPixelHeight photo.pngmdls -name kMDItemWhereFroms <file> — Show where a file was downloaded from.
mdls -name kMDItemWhereFroms ~/Downloads/installer.pkgmdls -name kMDItemDurationSeconds <file> — Show duration of an audio/video file.
mdls -name kMDItemDurationSeconds song.mp3Spotlight Index Management
mdutil -s / — Show Spotlight indexing status for a volume.
mdutil -s /mdutil -i on / — Enable Spotlight indexing on a volume.
sudo mdutil -i on /mdutil -i off <volume> — Disable Spotlight indexing on a volume.
sudo mdutil -i off /Volumes/Externalmdutil -E / — Erase and rebuild the Spotlight index.
sudo mdutil -E /Common Patterns
mdfind -name '.env' -onlyin ~/Projects — Find all .env files in your projects.
mdfind -name '.env' -onlyin ~/Projectsmdfind 'kMDItemContentType == "com.apple.application-bundle"' -onlyin /Applications — List all applications.
mdfind 'kMDItemContentType == "com.apple.application-bundle"' -onlyin /Applicationsmdfind -name '<filename>' | head -1 | xargs open — Find a file and open it immediately.
mdfind -name 'readme.md' | head -1 | xargs openmdfind 'kMDItemFSCreationDate > $time.today(-1)' -onlyin ~/Downloads — Find files downloaded today.
mdfind 'kMDItemFSCreationDate > $time.today(-1)' -onlyin ~/Downloads Conclusion
On indexed volumes, mdfind is almost always the faster choice over find: it queries the prebuilt Spotlight index instead of traversing the file system. The key caveat: it only finds what is actually indexed – system paths, excluded folders, or volumes with indexing turned off simply won't show up. When in doubt, check the status with mdutil -s, and use mdls to inspect a file's kMDItem attributes so you can refine your queries. Once you grasp how Spotlight collects metadata, a single precise query can replace many awkward find constructions.
Further Reading
- mdfind – man page (Apple Developer) – official reference for options and query syntax
- File Metadata Query Expression Syntax – overview of the
kMDItemattributes
Related Commands
- caffeinate – prevents the Mac from going to sleep
- defaults – reads and writes macOS preferences
- diskutil – manages disks, partitions and volumes