Sub topic 12
The index and file metadata
Spotlight does not search your disk when you type. It searches a prebuilt database called the index. This chapter explains what the index holds, how it stays current, and how metadata attributes make precise searches possible. It assumes you can use Finder and Terminal at a basic level and have already used Spotlight to find files by name and kind.
What the index is
The Spotlight index is a database that macOS builds and maintains for each volume. For every indexed file it stores two kinds of information:
- Metadata: facts about the file, such as its name, kind, size, dates, and author.
- Content: words extracted from inside the file, such as the text of a PDF, a Pages document, or a mail message.
A search looks up your terms in this database and returns matching entries. Because the lookup does not read the files themselves, results appear almost instantly, even across hundreds of thousands of files.
The index is stored on the volume it describes, in a hidden folder at the top of that volume called .Spotlight-V100. You should not edit or move it. If it is damaged, you rebuild it (covered in the next chapter) instead of repairing it by hand.
How the index stays current
macOS does not rescan the disk on a schedule. The file system reports changes as they happen, such as a file being created, renamed, edited, or deleted. Background processes then update the affected index entries. Two of these processes are named mds (the metadata server, which answers queries and coordinates updates) and mdworker (helpers that read files and extract their metadata and text).
This has practical consequences:
- A file you just saved is usually searchable within seconds.
- A large batch of changes, such as copying a big folder, can take minutes to index. During that time you may see extra CPU use from
mdworker. - After a macOS update or an index rebuild, indexing can run for hours on a large disk. Results are incomplete until it finishes.
Importers: how content gets in
Spotlight reads each file type through an importer, a plug-in that knows how to extract text and attributes from one family of formats. macOS ships with importers for common types such as PDF, plain text, Office documents, images, audio, and mail. Some apps install their own importers so that their private file formats become searchable.
The importer decides what is searchable. For example:
- A PDF with real text is searchable by content. A scanned PDF that is only a picture of a page has no text layer, so a content search will not find its words unless the text has been recognized first.
- A photo is searchable by attributes such as its capture date and camera, because the importer reads the embedded EXIF data.
- A file in a format with no importer is indexed by its name and basic file attributes only.
Metadata attributes
Every attribute in the index has a name beginning with kMDItem. Some are universal, and some apply only to certain kinds of file.
| Attribute | Meaning | Example value |
|---|---|---|
| `kMDItemFSName` | File name | `report.pdf` |
| `kMDItemContentType` | Uniform Type Identifier (UTI) of the file | `com.adobe.pdf` |
| `kMDItemFSSize` | Size in bytes | `482113` |
| `kMDItemContentCreationDate` | When the content was created | `2026-03-02 09:14:11 +0000` |
| `kMDItemContentModificationDate` | When the content last changed | `2026-11-20 16:40:02 +0000` |
| `kMDItemAuthors` | Document authors | `("Dana Reyes")` |
| `kMDItemPageCount` | Number of pages | `12` |
A Uniform Type Identifier is a standard name for a file type, written in reverse-domain style. com.adobe.pdf is a PDF, public.jpeg is a JPEG image, and public.plain-text is plain text. UTIs form a hierarchy: a JPEG is also an image, and public.image matches all images. This is how the kind: filter in the search box can match a whole family of files.
Inspecting metadata with mdls
The mdls command prints every attribute the index holds for one file. Use it to see exactly what Spotlight knows, which tells you what you can search on.
mdls ~/Documents/report.pdfYou can also ask for a single attribute with -name, and -raw removes the label so the output is easy to use in a script:
mdls -name kMDItemPageCount -raw ~/Documents/report.pdfIf an attribute does not apply to a file, mdls prints (null). That is normal. A text file has no page count, for example.
Querying attributes with mdfind
The attribute names are what mdfind queries use. A query has the form attribute operator value. The -onlyin option limits the search to one folder.
mdfind -onlyin ~/Documents "kMDItemContentType == 'com.adobe.pdf'"You can combine conditions with && (and) and || (or). This finds PDFs larger than one megabyte:
mdfind -onlyin ~/Documents "kMDItemContentType == 'com.adobe.pdf' && kMDItemFSSize > 1000000"The $time.today and $time.this_week helpers let you compare dates without writing timestamps:
mdfind -onlyin ~/Documents "kMDItemContentModificationDate >= \$time.this_week"Wildcards work in string values, and the modifier c makes a comparison ignore case. This finds names that contain "invoice" in any capitalization:
mdfind "kMDItemFSName == '*invoice*'c"Checking and rebuilding the index
mdutil reports and controls indexing for a volume. This command shows whether indexing is on for the startup volume:
mdutil -s /A healthy result reads Indexing enabled. If you see Indexing disabled., nothing new is being added to that volume. Erasing the index forces macOS to rebuild it:
sudo mdutil -E /Rebuilding can take a long time and uses extra CPU, so do it only when results are actually wrong. The chapter on troubleshooting covers when and how.
What is not indexed
Knowing the limits explains most "why can't Spotlight find it" cases.
- Excluded locations. Folders and disks you add to Spotlight's Search Privacy list are skipped on purpose.
- System and hidden files. Files inside system folders, and many files whose names begin with a dot, are not returned by default.
- Package contents. An app or a document package (a folder that Finder shows as a single file) is indexed as one item. Files deep inside are generally not listed separately.
- Unsupported or encrypted content. Password-protected documents expose their name and basic attributes, but not their text.
- Volumes that are not indexed. Some network shares and disks formatted for other systems are not indexed locally. Check with
mdutil -s /Volumes/Name.
How the pieces fit together
Common mistakes
- Expecting instant results after a bulk copy. The index trails behind large changes. Wait, then search again.
- Rebuilding the index first. Most missing results come from an excluded folder, an unsupported file type, or a scanned document with no text. Check
mdlsfor the file before erasing anything. - Quoting problems in Terminal. The
$in$time.todayis read by the shell. Escape it as\$time.todayinside double quotes, or wrap the whole query in single quotes and use double quotes inside. - Assuming file names are the only thing searched. A plain Spotlight query also matches content and metadata, so unexpected results often come from a word inside a file. Use
mdfind -namewhen you want names only.
Exercise
A colleague says Spotlight can't find a scanned contract by a phrase printed in it, though it finds the file by name. Give the most likely reason, and say what you would check with mdls.
Show answer
The scan is an image with no text layer, so the importer has no words to put in the index. Only the name and basic attributes are indexed. Run mdls on the file and check the content type (it will be a PDF or image type) and whether any text attributes appear. The fix is to run text recognition on the document so it has a real text layer, then let Spotlight reindex it.
Exercise
Write an mdfind command that lists PDFs in ~/Downloads modified this week.
Show answer
mdfind -onlyin ~/Downloads "kMDItemContentType == 'com.adobe.pdf' && kMDItemContentModificationDate >= \$time.this_week"