<span>grep</span> (Global Regular Expression Print) is a powerful and commonly used text search tool in Linux/Unix systems, used to search for lines that match a specified pattern (usually a regular expression) in files or standard input, and outputs the matching lines to standard output.
1. Basic Syntax
grep [options] pattern [file...]
- • Pattern: The string or regular expression to search for.
- • File: The list of files to search. If omitted, it reads from standard input.
2. Common Options Explained
| Option | Description |
|---|---|
<span>-i</span> |
Ignore case (case-insensitive) |
<span>-v</span> |
Invert match, show non-matching lines |
<span>-n</span> |
Show line numbers of matching lines |
<span>-c</span> |
Only output the count of matching lines |
<span>-l</span> |
Only show filenames containing matches (for multiple files) |
<span>-L</span> |
Only show filenames not containing matches |
<span>-w</span> |
Whole word match, avoiding partial matches (e.g., “the” does not match “other”) |
<span>-x</span> |
Whole line match (the entire line must match the pattern) |
<span>-r</span> or <span>-R</span> |
Recursive search in directories (including subdirectories) |
<span>-A n</span> |
Show matching lines and the following n lines (After) |
<span>-B n</span> |
Show matching lines and the preceding n lines (Before) |
<span>-C n</span> |
Show matching lines and n lines before and after (Context) |
<span>-E</span> |
Use extended regular expressions (equivalent to <span>egrep</span>) |
<span>-F</span> |
Treat pattern as a fixed string (do not use regular expressions, equivalent to <span>fgrep</span>) |
<span>-o</span> |
Only output the matching part (not the entire line) |
<span>--color=auto</span> |
Highlight matching content (usually enabled by default) |
<span>-h</span> |
Do not show filenames when searching multiple files |
<span>-H</span> |
Force show filenames (even when searching a single file) |
3. Basic Usage Examples
1. Simple String Search
grep "error" /var/log/syslog
Search for lines containing “error” in <span>/var/log/syslog</span>.
2. Ignore Case
grep -i "ERROR" /var/log/syslog
Matches “error”, “Error”, “ERROR”, etc.
3. Show Line Numbers
grep -n "main" script.py
Outputs lines containing “main” along with their line numbers.
4. Whole Word Match
grep -w "is" file.txt
Matches the word “is”, but does not match “this” or “list”.
5. Invert Match
grep -v "^#" config.conf
Displays all non-comment lines (lines not starting with <span>#</span>).
6. Recursive Search in Directories
grep -r "TODO" ./src/
Recursively search for files containing “TODO” in the <span>./src/</span> directory and its subdirectories.
7. Show Context
grep -C 2 "exception" app.log
Displays matching lines along with 2 lines before and after.
8. Only Output Matching Content
echo "Email: [email protected]" | grep -o "[a-zA-Z0-9._%+-]*@example\.com"
# Output: [email protected]
9. Count Matching Lines
grep -c "success" access.log
10. Match Multiple Patterns
Find lines containing “error” or “warning” or “critical”.
grep "error\|warning\|critical" /var/log/messages
Or use <span>grep -E</span>
grep -E "error|warning|critical" /var/log/messages
Or use <span>egrep</span>
egrep "error|warning|critical" /var/log/messages
11. Filter File Types
When recursively searching large directories, you can combine <span>--include</span> or <span>--exclude</span> to filter file types:
grep -r --include="*.py" "def " .
Only search for lines containing “def ” in <span>.py</span> files.
4. Regular Expression Support
<span>grep</span> uses Basic Regular Expressions (BRE) by default, while <span>grep -E</span> uses Extended Regular Expressions (ERE).
Common Regular Symbols (BRE/ERE):
| Symbol | BRE/ERE | Meaning |
|---|---|---|
<span>.</span> |
Matches any single character | |
<span>*</span> |
Matches the previous character 0 or more times | |
<span>^</span> |
Line start anchor | |
<span>$</span> |
Line end anchor | |
<span>[abc]</span> |
Matches a, b, or c | |
<span>[^abc]</span> |
Matches any character not a, b, or c | |
<span>\?</span> |
BRE | Matches the previous character 0 or 1 time (optional) |
<span>?</span> |
ERE | Matches the previous character 0 or 1 time (optional) |
<span>\+</span> |
BRE | Matches the previous character 1 or more times |
<span>+</span> |
ERE | Matches the previous character 1 or more times |
<span>\ |</span> |
BRE | Or operation |
<span>|</span> |
ERE | Or operation |
<span>\(\)</span> |
BRE | Grouping, <span>\(ab\)\{2\}</span> matches “abab” |
<span>()</span> |
ERE | Grouping, <span>(ab){2}</span> matches “abab” |
<span>\{n,m\}</span> |
BRE | Matches the previous character n to m times |
<span>\{n\}</span> |
BRE | Matches exactly n times |
<span>\{n,\}</span> |
BRE | Matches at least n times |
<span>{n,m}</span> |
ERE | Matches the previous character n to m times |
<span>{n}</span> |
ERE | Matches exactly n times |
<span>{n,}</span> |
ERE | Matches at least n times |
In Basic Regular Expressions (BRE), <span>+</span>, <span>?</span>, <span>|</span>, <span>()</span>, and <span>{}</span> need to be escaped to be used as metacharacters.
5. Combining with Other Commands
1. Combining with Pipe
ps aux | grep "nginx"
Find running nginx processes.
2. Combining with find
find . -name "*.log" -exec grep -l "404" {} \;
Find all <span>.log</span> files containing “404”.
6. Common Variants
| Command | Description |
|---|---|
<span>grep</span> |
Basic regular expressions |
<span>egrep</span> |
Equivalent to <span>grep -E</span>, supports extended regular expressions |
<span>fgrep</span> |
Equivalent to <span>grep -F</span>, treats pattern as a fixed string (faster, does not parse regex) |
Modern systems recommend using <span>grep -E</span> or <span>grep -F</span> directly, rather than <span>egrep</span> or <span>fgrep</span>.
7. Conclusion
<span>grep</span> is the Swiss Army knife of text processing; mastering its options and regular expression usage can greatly enhance efficiency in handling tasks such as processing logs, code, and configuration files in the command line.