grep flags & options

-r, --recursive

Search all files in a directory recursively.

grep -r "TODO" ./src
grep -rn "error" /var/log

-i, --ignore-case

Match regardless of case.

grep -i "error" log.txt

-n, --line-number

Show line numbers alongside matches.

grep -n "function" app.js

-l, --files-with-matches

Print only the names of files that contain matches, not the matches themselves.

grep -rl "TODO" ./src

-L, --files-without-match

Print only the names of files that do not contain matches.

grep -rL "test" ./src

-v, --invert-match

Show lines that do not match the pattern.

grep -v "^#" config.txt

-c, --count

Print only the count of matching lines per file.

grep -c "error" log.txt
grep -rc "TODO" ./src

-E, --extended-regexp

Use extended regular expressions (same as egrep).

grep -E "error|warning|fatal" log.txt
grep -E "^[0-9]{3}-" data.txt

-w, --word-regexp

Match only whole words, not substrings.

grep -w "log" app.js

-A

Show N lines after each match.

grep -A 3 "error" log.txt

-B

Show N lines before each match.

grep -B 2 "error" log.txt

-C

Show N lines before and after each match (context).

grep -C 5 "panic" log.txt

-o, --only-matching

Print only the matched part of the line, not the entire line.

grep -oE "[0-9]+\.[0-9]+" versions.txt

-q, --quiet

Suppress all output. Exit with 0 if a match is found, 1 otherwise. Useful in scripts.

if grep -q "error" log.txt; then echo "Errors found"; fi