Scope Spotlight searches to a folder with mdfind -onlyin
The rarely used -onlyin flag turns mdfind from a whole-system index query into a targeted search inside one directory tree, without the speed cost of find.
Run `mdfind -onlyin ~/Documents 'annual report'` to search only inside your Documents folder.
01Command
Command
$ mdfind -onlyin ~/Documents 'annual report'
mdfind -onlyin /Users/you/Projects 'kMDItemFSName == "*.swift"'02What it does
-onlyin accepts a directory path and constrains Spotlight's index query to that subtree. Because it still queries the prebuilt index rather than walking the filesystem, it returns instantly even in large folders — the trade-off is that only indexed metadata is searched.
03Risk
!Warning: If the target folder is excluded from Spotlight indexing, -onlyin will return nothing even though files exist there.
04Undo
✓No undo needed — this command does not change persistent state.
05Sources
- mdfind — find files using Spotlight metadataEvidence: Documents the -onlyin flag for limiting the search scope to a directory.
Last verified: 2026-09-17 · Category: Files & Folders