grep [options] pattern [file...]
Option | Description | Example |
---|---|---|
-i |
Case insensitive search | grep -i "error" log.txt |
-r or -R |
Recursive search | grep -r "TODO" /path/to/dir |
-n |
Show line numbers | grep -n "pattern" file.txt |
-w |
Match whole words only | grep -w "class" *.java |
-l |
Show only filenames | grep -l "pattern" *.txt |
-c |
Count matches | grep -c "error" log.txt |
-v |
Invert match (show non-matching) | grep -v "^#" config.txt |
-E |
Extended regex support | `grep -E "word1 |
# Show 3 lines before match
grep -B 3 "error" log.txt
# Show 3 lines after match
grep -A 3 "error" log.txt
# Show 3 lines before and after
grep -C 3 "error" log.txt
# Enable color output
grep --color=auto "pattern" file.txt
# Add to ~/.bashrc for permanent color:
export GREP_OPTIONS='--color=auto'
# Exclude specific directories
grep -r "pattern" --exclude-dir={.git,node_modules,vendor} .
# Exclude by pattern
grep -r "pattern" --exclude-dir=".*" .
# Search only specific file types
grep -r "pattern" --include="*.php" .
# Multiple file types
grep -r "pattern" --include="*.{js,jsx,ts,tsx}" .
# Match start of line
grep "^start" file.txt
# Match end of line
grep "end$" file.txt
# Match any single character
grep "h.t" file.txt
# Match zero or more occurrences
grep "wo*rd" file.txt
# Match specific number of occurrences
grep "o\{2\}" file.txt
# Match either pattern
grep -E "pattern1|pattern2" file.txt
# Match one or more occurrences
grep -E "o+" file.txt
# Match zero or one occurrence
grep -E "colou?r" file.txt
# Stop after first match
grep -m 1 "pattern" large_file.txt
# Use binary grep for faster search
grep -a "pattern" binary_file
# Count matches without printing
grep -c "pattern" large_file.txt
# Process files in parallel
parallel "grep 'pattern' {}" ::: *.txt
# Use find with grep
find . -type f -exec grep "pattern" {} +
# Suppress error messages
grep -s "pattern" /path/to/files/*
# Show only error messages
grep -q "pattern" file.txt 2>&1 >/dev/null
grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" log.txt
grep -E "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}" file.txt
grep -E "https?://[A-Za-z0-9./]+" file.txt
-
Use ripgrep (rg) for Better Performance
- Install:
sudo apt install ripgrep
- Usage:
rg "pattern"
- Install:
-
Create Aliases for Common Operations
# Add to ~/.bashrc alias grepjs='grep -r --include="*.js"' alias grepphp='grep -r --include="*.php"' alias greperr='grep -i -r "error|warning|critical"'
-
Use Process Substitution
# Search in command output grep "pattern" <(command)
-
Combine with Other Commands
# Find and grep find . -type f -name "*.log" -exec grep "error" {} + # Pipe with grep ps aux | grep "[n]ginx"
- Don't use grep without -r for directory searches
- Avoid using GREP_OPTIONS (deprecated)
- Remember to escape special characters in patterns
- Don't forget to use quotes around patterns with spaces
- Be careful with -R vs -r (symbolic links)
- Use
-v
to see what's being excluded - Use
--debug
to see detailed matching information - Use
-h
to suppress filenames in multi-file searches - Add
-n
to show line numbers for easier reference