Name

grep

Synopsis

grep [options] pattern [files]

Search one or more files for lines that match a regular expression pattern. Regular expressions are described in Chapter 7. Exit status is 0 if any lines match, 1 if none match, and 2 for errors. See also egrep and fgrep.

Options

-a, --text

Don’t suppress output lines with binary data; treat as text.

-A num, --after-context=num

Print num lines of text that occur after the matching line.

-b, --byte-offset

Print the byte offset within the input file before each line of output.

-B num, --before-context=num

Print num lines of text that occur before the matching line.

-c, --count

Print only a count of matched lines. With -v or --revert-match, count nonmatching lines.

-C[num], --context[=num], -num

Print num lines of leading and trailing context. Default context is 2 lines.

-d action, --directories=action

Define an action for processing directories. Possible actions are:

read

Read directories like ordinary files (default).

skip

Skip directories.

recurse

Recursively read all files under each directory. Same as -r.

-D action, --directories=action

Define an action for processing an input file that is a device, FIFO, or socket. Possible actions are read (default) and skip, as in -d.

-e pattern, --regexp=pattern

Search for pattern. Same as specifying a pattern as an argument, but useful in protecting patterns beginning with -.

-E, -extended-regexp

Act like egrep, recognizing extended regular expressions such as (UN|POS)IX to find UNIX and POSIX.

-f file, --file=file

Take ...

Get Linux in a Nutshell, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.