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
sed
Stream editor that applies a small program to each line of input. Widely used for search-and-replace and inline file edits.
awk
Pattern-action language for processing columnar text. Excellent for reshaping tabular data, aggregating fields, and quick reports.
find
Recursively walks directories evaluating expressions on each entry. Combines discovery with actions like exec or delete.
cat
Concatenates files and prints them to standard output. Also useful as a simple viewer or to feed content into pipelines.
sort
Sorts lines of text. Supports numeric, human-numeric, reverse, unique, and keyed sorting.