File and Path Selection
-a, --text |
Process all files as text. Beware of binary output interpreted 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 directories that match. Directories will also not be recursed. |
|
Don't match binary files |
--include=GLOB |
Search only for files matching glob. |
-R, -r |
Recurse directories |
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 performance. |
-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 expressions (ERE) |
-F |
Fixed strings |
-G |
Basic regular expressions (BRE) |
-P |
Perl regular expression (experimental?) |
Regular Expressions
. |
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[[:digit:]]
. 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, respectively. |
{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, respectively. |
\b |
Match both beginning and end of the word. \B matches the opposite. |
\w |
Match word character, aka [[:alnum:]]
. \W matches the opposite. |
\n |
Backreference 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-aligned for readability |
-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 |
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by njones