How to use Grep Command in Linux with Examples

Linux like operating system provides a searching tool known as grep (global regular expression print). grep command is used to search text 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 existing file.

In this tutorial we will learn how to use Linux grep command with 14 practical examples,

Example:1 Search the pattern (word) in a file

Search the “linuxtechi” word in the file /etc/passwd file

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

Example:2 Search the pattern in the multiple files.

[email protected]:~# grep linuxtechi /etc/passwd /etc/shadow /etc/gshadow
[email protected]:~#

Example:3 List the name of those files which contain a specific pattern using -l option.

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

Example:4 Search the pattern in the file along with associated line number(s) using the -n option

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

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


Example:5 Print the line excluding the pattern using -v option

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

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


Example:6 Display all the lines that starts with specific pattern using ^ symbol

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.

[email protected]:~# grep ^root /etc/passwd
[email protected]:~#

Example: 7 Display all the lines that ends with specific pattern using $ symbol

List all the lines of /etc/passwd that ends with “bash” word.

[email protected]:~# grep bash$ /etc/passwd
[email protected]:~#

Bash shell treats dollar ($) symbol as a special character which marks the end of line or word.

Example:8 Search the pattern recursively using -r option

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

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

Example:9 Search all the empty or blank lines of a file using grep

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

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

Example:10 Search the pattern using ‘grep -i’ option

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

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

[email protected]:~$ grep -i LinuxTechi /etc/passwd
[email protected]:~$

Note : In grep command,we can also do 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 using -e option

For example i want to search ‘linuxtechi’ and ‘root’ word in a single grep command , then using -e option we can search multiple patterns .

[email protected]:~# grep -e "linuxtechi" -e "root" /etc/passwd
[email protected]:~#
[email protected]:~# grep -E "linuxtechi|root" /etc/passwd

Example:12 Getting Search pattern from a file using “grep -f”

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
[email protected]:~#

Now try to search using grep_pattern file.

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


Example:13 Count the number of matching patterns using -c option

Let take the above example , we can count the number of matching patterns using -c option in grep command.

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

Example:14 Display N number of lines before & after pattern matching

a) Display Four lines before pattern matching using -B option

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


b) Display Four lines after pattern matching using -A option

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


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

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


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 ..


    ## 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