Grep Command
Grep Command
• About grep
• grep syntax
• grep examples
• Related commands
• Linux and Unix commands help
About grep
grep, which stands for "global regular expression print," processes text line by line and prints any lines
which match a specified pattern.
grep syntax
grep [OPTIONS] PATTERN [FILE...]
Overview
Grep is a powerful tool for matching a regular expression against text in a file, multiple files, or a
stream of input. It searches for the PATTERN of text that you specify on the command line, and outputs
the results for you.
Example Usage
Let's say want to quickly locate the phrase "our products" in HTML files on your machine. Let's start
by searching a single file. Here, our PATTERN is "our products" and our FILE is product-
listing.html.
A single line was found containing our pattern, and grep outputs the entire matching line to the
terminal. The line is longer than our terminal width so the text wraps around to the following lines, but
this output corresponds to exactly one line in our FILE.
Note: The PATTERN is interpreted by grep as a regular expression. In the above example, all the
characters we used (letters and a space) are interpreted literally in regular expressions, so only the exact
phrase will be matched. Other characters have special meanings, however — some punctuation marks,
for example. For more information, see our Regular Expression Quick Reference.
Our matching line is prefixed with "18:" which tells us this corresponds to line 18 in our file.
Notice that each line starts with the specific file where that match occurs.
Technical Description
grep searches the named input FILEs (or standard input if no files are named, or if a single dash ("-") is
given as the file name) for lines containing a match to the given PATTERN. By default, grep prints the
matching lines.
Also, three variant programs egrep, fgrep and rgrep are available:
• egrep is the same as running grep -E. In this mode, grep evaluates your PATTERN string as an
extended regular expression (ERE). Nowadays, ERE does not "extend" very far beyond basic
regular expressions, but they can still be very useful. For more information about extended
regular expressions, see Basic vs. Extended Regular Expressions, below.
• fgrep is the same as running grep -F. In this mode, grep evaluates your PATTERN string as a
"fixed string" — every character in your string is treated literally. For example, if your string
contains an asterisk ("*"), grep will try to match it with an actual asterisk rather than
interpreting this as a wildcard. If your string contains multiple lines (if it contains newlines),
each line will be considered a fixed string, and any of them can trigger a match.
• rgrep is the same as running grep -r. In this mode, grep will perform its search recursively. If it
encounters a directory, it will traverse into that directory and continue searching. (Symbolic
links are ignored; if you want to search directories that are symbolically linked, you should use
the -R option instead).
In older operating systems, egrep, fgrep and rgrep were distinct programs with their own executables.
In modern systems, these special command names are shortcuts to grep with the appropriate flags
enabled. They are functionally equivalent.
General Options
--help Print a help message briefly summarizing command-line options, and exit.
-V, --version Print the version number of grep, and exit.
Other Options
--line-buffered Use line buffering on output. This can cause a performance penalty.
If possible, use the mmap system call to read input, instead of the default read
system call. In some situations, --mmap yields better performance. However,
--mmap
--mmap can cause undefined behavior (including core dumps) if an input file shrinks
while grep is operating, or if an I/O error occurs.
Treat the file(s) as binary. By default, under MS-DOS and MS-Windows, grep
guesses the file type by looking at the contents of the first 32 KB read from the file. If
grep decides the file is a text file, it strips the CR characters from the original file
contents (to make regular expressions with ^ and $ work correctly). Specifying -U
-U, --binary
overrules this guesswork, causing all files to be read and passed to the matching
mechanism verbatim; if the file is a text file with CR/LF pairs at the end of each line,
this will cause some regular expressions to fail. This option has no effect on platforms
other than MS-DOS and MS-Windows.
Treat the input as a set of lines, each terminated by a zero byte (the ASCII NUL
-z, --null-data character) instead of a newline. Like the -Z or --null option, this option can be used
with commands like sort -z to process arbitrary file names.
Regular Expressions
A regular expression is a pattern that describes a set of strings. Regular expressions are constructed
analogously to arithmetic expressions, by using various operators to combine smaller expressions.
grep understands three different versions of regular expression syntax: "basic" (BRE), "extended"
(ERE) and "perl" (PRCE). In GNU grep, there is no difference in available functionality between basic
and extended syntaxes. In other implementations, basic regular expressions are less powerful. The
following description applies to extended regular expressions; differences for basic regular expressions
are summarized afterwards. Perl regular expressions give additional functionality.
The fundamental building blocks are the regular expressions that match a single character. Most
characters, including all letters and digits, are regular expressions that match themselves. Any meta-
character with special meaning may be quoted by preceding it with a backslash.
The period (.) matches any single character.
Anchoring
The caret ^ and the dollar sign $ are meta-characters that respectively match the empty string at the
beginning and end of a line.
Repetition
A regular expression may be followed by one of several repetition operators:
Alternation
Two regular expressions may be joined by the infix operator |; the resulting regular expression matches
any string matching either alternate expression.
Precedence
Repetition takes precedence over concatenation, which in turn takes precedence over alternation. A
whole expression may be enclosed in parentheses to override these precedence rules and form a
subexpression.
Environment Variables
The behavior of grep is affected by the following environment variables.
The locale for category LC_foo is specified by examining the three environment variables LC_ALL,
LC_foo, and LANG, in that order. The first of these variables that is set specifies the locale. For
example, if LC_ALL is not set, but LC_MESSAGES is set to pt_BR, then the Brazilian Portuguese
locale is used for the LC_MESSAGES category. The C locale is used if none of these environment
variables are set, if the locale catalog is not installed, or if grep was not compiled with national
language support (NLS).
Other variables of note:
This variable specifies default options to be placed in front of any explicit options.
For example, if GREP_OPTIONS is '--binary- files=without-match
GREP_OPTIO --directories=skip', grep behaves as if the two options --binary-files=without-
NS match and --directories=skip had been specified before any explicit options. Option
specifications are separated by whitespace. A backslash escapes the next character, so
it can be used to specify an option containing whitespace or a backslash.
This variable specifies the color used to highlight matched (non-empty) text. It is
deprecated in favor of GREP_COLORS, but still supported. The mt, ms, and mc
capabilities of GREP_COLORS have priority over it. It can only specify the color
GREP_COLO
used to highlight the matching non-empty text in any matching line (a selected line
R
when the -v command-line option is omitted, or a context line when -v is specified).
The default is 01;31, which means a bold red foreground text on the terminal's
default background.
GREP_COLO Specifies the colors and other attributes used to highlight various parts of the output.
RS Its value is a colon-separated list of capabilities that defaults to
ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36 with the rv and ne
boolean capabilities omitted (i.e., false). Supported capabilities are as follows:
SGR substring for whole selected lines (i.e., matching lines when the -v
command-line option is omitted, or non-matching lines when -v is specified).
sl= However, if the boolean rv capability and the -v command-line option are
both specified, it applies to context matching lines instead. The default is
empty (i.e., the terminal's default color pair).
SGR substring for whole context lines (i.e., non-matching lines when the -v
command-line option is omitted, or matching lines when -v is specified).
cx= However, if the boolean rv capability and the -v command-line option are
both specified, it applies to selected non-matching lines instead. The default
is empty (i.e., the terminal's default color pair).
Boolean value that reverses (swaps) the meanings of the sl= and cx=
rv capabilities when the -v command-line option is specified. The default is
false (i.e., the capability is omitted).
SGR substring for matching non-empty text in any matching line (i.e., a
selected line when the -v command-line option is omitted, or a context line
mt=01
when -v is specified). Setting this is equivalent to setting both ms= and mc=
;31
at once to the same value. The default is a bold red text foreground over the
current line background.
SGR substring for matching non-empty text in a selected line. (This is only
ms=01 used when the -v command-line option is omitted.) The effect of the sl= (or
;31 cx= if rv) capability remains active when this kicks in. The default is a bold
red text foreground over the current line background.
SGR substring for matching non-empty text in a context line. (This is only
mc=0 used when the -v command-line option is specified.) The effect of the cx= (or
1;31 sl= if rv) capability remains active when this kicks in. The default is a bold
red text foreground over the current line background.
SGR substring for file names prefixing any content line. The default is a
fn=35
magenta text foreground over the terminal's default background.
SGR substring for line numbers prefixing any content line. The default is a
ln=32
green text foreground over the terminal's default background.
SGR substring for byte offsets prefixing any content line. The default is a
bn=32
green text foreground over the terminal's default background.
SGR substring for separators that are inserted between selected line fields (:),
between context line fields, (-), and between groups of adjacent lines when
se=36
nonzero context is specified (--). The default is a cyan text foreground over
the terminal's default background.
Boolean value that prevents clearing to the end of line using Erase in Line
(EL) to Right (\33[K) each time a colorized item ends. This is needed on
terminals on which EL is not supported. It is otherwise useful on terminals
ne for which the back_color_erase (bce) boolean terminfo capability does not
apply, when the chosen highlight colors do not affect the background, or
when EL is too slow or causes too much flicker. The default is false (i.e., the
capability is omitted).
Note that boolean capabilities have no =... part. They are omitted (i.e., false) by
default and become true when specified.
See the Select Graphic Rendition (SGR) section in the documentation of the text
terminal that is used for permitted values and their meaning as character attributes.
These substring values are integers in decimal representation and can be concatenated
with semicolons. grep takes care of assembling the result into a complete SGR
sequence (\33[...m). Common values to concatenate include 1 for bold, 4 for
underline, 5 for blink, 7 for inverse, 39 for default foreground color, 30 to 37 for
foreground colors, 90 to 97 for 16-color mode foreground colors, 38;5;0 to 38;5;255
for 88-color and 256-color modes foreground colors, 49 for default background color,
40 to 47 for background colors, 100 to 107 for 16-color mode background colors, and
48;5;0 to 48;5;255 for 88-color and 256-color modes background colors.
LC_ALL,
These variables specify the locale for the LC_COLLATE category, which
LC_COLLATE
determines the collating sequence used to interpret range expressions like [a-z].
, LANG
LC_ALL,
These variables specify the locale for the LC_CTYPE category, which determines
LC_CTYPE,
the type of characters, e.g., which characters are whitespace.
LANG
LC_ALL, These variables specify the locale for the LC_MESSAGES category, which
LC_MESSAG determines the language that grep uses for messages. The default C locale uses
ES, LANG American English messages.
If set, grep behaves as POSIX requires; otherwise, grep behaves more like other
GNU programs. POSIX requires that options that follow file names must be treated
as file names; by default, such options are permuted to the front of the operand list
POSIXLY_CO
and are treated as options. Also, POSIX requires that unrecognized options be
RRECT
diagnosed as "illegal", but since they are not really against the law the default is to
diagnose them as "invalid". POSIXLY_CORRECT also disables
_N_GNU_nonoption_argv_flags_, described below.
_N_GNU_nono (Here N is grep's numeric process ID.) If the ith character of this environment
ption_argv_fla variable's value is 1, do not consider the ith operand of grep to be an option, even if it
appears to be one. A shell can put this variable in the environment for each command
it runs, specifying which operands are the results of file name wildcard expansion
gs_
and therefore should not be treated as options. This behavior is available only with
the GNU C library, and only when POSIXLY_CORRECT is not set.
Exit Status
The exit status is 0 if selected lines are found, and 1 if not found. If an error occurred the exit status is
2.
grep examples
Tip: If you haven't already see our example usage section we suggest reviewing that section first.
grep chope /etc/passwd
Search the Apache error_log file for any error entries that happened on May 31st at 3AM. By adding
quotes around the string this allows you to place spaces in the grep search.
grep -r "computerhope" /www/
Recursively search the directory /www/, and all subdirectories, for any lines of any files which contain
the string "computerhope".
grep -w "hope" myfile.txt
Search the file myfile.txt for lines containing the word "hope". Only lines containing the distinct word
"hope" will be matched. Lines in which "hope" is part of a word will not be matched.
grep -cw "hope" myfile.txt
Same as previous command, but displays a count of how many lines were matched, rather than the
matching lines themselves.
grep -cvw "hope" myfile.txt
Inverse of previous command: displays a count of the lines in myfile.txt which do not contain the word
"hope".
grep -l "hope" /www/*
Display the filenames (but not the matching lines themselves) of any files in /www/ (but not its
subdirectories) whose contents include the string "hope".