Show Menu
Cheatography

See: http://linuxcommand.org/lc3_man_pages/grep1.html

File and Path Selection

-a, --text
Process all files as text. Beware of binary output interp­reted as commands.
--exclude=GLOB
Skip files whose base names match glob.
--exclude-
from=FILE
Same as
--exclude
but get list of globs from file
--exclude-
dir=DIR
Skip direct­ories that match. Direct­ories will also not be recursed.
-
I
Don't match binary files
--include=GLOB
Search only for files matching glob.
-R, -r
Recurse direct­ories
Globs can use *, ?, and [...] as wildcards. Use \ as escape. Enclose multiples in curly braces, e.g.
--exclude={*.xml,*.xsf}
or
--exclude-dir={.git,.vs,my\ dir}
.

Other Options

--line-buffered
Use line buffering on output. Can reduce perfor­mance.
-U
Treat file(s) as binary.
-z
Treat lines as zero byte terminated instead of newline

Exit Status

0 - Selected lines are found
1 - Selected lines are not found
2 - Error occurred (unless a match is found and errors are ignored with the
-q
option)

Other

ls -rt * | xargs grep -e 'searchtext'

Searches through files in chronological order.
 

Matching Control

-e PATTERN
Used to specify multiple patterns or protect patterns starting with "­-"
-f FILE
Input file for patterns, one per line
-i
Ignore case
-v
Invert match
-w
Whole word matches only
-x
Match whole line only

Matcher Selection

-E
Extended regular expres­sions (ERE)
-F
Fixed strings
-G
Basic regular expres­sions (BRE)
-P
Perl regular expression (exper­ime­ntal?)

Regular Expres­sions

.
Match any character
[ ... ]
Match character list.
Use ^ to invert match.
Specify ranges with hyphen (
-
).
Ranges can also be specified using a character class, e.g.
number­[[:­dig­it:]]
. Valid character classes are: [:alnum:], [:alpha:], [:cntrl:], [:digit:], [:graph:], [:lower:], [:print:], [:punct:], [:space:], [:upper:], and [:xdigit:]
?, *, +
Repetition operators indicating at most once, zero or more, or at least once, respec­tively.
{n}
Match exactly n times.
{n,}
Match at least n times.
{,m}
Match 0 to m times.
{n,m}
Match n to m times.
\<, \>
Match beginning or end of a word, respec­tively.
\b
Match both beginning and end of the word. \B matches the opposite.
\w
Match word character, aka
[[:alnum:]]
. \W matches the opposite.
\n
Backre­ference to previously matched group where
n
is a single digit.
The above represents extended regular expression syntax. For basic syntax, you must escape
?
,
+
,
{
,
|
,
(
, and
)
.
 

Output Prefix

-b
Print 0-based byte offset of match within file
-H
Print the file name for each match (default)
-h
Hide filename from output
-n
Prefix output with line number
-T
Ensure first line of content is tab-al­igned for readab­ility
-Z
Output a zero byte after file names

Output Options

-c
Count number of lines
--color
[=WHEN]
With no WHEN it will show colors when it's the final command in the pipe. WHEN is never, always, or auto.
-L
Show only names of files that do NOT match
-l
Show only names of matching files
-m
Max number of lines to read from any file
-o
Output only the matching text
-q
Quiet, no output
-s
Suppress error messages for missing or unreadable files

Context Lines

-A NUM
Print NUM lines of trailing context after match
-B NUM
Print NUM lines of lead context before match
-C NUM
Print NUM lines of context
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          grep Cheat Sheet
          grep (english) Cheat Sheet

          More Cheat Sheets by njones