May 20, 2022 4 min read

Common Examples of the Grep Command in Linux [With Free Cheatsheet Download]

Here are some practical and common use cases of the grep command. You can also download the cheat sheet for quick reference.
Table of Contents

Grep is a powerful UNIX command that lets you search inside the file contents on a variety of parameters. It's especially helpful when you are troubleshooting or debugging.

The grep command has a huge number of options and use cases. You probably will never need or use all of them. However, you'll end up using a handful of grep commands most of the time.

This article lists the most common grep commands with quick examples. Linux Handbook already has a detailed article on grep, so I won't go in depth here. I'll just the common options and their explanation here.

It's good if you are already familiar with the grep command but you keep forgetting which option does what.

Option Command Example Description
i grep -i pattern file Case insensitive search
A grep -A n pattern file Show n lines after the match
B grep -B n pattern file Show n lines before the match
C grep -C n pattern file Show n lines before and after the match
v grep -v pattern file Show lines that do not match
c grep -c pattern file Count number of matching lines
l grep -l pattern file Display only the file names
w grep -w pattern file Match the exact word
e grep -e regex file Match the regex pattern
a grep -a pattern file Search into binary files
r grep -r pattern dir Recursively search into directory

I have included a PDF cheatsheet which you can download, print and keep on your desk.

Quick examples of the Grep command

You probably already know that to search for a particular text or pattern in a file, you have to use grep like this:

grep search_pattern filename

Let's see a few common use cases of the grep command.

By default, the search with grep is case-sensitive. You can ignore case matching with the -i option:

grep -i search_pattern filename

This way, grep will return lines that match both Holmes and holmes.

Show lines before and after the matching lines

By default, you only see the matching lines. But when you are troubleshooting something, it helps to see a couple of lines before and/or after the matching lines.

You can use the -A to show lines after the matching ones. Remember, A is for After.

The command below will show the matching lines along with the 5 lines after the match.

grep -A 5 search_pattern filename

Similarly, you can use the -B option to show lines before the matching ones. Remember, B is for Before.

The command below will show 5 lines before the matching ones along with the matching line(s).

grep -B 5 search_pattern filename

My favorite is the option -C because it shows lines that are before and after the matching ones. Remember, C here stands for Circle.

The command below will show 5 lines before the matching one, the matching line and 5 lines after the matching line.

grep -C 5 search_pattern filename

Show the lines that do not match

You can use grep to display all the lines that DO NOT match the given pattern. This 'invert matching' is used with the -v option:

grep -v search_pattern filename

You can combine -i and -v options.

Count the number of matching lines

Instead of showing the matching lines, you can just get how many lines match the pattern with -c option. This is lowercase c.

grep -c search_pattern filename

You can combine the -c and -v option to get the number of lines that do not match the given pattern. You can of course, use the case-insensitive option -i.

Show line numbers of matching lines

To show the line numbers of the matching lines, you can use the -n option.

grep -n search_pattern filename

You can do the same with inverted search.

Search in multiple files

You may provide more than one file to grep to search into.

grep search_pattern file1 file2

That could work, but a more practical example is to search into a particular type of files. For example, if you want to look for a string in shell scripts only (files ending with .sh), you could use:

grep search_pattern *.sh

Search for all the files in a directory recursively

You can perform a recursive search with grep option -r. It will search for the given pattern in all the files in the current directory and its subdirectories.

grep -r search_pattern directory_path

Display only the file names

By default, grep shows the matching lines. If you have run the search on several files and you only want to see which files contain the string, you can use the -l option.

grep -l search_pattern files_pattern

Say you want to see which Markdown files contain the word "handbook," you can use:

grep -l handbook *.md

Search for full word only

By default, grep will show any lines that contain the given string. You may not always want that. If you are searching for the word 'done,' it will also show lines that contain the words 'doner' or 'abandoned.'

To make grep search for full word only, you can use the option -w:

grep -w search_string file

This way, if you search for the word 'done,' it will only show lines containing 'done,' not 'doner' or 'abandoned'.

Search for regex patterns

You can superpower your search by using a regex pattern. There is a dedicated option -e that allows using regex pattern and option -E that allows using extended regex patterns.

grep -e regex_pattern file

Search for this or that pattern

You can search for multiple patterns in the same grep search. If you want to see the lines that contain one pattern or the other, you can use the OR operator |. However, you have to escape this special character in the following manner.

grep 'pattern1\|pattern' filename

You can use multiple patterns with the OR operator.

There is no specific option for the AND operator. You may use grep multiple times with pipe redirection for that.

Search binary files

Grep ignores binary files by default. You can make it search in binary files as if it was a text file using the -a option.

grep -a pattern binary_file

Download Grep command cheat sheet

You can bookmark this page for quickly referencing the grep command options and use cases.

You can also download this grep cheatsheet in PDF format, print it, and keep it at your desk to have a quick glance without losing time.

Let me know if you have any other common grep use case that you would like to see on this page.

Join the conversation

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Linux Handbook.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.