A FAQ Style Introduction To Grep

The grep command is one of the best pattern matching tools available for Ubuntu and Linux users in general. This is a quick FAQ style flyby to show beginners some handy grep functionality.

What is Grep?

Grep is a command line program that you can use to match lines in a file that contain a specific a pattern. Grep is possibly the most useful command available for such a task.
If grep finds a string that matches the supplied pattern(s), it returns the matching line as a result. You can change the options supplied to grep to influence what is determined to be a match, and also to determine what way to present the results.
For example, you may just want the matching text, or you may just want to know that there are one or more matches in a specific file.
Now lets run through what you can use grep for ...

Grep FAQ

As per man page, grep for most begginers will take the form: grep [options] PATTERN [FILE...] options put grep into a particular mode of functionality, PATTERN is used to fine tune what your looking for and FILE is where to look. With these in mind, lets answer some basic questions about using grep:

Q) How can I find out what lines in a file contain a specific string?
A) Simply use:

grep mystring filename
Q) How can I find out what lines from all files in a specific directory contain a specific string?
A) Use the wildcard "*" as the file name to search in.
grep mystring *
Q) How can I do this recursively (not just a specific directory, but also its sub directories and their sub directories)?
A) Use the "-r" recursive flag:
grep -r mystring *
Q) How can I ensure that the file's name where the matched lines exist get's included in the displayed result?
A) This is the default when more than one file is searched, however, sometimes if a script searches only one file it is still useful to include the file name in the match. You can force the behaviour using the "-H" option.
grep -H mystring filename
Q) How can I just return the first matched line in a matching file, not every line?
A) Use the "-m" option to specify the number of matches you require:
grep -m 1 mystring filename
Q) How can I search for only whole words that match a pattern, i.e. omit matches where only part of a word matches the pattern?
A) Use the "-w" option:
grep -w pattern filename
Q) How can I just display the matched part rather than the whole line?
A) Use the "-o" option
grep -o mystring filename
Q) How can I simply get a result for the number of matches found rather than the matches themselves?
A) Use the "-c" option:
grep -c mystring filename
Q) How can I get grep to tell me the actual line numbers of matched lines?
A) Use the "-n" option:
grep -n mystring filename
Q) How can I do a case-insensitive search?
A) Use the "-i" option:
grep -i mystring filename
Q) How can I search for lines that do not match a specific string or pattern?
A) Use the "-v" option to invert the match logic:
grep -v pattern filename
Q) How can I suppress error messages, e.g. suppress permissions related messages?
A) Use the "-s" option to suppress the errors:
grep -s pattern filename
Q) How can I ignore binary files?
A) Use the "-I" option:
grep -I pattern filename
Q) How can I ensure binary files are processed for matches just like text files?
A) Use the "-a" option:
grep -a pattern filename
Q) How can I get grep to only print the file name where there are matching line(s) rather than print the actual lines that match?
A) Use the "-l" option:
grep -l pattern filename
Q) How can I also display n lines of text before a matching line also?
A) Use the "-B" option to show context before each match (Matches will normally be separated by "--"). For example, to include 5 lines before the match(es) with the results, use:
grep -B  5 pattern filename
Q) How can I include n lines of text after a matching line also?
A) Use the "-A" option to show context after each match (Matches will normally be separated by "--"). For example, to include 5 lines after the match(es) with the results, use:
grep -A 5 pattern filename
Q) How can I include n lines of text before and n lines after a matching line also?
A) Use the "-C" option to show context before and after each match (Matches will normally be separated by "--"). For example, to include 5 lines before the match(es) along with 5 lines after the match(es) with the results, use:
grep -C 5 pattern filename
As you'd probably expect, these options can be combined in various way to produce pretty powerful commands in order to match exactly what you're looking for. We shall be covering grep in more detail (along with the use of regular expressions) in future posts.

Thanks to the Free Software Foundation for this powerful tool (and to Ken Thompson for the original).