The grep
command is a powerful text searching utility in Unix/Linux systems. It searches input files for lines that match a given pattern and outputs the matching lines. This reference provides commonly used grep
commands, options, and examples to help you efficiently search and process text files.
The general syntax for using grep
is:
grep [options] 'pattern' file(s)
Or to search standard input:
command | grep [options] 'pattern'
Option | Description |
---|---|
-i |
Ignore case distinctions in patterns and input data. |
-v |
Invert the match, showing lines that do not match the pattern. |
-c |
Count the number of matching lines. |
-n |
Prefix each line of output with the line number. |
-l |
List filenames containing the match. |
-w |
Match whole words only. |
-r or -R |
Recursively search directories. |
-e 'pattern' |
Specify multiple patterns. |
-E |
Interpret patterns as extended regular expressions. |
-A num |
Print num lines of trailing context after matching lines. |
-B num |
Print num lines of leading context before matching lines. |
-C num |
Print num lines of output context. |
--color=auto |
Highlight matching strings. |
Command | Description |
---|---|
grep 'pattern' file |
Search for 'pattern' in a file. |
grep -i 'pattern' file |
Case-insensitive search. |
grep -v 'pattern' file |
Show lines that do not match 'pattern'. |
grep -n 'pattern' file |
Show matching lines with line numbers. |
grep -c 'pattern' file |
Count the number of matching lines. |
grep -w 'word' file |
Search for whole words only. |
grep -r 'pattern' /path/to/directory |
Recursively search files in a directory. |
grep -l 'pattern' *.txt |
List filenames that contain 'pattern'. |
grep 'pattern' file1 file2 |
Search multiple files. |
command | grep 'pattern' |
Filter the output of a command. |
grep -A 2 'pattern' file |
Show 2 lines after each matching line. |
grep -B 2 'pattern' file |
Show 2 lines before each matching line. |
grep -C 2 'pattern' file |
Show 2 lines before and after each matching line. |
grep --color=auto 'pattern' file |
Highlight matching patterns in the output. |
grep
supports regular expressions for pattern matching. By default, it uses Basic Regular Expressions (BRE). Use the -E
option for Extended Regular Expressions (ERE), or use egrep
(equivalent to grep -E
).
^
- Matches the beginning of a line.$
- Matches the end of a line..
- Matches any single character.*
- Matches zero or more of the preceding character.\[abc\]
- Character class, matches any character inside the brackets.\[^abc\]
- Negated character class, matches any character not inside the brackets.\(pattern\)
- Groups a pattern (used with backreferences).\{n,m\}
- Matches the preceding element at least n and at most m times.When using -E
or egrep
, you can use additional metacharacters without escaping:
+
- Matches one or more of the preceding character.?
- Matches zero or one of the preceding character.|
- Alternation, matches either the expression before or after the operator.(pattern)
- Groups a pattern.{n,m}
- Matches the preceding element at least n and at most m times.Pattern | Description |
---|---|
'^pattern' |
Matches lines starting with 'pattern'. |
'pattern$' |
Matches lines ending with 'pattern'. |
'^$' |
Matches empty lines. |
'[0-9]' |
Matches any digit. |
'[A-Za-z]' |
Matches any uppercase or lowercase letter. |
'foo.*bar' |
Matches 'foo' followed by any characters, then 'bar'. |
'^#' |
Matches lines starting with '#', often used to find comments. |
'\ |
Matches 'word' as a whole word (requires escaping). |
grep
is often used in combination with other commands using pipes.
# List all running processes containing 'ssh'
ps aux | grep 'ssh'
# Find files modified today
find /path -type f -mtime 0 | grep '\.log$'
# Display network connections containing 'ESTABLISHED'
netstat -an | grep 'ESTABLISHED'
Search all files in a directory tree for a pattern:
# Search for 'TODO' in all files under the current directory
grep -r 'TODO' .
Exclude binary files:
grep -rI 'pattern' /path/to/directory
Search only specific file types:
# Search only in .txt files
grep --include=\*.txt -rn 'pattern' /path/to/directory
Search for multiple patterns by using the -e
option or by providing patterns separated by newlines in a file.
# Search for 'error' or 'fail'
grep -E 'error|fail' file
# Using multiple -e options
grep -e 'error' -e 'fail' file
# Search patterns from a file
grep -f patterns.txt file
Where patterns.txt
contains:
error
fail
warning
Count the number of lines that match a pattern:
# Count lines containing 'pattern'
grep -c 'pattern' file
Count the total number of matches (including multiple matches per line):
# Use -o to output each match on a separate line, then count them
grep -o 'pattern' file | wc -l
Show lines that do not match a pattern:
grep -v 'pattern' file
Example: Show all lines that do not start with '#'
grep -v '^#' file
Use the --color
option to highlight matching text:
grep --color=auto 'pattern' file
Show line numbers with matches:
grep -n 'pattern' file
Display lines before and/or after matching lines:
-A num
- Show num lines After match.-B num
- Show num lines Before match.-C num
- Show num lines Before and After match.# Show 2 lines after each match
grep -A 2 'pattern' file
# Show 2 lines before each match
grep -B 2 'pattern' file
# Show 2 lines before and after each match
grep -C 2 'pattern' file
Use zgrep
to search compressed files:
zgrep 'pattern' file.gz
grep -F
or fgrep
to search for fixed strings (no regex).grep -P
to use Perl-compatible regular expressions (may not be available on all systems).grep
can be influenced by certain environment variables:
GREP_COLOR
- Specifies the color used to highlight matches.GREP_OPTIONS
- Specifies default options for grep
.