The grep Command in Linux
Last Updated: 2025-01-01
3 min read
Overview
grep (Global Regular Expression Print) searches files or input for lines matching a text pattern. It’s indispensable for finding configuration values, troubleshooting error logs, and filtering command output.
Basic Syntax
grep [options] "pattern" [file(s)]
Simple Searches
Search for a string in a file
grep "error" /var/log/syslog
Case-insensitive search
grep -i "warning" /var/log/apache2/error.log
Show line numbers
grep -n "404" /var/log/apache2/access.log
Recursive Search
Search through all files in a directory tree:
grep -r "database_name" /home/username/public_html/
Recursive with specific file types
grep -r --include="*.php" "DB_PASSWORD" /home/username/public_html/
Exclude directories from recursive search
grep -r --exclude-dir=".git" "TODO" /home/username/project/
Useful Flags Reference
| Flag | Description |
|---|---|
-i | Case-insensitive matching |
-n | Show line numbers |
-r / -R | Search recursively in directories |
-l | Show only filenames with matches (not the matching lines) |
-c | Show only a count of matching lines per file |
-v | Invert match — show lines that do not match |
-w | Match whole words only |
-A N | Show N lines after each match |
-B N | Show N lines before each match |
-C N | Show N lines of context (before and after) |
--include | Only search files matching a pattern |
--exclude | Skip files matching a pattern |
--exclude-dir | Skip directories matching a pattern |
-E | Extended regex (same as egrep) |
Using Regular Expressions
# Lines starting with "Error"
grep "^Error" logfile.txt
# Lines ending with a number
grep "[0-9]$" data.txt
# Match IP address pattern
grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" access.log
# Match multiple patterns (OR)
grep -E "error|warning|critical" /var/log/syslog
Piping with grep
One of grep’s most powerful features is filtering the output of other commands:
# Find a running process
ps aux | grep "apache"
# Filter active network connections
netstat -tuln | grep ":80"
# Check environment variables
env | grep "PATH"
# Find specific entries in command history
history | grep "mysql"
Practical Hosting Examples
Find the database name in a WordPress config
grep "DB_NAME" /home/username/public_html/wp-config.php
Search error logs for 500 errors
grep -c "500" /var/log/apache2/access.log
Find which files contain a hardcoded domain
grep -rl "old-domain.com" /home/username/public_html/
View context around an error
grep -C 3 "Fatal error" /var/log/apache2/error.log
This shows 3 lines before and after each match, helping you understand the context of the error.
Exclude binary files from search
grep -rI "config" /home/username/public_html/
The -I flag skips binary files, keeping output clean.
Tips
- Combine
-rnfor recursive search with line numbers — the most frequently used combo. - Use
-lwhen you only need to know which files contain a match, not the matching lines themselves. - When piping
ps aux | grep something, you’ll often see the grep process itself in the results. Add| grep -v grepto filter it out.
Tags:
ssh
linux
grep
text-search