Pawan Bahuguna

Let's Learn and Share
Menu
  • Home
  • Free Books
  • RHEL 7 Books
  • Practice Papers
  • Contact
  • Privacy

Preparing for RHCSA or RHCE Certification? >>> Check RHEL 7 : RHCSA & RHCE Practice Papers

YES, Show Me Sample Papers.
Home
Linux
Must Know Usage of “grep” Command with Examples
Linux

Must Know Usage of “grep” Command with Examples

Pawan Bahuguna

There are lots of commands in Unix/Linux which makes system administrator life easy. Whether you have to search for some particular string in a very large file or want to limit the output of the terminal, there is always a command for your help and today I’m going to tell you about “grep” command with the practical example which is used to look for particular words in a file.

 

Must Know Usage of grep Command with Examples

 

First of all, let us see why grep command is used. Below is the snippet from the man page of ‘grep’.

 

“grep” searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.

 

Now, let us see some of the very common and important uses of the grep commands with the practical examples.

Note: For this tutorial, I have copied some part of ‘/etc/passwd’ file to test.txt file for showing the practical use of the grep command. Below is the content of test.txt file.

Advertisements
[[email protected] ~]# cat test.txt
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:988:983::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
pawan:x:1000:1000:pawan:/home/pawan:/bin/bash
[[email protected] ~]#

 

“grep” Command usage with Practical Examples

 

1. Find string or word in the file.

Simply, you can use grep command directly without any option to search for the string as given in the example below. In this example, I tried to search my name ‘pawan‘ in two ways. Remember this is case sensitive search.

Example:

[[email protected] ~]# cat test.txt | grep pawan
pawan:x:1000:1000:pawan:/home/pawan:/bin/bash
OR
[[email protected] ~]# grep pawan test.txt
pawan:x:1000:1000:pawan:/home/pawan:/bin/bash

 

2. Case Insensitive Search with grep.

The 1 example showed us how to search for a string, but it was case sensitive and if you will try to search ‘Pawan’ in above example you won’t get any output. That is why most common usage of grep command is with ‘-i’ switch or operator. It will make your search case insensitive.

Example:

[[email protected] ~]# grep Pawan test.txt
[[email protected] ~]#
[[email protected] ~]# grep -i Pawan test.txt
pawan:x:1000:1000:pawan:/home/pawan:/bin/bash
[[email protected] ~]#

 

3. Excluding particular word with grep.

‘grep’ command is also useful if you don’t want to see a particular word in your search. You can use option ‘-v’ with this command and you will see all the words except that particular word. This helps sometimes in minimizing the content from big files, if there are repetitive words and redirecting the output to a new file.

Example:

[[email protected] ~]# grep -v pawan test.txt
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:988:983::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
[[email protected] ~]#

 

Recommended Article: RHEL 7 Books for RHCE & RHCSA

 

4. Search for word along with the line number

You can also search for a particular word with the line number. In below example, I tried to search word ‘tcpdump‘ along with the line in which it is present.

Example:

[[email protected] ~]# grep -n tcpdump test.txt
7:tcpdump:x:72:72::/:/sbin/nologin

[[email protected] ~]# grep -in Tcpdump test.txt
7:tcpdump:x:72:72::/:/sbin/nologin

 

5. Searching Two Words or more at a time.

‘grep’ can also be used to search more than 1 word or string in a file. For this, we use ‘-E’ option with grep command. It is equivalent to ‘egrep’ command. Below is one such example where we have first tried to look for 2 words and then 3 words.

Example:

[[email protected] ~]# grep -E "pawan|gdm" test.txt
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
pawan:x:1000:1000:pawan:/home/pawan:/bin/bash
[[email protected] ~]#
[[email protected] ~]# grep -E "pawan|gdm|tcp" test.txt
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
pawan:x:1000:1000:pawan:/home/pawan:/bin/bash
[[email protected] ~]#
OR
[[email protected] ~]# egrep "pawan|gdm|tcp" test.txt
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
pawan:x:1000:1000:pawan:/home/pawan:/bin/bash
[[email protected] ~]#

 

