Linux Commands

Most Common grep Syntax

In Linux, grep is one of the many tools that come pre-installed. It’s actually an acronym that stands for “global regular expression print.” The grep command can search for a string in files described by regular expression. If any match is found, grep prints the result in the console.

In this guide, we’ll have a quick look at some of the most common implementations of the grep command.

The grep command structure

Any grep command will have the following components.

$ grep <options> <regex_pattern> <files_to_search>

Common grep syntaxes

Searching for a string in a file

This is the simplest way of using the grep command. In the following example, grep will search for the term “dkms” (without quotes) in the file apt-packages-installed.txt (containing all the APT packages installed in the system).

$ grep "dkms" apt-packages-installed.txt

It’s recommended to use double quotations (“”) to indicate a string because a string may contain special characters and whitespaces.

Searching for a string in multiple files

The grep command can also perform the searches on multiple files. All you have to do is specify the files sequentially.

$ grep "dkms" apt-packages-installed-1.txt apt-packages-installed-2.txt

Instead of manually specifying the files, you can also use the wildcard (*) expression.

$ grep "dkms" apt-packages-installed-*.txt

What if we wanted to perform the search on all the files in the current directory? Just set the wildcard expression to indicate every file in the current directory.

$ grep "dkms" *

As we can see, grep found all the matches in the text files but didn’t handle the directories well. This perfectly leads to the next point.

Searching subdirectories

By default, grep won’t search the subdirectories for matches. If you want to grep to do so, you have to tell it by adding the flag “-r.”

Try performing the previous search once again with this new trick.

$ grep -r "dkms" *

Note that given the layers of subdirectories and the number of files to sift through, grep can take some time. On-screen, it will appear as if it’s frozen. So, don’t panic if you face similar situations.

Checking the line number of the match

Having the line number of the grep match can be a godsend. To see the line number of the match, use the flag “-n.”

$ grep -n "dkms" *.txt

Counting number of matches

The grep command can count the number of matches in the search result. To do so, use the flag “-l.”

$ grep -c "dkms" *.txt

Here, grep will show the number of matches for each searched file.

Print name of matching files

Sometimes, you want to know which files matched the pattern specified without the search result. If that’s the case, use the flag “-l.”

$ grep -l "dkms" *.txt

Print lines before and after the matching string

By default, grep will print the line it finds the specified string. However, we can calibrate the output to print a couple of lines before and/or after the string match.

Grep will print the matching string along with 5 previous lines on the console in the following command, thanks to the “-B” flag.

$ grep -B 5 "dkms" apt-packages-installed-1.txt

Similarly, we can also tell grep to print several lines after finding the matching string. To do so, we’ll use the flag “-A.” In the following example, grep will print 5 lines after finding the match.

$ grep -A 5 "dkms" apt-packages-installed-1.txt

Now, it’s time to combine both of these features.

$ grep -A 5 -B 5 "dkms" apt-packages-installed-1.txt

The command becomes unnecessarily confusing in this manner. We can tell grep to print several before and after the match is found to solve this. To do so, we’ll use the flag “-C.”

$ grep -C 5 "dkms" apt-packages-installed-1.txt

Note that the output will be symmetrical around the matching string if you’re using the “-C” flag. If you want an asymmetric number of lines, you have to separately describe them using the “-A” and “-B” flags.

Limiting grep output

If you’re working with a large text file that contains many instances of the search pattern, then the grep output will print a big chunk of output with lots of mess. For example,

$ grep "installed" apt-packages-installed-1.txt

We can tell grep to limit the number of lines it’s allowed to output in such a situation. To do so, we’ll use the flag “-m.” The command will look like this:

$ grep -m 10 "installed" apt-packages-installed-1.txt

Here, grep will only print the first 10 of the matching patterns.

Using grep with STDOUT

The STDOUT is a file stream. It’s a powerful feature of shells (like Bash). It’s a temporary file that contains the output of the previous command. When the next command is run, the value of STDOUT is updated. Learn more about STDIN, STDERR, and STDOUT in Bash.

The grep command can also work with STDOUT content. For example, you can pipe the output of a command for grep to work on.

In the following example, the APT command returns the list of all the packages installed in the system. We pipe the output to grep and search for the term “dkms” in the output. According to grep‘s default behavior, it will print all the lines that contain the string.

$ apt list --installed | grep "dkms"

The output may warn that using APT in scripts isn’t recommended as APT doesn’t offer a stable CLI interface. Learn more about the APT package manager on Ubuntu.

Final Thoughts

This is a brief guide on some of the most common usages of the grep command. For mastering grep, it’s recommended to check out this mega guide demonstrating 30 grep examples.

The help page of grep prints a short page of all the available options.

$ grep --help

The man page of grep contains all the options with detailed explanations.

$ man grep

For beginners, this guide on grep command in Linux is an excellent starting point.

Happy Computing!

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.