AgentSkillsCN

spotlight-search

利用 macOS Spotlight 搜索文件与元数据(mdfind)。

SKILL.md
--- frontmatter
name: spotlight-search
description: Search files and metadata using macOS Spotlight (mdfind).
tags: [macos, search, files, utility]

Spotlight Search

Search files using macOS Spotlight index via mdfind and inspect metadata with mdls. Much faster than find for indexed locations.

Search by Filename

code
exec: mdfind -name "FILENAME"

Search by Content (Full-Text)

code
exec: mdfind "SEARCH_TERM"

Search Within a Specific Directory

code
exec: mdfind -onlyin ~/Documents "SEARCH_TERM"
code
exec: mdfind -onlyin ~/Desktop -name "report"

Search by File Type

PDF files:

code
exec: mdfind "kMDItemContentType == 'com.adobe.pdf'"

Images:

code
exec: mdfind "kMDItemContentType == 'public.image'"

Word documents:

code
exec: mdfind "kMDItemContentType == 'org.openxmlformats.wordprocessingml.document'"

Applications:

code
exec: mdfind "kMDItemContentType == 'com.apple.application-bundle'"

Search by Date

Modified today:

code
exec: mdfind "kMDItemFSContentChangeDate >= $time.today"

Modified in last 7 days:

code
exec: mdfind "kMDItemFSContentChangeDate >= $time.today(-7)"

Created this week:

code
exec: mdfind "kMDItemFSCreationDate >= $time.this_week"

Search by Size

Files larger than 100MB:

code
exec: mdfind "kMDItemFSSize > 100000000"

Combine Conditions

PDFs modified this week:

code
exec: mdfind "kMDItemContentType == 'com.adobe.pdf' && kMDItemFSContentChangeDate >= $time.this_week"

Images larger than 5MB:

code
exec: mdfind "kMDItemContentType == 'public.image' && kMDItemFSSize > 5000000"

Inspect File Metadata

code
exec: mdls /path/to/file

Specific attribute:

code
exec: mdls -name kMDItemContentType /path/to/file

Useful Metadata Attributes

  • kMDItemDisplayName — display name
  • kMDItemContentType — file type UTI
  • kMDItemFSSize — file size in bytes
  • kMDItemFSCreationDate — creation date
  • kMDItemFSContentChangeDate — last modified
  • kMDItemAuthors — document authors
  • kMDItemTitle — document title
  • kMDItemKind — human-readable file kind
  • kMDItemWhereFroms — download source URL
  • kMDItemPixelWidth / kMDItemPixelHeight — image dimensions

Count Results

code
exec: mdfind -count "SEARCH_TERM"

Limit Results

code
exec: mdfind "SEARCH_TERM" | head -20

Notes

  • mdfind uses the Spotlight index, so it's extremely fast but only covers indexed locations.
  • Excluded directories (like .git, node_modules) may not appear in results.
  • To rebuild the Spotlight index: sudo mdutil -E /.
  • For searching file contents by code patterns, consider rg (ripgrep) or grep instead.
  • Combine with read_file to inspect matched files after searching.