grep — Basics
Search
Search for a pattern in a file
grep 'pattern' file
Case-insensitive search
grep -i 'pattern' file
Show line numbers with matches
grep -n 'pattern' file
Whole word match only — prevents partial matches
grep -w 'word' file
Whole line match only
grep -x 'exact line' file
Stop after first match for fast early exit
grep -m 1 'pattern' file
Invert and Count
Invert match — show non-matching lines
grep -v 'pattern' file
Count matching lines
grep -c 'pattern' file
File Matching
List filenames containing a match
grep -l 'pattern' *.conf
List filenames NOT containing a match
grep -L 'pattern' *.conf
Extended regex — OR multiple patterns
grep -E 'error|warning|fatal'
Extended regex — alternation with anchors
grep -E '^(start|begin)'
Quiet mode — use exit code in conditionals
grep -q 'pattern' file && echo found
Match against patterns from a file (one per line)
grep -f patterns.txt file
Fixed-string exact-line intersection of two files
grep -Fxf list1.txt list2.txt
Null-terminated match across newlines (GNU extension)
grep -zo 'start.*end' file