Sub topic 4
How Spotlight metadata makes searching possible
This chapter assumes you can navigate Finder and run a command in Terminal. It also assumes you have applied a tag to a file, which an earlier chapter covered. Here you will see where tag searches and smart folders get their data, so later chapters on criteria and troubleshooting make sense.
Learning goals
- Explain what the Spotlight index is and how it differs from the files themselves.
- Name the kinds of metadata Spotlight stores and tell which ones Finder search criteria use.
- Read a file's metadata with
mdlsand run basic queries withmdfind. - Describe how a tag moves from your click to a search result.
Search reads an index, not your files
When you type in the Finder search field, macOS does not open every file on your disk. It asks the Spotlight index. The index is a database that Spotlight builds and keeps up to date in the background. Each volume has its own index, stored in a hidden folder at the root of that volume.
The index holds two kinds of data:
- Content. Words found inside documents, such as the text of a PDF or a Pages file.
- Metadata. Facts about the file: its name, kind, size, dates, author, and your tags.
Tags and smart folders depend on metadata. A tag search asks, "which indexed items have this tag value?" A smart folder asks the same sort of question, with criteria you choose.
Because the index is a separate copy of this information, it can go stale. Chapter 15 covers what to do when that happens. For now, remember that search results are only as current as the index.
The pipeline from file to result
A background process called mdworker reads new and changed files. It uses small plug-ins called importers, one for each file type. An importer extracts metadata and content and hands it to the index. Finder, mdfind, and smart folders then query the index.
Finder search, smart folders, and mdfind all query the same index. A smart folder is a saved query, so it can only show what mdfind could also find.
Metadata attributes
Each piece of metadata is an attribute with a name that starts with kMDItem. Some attributes come from the file system, and some come from the importer reading the file. A few that matter for Finder work:
| Attribute | What it holds | Where you see it in Finder |
|---|---|---|
| `kMDItemFSName` | The file name | Name criterion |
| `kMDItemKind` | Human-readable kind, such as "PDF Document" | Kind criterion |
| `kMDItemContentType` | Uniform Type Identifier, such as `com.adobe.pdf` | Kind criterion (the underlying value) |
| `kMDItemFSContentChangeDate` | When the content last changed | Last modified date criterion |
| `kMDItemFSCreationDate` | When the file was created | Created date criterion |
| `kMDItemFSSize` | Size in bytes | File size criterion |
| `kMDItemUserTags` | The tags you applied | Tags in the sidebar and in search |
Finder's criteria menu is a friendly layer over these attributes. When you pick "Last modified date is within the last 7 days", Finder builds a query on kMDItemFSContentChangeDate. You will build such searches in the next chapters.
Reading metadata with mdls
mdls lists the metadata Spotlight holds for one file. Use -name to pick a single attribute. This command prints a file's tags:
mdls -name kMDItemUserTags ~/Documents/report.pdfFor a file tagged Red and Work, the output looks like this:
kMDItemUserTags = (
Red,
Work
)A file with no tags prints kMDItemUserTags = (null). That value means the index holds no tags for the file. It does not mean the file is missing.
You can repeat -name to ask for several attributes at once:
mdls -name kMDItemKind -name kMDItemFSSize -name kMDItemFSContentChangeDate ~/Documents/report.pdfRun mdls with only a file path to see every attribute. The list is long, and it differs by file type. A photo has camera and location fields. A PDF has page count and author. A plain text file has far fewer.
To list every attribute name the system knows, run mdimport -A. The output is long, so pipe it through less.
Querying the index with mdfind
mdfind runs a query against the index and prints matching paths. With -onlyin it limits results to one folder. This command finds files that carry the tag Red:
mdfind -onlyin ~/Documents 'kMDItemUserTags == "Red"'This is the same data Finder uses when you click a tag in the sidebar. The query is an expression: an attribute name, a comparison operator, and a value in quotes.
Date attributes accept time functions. This command finds files in Documents that changed in the last seven days:
mdfind -onlyin ~/Documents 'kMDItemFSContentChangeDate >= $time.today(-7)'Here $time.today(-7) means midnight seven days ago. A smart folder with a "last modified within 7 days" rule stores a query of this kind.
Two features help when you write queries by hand:
- Wildcards.
kMDItemFSName == "report*"matches names that start with "report". Addcafter the closing quote, as in"report*"c, to ignore case. - Combining. Join conditions with
&&(and) and||(or). For example,kMDItemUserTags == "Red" && kMDItemKind == "PDF Document"finds red-tagged PDFs.
How a tag reaches the index
A tag is stored as metadata on the file itself, not only in the index. macOS writes it into an extended attribute named com.apple.metadata:_kMDItemUserTags. An extended attribute is a small piece of data attached to a file by the file system, separate from the file's contents.
When the attribute changes, Spotlight notices and re-indexes the file. The index then exposes the tag as kMDItemUserTags. You can look at the raw attribute, which is stored as a binary property list:
xattr -px com.apple.metadata:_kMDItemUserTags ~/Documents/report.pdf | xxd -r -p | plutil -p -The pipeline converts the printed hex back to bytes and lets plutil print them as readable text. Colored tags appear as the tag name followed by a line break and a number for the color. This is why a tag's color travels with the file.
This design has practical consequences:
- Tags survive copies and moves on file systems that support extended attributes, because they are part of the file.
- Tags can be lost when a file goes through something that drops extended attributes, such as some archive formats, email attachments, and some cloud or network file systems.
- A tag can exist on the file but be missing from search if the index has not caught up.
What gets indexed and what does not
Spotlight does not index everything. Knowing the limits explains many "missing file" cases.
- Excluded locations. Folders listed in System Settings under Spotlight, in Search Privacy, are skipped. A tag you apply there stays on the file but never appears in tag search.
- System and hidden areas. Most system folders and many hidden files are left out of Finder searches by default.
- Volumes. Each drive has its own index. A newly attached external drive may need time before its files appear. Some network volumes are not indexed on your Mac at all, though the server may offer its own search.
- Content you cannot read. An importer can only index what it understands. A file type with no importer still has a name, size, and dates, but no content.
You can check whether indexing is on for a volume:
mdutil -s /The output reports "Indexing enabled" when the index is active. Reading this status is safe. Changing it with other mdutil options needs administrator rights and is a troubleshooting step for a later chapter.
Why this matters for smart folders
A smart folder does not contain files. It stores a query and shows the current answer each time you open it. That is why a new matching file appears in it without you doing anything. It is also why the folder can be wrong: if the index has not caught up, the answer is wrong, even though the saved query is fine.
Keep one rule in mind for the rest of this tutorial: tags label files, metadata exposes those labels, and the index answers queries about them. Smart folders and sidebar tag items only offer convenient ways to ask.
Exercise
You tagged a PDF "Review" a minute ago. Clicking the Review tag in the sidebar does not list it. xattr -l on the file shows the tag attribute, but mdls -name kMDItemUserTags prints (null). Where is the problem, and how do you tell?
Show answer
The tag is stored on the file correctly, because the extended attribute exists. The problem is that the Spotlight index has not yet picked up the change, or the file sits in a location Spotlight does not index. Finder search reads the index, not the extended attribute, so it shows nothing. The mismatch between xattr (file) and mdls (index) is what tells you the problem is indexing. Check that the folder is not in Search Privacy and that mdutil -s / reports indexing enabled, then wait for indexing to finish.
Exercise
Write an mdfind command that lists PDF files under ~/Documents that carry the tag Red. Use the attribute names from this chapter.
Show answer
mdfind -onlyin ~/Documents 'kMDItemUserTags == "Red" && kMDItemKind == "PDF Document"'