Text Processing // Search
grep
Search for patterns in files — line by line, recursively, globally.
$ grep [OPTIONS] PATTERN [FILE...]
Key Flags
-rRecursive — search all files in a directory -nShow line numbers with each match -iCase-insensitive matching -vInvert — show lines that do NOT match -cCount matching lines instead of printing them -EExtended regex (alternation, +, ?, groups) -lList only filenames that contain a match
Examples
$ grep -rn 'ERROR' /var/log/
/var/log/syslog:4821: kernel: ERROR unable to enumerate USB device
/var/log/auth.log:1203: pam_unix: ERROR conversation failed
Recursive search with line numbers — great for log triage
$ grep -v '^#' /etc/ssh/sshd_config
Port 22
PermitRootLogin no
PasswordAuthentication yes
PubkeyAuthentication yes
Strip comments — shows only active config lines
$ grep -c 'Failed password' /var/log/auth.log
347
Count brute-force attempts without reading every line