Search text recursively in a directory with grep

Find every occurrence of a pattern across all files in a tree, printed as file:line:number matches.

LinuxFilesTerminaldeveloperLow risk
Run grep -rn "TODO" src/ to list every line containing TODO under src/, prefixed with file name and line number.

01Command

Command
$ grep -rn "TODO" src/

02What it does

-r recurses into directories, -n numbers the matching lines, and output is colored file:line:content on terminals. The pattern is a regular expression; quote it so the shell doesn't expand it. Useful add-ons: -i for case-insensitive, -l to list only file names, -w for whole-word matches, --include='*.py' to limit file types, and --exclude-dir=node_modules to skip noise. For literal strings pass -F so regex metacharacters like . or * match themselves. If results are huge, ripgrep (rg) is a faster drop-in alternative with these defaults built in.

03Risk

RiskLow risk

04Undo

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

05Sources

  • grep(1) — print lines matching a pattern
    man7.org · technical · 2026-09-17
    Evidence: The grep man page documents -r for recursive directory search and -n to prefix each output line with its 1-based line number.

Last verified: 2026-09-17 · Category: Developer

06Related solutions