GNU grep Reference Guide
Overview
grep (Global Regular Expression Print) searches for patterns in text files. It's one of the most commonly used Unix tools.
Official Manual: https://www.gnu.org/software/grep/manual/
Man Page: man grep
Basic Syntax
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] -e PATTERN ... [FILE...]
grep [OPTIONS] -f PATTERN_FILE ... [FILE...]Common Options
Basic Options
-i, --ignore-case # Case-insensitive search
-v, --invert-match # Invert match (select non-matching lines)
-w, --word-regexp # Match whole words only
-x, --line-regexp # Match whole lines only
-c, --count # Count matching lines
-n, --line-number # Show line numbers
-H, --with-filename # Print filename with matches
-h, --no-filename # Suppress filename output
-l, --files-with-matches # Print only filenames with matches
-L, --files-without-match # Print only filenames without matchesContext Options
-A NUM, --after-context=NUM # Print NUM lines after match
-B NUM, --before-context=NUM # Print NUM lines before match
-C NUM, --context=NUM # Print NUM lines before and afterRegular Expression Options
-E, --extended-regexp # Use Extended Regular Expressions (ERE)
-F, --fixed-strings # Treat PATTERN as fixed strings, not regex
-G, --basic-regexp # Use Basic Regular Expressions (BRE) - default
-P, --perl-regexp # Use Perl-compatible regex (PCRE)Output Options
-o, --only-matching # Print only matched parts
-q, --quiet, --silent # Suppress output, just return exit code
--color[=WHEN] # Colorize output (auto, always, never)
-s, --no-messages # Suppress error messagesFile Selection
-r, --recursive # Recursive search
-R, --dereference-recursive # Recursive, following symlinks
--include=PATTERN # Search only files matching PATTERN
--exclude=PATTERN # Skip files matching PATTERN
--exclude-dir=PATTERN # Skip directories matching PATTERNCommon Usage Patterns
Basic Searches
# Simple string search
grep "error" logfile.txt
# Case-insensitive search
grep -i "error" logfile.txt
# Search for whole word
grep -w "error" logfile.txt
# Count matches
grep -c "error" logfile.txt
# Show line numbers
grep -n "error" logfile.txtMultiple Files
# Search in multiple files
grep "pattern" file1.txt file2.txt
# Search in all txt files
grep "pattern" *.txt
# Recursive search
grep -r "pattern" /path/to/dir
# Recursive with file pattern
grep -r --include="*.log" "error" /var/logContext Display
# Show 3 lines after match
grep -A 3 "error" logfile.txt
# Show 3 lines before match
grep -B 3 "error" logfile.txt
# Show 3 lines before and after
grep -C 3 "error" logfile.txtInvert Match
# Show lines that DON'T contain pattern
grep -v "debug" logfile.txt
# Exclude multiple patterns
grep -v "debug\|info" logfile.txtMultiple Patterns
# Match any pattern (OR)
grep -e "error" -e "warning" file.txt
grep "error\|warning" file.txt
# Match all patterns (AND) - requires pipeline
grep "error" file.txt | grep "critical"File Selection
# List files containing match
grep -l "pattern" *.txt
# List files NOT containing match
grep -L "pattern" *.txt
# Recursive with excludes
grep -r --exclude-dir=".git" "pattern" .
grep -r --exclude="*.min.js" "pattern" .Regular Expressions in grep
Basic Regular Expressions (BRE) - Default
# BRE Metacharacters (no escaping needed)
. # Any single character
^ # Start of line
$ # End of line
[...] # Character class
[^...] # Negated character class
* # Zero or more of previous
# BRE Metacharacters (MUST be escaped)
\+ # One or more (requires \)
\? # Zero or one (requires \)
\{m,n\} # Between m and n occurrences (requires \)
\(...\) # Group (requires \)
\| # Alternation (requires \)BRE Examples
# Match lines starting with "Error"
grep "^Error" file.txt
# Match lines ending with "failed"
grep "failed$" file.txt
# Match any character
grep "a.c" file.txt # Matches abc, aXc, a5c
# Character class
grep "[0-9]" file.txt # Match any digit
grep "[A-Za-z]" file.txt # Match any letter
# One or more (escaped)
grep "a\+b" file.txt # Matches ab, aab, aaab
# Groups and alternation (escaped)
grep "\(error\|warning\)" file.txtExtended Regular Expressions (ERE) - grep -E
# ERE - No escaping needed for +, ?, |, (), {}
+ # One or more
? # Zero or one
{m,n} # Between m and n occurrences
(...) # Group
| # AlternationERE Examples
# One or more (no escape)
grep -E "a+b" file.txt
# Zero or one
grep -E "colou?r" file.txt # Matches color or colour
# Alternation
grep -E "(error|warning)" file.txt
# Quantifiers
grep -E "[0-9]{3}-[0-9]{4}" file.txt # Phone: 123-4567
grep -E "[0-9]{1,3}" file.txt # 1 to 3 digits
# Groups
grep -E "(http|https)://[^ ]+" file.txt # URLsCharacter Classes
POSIX Character Classes
[:alnum:] # Alphanumeric [A-Za-z0-9]
[:alpha:] # Alphabetic [A-Za-z]
[:digit:] # Digits [0-9]
[:lower:] # Lowercase [a-z]
[:upper:] # Uppercase [A-Z]
[:space:] # Whitespace [ \t\n\r\f\v]
[:blank:] # Space and tab [ \t]
[:punct:] # Punctuation
[:xdigit:] # Hex digits [0-9A-Fa-f]
[:word:] # Word characters [A-Za-z0-9_]Usage
# Match any digit
grep "[[:digit:]]" file.txt
# Match any whitespace
grep "[[:space:]]" file.txt
# Match uppercase letters
grep "[[:upper:]]" file.txtPractical Examples for Shell Scripts
Log File Analysis
# Find errors in last hour
grep "$(date -d '1 hour ago' '+%Y-%m-%d %H')" /var/log/app.log | grep -i error
# Count error types
grep -i "error" logfile.log | cut -d: -f2 | sort | uniq -c | sort -rn
# Extract IP addresses
grep -oE '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b' access.logConfiguration Validation
# Find uncommented lines in config
grep -v "^#" config.file | grep -v "^$"
# Check for specific settings
if grep -q "debug = true" config.ini; then
echo "Debug mode is enabled"
fiCode Search
# Find function definitions
grep -n "^function " script.sh
# Find TODO comments
grep -rn "TODO" --include="*.sh" .
# Find unquoted variables (simple check)
grep -n '\$[A-Za-z_][A-Za-z0-9_]*\s' script.shPerformance Tips
1. Use -F for Fixed Strings
# Faster when not using regex
grep -F "literal string" large_file.txt2. Use -q When Only Checking Existence
# Don't need output, just exit code
if grep -q "pattern" file.txt; then
echo "Found"
fi3. Limit Search Depth
# Don't recurse too deep
grep -r --max-depth=2 "pattern" /path4. Exclude Unnecessary Directories
grep -r --exclude-dir={.git,.svn,node_modules} "pattern" .Exit Codes
- 0: Match found
- 1: No match found
- 2: Error occurred
Common Pitfalls in Shell Scripts
1. Not Quoting Patterns with Spaces
# Wrong
grep $pattern file.txt
# Right
grep "$pattern" file.txt2. Using grep in Tests Without -q
# Inefficient
if [ "$(grep pattern file)" ]; then
# Better
if grep -q pattern file; then3. Useless Use of cat
# Wrong (UUOC)
cat file | grep pattern
# Right
grep pattern file
# or
< file grep pattern4. Not Handling No Match Case
# grep returns 1 if no match, can cause set -e to exit
grep "pattern" file || true
# Or check explicitly
if grep -q "pattern" file; then
echo "Found"
else
echo "Not found"
fi5. Forgetting to Escape Regex Metacharacters
# Wrong - . matches any character
grep "192.168.1.1" file
# Right - escape the dots
grep "192\.168\.1\.1" file
# Or use -F for literal match
grep -F "192.168.1.1" fileUseful Combinations
# Case-insensitive recursive search with line numbers
grep -rni "pattern" /path
# Count total matches across files
grep -r "pattern" . | wc -l
# Find and highlight matches
grep --color=always "pattern" file.txt | less -R
# Search compressed files
zgrep "pattern" file.gz
# Search with extended regex and only show matches
grep -Eo "pattern" file.txt