Search Inside Files with grep -rn

Find which file contains a specific phrase — with file name and line number — using grep's recursive search.

macOSsearchFilesTerminalLow risk
Run grep -rn "invoice total" ~/Documents to search every file in Documents and print each match with its file name and line number.

01Command

Command
$ grep -rn "invoice total" ~/Documents

02What 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

RiskLow risk
!Warning: grep matches plain text only — for Apple's Pages, PDF, or Word documents it will report 'Binary file matches' at best; convert or unzip those first.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

  • GREP(1) - macOS manual page
    Apple (Xcode man pages mirror) · vendor-doc · 2026-09-17
    Evidence: The manual page documents --recursive for searching directories recursively and -n for printing line numbers with output lines.
  • grep - Search files for matching lines
    SS64 · technical · 2026-09-17
    Evidence: 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