6. Searching a string or word Recursively in all directories.

If you want to look for a particular string in the current directory and all the subdirectory, you can use grep with ‘-r’ option. This will recursively search for given path and in its all sub-directories.

In below example, I have tried to look for ‘klog’ in /var/ directory. So, it has searched all the files and folders for the line containing that word. In below example, backlog_limit contains the word klog.

Example:

[[email protected] ~]# grep -r klog /var/
/var/log/messages-20180221:Oct 14 10:54:35 localhost augenrules: backlog 1
/var/log/messages-20180221:Oct 25 09:07:17 localhost augenrules: backlog_limit 320
/var/log/messages-20180221:Oct 25 09:07:17 localhost augenrules: backlog 1
/var/log/messages-20180221:Feb 19 00:14:15 localhost augenrules: backlog_limit 320
/var/log/messages-20180221:Feb 19 00:14:15 localhost augenrules: backlog 1
/var/log/messages-20180221:Feb 21 07:42:39 localhost augenrules: backlog_limit 320
/var/log/messages-20180221:Feb 21 07:42:39 localhost augenrules: backlog 1
[[email protected] ~]#

 

7. Display lines above the search string.

If you want to display the lines above you search strong along with the search term, you can use grep with the option ‘-B’ like below. In below example, the number 2 is the number of lines which you want to display along with search term.

Example:

[[email protected] ~]# grep -i -B 2 pawan test.txt
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
pawan:x:1000:1000:pawan:/home/pawan:/bin/bash
[[email protected] ~]#

 

8. Display lines below the search string.

Above, we saw how to display lines above the search term and now if you want to display the lines which are below the search term, you can use grep command with ‘-A’ switch like below.

Example:

[[email protected] ~]# grep -i -A 2 gdm test.txt
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:988:983::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[[email protected] ~]#

 

9. Count the total number of lines.

If you want to know how many lines the particular string is coming, you can use ‘-c’ option. It will give you the total count.

Example:

 [[email protected] ~]# grep -c wheel /etc/sudoers
3
[[email protected] ~]#
[[email protected] ~]# grep -c pawan test.txt
1
[[email protected] ~]#

 

These were the most important and commonly used grep command. If you have any other in your mind which you use, please let us know through the comment with the example (if possible).

Hope you like the article and If you are naive in Linux/Unix, I would request you to practice ‘grep’ command in your lab machine. If you like the article do not forget to share it on your social media profiles and do subscribe to my blog for more such article.

Show your love by Sharing:

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • More
  • Click to share on Telegram (Opens in new window)
  • Click to print (Opens in new window)
  • Click to email a link to a friend (Opens in new window)
  • Click to share on Skype (Opens in new window)
  • Click to share on Pocket (Opens in new window)
Share
Pinterest
Linkedin
Tweet
Google+
Stumble
Email
Prev Article
Next Article

Related Articles

I RAN rm -rf on Root Filesystem
We keep on listening that there are dangerous commands in …

I RAN “rm -rf” on Root Filesystem (rm -rf /)

Run Same command on Multiple Unix Linux Terminals
Many times it happens when we have to run one …

Executing the Same Command on Multiple Unix-Linux Terminals

Tags:Fun With Linux How To Linux Linux Basics Redhat Redhat Certification RHCE RHCSA RHEL Tutorial

One Response

  1. Labor

    Thank you for your advise anirudh.

    March 16, 2018

Leave a Reply

Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 

Find a domain starting at $0.48

powered by Namecheap

Like Our Book

Like Our Book

Lets Connect

  • Popular
  • Recent

Free Newsletter

Subscribe NOW!

We don’t spam! Read our privacy policy for more info.

Check your inbox or spam folder to confirm your subscription.

Pawan Bahuguna

Let's Learn and Share
Copyright © 2023 Pawan Bahuguna
Home | Privacy Policy | Contact