This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

GNU grep Cheatsheet

A quick reference guide for GNU grep: regex pattern searching, BRE/ERE/PCRE options, context control, invert match, recursive directory searches, and color output.

· Published

grep searches named input files for lines matching a pattern and writes matching lines to standard output.

Usage

grep <options> pattern <file...>

Matching Options

-e, --regexp=PATTERN  # Explicit pattern specifier (allows patterns starting with -)
-f, --file=FILE       # Read patterns from file, one per line
-i, --ignore-case     # Case-insensitive matching
-v, --invert-match    # Invert sense of match: select non-matching lines
-w, --word-regexp     # Match only whole words
-x, --line-regexp     # Match only whole lines

Pattern Dialect Options

-F, --fixed-strings   # Interpret patterns as fixed strings (formerly fgrep)
-G, --basic-regexp    # Basic Regular Expressions (BRE, default)
-E, --extended-regexp # Extended Regular Expressions (ERE, formerly egrep)
-P, --perl-regexp     # Perl-Compatible Regular Expressions (PCRE)

Regular Expression Dialects

Basic Regular Expressions (BRE)

In BRE, the following meta-characters have special meaning unescaped:

^ $ . * [ ] \

The following require backslash escapes to act as operators:

\? \+ \{ \} \| \( \)

Extended Regular Expressions (ERE)

In ERE (-E), all meta-characters have special meaning without escaping:

^ $ . * + ? [ ] ( ) | { }

Perl-Compatible Regular Expressions (PCRE)

PCRE (-P) enables lookarounds ((?=...), (?!...)), non-greedy quantifiers (*?, +?), and character class shorthands (\d, \w, \s). See the Regular Expressions Cheatsheet.

Output Control

-c, --count           # Print count of matching lines only
    --color[=WHEN]    # Highlight matches with color (never, always, or auto)
-m, --max-count=NUM   # Stop reading after NUM matching lines
-o, --only-matching   # Print only the matched parts of lines
-q, --quiet, --silent # Suppress output; exit with 0 if match found, 1 if none
-s, --no-messages     # Suppress error messages about nonexistent or unreadable files
-n, --line-number     # Prefix line numbers
-l, --files-with-matches # Print only names of files with matches
-L, --files-without-match# Print only names of files without matches
-r, -R, --recursive   # Search directories recursively

Context Control

-B NUM, --before-context=NUM  # Print NUM lines before each match
-A NUM, --after-context=NUM   # Print NUM lines after each match
-C NUM, -NUM, --context=NUM   # Print NUM lines before and after each match

Practical Examples

# Case-insensitive match in file
grep -i "database" config.yml

# Extended regex: match foo or bar
grep -E "foo|bar" app.log

# Extract only URLs from a document
grep -oE "https?://[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}[^ \"\'<]*" index.html

# Count occurrences of an error
grep -c "ERROR 500" access.log

# Pipe filtering
grep "export" ~/.bash_profile | grep "PATH"

# Tail live log with 5 lines of surrounding context
tail -f server.log | grep -iC 5 "exception"

References

Related topics