← All commands
THE COMMAND REFERENCE

grep

Find lines of text that match a pattern.

Last updated

BusyBox

Implementation-level reference; examples are not certified for every installed version.

Overview

Use grep to select lines from a file or from another command's output. The pattern comes before the input filename. Start with a quoted literal word; regular expressions add more powerful matching rules.

For the full GNU reference, read the GNU grep manual.

Background

The name comes from the ed editor command g/re/p: globally search for a regular expression and print the matching lines. grep first appeared in Version 6 Unix, according to the FreeBSD manual. Today the name covers several separate programs that share that interface but not their source code: GNU grep, the BSD-derived grep that macOS ships, and a small reimplementation inside BusyBox. They agree on the common options below and differ on extensions such as the Perl-style -P patterns, regular-expression engines, and recursive-search details. That is why the sections that follow are split by implementation.

Examples

Find lines containing ERROR in a log:

grep 'ERROR' app.log

Show matching line numbers and ignore letter case:

grep -n -i 'error' app.log

Match a literal string rather than treating punctuation as a regular expression:

grep -F 'example.org' app.log

Show the lines that do not match:

grep -v 'ERROR' app.log

Count matching lines instead of printing them:

grep -c 'ERROR' app.log

List the names of files that contain a match:

grep -l 'ERROR' app.log logs/server.log

Match whole words only, so error does not match inside a longer word:

grep -w 'error' app.log

Match either of two patterns with an extended regular expression:

grep -E 'ERROR|INFO' app.log

Combine several patterns without regular-expression syntax by repeating -e:

grep -e 'INFO' -e 'example' app.log

Show one line of context before and after each match:

grep -C 1 'ERROR' app.log

Search every file under a directory:

grep -r 'ERROR' logs

Options

-n
Print each matching line with its line number. This helps you locate a result in the original file.

-i
Ignore letter case when matching. For predictable examples, use an ASCII fixture and the C locale.

-F
Treat the pattern as fixed text rather than a regular expression.

-v
Select lines that do not match the pattern.

-c
Print only the number of matching lines for each file, instead of the lines. With several files,
each count is prefixed by the filename.

-l
Print only the names of files that contain at least one match, once each.

-w
Require the match to be a whole word: it must not be preceded or followed by a letter, digit, or
underscore.

-E
Use extended regular expressions, so |, +, ?, and () work without backslashes. Quote the pattern.

-e
Give a pattern explicitly. Repeat it to match any of several patterns, or use it for a pattern that
starts with a dash.

-C
Print the given number of lines of context before and after each match. The value is the next
argument, as in -C 1.

-r
Search directories recursively. Name the directory explicitly so the command is portable: without
one, GNU grep and macOS grep search the current directory but print paths differently (a.txt versus
./a.txt), and other builds are untested here.

Common mistakes

Quote the pattern so the shell does not expand special characters before grep sees them. A pattern beginning with a dash can look like an option; use grep -e '-value' file.txt.

An exit status of 1 means no lines matched; it does not necessarily mean the command failed to read the file.

Portability

The examples in Common use options that GNU grep, macOS grep, and BusyBox grep all accept; they were executed on GNU grep 3.12, macOS grep (BSD) 2.6.0-FreeBSD, and BusyBox 1.37.0 (see the development guide for the exact check). Optional regex engines and recursive-search details differ. Implementation detection does not guarantee every installed version supports every feature; inspect the installed manual for version-specific behavior.

The names egrep and fgrep are older spellings of grep -E (extended regular expressions) and grep -F (fixed strings), and this page answers for them. GNU grep 3.12 prints a warning that they are obsolescent and then behaves as grep -E or grep -F; the macOS ones print no warning. Use the grep options in new scripts.

About BusyBox grep

BusyBox bundles a compact grep as one of its applets, so it is what you get on systems built around BusyBox, such as the Alpine Linux guest this project's Linux check uses. Which options exist depends on how BusyBox was configured when it was built, so check grep --help in the actual environment.

BusyBox notes

BusyBox applets can vary with build configuration. Prefer the common short options and consult grep --help in the actual environment before using optional features.

References

Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.