man.lsPlain-English help for Unix commands

grep Search text for lines that match a pattern

Reads files or piped input and prints the lines that match a pattern. Use it to find an error in a log, a word in a set of files, or to filter the output of another command.

Synopsis

grep [OPTION]... PATTERN [FILE]...
command | grep [OPTION]... PATTERN

Common options

Option or formWhat it does
-iIgnore case, so error matches Error and ERROR
-vInvert the match: print the lines that do not match
-nPut the line number in front of each matching line
-r, -RSearch every file under the given folders. On macOS and BSD, give a folder such as . explicitly
-cPrint only the number of matching lines, not the lines themselves
-EUse extended regular expressions, so | (or), + and ? work without backslashes
-lPrint only the names of files that contain a match
-wMatch whole words only, so cat does not match category

Examples

Find lines containing a word
grep error notes.txt
beta error
error: disk full
Ignore case and show line numbers
grep -in beta notes.txt
2:beta error
4:Beta again
Count matching lines
grep -c ERROR app.log
2
Show every line except the matches
grep -v error notes.txt
alpha
gamma
Beta again
delta
List the files that contain a word
grep -rl ERROR .
./app.log
Match one of several patterns
grep -E 'ERROR|WARN' app.log
2026-01-01 ERROR failed to open
2026-01-02 WARN slow
2026-01-02 ERROR timeout

Notes

See also

Last reviewed 2026-10-07.