Spotlight Search
Overview
Use mdfind first for indexed search, mdls for metadata inspection, and mdutil only when diagnosing indexing issues. If Spotlight misses a known file, use the bundled fallback script instead of hand-rolling a find command every time.
Quick Start
Preferred flow:
- 1. Use
mdfind for normal search. - Use
mdls when the task is about metadata, not discovery. - Use
./scripts/spotlight_search.sh when Spotlight indexing is incomplete or results are unexpectedly empty. - Treat the fallback script as a filename/path matcher, not a general shell helper.
- Use
mdutil only to inspect indexing, and only suggest re-indexing when the user explicitly wants that system-level change.
CODEBLOCK0
For a hybrid search that tries Spotlight first and falls back to find, use the bundled script:
CODEBLOCK1
Core Capabilities
1. File Search
Spotlight indexes file contents and metadata, making it ideal for locating documents when you remember words inside them.
Common patterns:
- - Simple keyword: INLINECODE9
- By file type: INLINECODE10
- By extension: INLINECODE11
- By date: INLINECODE12
- By size:
mdfind 'kMDItemFSSize > 1000000' (files >1 MB)
When to use: You recall words from the file's content, need to filter by type/date, or want to search across all indexed volumes quickly.
2. Application Search
Spotlight knows about installed applications (.app bundles) and can find them by name or bundle identifier.
Examples:
CODEBLOCK2
When to use: You need to locate an installed app, verify its presence, or get its bundle path.
3. Metadata Inspection
Use mdls to view the rich metadata Spotlight stores for any file.
CODEBLOCK3
Common attributes: kMDItemKind, kMDItemContentType, kMDItemAuthors, kMDItemContentCreationDate, kMDItemPixelHeight, kMDItemDurationSeconds.
When to use: You need to check file properties (creation date, dimensions, author) without opening the file.
4. Hybrid Search (Fallback)
Spotlight indexing may be incomplete (excluded directories, recent files not yet indexed). The bundled script scripts/spotlight_search.sh provides a hybrid approach:
- 1. Attempts Spotlight search via INLINECODE23
- If no results, falls back to
find with name‑based matching - Uses shell argument arrays instead of
eval, so the query is passed as data rather than executable shell text - Supports filtering by kind (
pdf, image, application, etc.)
Usage:
CODEBLOCK4
When to use: You want maximum recall, or Spotlight is returning empty results for known files.
Reference Materials
For detailed query syntax, operator reference, and advanced examples, see:
- - mdfind Query Reference – Comprehensive guide to
mdfind syntax, attribute filters, date comparisons, and practical examples.
Load this reference when you need to construct complex queries or understand Spotlight's capabilities.
Workflow Guidance
Choosing the Right Tool
| Scenario | Recommended Tool | Example |
|---|
| "Find files containing 'budget'" | INLINECODE30 | Content‑aware search |
| "Find all PDFs modified this week" |
mdfind 'kind:pdf AND kMDItemContentModificationDate >= $time.this_week' | Metadata‑filtered search |
| "Find the Chrome app" |
mdfind 'kMDItemContentType == com.apple.application-bundle && kMDItemFSName == "*chrome*"cd' | Application search |
| "Search only in Downloads folder" |
mdfind -onlyin ~/Downloads "kind:image" | Scoped search |
| "Check when a file was created" |
mdls -name kMDItemContentCreationDate /path/to/file | Metadata inspection |
| "Spotlight returns nothing but I know the file exists" |
./scripts/spotlight_search.sh "filename" | Hybrid fallback |
Troubleshooting
No results for known files?
- 1. Check indexing status: INLINECODE36
- Ensure the directory is indexed (Spotlight excludes some system and Library folders)
- Re-index only with explicit user approval because it is a privileged, system-wide action: INLINECODE37
Query syntax errors?
- - Enclose queries containing spaces or special characters in single quotes.
- Use
cd after == for case‑ and diacritic‑insensitive matching: INLINECODE40 - Combine filters with
AND/OR and parentheses.
Slow performance?
- - Limit results with INLINECODE43
- Restrict search scope with INLINECODE44
Notes
- - Spotlight indexing is not real‑time; newly created files may take minutes to appear.
- Some directories (e.g.,
~/Library, system folders) may be excluded from indexing. - INLINECODE46 searches all indexed volumes by default; use
-onlyin to constrain. - The hybrid script requires
find (always present) and optionally fd (faster).
See Also
- - INLINECODE50
- INLINECODE51
- INLINECODE52
- Apple's Uniform Type Identifier reference: https://developer.apple.com/documentation/uniformtypeidentifiers
Spotlight 搜索
概述
优先使用 mdfind 进行索引搜索,使用 mdls 检查元数据,仅在诊断索引问题时使用 mdutil。如果 Spotlight 找不到已知文件,请使用附带的回退脚本,而不是每次都手动编写 find 命令。
快速入门
推荐流程:
- 1. 使用 mdfind 进行常规搜索。
- 当任务涉及元数据而非查找时,使用 mdls。
- 当 Spotlight 索引不完整或结果意外为空时,使用 ./scripts/spotlight_search.sh。
- 将回退脚本视为文件名/路径匹配器,而非通用 shell 辅助工具。
- 仅使用 mdutil 检查索引状态,且仅在用户明确要求进行系统级更改时,才建议重新索引。
bash
查找名称或内容中包含 invoice 的文件
mdfind invoice
查找今天修改过的 PDF 文件
mdfind kind:pdf AND kMDItemContentModificationDate >= $time.today
查找名称中包含 chrome 的应用程序
mdfind kMDItemContentType == com.apple.application-bundle && kMDItemFSName ==
chromecd
仅在 ~/Documents 目录中搜索
mdfind -onlyin ~/Documents kind:pdf
查看文件的元数据
mdls /path/to/file
对于先尝试 Spotlight 再回退到 find 的混合搜索,请使用附带的脚本:
bash
./scripts/spotlight_search.sh query
./scripts/spotlight_search.sh -t pdf report
./scripts/spotlight_search.sh -d ~/Documents meeting
核心功能
1. 文件搜索
Spotlight 会索引文件内容和元数据,非常适合在记住文件内容关键词时定位文档。
常见模式:
- - 简单关键词:mdfind quarterly report
- 按文件类型:mdfind kind:pdf invoice
- 按扩展名:mdfind kMDItemFSName == *.mdcd
- 按日期:mdfind kMDItemContentModificationDate >= $time.yesterday
- 按大小:mdfind kMDItemFSSize > 1000000(大于 1 MB 的文件)
使用场景: 你记得文件内容中的关键词,需要按类型/日期筛选,或希望快速搜索所有已索引的卷。
2. 应用程序搜索
Spotlight 能够识别已安装的应用程序(.app 包),并可通过名称或包标识符进行查找。
示例:
bash
所有应用程序
mdfind kMDItemContentType == com.apple.application-bundle
名称中包含 visual 的应用程序
mdfind kMDItemContentType == com.apple.application-bundle && kMDItemFSName ==
visualcd
使用场景: 你需要定位已安装的应用程序,验证其是否存在,或获取其包路径。
3. 元数据检查
使用 mdls 查看 Spotlight 为任何文件存储的丰富元数据。
bash
所有元数据
mdls /path/to/file
特定属性
mdls -name kMDItemKind /path/to/file
常见属性: kMDItemKind、kMDItemContentType、kMDItemAuthors、kMDItemContentCreationDate、kMDItemPixelHeight、kMDItemDurationSeconds。
使用场景: 你需要检查文件属性(创建日期、尺寸、作者)而无需打开文件。
4. 混合搜索(回退)
Spotlight 索引可能不完整(排除的目录、尚未索引的新文件)。附带的脚本 scripts/spotlight_search.sh 提供了混合方法:
- 1. 通过 mdfind 尝试 Spotlight 搜索
- 如果没有结果,则回退到基于名称匹配的 find
- 使用 shell 参数数组而非 eval,因此查询作为数据而非可执行的 shell 文本传递
- 支持按类型过滤(pdf、image、application 等)
用法:
bash
./scripts/spotlight_search.sh query
./scripts/spotlight_search.sh -t pdf report
./scripts/spotlight_search.sh -d ~/Documents -l 10 draft
使用场景: 你希望获得最大召回率,或者 Spotlight 对已知文件返回空结果。
参考资料
有关详细的查询语法、运算符参考和高级示例,请参阅:
当你需要构建复杂查询或了解 Spotlight 的功能时,请加载此参考。
工作流程指南
选择合适的工具
| 场景 | 推荐工具 | 示例 |
|---|
| 查找包含 budget 的文件 | mdfind budget | 内容感知搜索 |
| 查找本周修改过的所有 PDF |
mdfind kind:pdf AND kMDItemContentModificationDate >= $time.this_week | 元数据过滤搜索 |
| 查找 Chrome 应用 | mdfind kMDItemContentType == com.apple.application-bundle && kMDItemFSName ==
chromecd | 应用程序搜索 |
| 仅在 Downloads 文件夹中搜索 | mdfind -onlyin ~/Downloads kind:image | 限定范围搜索 |
| 检查文件创建时间 | mdls -name kMDItemContentCreationDate /path/to/file | 元数据检查 |
| Spotlight 返回空结果,但我确定文件存在 | ./scripts/spotlight_search.sh filename | 混合回退 |
故障排除
已知文件无结果?
- 1. 检查索引状态:mdutil -s /
- 确保目录已被索引(Spotlight 会排除某些系统和 Library 文件夹)
- 仅在用户明确批准后重新索引,因为这是需要特权的系统级操作:sudo mdutil -E /
查询语法错误?
- - 将包含空格或特殊字符的查询用单引号括起来。
- 在 == 后使用 cd 进行大小写和变音符号不敏感匹配:kMDItemFSName == *.pdfcd
- 使用 AND/OR 和括号组合过滤器。
性能缓慢?
- - 使用 -limit N 限制结果数量
- 使用 -onlyin 限制搜索范围
注意事项
- - Spotlight 索引并非实时;新创建的文件可能需要几分钟才能出现。
- 某些目录(例如 ~/Library、系统文件夹)可能被排除在索引之外。
- mdfind 默认搜索所有已索引的卷;使用 -onlyin 进行限制。
- 混合脚本需要 find(始终存在)和可选的 fd(更快)。
另请参阅
- - man mdfind
- man mdls
- man mdutil
- Apple 的统一类型标识符参考:https://developer.apple.com/documentation/uniformtypeidentifiers