Top 5 uses of GREP command in Linux

The grep utility searches input files, selecting lines matching one or more patterns; pattern types are controlled by the specified options.

Are you a Linux user and Server Admin, Analyst or developer? If YES, then you must know about the GREP command. Even if you are not any one of them, this article helps you out to strengthen your Linux skill and helps you to get noticed in your Organization, School, College, so on and so forth.

In this article, we shall see how to optimize your day-to-day task, increase the chances of getting hired, and most importantly, how to save precious hours of your life.

‘grep’ command is the prime weapon in the war zone of Linux. Once you master the GREP command, you should get the tons of benefits out of it.

GREP Command Usage Benefits

  1. Saves time over finding the required configuration
  2. Solves the problem related to the troubleshooting more quickly
  3. Help for debugging the code more quickly
  4. Finding out the blank files and folders in Linux

Yes, you read it right. If you master the ‘grep’ command, you can save your 90% of the valuable time. Let me tell you how.

Use Case

We assume, a Security analyst is troubleshooting the logs for a recent attack. In this use case, the analyst may have around GBs, which takes an ample amount of time to troubleshoot the issue. By that time, your website is down, that impact is different.

However, Using the grep command Security Analyst can easily find out the required set of logs with the ‘Parameter’ he/she searching in the captured logs, that too in a brief period.

Background of ‘grep’ Command

The abbreviation ‘grep’ stands for ‘Global Regular Expression Print.’ This is one of the most widely used commands in the Linux world. Approx., 90% of users, use ‘grep’ command to find the matching pattern/string in a file. However, we always miss the real power of the grep command. The regular expression capability makes it one of the prime command of the Linux world. Which helps a lot to process the data or analyses the broad set of logs.

Let’s dive into the magical world of ‘grep’ command. We start right from the basics and move toward the pro version of this command. Here are the top 5 uses of the ‘grep’ command.

1. How to exclude the parameter/search string?

Every day we work on data and process the GBs of logs. The time when we hit the basic ‘grep’  command to find the matching pattern, it returns a hundred lines of results. We do not bother about all the line in the logs, we need only selected set of lines.

We can overcome this the situation using the -v flag of the ‘grep’ command.


 grep -i token catalina.out|grep -v session

How to count the number of matching string
How to count the number of matching string

In the example, we are searching for the pattern ‘token ‘ in the log file named ‘catalina.out.’ As shown, the output gives several lines which contain the string ‘Token.’ However, we are excluding the search result using the ‘-v’ flag.

2. Count the occurrence of a string

Let’s continue the last example, and we searched the work ‘Token’ in the ‘catalina.out’ and couple of lines showed up in the output console. What if you want to count how much Token has been assigned/authenticated. To get this count, one can use the flag -c.


grep -ic token catalina.out

How to count the number of matching string
Count the number of matching string

As shown in the attached screenshot(above), it clearly shows the count of the work ‘Token’ in the given set of logs. If you have observed the flag -i and still confused about its use, then let me tell you, it is used to ignore the case of the word/string.

3. How to search in the tar(zip) file

Many time to avoid the disk space issue we tar our logs. What if we need to find a string in our zipped file, will you untar it and then search a required string inside it? No, you no need to do it going forward. You can use the below command to seek the necessary string in the zipped file.


 zgrep token logs.tar.gz

Find the string in zipped and tar files

You have to use the zgrep to search inside the tar file.

4. How to get the line before and after the searched string?

If you are debugging the code and want to check the details of any specific function, you may want to check the few lines before the search word and after the search word.  You can achieve this using the ‘context‘ flag.


grep 12:26:59.971 --context=3 catalina.out

Get lines before and after the search word

As shown in the given case, one can quickly analyze the logs or code using the context flag and can get the line before and after the searched string.

5. BONUS and the Bumper One, How the search a string in all file in the directory

Let’s assume you are configuring the web server and you want to update a parameter however not sure about the file name and the location of the file, what to do?

Use our BONUS trick to get the location and file name which contain your search string. Below example shows how we can achieve this.


grep -rwn ./ -e 12:26:59.971

get the file name which contain the searched string
Get the file name which contains the searched string

To make this possible, we use the regular expression with the grep command. You can see we use the command to search the string ’12:26:59.971′ in the current directory. On the left-hand side, we can see the name of the file along with the file location in the purple color. One can get the name of a file which contains a specific keyword with the help of regular expression. 

This is all about the top 5 uses of the grep command. It has huge potential. Keep using it and explore more, learn more.

Divya Kiran Kumar
I'm the Editor of FOSS Linux. I worked as a Software Engineer before taking up blogging as my full-time job. I enjoy using Linux, and can't imagine anything else for my PC. Apart from writing for FOSS Linux, I enjoy reading non-fictional books. Sapiens was my favorite last read. I hope you enjoy reading and using this blog to enhance your Linux experience! Have a great day ahead!


Please enter your comment!
Please enter your name here





Photo editing is a global hobby, profession, and exploit. Its execution is not dependant on a specific Operating System or device. For this reason, anyone can be a photo editor regardless of their Operating system preference. The power of an ideal and reliable photo editor is in the many unique features they present to their users. Some features pose unique photo editing benefits like correcting brightness imbalances and color hue. Some editors are efficient in sharpness adjustments and red-eye removal. Others present flexible auto-cropping and zoom features. These are some of the characteristics that define a photo editor.
You might ask, what is the necessity of a password manager? To answer this question, we have to breakdown the attributes of a good and secure password. These attributes are not related to the password we compose at a moment's notice. You do not need a password manager or a password wallet for passwords related to your pet’s name, dream city to visit, or even your favorite pronounceable noun or verb.