Apple - Does mdfind allow wildcard searches on filename?

This should work:

mdfind "kMDItemDisplayName == test*.pdf"

Enclose the pattern in single quotes and add 'c' to match case-insensitively:

mdfind "kMDItemDisplayName == 'test*.pdf'c"

Here is a list of available attributes. You can combine multiple attribute/value pairs with &&.


macOS ships with the regular find command, and mdfind is not a replacement for find.

If you really just want to search the current directory for all pdf files, you should probably just do find . -name '*.pdf'

By default mdfind searches by file content and metadata, and it searches your entire hard drive instead of just a single directory — on my system mdfind pdf finds tens of thousands of results.

If you must use mdfind, because it's faster, you have a few options to limit the results, for example:

mdfind pdf -onlyin .
mdfind 'kMDItemFSName = *.pdf' -onlyin .
mdfind 'kMDItemContentTypeTree = *.pdf' -onlyin .

Use mdls to learn what metadata is available for a file, and the syntax for pattern matching is very minimalist since it needs to match the database index of your filesystem.

Alternatively you can mdfind with grep, giving the speed of mdfind and full regex matching on the file path. For example:

mdfind pdf -onlyin . | grep 'pdf$'

One final caveat is mdfind does not search your actual file system, it only searches the spotlight database which may be missing parts of the filesystem or inaccurate. Use find if you require reliable results. Use mdfind when you require speed over accuracy and when you need complicated search terms (eg find all pdf files that were created within a specific date range and contain a phrase of text).


When explicitly searching for filenames, my experience is that the kMDItemFSName attribute provides more concise results. Another suggestion: add the d comparison modifier (insensitive to diacritical marks) to e.g. match a file called "Entrée Menu.pdf" when searching for "entree":

mdfind 'kMDItemFSName == "*entree*"cd'

Also, since @tcmb's link to the attributes reference is dead, here are two working ones: Spotlight Metadata Attributes and iCloud Metadata Attributes.

Tags:

Macos

Search