text

grep

Searches text for lines matching a regular expression. Central to log analysis and pipeline filtering.

grep [OPTION]... PATTERN [FILE]...

Common flags

Flag Purpose
-i Case-insensitive match
-r Recurse into directories
-n Print the line number of each match
-v Invert match -- print lines that do not match
-E Interpret PATTERN as an extended regex
-F Interpret PATTERN as fixed strings (no regex)
-l Print only names of files with matches
-c Print only the count of matching lines per file

Examples

grep -rn 'TODO' src/

Find all TODO markers with file and line numbers

grep -Ei 'error|warn' app.log

Case-insensitive extended-regex search for errors or warnings

grep -v '^#' /etc/hosts

Show non-comment lines only

grep -Fl 'password=' *.env

List env files containing a fixed literal string

Gotcha

Basic grep treats characters like ( and | literally -- use -E for extended regex or -F for plain strings.

Related commands

← All Linux commands · Linux cheat sheet