A FAQ style introduction to grep

inspect-code

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.

Reading time:
3 min

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 beginners 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:

How can I find out what lines in a file contain a specific string?

Simply use:

grep my_string filename

How can I find out what lines from all files in a specific directory contain a specific string?

Use the wildcard * as the file name to search in.

grep my_string *

How can I do this recursively (not just a specific directory, but also its sub directories and their sub directories)?

Use the -r recursive flag:

grep -r my_string *

How can I ensure that the file's name where the matched lines exist gets included in the displayed result?

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 my_string filename

How can I just return the first matched line in a matching file, not every line?

Use the -m option to specify the number of matches you require:

grep -m 1 my_string filename

How can I search for only whole words that match a pattern, that is omit matches where only part of a word matches the pattern?

Use the -w option:

grep -w pattern filename

How can I just display the matched part rather than the whole line?

Use the -o option

grep -o my_string filename

How can I simply get a result for the number of matches found rather than the matches themselves?

Use the -c option:

grep -c my_string filename

How can I get grep to tell me the actual line numbers of matched lines?

Use the -n option:

grep -n my_string filename

How can I do a case-insensitive search?

Use the -i option:

grep -i my_string filename

How can I search for lines that do not match a specific string or pattern?

Use the -v option to invert the match logic:

grep -v pattern filename

How can I suppress error messages, for example suppress permissions related messages?

Use the -s option to suppress the errors:

grep -s pattern filename

How can I ignore binary files?

Use the -I option:

grep -I pattern filename

How can I ensure binary files are processed for matches just like text files?

Use the -a option:

grep -a pattern filename

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?

Use the -l option:

grep -l pattern filename

How can I also display n lines of text before a matching line also?

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

How can I include n lines of text after a matching line also?

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

How can I include n lines of text before and n lines after a matching line also?

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).

Thank you for reading this article.
Please share if you liked it.