Grep Command Reference

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.

Basic Syntax

The general syntax for using grep is:

grep [options] 'pattern' file(s)

Or to search standard input:

command | grep [options] 'pattern'

Commonly Used Options

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.

Basic Examples

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.

Regular Expressions in Grep

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).

Basic Regular Expressions (BRE)

Extended Regular Expressions (ERE)

When using -E or egrep, you can use additional metacharacters without escaping:

Examples

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).

Using Grep with Other Commands

grep is often used in combination with other commands using pipes.

Examples

# 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'

Recursive Search Examples

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

Using Multiple Patterns

Search for multiple patterns by using the -e option or by providing patterns separated by newlines in a file.

Examples

# 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

Counting Matches

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

Inverting Matches

Show lines that do not match a pattern:

grep -v 'pattern' file

Example: Show all lines that do not start with '#'

grep -v '^#' file

Highlighting Matches

Use the --color option to highlight matching text:

grep --color=auto 'pattern' file

Display Line Numbers

Show line numbers with matches:

grep -n 'pattern' file

Context Lines

Display lines before and/or after matching lines:

Examples

# 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

Search Compressed Files

Use zgrep to search compressed files:

zgrep 'pattern' file.gz

Useful Tips

Environment Variables

grep can be influenced by certain environment variables:

Return to Home