Show Menu


File and Path Selection

-a, --text
Process all files as text. Beware of binary output interp­reted as commands.
Skip files whose base names match glob.
Same as --exc­lude but get list of globs from file
Skip direct­ories that match. Direct­ories will also not be recursed.
Don't match binary files
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

Use line buffering on output. Can reduce perfor­mance.
Treat file(s) as binary.
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)


ls -rt * | xargs grep -e 'searchtext'
Searches through files in chronological order.

Matching Control

Used to specify multiple patterns or protect patterns starting with "­-"
Input file for patterns, one per line
Ignore case
Invert match
Whole word matches only
Match whole line only

Matcher Selection

Extended regular expres­sions (ERE)
Fixed strings
Basic regular expres­sions (BRE)
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. numbe­r[[­:di­git­:]]. 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.
Match exactly n times.
Match at least n times.
Match 0 to m times.
Match n to m times.
\<, \>
Match beginning or end of a word, respec­tively.
Match both beginning and end of the word. \B matches the opposite.
Match word character, aka [[:alnum:]]. \W matches the opposite.
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

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

Output Options

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

Context Lines

Print NUM lines of trailing context after match
Print NUM lines of lead context before match
Print NUM lines of context

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi


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 (english) Cheat Sheet
          grep Cheat Sheet

          More Cheat Sheets by njones