Sub topic 14
Spotlight from Terminal: mdfind and mdls
This chapter assumes you can open Terminal and run commands, and that you know how Spotlight indexes files and stores metadata. Spotlight keeps an index of file names, contents, and metadata. The mdfind and mdls commands let you query that index from the command line, so you can use the results in scripts or pipelines.
How the two commands relate
mdfind searches the index and prints the paths of matching files. mdls prints the metadata attributes stored for one file. You use mdls to learn which attributes exist, then use those attributes in mdfind queries.
Searching by name and content
A plain mdfind query behaves like Spotlight. It matches file contents and metadata, not only names. To match file names only, add -name.
mdfind -name "invoice"Without -name, a query for invoice also returns files that mention the word inside them. Use -name when you only want files called "invoice". Matching is not case-sensitive by default.
Limiting the search to a folder
By default mdfind searches every indexed location. The -onlyin option limits the search to one folder and its subfolders. You can combine it with an attribute query. This example returns only PDF files in your Documents folder.
mdfind -onlyin ~/Documents "kMDItemContentType == 'com.adobe.pdf'"The query has three parts. kMDItemContentType is the attribute name. == is the comparison. 'com.adobe.pdf' is the value, written in single quotes inside the double-quoted query. The content type identifies the kind of file by its content, not by its extension.
Reading metadata with mdls
To find attribute names, run mdls on a file you already have.
mdls ~/Documents/report.pdfThe output lists lines such as kMDItemContentType, kMDItemFSName, kMDItemContentModificationDate, and kMDItemPageCount. Which attributes appear depends on the file type. A photo has camera fields, and a PDF has page counts. To print one attribute, add -name:
mdls -name kMDItemContentType ~/Documents/report.pdfCopy the value it prints into an mdfind query to find other files of that kind.
Useful comparison forms
The same attribute syntax supports more than equality.
| Query | Meaning |
|---|---|
| `kMDItemFSName == 'report*'` | Names that start with "report" |
| `kMDItemFSName == '*.pdf'c` | Names that end in .pdf, ignoring case (`c` suffix) |
| `kMDItemPageCount > 10` | More than ten pages |
| `kMDItemContentModificationDate >= $time.today(-7)` | Modified in the last seven days |
Combine conditions with && for AND and || for OR.
Checking the index
If mdfind returns nothing for a file you know exists, the index may be off or stale. Check whether indexing is enabled for the startup volume:
mdutil -s /To rebuild the index, erase it and let macOS recreate it:
sudo mdutil -E /Common errors
- Quoting problems. The whole query must be one argument. Wrap it in double quotes and use single quotes around values.
- Expecting live results for excluded folders. Folders excluded in Spotlight settings are not indexed, so
mdfindwill not find files in them. - Using the extension instead of the content type. The attribute
kMDItemContentTypeexpects an identifier such ascom.adobe.pdf, notpdf. - Forgetting that plain queries search content. Use
-nameto match names only.
Exercise
You want to list only the PDF files inside ~/Documents, and you are not sure of the attribute name for file type. Which two commands do you use, and in what order?
Show answer
First run mdls ~/Documents/report.pdf (or any PDF) to see that the type is stored in kMDItemContentType as com.adobe.pdf. Then run mdfind -onlyin ~/Documents "kMDItemContentType == 'com.adobe.pdf'".
Exercise
mdfind invoice returns many files, but you only want files with "invoice" in the file name. What do you change?
Show answer
Use the -name option: mdfind -name "invoice". This matches file names only, not file contents.