LinuxCommandLibrary

grep

Search text for matching patterns

TLDR

Search for a pattern within files

$ grep "[search_pattern]" [path/to/file1 path/to/file2 ...]
copy

Search for an exact string (disables regexes)
$ grep [[-F|--fixed-strings]] "[exact_string]" [path/to/file]
copy

Search for a pattern in all files recursively in a directory, ignoring binary files
$ grep [[-rI|--recursive --binary-files=without-match]] "[search_pattern]" [path/to/directory]
copy

Print 3 lines of [C]ontext around, [B]efore, or [A]fter each match
$ grep [--context|--before-context|--after-context] 3 "[search_pattern]" [path/to/file]
copy

Print file name and line number for each match with color output
$ grep [[-Hn|--with-filename --line-number]] --color=always "[search_pattern]" [path/to/file]
copy

Print only the matched text
$ grep [[-o|--only-matching]] "[search_pattern]" [path/to/file]
copy

Read data from stdin and do not print lines that match a pattern
$ cat [path/to/file] | grep [[-v|--invert-match]] "[search_pattern]"
copy

Use extended regexes (supports ?, +, {}, (), and |), in case-insensitive mode
$ grep [[-Ei|--extended-regexp --ignore-case]] "[search_pattern]" [path/to/file]
copy

SYNOPSIS

grep [OPTION]... [PATTERN] [FILE]...

PARAMETERS

-a, --text
    Process binary files as text

-A NUM, --after-context=NUM
    Print NUM lines of trailing context after each match

-b, --byte-offset
    Prefix each line with byte offset

-B NUM, --before-context=NUM
    Print NUM lines of leading context before each match

-c, --count
    Suppress normal output; show only count of matching lines

-C NUM, --context=NUM
    Equivalent to -A and -B NUM

-d ACTION, --directories=ACTION
    How to handle directories: recurse, read, skip

-D ACTION, --devices=ACTION
    How to handle devices, FIFOs, sockets: read, skip

-E, --extended-regexp
    Use extended regular expressions

-e PATTERN, --regexp=PATTERN
    Add PATTERN; useful for multiple patterns or starting with -

-f FILE, --file=FILE
    Get patterns from FILE

-F, --fixed-strings
    Interpret PATTERN as fixed strings, not regex

-G, --basic-regexp
    Use basic regular expressions (default)

-H, --with-filename
    Always print filename (default for multiple files)

-h, --no-filename
    Suppress filename prefix

-i, --ignore-case
    Ignore case distinctions

-I
    No process binary files

-L, --files-without-match
    Print only names of FILEs with no matches

-l, --files-with-matches
    Print only names of FILEs with matches

-m NUM, --max-count=NUM
    Stop after NUM matching lines per file

-n, --line-number
    Prefix each line with its line number

-o, --only-matching
    Show only the part of line matching PATTERN

-q, --quiet, --silent
    Suppress all output; exit status only

-r, --recursive
    Recurse into directories

-R, --dereference-recursive
    Recurse and follow symlinks

-s, --no-messages
    Suppress error messages

-v, --invert-match
    Select non-matching lines

-V, --version
    Display version info

-w, --word-regexp
    Match whole words only

-x, --line-regexp
    Match whole lines only

--color[=WHEN]
    Highlight matches; WHEN=always|never|auto

--line-buffered
    Flush output after each line

-P, --perl-regexp
    Use Perl-compatible regex (GNU)

-z, --null-data
    Treat input as null-delimited

--help
    Display help

DESCRIPTION

grep is a fundamental Unix/Linux command-line tool for searching text using regular expressions (regex). Standing for "global regular expression print," it scans files or standard input for lines matching a specified PATTERN and prints them to stdout.

Patterns can range from simple strings to complex regex. Basic regex (BRE, default) supports . * [ ] ^ $, while extended regex (-E) adds | ? + (). Fixed strings (-F) treat patterns literally, ignoring regex metacharacters. GNU grep also supports Perl-compatible regex (-P).

Common use cases include log analysis (grep error logfile), code searching, and filtering pipeline output (ps aux | grep ssh). Options control output format, recursion (-r), case-insensitivity (-i), inversion (-v), counts (-c), line numbers (-n), and context lines (-A/-B/-C). It processes multiple files, stdin (if no files given), and supports binary files with caveats.

Efficient for small-to-medium datasets, grep leverages finite automata for fast matching. It's POSIX-standardized, portable across systems, and script-friendly, often paired with sed, awk, or cut. GNU version adds enhancements like color output (--color=auto) and PCRE.

CAVEATS

Slow on very large files or complex regex without optimization; binary files may output garbage unless -a used; default BRE lacks some ERE features (use -E); locale affects multibyte chars; GNU-specific options not portable.

EXIT STATUS

0 if matches found (or -q/-l/-L/-c); 1 if no matches; 2 on error.
Non-zero also if file access denied.

ENVIRONMENT

GREP_OPTIONS (deprecated); GREP_COLOR for highlight color; LC_ALL/LC_MESSAGES affect behavior.

REGEX NOTES

Escape metachars in BRE (\. \[); -E/-P unescaped. Multiline with ^/$ respects -z.

HISTORY

Created 1973 by Mike McQuaid as egrep for ed; Ken Thompson rewrote with efficient DFA matcher for Version 4 Unix. Became grep, egrep, fgrep. POSIX-standardized 1992; GNU grep (1992+) by Michel Renduchon adds PCRE, colors, more options.

SEE ALSO

egrep(1), fgrep(1), zgrep(1), sed(1), awk(1), ripgrep(1)

Copied to clipboard