Linux "rgrep" Command Line Options and Examples
print lines matching a pattern

grep searches for PATTERN in each FILE. A FILE of “-” stands for standard input. If no FILE is given, recursive searches examine the working directory, and nonrecursive searches read standard input.


Usage:

grep [OPTIONS] PATTERN [FILE...]
    grep [OPTIONS] -e PATTERN ... [FILE...]
    grep [OPTIONS] -f FILE ... [FILE...]






Command Line Options:

--help
Output a usage message and exit.
rgrep --help ...
-V
Output the version number of grep and exit.Matcher Selection
rgrep -V ...
-E
Interpret PATTERN as an extended regular expression (ERE, see below).
rgrep -E ...
-F
Interpret PATTERN as a list of fixed strings (instead of regular expressions), separated by newlines,any of which is to be matched.
rgrep -F ...
-G
Interpret PATTERN as a basic regular expression (BRE, see below). This is the default.
rgrep -G ...
-P
Interpret the pattern as a Perl-compatible regular expression (PCRE). This is experimental and grep -Pmay warn of unimplemented features.Matching Control
rgrep -P ...
-e
Use PATTERN as the pattern. If this option is used multiple times or is combined with the -f (--file)option, search for all patterns given. This option can be used to protect a pattern beginning with
rgrep -e ...
-f
Obtain patterns from FILE, one per line. If this option is used multiple times or is combined with the
rgrep -f ...
-i
Ignore case distinctions, so that characters that differ only in case match each other.
rgrep -i ...
-v
Invert the sense of matching, to select non-matching lines.
rgrep -v ...
-w
Select only those lines containing matches that form whole words. The test is that the matchingsubstring must either be at the beginning of the line, or preceded by a non-word constituent character.Similarly, it must be either at the end of the line or followed by a non-word constituent character.Word-constituent characters are letters, digits, and the underscore. This option has no effect if -xis also specified.
rgrep -w ...
-x
Select only those matches that exactly match the whole line. For a regular expression pattern, this islike parenthesizing the pattern and then surrounding it with ^ and $.
rgrep -x ...
-y
General Output Control
rgrep -y ...
-c
Suppress normal output; instead print a count of matching lines for each input file. With the -v,
rgrep -c ...
--invert-match
option (see below), count non-matching lines.
rgrep --invert-match ...
--color[
Surround the matched (non-empty) strings, matching lines, context lines, file names, line numbers, byteoffsets, and separators (for fields and groups of context lines) with escape sequences to display themin color on the terminal. The colors are defined by the environment variable GREP_COLORS. Thedeprecated environment variable GREP_COLOR is still supported, but its setting does not have priority.WHEN is never, always, or auto.
rgrep --color[ ...
-L
Suppress normal output; instead print the name of each input file from which no output would normallyhave been printed. The scanning will stop on the first match.
rgrep -L ...
-l
Suppress normal output; instead print the name of each input file from which output would normally havebeen printed. The scanning will stop on the first match.
rgrep -l ...
-m
Stop reading a file after NUM matching lines. If the input is standard input from a regular file, andNUM matching lines are output, grep ensures that the standard input is positioned to just after thelast matching line before exiting, regardless of the presence of trailing context lines. This enablesa calling process to resume a search. When grep stops after NUM matching lines, it outputs anytrailing context lines. When the -c or --count option is also used, grep does not output a countgreater than NUM. When the -v or --invert-match option is also used, grep stops after outputting NUMnon-matching lines.
rgrep -m ...
-o
Print only the matched (non-empty) parts of a matching line, with each such part on a separate outputline.
rgrep -o ...
-q
Quiet; do not write anything to standard output. Exit immediately with zero status if any match isfound, even if an error was detected. Also see the -s or --no-messages option.
rgrep -q ...
-s
Suppress error messages about nonexistent or unreadable files.Output Line Prefix Control
rgrep -s ...
-b
Print the 0-based byte offset within the input file before each line of output. If -o
rgrep -b ...
--only-matching)
print the offset of the matching part itself.
rgrep --only-matching) ...
-H
Print the file name for each match. This is the default when there is more than one file to search.
rgrep -H ...
-h
Suppress the prefixing of file names on output. This is the default when there is only one file (oronly standard input) to search.
rgrep -h ...
--label
Display input actually coming from standard input as input coming from file LABEL. This is especiallyuseful when implementing tools like zgrep, e.g., gzip -cd foo.gz | grep --label=foo -H something. Seealso the -H option.
rgrep --label ...
-n
Prefix each line of output with the 1-based line number within its input file.
rgrep -n ...
-T
Make sure that the first character of actual line content lies on a tab stop, so that the alignment oftabs looks normal. This is useful with options that prefix their output to the actual content: -H,-n,and -b. In order to improve the probability that lines from a single file will all start at the samecolumn, this also causes the line number and byte offset (if present) to be printed in a minimum sizefield width.
rgrep -T ...
-u
Report Unix-style byte offsets. This switch causes grep to report byte offsets as if the file were aUnix-style text file, i.e., with CR characters stripped off. This will produce results identical torunning grep on a Unix machine. This option has no effect unless -b option is also used; it has noeffect on platforms other than MS-DOS and MS-Windows.
rgrep -u ...
-Z
Output a zero byte (the ASCII NUL character) instead of the character that normally follows a filename. For example, grep -lZ outputs a zero byte after each file name instead of the usual newline.This option makes the output unambiguous, even in the presence of file names containing unusualcharacters like newlines. This option can be used with commands like find -print0, perl -0, sort -z,and xargs -0 to process arbitrary file names, even those that contain newline characters.Context Line Control
rgrep -Z ...
-A
Print NUM lines of trailing context after matching lines. Places a line containing a group separator
rgrep -A ...
-B
Print NUM lines of leading context before matching lines. Places a line containing a group separator
rgrep -B ...
-C
Print NUM lines of output context. Places a line containing a group separator (--) between contiguousgroups of matches. With the -o or --only-matching option, this has no effect and a warning is given.File and Directory Selection
rgrep -C ...
-a
Process a binary file as if it were text; this is equivalent to the --binary-files=text option.
rgrep -a ...
--binary-files
If a file's data or metadata indicate that the file contains binary data, assume that the file is oftype TYPE. Non-text bytes indicate binary data; these are either output bytes that are improperlyencoded for the current locale, or null input bytes when the -z option is not given.By default, TYPE is binary, and when grep discovers that a file is binary it suppresses any furtheroutput, and instead outputs either a one-line message saying that a binary file matches, or no messageif there is no match.If TYPE is without-match, when grep discovers that a file is binary it assumes that the rest of thefile does not match; this is equivalent to the -I option.If TYPE is text, grep processes a binary file as if it were text; this is equivalent to the -a option.When type is binary, grep may treat non-text bytes as line terminators even without the -z option.This means choosing binary versus text can affect whether a pattern matches a file. For example, whentype is binary the pattern q$ might match q immediately followed by a null byte, even though this isnot matched when type is text. Conversely, when type is binary the pattern . (period) might not matcha null byte.Warning: The -a option might output binary garbage, which can have nasty side effects if the output isa terminal and if the terminal driver interprets some of it as commands. On the other hand, whenreading files whose text encodings are unknown, it can be helpful to use -a or to set LC_ALL='C' in theenvironment, in order to find more matches even if the matches are unsafe for direct display.
rgrep --binary-files ...
-D
If an input file is a device, FIFO or socket, use ACTION to process it. By default, ACTION is read,which means that devices are read just as if they were ordinary files. If ACTION is skip, devices aresilently skipped.
rgrep -D ...
-d
If an input file is a directory, use ACTION to process it. By default, ACTION is read, i.e., readdirectories just as if they were ordinary files. If ACTION is skip, silently skip directories. IfACTION is recurse, read all files under each directory, recursively, following symbolic links only ifthey are on the command line. This is equivalent to the -r option.
rgrep -d ...
--exclude
Skip any command-line file with a name suffix that matches the pattern GLOB, using wildcard matching; aname suffix is either the whole name, or any suffix starting after a / and before a +non-/. Whensearching recursively, skip any subfile whose base name matches GLOB; the base name is the part afterthe last /. A pattern can use *, ?, and [...] as wildcards, and \ to quote a wildcard or backslashcharacter literally.
rgrep --exclude ...
--exclude-from
Skip files whose base name matches any of the file-name globs read from FILE (using wildcard matchingas described under --exclude).
rgrep --exclude-from ...
--exclude-dir
Skip any command-line directory with a name suffix that matches the pattern GLOB. When searchingrecursively, skip any subdirectory whose base name matches GLOB. Ignore any redundant trailing slashesin GLOB.
rgrep --exclude-dir ...
-I
Process a binary file as if it did not contain matching data; this is equivalent to the
rgrep -I ...
--include
Search only files whose base name matches GLOB (using wildcard matching as described under --exclude).
rgrep --include ...
-r
Read all files under each directory, recursively, following symbolic links only if they are on thecommand line. Note that if no file operand is given, grep searches the working directory. This isequivalent to the -d recurse option.
rgrep -r ...
-R
Read all files under each directory, recursively. Follow all symbolic links, unlike -r.Other Options
rgrep -R ...
--line-buffered
Use line buffering on output. This can cause a performance penalty.
rgrep --line-buffered ...
-U
Treat the file(s) as binary. By default, under MS-DOS and MS-Windows, grep guesses whether a file istext or binary as described for the --binary-files option. If grep decides the file is a text file, itstrips the CR characters from the original file contents (to make regular expressions with ^ and $ workcorrectly). Specifying -U overrules this guesswork, causing all files to be read and passed to thematching mechanism verbatim; if the file is a text file with CR/LF pairs at the end of each line, thiswill cause some regular expressions to fail. This option has no effect on platforms other than MS-DOSand MS-Windows.
rgrep -U ...
-z
Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NULcharacter) instead of a newline. Like the -Z or --null option, this option can be used with commandslike sort -z to process arbitrary file names.REGULAR EXPRESSIONSA regular expression is a pattern that describes a set of strings. Regular expressions are constructedanalogously 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” (PCRE). In GNU grep there is no difference in available functionality between basic and extendedsyntaxes. In other implementations, basic regular expressions are less powerful. The following descriptionapplies to extended regular expressions; differences for basic regular expressions are summarized afterwards.Perl-compatible regular expressions give additional functionality, and are documented in pcresyntax(3) andpcrepattern(3), but work only if PCRE is available in the system.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 withspecial meaning may be quoted by preceding it with a backslash.The period . matches any single character.Character Classes and Bracket ExpressionsA bracket expression is a list of characters enclosed by [ and ]. It matches any single character in thatlist; if the first character of the list is the caret ^ then it matches any character not in the list. Forexample, the regular expression [0123456789] matches any single digit.Within a bracket expression, a range expression consists of two characters separated by a hyphen. It matchesany single character that sorts between the two characters, inclusive, using the locale's collating sequenceand character set. For example, in the default C locale, [a-d] is equivalent to [abcd]. Many locales sortcharacters in dictionary order, and in these locales [a-d] is typically not equivalent to [abcd]; it might beequivalent to [aBbCcDd], for example. To obtain the traditional interpretation of bracket expressions, youcan use the C locale by setting the LC_ALL environment variable to the value C.Finally, certain named classes of characters are predefined within bracket expressions, as follows. Theirnames are self explanatory, and they are [:alnum:], [:alpha:], [:cntrl:], [:digit:], [:graph:], [:lower:],[:print:], [:punct:], [:space:], [:upper:], and [:xdigit:]. For example, [[:alnum:]] means the characterclass of numbers and letters in the current locale. In the C locale and ASCII character set encoding, this isthe same as [0-9A-Za-z]. (Note that the brackets in these class names are part of the symbolic names, andmust be included in addition to the brackets delimiting the bracket expression.) Most meta-characters losetheir special meaning inside bracket expressions. To include a literal ] place it first in the list.Similarly, to include a literal ^ place it anywhere but first. Finally, to include a literal - place it last.AnchoringThe caret ^ and the dollar sign $ are meta-characters that respectively match the empty string at thebeginning and end of a line.The Backslash Character and Special ExpressionsThe symbols \< and \> respectively match the empty string at the beginning and end of a word. The symbol \bmatches the empty string at the edge of a word, and \B matches the empty string provided it's not at the edgeof a word. The symbol \w is a synonym for [_[:alnum:]] and \W is a synonym for [^_[:alnum:]].RepetitionA regular expression may be followed by one of several repetition operators:? The preceding item is optional and matched at most once.* The preceding item will be matched zero or more times.+ The preceding item will be matched one or more times.{n} The preceding item is matched exactly n times.{n,} The preceding item is matched n or more times.{,m} The preceding item is matched at most m times. This is a GNU extension.{n,m} The preceding item is matched at least n times, but not more than m times.ConcatenationTwo regular expressions may be concatenated; the resulting regular expression matches any string formed byconcatenating two substrings that respectively match the concatenated expressions.AlternationTwo regular expressions may be joined by the infix operator |; the resulting regular expression matches anystring matching either alternate expression.PrecedenceRepetition takes precedence over concatenation, which in turn takes precedence over alternation. A wholeexpression may be enclosed in parentheses to override these precedence rules and form a subexpression.Back References and SubexpressionsThe back-reference \n, where n is a single digit, matches the substring previously matched by the nthparenthesized subexpression of the regular expression.Basic vs Extended Regular ExpressionsIn basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead usethe backslashed versions \?, \+, \{, \|, \(, and \).ENVIRONMENT VARIABLESThe 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, LANG,in that order. The first of these variables that is set specifies the locale. For example, if LC_ALL is notset, but LC_MESSAGES is set to pt_BR, then the Brazilian Portuguese locale is used for the LC_MESSAGEScategory. The C locale is used if none of these environment variables are set, if the locale catalog is notinstalled, or if grep was not compiled with national language support (NLS). The shell command locale -alists locales that are currently available.GREP_OPTIONSThis variable specifies default options to be placed in front of any explicit options. As this causesproblems when writing portable scripts, this feature will be removed in a future release of grep, andgrep warns if it is used. Please use an alias or script instead.GREP_COLORThis variable specifies the color used to highlight matched (non-empty) text. It is deprecated infavor of GREP_COLORS, but still supported. The mt, ms, and mc capabilities of GREP_COLORS havepriority over it. It can only specify the color used to highlight the matching non-empty text in anymatching line (a selected line when the -v command-line option is omitted, or a context line when -v isspecified). The default is 01;31, which means a bold red foreground text on the terminal's defaultbackground.GREP_COLORSSpecifies the colors and other attributes used to highlight various parts of the output. Its value isa colon-separated list of capabilities that defaults toms=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.sl= SGR substring for whole selected lines (i.e., matching lines when the -v command-line option isomitted, or non-matching lines when -v is specified). If however the boolean rv capability andthe -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).cx= SGR substring for whole context lines (i.e., non-matching lines when the -v command-line optionis omitted, or matching lines when -v is specified). If however the boolean rv capability andthe -v command-line option are both specified, it applies to selected non-matching linesinstead. The default is empty (i.e., the terminal's default color pair).rv Boolean value that reverses (swaps) the meanings of the sl= and cx= capabilities when the -vcommand-line option is specified. The default is false (i.e., the capability is omitted).mt=01;31SGR substring for matching non-empty text in any matching line (i.e., a selected line when the
rgrep -z ...