Skip to main content

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.

Bash
grep 'ERROR' /var/log/app.log

How to Use grep Options

Bash
grep -i 'error' app.log # ignore case
grep -v 'DEBUG' app.log # lines NOT matching
grep -n 'ERROR' app.log # show line numbers
grep -c 'ERROR' app.log # count matching lines
grep -l 'ERROR' /var/log/*.log # filenames only
grep -r 'database_url' /etc/ # recursive
grep -r 'API_KEY' /opt/apps --include='*.py'
grep -B 3 -A 3 'CRITICAL' app.log # context lines
grep -F '10.0.1.50' app.log # literal text, not regex
zgrep 'ERROR' app.log.gz # compressed files

Which 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.

Bash
grep -E 'ERROR|WARN|FATAL' app.log
grep -E 'error[0-9]+' app.log
grep -P '(?<=user_id=)\d+' app.log

Practical Log Analysis Patterns

Bash
# 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 scripts
if grep -q 'FATAL' app.log; then echo "Fatal errors found"; fi

Troubleshooting 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
Tip

Prefer grep -E for log work. Writing ERROR|WARN|FATAL is clearer than escaping every operator in basic mode.

Common Mistake

Searching for an IP like 10.0.1.5 without -F. Each dot matches any character, so it can also match 10.0.1.50 and 10x0y1z5. Use grep -F for literal strings.

Remember

Exit code 0 means a match was found, 1 means no match, and 2 means an error. That makes grep -q ideal 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.

Found a mistake, an outdated step or a broken link?