14 Grep Command Examples in Linux

Linux like operating system provides a searching tool known as grep (global regular expression print). Grep command in Linux is used to search lines from one or more files based on the pattern. A pattern may be a single character, bunch of characters, single word or a sentence.

When we execute the grep command with specified pattern, if its is matched, then it will display the line of file containing the pattern without modifying the contents of the existing file.

In this tutorial we will discuss 14 useful grep command examples, let dive into the examples.

Example 1) Search the pattern or word in a file

Search the “linuxtechi” word in the file /etc/passwd file, run the following grep command

[email protected]:~# grep linuxtechi /etc/passwd
linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
[email protected]:~#

Example 2) Search the pattern in the multiple files

To search ‘linuxtechi’ word in multiple files like /etc/passwd, /etc/shadow and /etc/gshadow, run

[email protected]:~# grep linuxtechi /etc/passwd /etc/shadow /etc/gshadow
/etc/passwd:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
/etc/shadow:linuxtechi:$6$DdgXjxlM$4flz4JRvefvKp0DG6re:16550:0:99999:7:::/etc/gshadow:adm:*::syslog,linuxtechi
/etc/gshadow:cdrom:*::linuxtechi
/etc/gshadow:sudo:*::linuxtechi
/etc/gshadow:dip:*::linuxtechi
/etc/gshadow:plugdev:*::linuxtechi
/etc/gshadow:lpadmin:!::linuxtechi
/etc/gshadow:linuxtechi:!::
/etc/gshadow:sambashare:!::linuxtechi
[email protected]:~#

Example 3) List the name of those files which contain word ( grep -l)

Let’s assume we want to list the files which contain word ‘linuxtechi’ from multiple files, to do so use ‘-l’ option in grep command followed by word (pattern) and files.

[email protected]:~# grep -l linuxtechi /etc/passwd /etc/shadow /etc/fstab /etc/mtab
/etc/passwd
/etc/shadow
[email protected]:~#

Example 4) Search the pattern in the file along with associated line number (grep -n)

Let’s suppose we want list line and its number which matches the pattern or word. Use ‘-n’ option in grep command, In our example pattern is ‘linuxtechi’

[email protected]:~# grep -n linuxtechi /etc/passwd
39:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
[email protected]:~#

Below is the another example which displays the line and its number after matching a word ‘root’ in /etc/passwd and /etc/shadow file

[email protected]:~# grep -n root /etc/passwd /etc/shadow

grep-n-root

Example 5) Print the lines excluding the pattern (grep -v)

List all the lines of the file /etc/passwd that does not contain specific word “linuxtechi”.

[email protected]:~# grep -v linuxtechi /etc/passwd

grep-v-option

Example 6) Search all the lines that starts with specific pattern (grep ^)

Bash shell treats caret symbol (^) as a special character which marks the beginning of line or a word. Let’s display the lines which starts with “root” word in the file /etc/passwd, run beneath grep command

[email protected]:~# grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash
[email protected]:~#

Example 7) Search all the lines that ends with specific word (grep <word>$)

Bash shell treats dolar sysmbol ‘$’ as a special character which marks the ends of line or word. List all the lines of /etc/passwd that ends with “bash” word.

[email protected]:~# grep bash$ /etc/passwd
root:x:0:0:root:/root:/bin/bash
linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
[email protected]:~#

Example 8) Search word or pattern recursively (grep -r)

To search a word recursively in all the files of a folder then use ‘-r’ option in grep command, example is shown below:

[email protected]:~# grep -r linuxtechi /etc/
/etc/subuid:linuxtechi:100000:65536
/etc/group:adm:x:4:syslog,linuxtechi
/etc/group:cdrom:x:24:linuxtechi
/etc/group:sudo:x:27:linuxtechi
/etc/group:dip:x:30:linuxtechi
/etc/group:plugdev:x:46:linuxtechi
/etc/group:lpadmin:x:115:linuxtechi
/etc/group:linuxtechi:x:1000:
/etc/group:sambashare:x:131:linuxtechi
/etc/passwd-:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
/etc/passwd:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
............................................................................

Above command will search ‘linuxtechi’ word in the “/etc” directory recursively.

Example 9) Search all the empty or blank lines of a file (grep ^$)

To search and list all the empty or blank lines from a file use the special character combination ‘^$’ in grep command, example is shown below:

[email protected]:~# grep ^$ /etc/shadow
[email protected]:~#

As there is no empty line in /etc/shadow file , so nothing is displayed.

Example 10) Ignore letter case while searching (grep -i)

-i option in the grep command ignores the letter case i.e it will not discriminate upper case or lower case letters while searching

Let’s take an example , i want to search “LinuxTechi” word in the passwd file.

[email protected]:~$ grep -i LinuxTechi /etc/passwd
linuxtechi:x:1001:1001::/home/linuxtechi:/bin/bash
[email protected]:~$

Note : Grep command allow the search based on exact word using ‘-w’ option, example is shown below,

[email protected]:~$ grep -w cook /mnt/my_dish.txt

Above command will search and look for the lines which have “cook” word. It wont give results which has cooking.

Example 11) Search multiple patterns or words (grep -e)

For example i want to search ‘linuxtechi’ and ‘root’ word in a single grep command , then use -e option in grep command followed by search pattern

[email protected]:~# grep -e "linuxtechi" -e "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
[email protected]:~#
or 
[email protected]:~# grep -E "linuxtechi|root" /etc/passwd

Example 12) Getting Search pattern from a file (grep -f)

Use ‘-f’ option in grep command to get search patter from a file. Example is demonstrated below:

First create a search pattern file “grep_pattern” in your current working directory. In my case i have put the below contents.

[email protected]:~# cat grep_pattern
^linuxtechi
root
false$
[email protected]:~#

Now try to search using grep_pattern file.

[email protected]:~# grep -f grep_pattern /etc/passwd

grep-f-option

Example 13) Count the number of lines which match the search pattern (grep -c)

If you wish to count number of lines which match the search pattern then use ‘-c’ option in grep command.

Let’s consider the above example and count the lines which match search pattern,

[email protected]:~# grep -c -f grep_pattern /etc/passwd
22
[email protected]:~#

Example 14) Display N number of lines before & after pattern matching (grep -B -A)

a) Display four lines before pattern matching, use -B option in grep command, example is shown below:

[email protected]:~# grep -B 4 "games" /etc/passwd

grep-B-option

b) Display four lines after pattern matching, use -A option in grep command

[email protected]:~# grep -A 4 "games" /etc/passwd

grep-A-option

c) Display Four lines around the pattern matching using -C option

[email protected]:~# grep -C 4 "games" /etc/passwd

grep-C-option

That’s all from article, i hope these examples will help you to use grep command more efficiently. Please do share your feedback and comments in the comments section below.

Also Read10 ‘rm’ Command Examples for Linux Beginners

3 Responses

  1. Malcolm Hutchison says:

    “-i option … i.e it will ignore upper case or lower case letters while searching”

    the “i.e ” description is not correct – it doesn’t ignore uppercase or lowercase letters (which would mean it ignores all letters) but it does not discriminate between upper and lower case lettesr, so uppercase A will match either uppercase or lowercase A.

  2. swathi says:

    Awesome article on grep command ..

  3. SRINIVAS DARIPELLI says:

    ## cat myfile| grep –w “cook”

    with the above command,we will exactly find out the searches which have “cook” word.It wont give results which has cooking.

    we can add the above command also ? Nice Article

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest