grep, egrep, fgrep - search a file for a pattern
Plain call with pattern
pattern [file ...]
Call with (multiple) −e pattern
pattern] ... [file ...]
Call with −f file
pattern_file] [file ...]
[expression] [file ...]
[strings] [file ...]
The command searches the input text files (standard input default) for
lines matching a pattern. Normally, each line found is copied to the
standard output. supports the Basic Regular Expression syntax (see
regexp(5)). The option supports Extended Regular Expression (ERE) syn‐
tax (see regexp(5)). The option searches for fixed strings using the
fast Boyer-Moore string searching algorithm. The and options treat
newlines embedded in the pattern as alternation characters. A null
expression or string matches every line.
The forms and are maintained for backward compatibility. The use of
the and options is recommended for portability.
Extended regular expressions.
Each pattern specified is a sequence of one
or more EREs. The EREs can be separated by
newline characters or given in separate
expression options. A pattern matches an
input line if any ERE in the sequence
matches the contents of the input line with‐
out its trailing newline character. The
same functionality is obtained by using
Fixed strings. Each pattern specified is a sequence of one
or more strings. Strings can be separated
by newline characters or given in separate
expression options. A pattern matches an
input line if the line contains any of the
strings in the sequence. The same function‐
ality is obtained by using
Each line is preceded by the block number on which it was found.
This is useful in locating disk block num‐
bers by context. Block numbers are calcu‐
lated by dividing by 512 the number of bytes
that have been read from the file and round‐
ing down the result.
Only a count of matching lines is printed.
Same as a simple expression argument, but useful when the
expression begins with a hyphen Multiple
options can be used to specify multiple pat‐
terns; an input line is selected if it
matches any of the specified patterns.
The regular expression and or strings list is taken from
Suppress printing of filenames when searching multiple files.
Ignore uppercase/lowercase distinctions during comparisons.
Only the names of files with matching lines are listed (once),
separated by newlines. If standard input is
searched, a path name of will be written, in
the POSIX locale. In other locales, may be
replaced by something more appropriate in
Each line is preceded by its relative line number in the file
starting at 1.
The line number is reset for each file
searched. This option is ignored if or is
(Quiet) Do not write anything to the standard out‐
put, regardless of matching lines. Exit
with zero status upon finding the first
matching line. Overrides any options that
would produce output.
Error messages produced for nonexistent or unreadable files are
All lines but those matching are printed.
Select only those lines containing matches that
form whole words. The test is that the
matching substring 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 charac‐
ter. Word-constituent characters are let‐
ters, digits, and the underscore.
(eXact) Matches are recognized only when the entire
input line matches the fixed string or regu‐
The file name is output in all the cases in which output is generated
if there are more than one input file, unless the -h option is speci‐
fied. Care should be taken when using the characters and in expres‐
sion, because they are also meaningful to the shell. It is safest to
enclose the entire expression argument in single quotes
determines the locale to use for the locale categories when both and
the corresponding environment variable (beginning with do not specify a
locale. If is not specified or is set to the empty string, a default
of (see lang(5)) is used.
determines the locale to use to override any values for locale cate‐
gories specified by the settings of or any environment variables begin‐
determines the collating sequence used in evaluating regular expres‐
determines the interpretation of text as single byte and/or multi-byte
characters, the classification of characters as letters, the case
information for the option, and the characters matched by character
class expressions in regular expressions.
determines the language in which messages are displayed.
If any internationalization variable contains an invalid setting, the
commands behave as if all internationalization variables are set to See
International Code Set Support
Single-byte and multi-byte character code sets are supported.
Upon completion, returns one of the following values:
One or more matches found.
No match found.
Syntax error or inaccessible file (even if matches were found).
In the POSIX shell (sh(1)) the following example searches two files,
finding all lines containing occurrences of any of four strings:
Note that the single quotes are necessary to tell when the strings have
ended and the file names have begun.
For the C shell (see csh(1)) the following command can be used:
To search a file named containing the following entries:
To search a file for lines that contain either a or use either of the
Search all files in the current directory for the string
Search all files in the current directory subtree for the string and
ensure that no error occurs due to file name expansion exceeding system
argument list limits:
The previous example does not print the name of files where string
appears. To force to print file names, add a second argument to the
command portion of the command line:
In this form, the first file name is that produced by and the second
file name is the null file.
(XPG4 only.) If the option is specified, the exit status will be zero
if an input line is selected, even if an error was detected. Other‐
wise, default actions will be performed.
If the option is specified with non-word constituent characters, then
the output is unexpected.
SEE ALSOsed(1), sh(1), regcomp(3C), environ(5), lang(5), regexp(5).