用 mdfind -onlyin 限定 Spotlight 搜索范围
-onlyin 是 mdfind 中少有人用的标志,能把全系统索引查询变成只在某个目录树内进行的定向搜索,速度远快于 find。
运行 `mdfind -onlyin ~/Documents 'annual report'` 即可只搜索"文稿"文件夹内的内容。
01命令
命令
$ mdfind -onlyin ~/Documents 'annual report'
mdfind -onlyin /Users/you/Projects 'kMDItemFSName == "*.swift"'02作用说明
-onlyin 接受一个目录路径,把 Spotlight 的索引查询限制在该子树内。由于它仍然查询预先建好的索引而不是遍历文件系统,即使在大目录中也能立即返回——代价是只能搜索已被索引的元数据。
03风险
!注意: 如果目标文件夹被排除在 Spotlight 索引之外,即使里面有文件,-onlyin 也会返回空结果。
04撤销
✓无需撤销——该命令不会改变持久状态。
05来源
- mdfind — find files using Spotlight metadata依据: Documents the -onlyin flag for limiting the search scope to a directory.
最后验证: 2026-09-17 · 分类: 文件与文件夹