This post was originally published on go2linux.org on August 15, 2007. The domain is no longer mine, but I am the original author. I am republishing it here on garron.me with corrections and improvements.
Introduction
grep is a utility to look for specific strings in files, searching line by line. You can also use regular expressions instead of plain strings.
Usage
grep [options] pattern [file-list]
grep output with no options shows all the lines that contain the pattern in the given file.
Some of its more useful options are:
-v Reverse the behavior of grep and make it list the lines that do not contain the pattern.
-n
Shows the line where the pattern is (or is not, when using the -v option) together with the line number.
-c Displays the number of lines that contain the pattern in the given file(s).
-i Makes grep match regardless of case (lower or upper), so it is no longer case sensitive.
Examples
Given a file grep.txt (the man page of grep):
grep -c very grep.txt
1
grep -c in grep.txt
181
grep -i email grep.txt
Email bug reports to [email protected]. Be sure to include the
grep -i -n email grep.txt
408: Email bug reports to [email protected]. Be sure to include the
It is also great for debugging. If you are trying to find a message that comes from dhcpd in your /var/log/messages file, but it has too much noise, you can pipe it with tail:
tail -f /var/log/messages | grep dhcp
You will now see on the screen only the messages that contain the string "dhcp", no matter how many other messages are arriving in the log file.
The man page
GREP(1) GREP(1)
NAME
grep, egrep, fgrep, rgrep - print lines matching a pattern
SYNOPSIS
grep [options] PATTERN [FILE...]
grep [options] [-e PATTERN | -f FILE] [FILE...]
DESCRIPTION
grep searches the named input FILEs (or standard input if no files are
named, or the file name - is given) for lines containing a match to the
given PATTERN. By default, grep prints the matching lines.
In addition, three variant programs egrep, fgrep and rgrep are available.
egrep is the same as grep -E. fgrep is the same as grep -F.
rgrep is the same as grep -r.
OPTIONS
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines.
-a, --text
Process a binary file as if it were text.
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines.
-b, --byte-offset
Print the byte offset within the input file before each line of output.
-c, --count
Suppress normal output; instead print a count of matching lines
for each input file.
--colour[=WHEN], --color[=WHEN]
Surround the matching string with the marker found in the GREP_COLOR
environment variable. WHEN may be 'never', 'always', or 'auto'.
-E, --extended-regexp
Interpret PATTERN as an extended regular expression.
-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern; useful to protect patterns beginning with -.
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by newlines.
-f FILE, --file=FILE
Obtain patterns from FILE, one per line.
-i, --ignore-case
Ignore case distinctions in both the PATTERN and the input files.
-l, --files-with-matches
Suppress normal output; instead print the name of each input file
that contains a match.
-n, --line-number
Prefix each line of output with its line number within its input file.
-r, --recursive
Read all files under each directory, recursively.
-v, --invert-match
Invert the sense of matching, selecting non-matching lines.
-w, --word-regexp
Select only those lines containing matches that form whole words.