使用 Spotlight 命令行工具(mdfind、mdls、mdutil)在 macOS 上搜索文件、应用程序和元数据,并针对索引不完整的情况提供备用脚本。
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.
Preferred flow:
mdfind for normal search.mdls when the task is about metadata, not discovery../scripts/spotlight_search.sh when Spotlight indexing is incomplete or results are unexpectedly empty.mdutil only to inspect indexing, and only suggest re-indexing when the user explicitly wants that system-level change.bash
# Find files containing "invoice" in name or content
mdfind "invoice"
# Find PDFs modified today
mdfind 'kind:pdf AND kMDItemContentModificationDate >= $time.today'
# Find applications whose name contains "chrome"
mdfind 'kMDItemContentType == com.apple.application-bundle && kMDItemFSName == "*chrome*"cd'
# Search only within ~/Documents
mdfind -onlyin ~/Documents "kind:pdf"
# View metadata for a file
mdls /path/to/file
For a hybrid search that tries Spotlight first and falls back to find, use the bundled script:
bash
./scripts/spotlight_search.sh "query"
./scripts/spotlight_search.sh -t pdf "report"
./scripts/spotlight_search.sh -d ~/Documents "meeting"
Spotlight indexes file contents and metadata, making it ideal for locating documents when you remember words inside them.
Common patterns:
mdfind "quarterly report"mdfind "kind:pdf invoice"mdfind 'kMDItemFSName == "*.md"cd'mdfind 'kMDItemContentModificationDate >= $time.yesterday'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.
Spotlight knows about installed applications ( .app bundles) and can find them by name or bundle identifier.
Examples:
bash
# All applications
mdfind 'kMDItemContentType == com.apple.application-bundle'
# Applications containing "visual" in name
mdfind 'kMDItemContentType == com.apple.application-bundle && kMDItemFSName == "*visual*"cd'
When to use: You need to locate an installed app, verify its presence, or get its bundle path.
Use mdls to view the rich metadata Spotlight stores for any file.
bash
# All metadata
mdls /path/to/file
# Specific attribute
mdls -name kMDItemKind /path/to/file
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.
Spotlight indexing may be incomplete (excluded directories, recent files not yet indexed). The bundled script scripts/spotlight_search.sh provides a hybrid approach:
mdfindfind with name‑based matchingeval, so the query is passed as data rather than executable shell textpdf, image, application, etc.)Usage:
bash
./scripts/spotlight_search.sh "query"
./scripts/spotlight_search.sh -t pdf "report"
./scripts/spotlight_search.sh -d ~/Documents -l 10 "draft"
When to use: You want maximum recall, or Spotlight is returning empty results for known files.
For detailed query syntax, operator reference, and advanced examples, see:
mdfind syntax, attribute filters, date comparisons, and practical examples.Load this reference when you need to construct complex queries or understand Spotlight's capabilities.
ScenarioRecommended ToolExample"Find files containing 'budget'"mdfind "budget"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/fileMetadata inspection"Spotlight returns nothing but I know the file exists"./scripts/spotlight_search.sh "filename"Hybrid fallback
No results for known files?
mdutil -s /sudo mdutil -E /Query syntax errors?
cd after == for case‑ and diacritic‑insensitive matching: kMDItemFSName == "*.pdf"cdAND/ OR and parentheses.Slow performance?
-limit N-onlyin~/Library, system folders) may be excluded from indexing.mdfind searches all indexed volumes by default; use -onlyin to constrain.find (always present) and optionally fd (faster).man mdfindman mdlsman mdutil