-
-
Notifications
You must be signed in to change notification settings - Fork 1
Grep Command Guide
- Scenario 1: Find Errors in Log File
- Scenario 2: Search Multiple Files
- Scenario 3: Filter Out Comments
- Scenario 4: Find IP Addresses
- Scenario 5: Count Occurrences
- Scenario 6: Find Files Containing Pattern
- Scenario 7: Search with Context
-
grep(Global Regular Expression Print) searches for text patterns in files - One of the most essential Linux commands
- Used for searching, filtering, and extracting information
- Works with files, command output, and pipes
What grep can do:
- Search for text in files
- Filter command output
- Find patterns (exact text or regular expressions)
- Count matches
- Show context around matches
- Search recursively in directories
Common uses:
- Searching log files for errors
- Filtering command output
- Finding specific text in code
- Extracting information from files
grep "pattern" filenameWhat this does:
- Searches for "pattern" in the file
- Shows all lines containing the pattern
- Case-sensitive by default
Example:
grep "error" logfile.txtShows all lines containing "error".
grep "pattern" file1.txt file2.txtWhat this does:
- Searches for pattern in multiple files
- Shows filename with each match
Example:
grep "ERROR" *.logSearches for "ERROR" in all .log files.
grep -r "pattern" /path/to/directoryWhat this does:
- Recursively searches all files in directory
- Includes all subdirectories
- Very useful for searching codebases
Example:
grep -r "function_name" /path/to/projectgrep -i "pattern" filenameWhat this does:
- Searches case-insensitively
-
-i= ignore case - Finds "Error", "ERROR", "error", etc.
Example:
grep -i "error" logfile.txtgrep -n "pattern" filenameWhat this does:
- Shows line number with each match
- Very useful for locating matches in files
Example:
grep -n "error" logfile.txtOutput:
15:error: connection failed
42:error: timeout occurred
grep -H "pattern" filenameWhat this does:
- Shows filename with each match
- Useful when searching multiple files
-
-His default when multiple files are searched
Example:
grep -H "error" *.loggrep -h "pattern" file1 file2What this does:
- Suppresses filename in output
- Only shows matching lines
- Useful when you don't need to know which file
grep -l "pattern" *.txtWhat this does:
- Lists only filenames containing the pattern
- Doesn't show the actual matches
- Useful for finding which files contain the pattern
Example:
grep -l "TODO" *.mdShows which .md files contain "TODO".
grep -v "pattern" filenameWhat this does:
- Shows lines that DON'T match the pattern
-
-v= invert match - Useful for filtering out unwanted lines
Example:
grep -v "^#" configfileShows all lines except those starting with # (comments).
grep -B 3 "pattern" filenameWhat this does:
- Shows 3 lines before each match
-
-B= before - Useful for understanding context
Example:
grep -B 5 "error" logfile.txtgrep -A 3 "pattern" filenameWhat this does:
- Shows 3 lines after each match
-
-A= after - Useful for seeing what happens after an error
Example:
grep -A 5 "error" logfile.txtgrep -C 3 "pattern" filenameWhat this does:
- Shows 3 lines before AND after each match
-
-C= context - Best of both worlds
Example:
grep -C 5 "error" logfile.txtShows 5 lines before and after each error.
grep -c "pattern" filenameWhat this does:
- Counts number of matching lines
-
-c= count - Doesn't show the actual matches, just the count
Example:
grep -c "error" logfile.txtOutput:
42
Means 42 lines contain "error".
grep -w "pattern" filenameWhat this does:
- Matches only whole words
-
-w= word boundary - Prevents partial matches
Example:
grep -w "error" file.txtMatches "error" but not "errors" or "terror".
grep -x "pattern" filenameWhat this does:
- Matches only if entire line matches pattern
-
-x= exact line match - Useful for exact matches
grep -m 10 "pattern" filenameWhat this does:
- Stops after finding 10 matches
-
-m= max count - Useful for large files
Example:
grep -m 5 "error" huge_logfile.txtShows only first 5 errors.
command | grep "pattern"What this does:
- Filters command output through grep
- Shows only lines matching pattern
- Very common pattern
Examples:
ps aux | grep nginx
systemctl status | grep failed
journalctl | grep errorgrep "pattern" file.txt | wc -lWhat this does:
- Counts matching lines
- Pipes grep output to
wc -l - Alternative to
grep -c
Example:
grep "error" logfile.txt | wc -lProblem: Need to find all errors in a log file.
Solution:
grep -i "error" logfile.txtWith line numbers:
grep -n -i "error" logfile.txtWith context:
grep -C 5 -i "error" logfile.txtProblem: Need to search for text in multiple files.
Solution:
grep -r "pattern" /path/to/directoryOnly in specific file types:
grep -r "pattern" --include="*.txt" /pathProblem: Need to see config file without comments.
Solution:
grep -v "^#" configfileRemoves lines starting with #.
Also remove empty lines:
grep -v "^#" configfile | grep -v "^$"Problem: Need to extract IP addresses from a file.
Solution:
grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" file.txtUses extended regex (-E) for pattern matching.
Problem: Need to count how many times a pattern appears.
Solution:
grep -c "pattern" file.txtOr:
grep "pattern" file.txt | wc -lProblem: Need to know which files contain a pattern.
Solution:
grep -l "pattern" *.txtLists filenames only.
Problem: Need to see what happened before/after an error.
Solution:
grep -C 10 "error" logfile.txtShows 10 lines before and after each error.
grep "pattern" file # Search in file
grep "pattern" file1 file2 # Multiple files
grep -r "pattern" /path # Recursive search
grep -i "pattern" file # Case-insensitivegrep -n "pattern" file # Show line numbers
grep -H "pattern" file # Show filename
grep -l "pattern" *.txt # Show filenames only
grep -v "pattern" file # Invert match
grep -c "pattern" file # Count matchesgrep -B 5 "pattern" file # 5 lines before
grep -A 5 "pattern" file # 5 lines after
grep -C 5 "pattern" file # 5 lines before/aftergrep -w "pattern" file # Whole words only
grep -x "pattern" file # Whole lines only
grep -m 10 "pattern" file # Limit to 10 matchescommand | grep "pattern" # Filter output
grep "pattern" file | wc -l # Count matches- Use quotes around patterns with spaces or special characters
-
Case sensitivity - use
-ifor case-insensitive search -
Regular expressions - use
-Efor extended regex -
Large files - use
-mto limit matches -
Recursive search - use
-rfor directory searches -
Context - use
-Cto see surrounding lines
This guide covered:
- Basic Usage:
- Searching in files
- Multiple files
- Recursive search
- Output Options:
- Line numbers
- Filenames
- Invert match
- Count matches
- Context Options:
- Lines before/after
- Context around matches
- Advanced Options:
- Whole words/lines
- Limit matches
- Extended regex
- Using with Pipes:
- Filtering command output
- Combining commands
- Troubleshooting Scenarios:
- Finding errors
- Searching multiple files
- Filtering output
Next Steps:
- Practice with log files
- Combine with other commands (find, ps, etc.)
- Learn regular expressions for advanced patterns
- Master context options for debugging
For finding files, see the Find Command Guide. For process management, see the PS Process Management Guide.