Using the GREP command in Linux with examples

Grep is one of the most powerful commands used by every Linux administrator. This tutorial guides you through its usage with examples.

In the Linux world, General Regular Expression Parser or grep is one of the most powerful commands used by every Linux administrator. Using it, you should be able to search files content and also perform a search within the command output: not just that, but a lot more, which we will be discussing in this tutorial.

The GREP command usage with examples

In this guide, we will walk you through the command usage and show you some helpful and useful examples that should save a lot of time. This tutorial is performed on the Ubuntu machine, but the command works on all Linux distributions.

Installation

To start using the command, you need first to check if it is already installed on your system. If not installed, run the following command in the terminal.

sudo apt install grep

You can check the command version to ensure that it is installed successfully.

grep --version

Grep Commad Version
Grep Command Version

Syntax

grep [option/s] [pattern] [file]

Where the [option/s] can be:

  • -i –> used to ensure the search pattern regardless of its case sensitivity.
  • -c –> Preview, the count of the matching pattern.
  • -v –> select non-matching lines that do not contain the given pattern.
  • -n –> Preview the line number.

Where the [pattern] is a regular expression, and the [file] is the file you are going to search in its content.

Examples

We shall create a test file to use in our case scenario examples.

vi grep_tuts

Create New File
Create a New File

Paste the following lines in the grep_tuts file:

Hi Guys,
i am hend.
And this is a tutorial file for the usage of the grep commad.
The next 2 lines are empty lines.


Hope You Will Enjoy This Tutorial. 
Bye.

Content Of Grep Tutorials Example File
Content Of Grep Tutorials Example File

Example 1. To search for a string in a single file.

grep "hend" grep_tuts

Search For a Certain String in a File
Search For a Certain String in a File

Example 2. Let’s make a copy from the grep_tuts file, which will use later on.

cp grep_tuts grep_tuts2

Create Copy From The Example File
Create Copy From The Example File

Now to search for a specific string in multiple files, use the next command.

grep "hend" grep_tuts grep_tuts2

Search For a Certain String in Multiple Files
Search For a Certain String in Multiple Files

Example 3. To search for a certain string in a file regardless of its case.

grep -i "tutorial" grep_tuts

Search For Case Insensitive String
Search For Case Insensitive String

Example 4. To search for a certain pattern in a file regardless of case sensitivity.

grep -i "the.*lines" grep_tuts

This means to search for any lines start with “the” string and ends with “lines.” Where the * denotes zero or more characters, and the -i option is to ignore “the” and “lines” case.

Search Using Regular Expression and Case Sensitive
Search Using Regular Expression and Case Sensitive

But in case you did not use the -i option, there will be no result.

grep "the.*lines" grep_tuts

Search Using Regular Expression and Case Sensitive
Search Using Regular Expression and Case Sensitive

Example 5. To display two lines after the pattern you are searching for.

grep -A 2 "hend" grep_tuts

Preview Number of Lines After a Certain String
Preview Number of Lines After a Certain String

Example 6. To display one line before the pattern you are searching for.

grep -B 1 "hend" grep_tuts

Preview Number of Lines Before a Certain String
Preview Number of Lines Before a Certain String

Example 7. To display one line around (or after and before) the pattern you are searching for.

grep -C 1 "hend" grep_tuts

Preview Number of Lines Around a Certain String
Preview Number of Lines Around a Certain String

Example 8. To search for a certain string in all files that exist in the current directory.

grep -r "Enjoy" *

Search For a Certain String Within The Current Directory
Search For a Certain String Within The Current Directory

Example 9. To display all the lines that do not contain the given string.

grep -v -i -e "Enjoy" grep_tuts

Preview Lines That Does not Contain the Given String
Preview Lines That Does not Contain the Given String

Example 10. To display the count of all lines that contain the string you are searching for.

grep -c "this" grep_tuts

Preview Count Of Matching String
Preview Count Of Matching String

Example 11. To display the count of all lines that contain the string you are searching for regardless of case sensitivity.

grep -c -i "this" grep_tuts

Preview Count Of Matching a Case Insenstive String
Preview Count Of Matching a Case Insensitive String

Example 12. To display the count of the un-matching pattern.

grep -v -c "this" grep_tuts

Preview Count Of Un-matching String
Preview Count Of Un-matching String

Example 13. To display all file names that contain the string you are searching for.

grep -l "this" *

Preview File Names That Contain a Certain String
Preview File Names That Contain a Certain String

Example 14. To display the number of lines.

grep -n "this" grep_tuts

Preview Line Number
Preview Line Number

Example 15. To search the output of a specific command. For example, list all the running processes that are started by the “tuts” user.

ps aux | grep tuts

Search Output Of Command
Search Output Of Command

That’s all about the usage of grep command in Linux.

Hend Adel
Hi! I'm Hend Adel, a freelancer technical geek with successful experience in Database, Linux and many other IT fields. I help to build solutions to suit business needs and creating streamlined processes. I love Linux and I'm here to share my skills via FOSS Linux! Thanks for reading my article.

1 COMMENT

  1. Hello,
    Sometimes, when you need to filter out the process list, you end up with an extra line you may not want to see.
    $ps ax | grep kworker
    23421 ? I 0:00 [kworker/u8:1-events_unbound]
    25365 ? I 0:00 [kworker/u8:0-events_unbound]
    31422 ? I 0:00 [kworker/3:2-cgroup_destroy]
    31490 ? I 0:00 [kworker/2:0-events]
    31816 ? I 0:00 [kworker/1:1-events]
    31983 ? I 0:00 [kworker/0:3-events]
    355 pts/0 S+ 0:00 grep –color=auto kworker

    This is because the grep command contains the word that match. If you don’t want to see it, then use this :
    $ps ax | grep [k]worker
    23421 ? I 0:00 [kworker/u8:1-events_unbound]
    25365 ? I 0:00 [kworker/u8:0-events_unbound]
    31422 ? I 0:00 [kworker/3:2-cgroup_destroy]
    31490 ? I 0:00 [kworker/2:0-events]
    31816 ? I 0:00 [kworker/1:1-events]
    31983 ? I 0:00 [kworker/0:3-events]

    The grep line has gone! How’s that?

    A+
    (My 2 cents)

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,312FansLike
385FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

7 Best Ways to Kill Unresponsive Programs in Linux

For dealing with a frozen app or desktop, you can't use the CTRL+ALT+DEL in Linux system. Instead, there are powerful alternatives that come in handy in frustrating situations. We pick the best methods available for you.

The 5 Best Open Source Password Managers

It won't be wrong to say that managing passwords on your own could be a tad tricky, especially if you're frequently registering on new websites. Although your web browser's built-in password manager could do the trick, your passwords could still come into jeopardy in case you log in to your main account from another computer and forget to log out.

Pop!_OS 20.04 Review: Professional Linux Distribution Ever Made

Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

Manjaro ‘Gellivara’ XFCE Edition (17.0.5) in 10 Screenshots

Want to take a quick virtual tour Manjaro XFCE edition instead of downloading GBs worth of ISO image and then making a Live USB of it? We will make it easy for you. Here are a series of screenshots of the important aspects of Manjaro Linux in XFCE edition. This is a light-weight edition, and aims to be fast and low on system resources. You will be amazed on how it is still visually appealing and user friendly.