Search Inside Files with grep -rn
Find which file contains a specific phrase — with file name and line number — using grep's recursive search.
01Command
$ grep -rn "invoice total" ~/Documents02What it does
grep -r walks an entire folder tree looking inside every file, and -n prefixes each hit with its line number so you can jump straight to it. Add -i to ignore case (grep -rni "invoice" ...) or limit to certain files with --include, e.g. --include="*.txt". Spotlight indexes content too, but grep searches the actual bytes on disk, which makes it reliable for config files, code, and freshly created text that Spotlight hasn't indexed yet.
03Risk
04Undo
✓No undo needed — this command does not change persistent state.
05Sources
- GREP(1) - macOS manual pageEvidence: The manual page documents --recursive for searching directories recursively and -n for printing line numbers with output lines.
- grep - Search files for matching linesEvidence: Documents the -r option ('search recursively through subdirectories') and -n ('each output line is preceded by its relative line number').
Last verified: 2026-09-19 · Category: Files & Folders
06Related solutions
List the biggest files in your home folder that are eating disk space.
Change the extension of every matching file in a folder with one shell loop.
Make a file or folder invisible in Finder without moving or renaming it.