grep
grep searches files or standard input for lines that match a pattern and prints them. It is the standard tool for filtering logs, searching code, and narrowing command output.
Understanding grep
What Is grep Used For?
grep reads input line by line and prints only the lines matching a pattern, which can be plain text or a regular expression. It is usually the first tool to reach for when hunting an error in a log, a setting in a config file, or a string in a codebase.
grep 'ERROR' /var/log/app.logHow to Use grep Options
grep -i 'error' app.log # ignore casegrep -v 'DEBUG' app.log # lines NOT matchinggrep -n 'ERROR' app.log # show line numbersgrep -c 'ERROR' app.log # count matching linesgrep -l 'ERROR' /var/log/*.log # filenames onlygrep -r 'database_url' /etc/ # recursivegrep -r 'API_KEY' /opt/apps --include='*.py'grep -B 3 -A 3 'CRITICAL' app.log # context linesgrep -F '10.0.1.50' app.log # literal text, not regexzgrep 'ERROR' app.log.gz # compressed filesWhich Regex Mode Should I Use?
Basic grep uses Basic Regular Expressions (BRE), where +, ?, and | need a backslash. -E enables Extended Regular Expressions (ERE) where they work directly. -P enables Perl-compatible regex (a GNU grep option) for lookarounds and non-greedy matching.
grep -E 'ERROR|WARN|FATAL' app.loggrep -E 'error[0-9]+' app.loggrep -P '(?<=user_id=)\d+' app.logPractical Log Analysis Patterns
# Errors per hour (timestamps like 2026-01-15 10:23:45)grep 'ERROR' app.log | cut -c1-13 | sort | uniq -c # Top 10 IPs with failed SSH logins (field position varies by distro and log format)grep 'Failed password' /var/log/auth.log | awk '{print $(NF-3)}' | sort | uniq -c | sort -rn | head # Use in scriptsif grep -q 'FATAL' app.log; then echo "Fatal errors found"; fiTroubleshooting grep
| Symptom | Command | What to Check |
|---|---|---|
| Pattern not matching | echo 'test' | grep 'pat' |
Test on simple input |
| Too many results | Add -v or narrow pattern |
Filter noise |
| "Binary file matches" | grep -a |
Treat as text |
| Dots match too much | grep -F |
Literal vs regex |
TipPrefer
grep -Efor log work. WritingERROR|WARN|FATALis clearer than escaping every operator in basic mode.
Common MistakeSearching for an IP like
10.0.1.5without-F. Each dot matches any character, so it can also match10.0.1.50and10x0y1z5. Usegrep -Ffor literal strings.
RememberExit code 0 means a match was found, 1 means no match, and 2 means an error. That makes
grep -qideal in script conditions.
Frequently Asked Questions
What is the difference between grep, grep -E, and grep -P?
Plain grep uses basic regex where `+`, `?`, and `|` must be escaped. `-E` uses extended regex where they work unescaped. `-P` adds Perl features such as lookahead, lookbehind, and non-greedy quantifiers, but is a GNU grep option and may be missing on other systems.
How do I search for text that contains special characters?
Use `grep -F` for fixed-string matching so dots, brackets, and asterisks are treated literally. It is also faster because it skips the regex engine.
How do I grep for a pattern and show surrounding lines?
Use `-A N` for lines after, `-B N` for lines before, and `-C N` for both. This is how you see the stack trace that follows an error line.