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.

macOSmdfindscopespotlightLow risk
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

RiskLow risk
RequirementsSpotlight indexing must be enabled for the folder being searched
!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

Last verified: 2026-09-17 · Category: Files & Folders

06Related solutions