14 Grep Command Examples in Linux

Are you looking for hands on guide on Linux grep command ? In this guide, we will cover 14 grep command examples in linux.

Grep is a command line tool in Linux/Unix systems that is used to search text or string from a file. Grep stands for global regular expression print. When we run a grep command with specified string, if its is matched, then it will display the line of the file containing that string without modifying the contents of the existing file.

Syntax of Grep Command

$ grep  <Options> <Search String>  <File-Name>

Options:

Grep-Command-Options

Without any further delay, let’s deep dive into grep command examples.

1) Searching a word or string in a file

When we run grep command followed by search string or pattern then it will print the matching line of a file. Example is shown below.

Search a word “nobody” word in the file /etc/passwd file,

$ grep nobody /etc/passwd
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
$

2) Searching pattern in the multiple files

A word or a pattern can be searched in multiple files using grep command. Run following to search ‘linuxtechi’ word in /etc/passwd, /etc/shadow and /etc/gshadow files.

$ sudo grep linuxtechi /etc/passwd /etc/shadow /etc/gshadow

Output,

Search-Word-Multiple-files-Grep-command

3) Print file names that matches the pattern

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

$ grep -l 'root' /etc/fstab /etc/passwd /etc/mtab
/etc/passwd
$

4) Display the line number with output lines

Use ‘-n’ option in grep command to display line and its number which matches the pattern or word. In below example, pattern is ‘nobody’

$ grep -n 'nobody' /etc/passwd
18:nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
$

5) Invert the pattern match

Using the option ‘-v’ in grep command, we can display the lines which don’t match the pattern

$ grep -v 'nobody' /etc/passwd

Invert-Match-Pattern-Grep-Command

6) Print all lines that starts with specific pattern

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 “backup” word in the file /etc/passwd, run

$ grep ^backup /etc/passwd
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
$

7) Print all the lines that ends with specific word

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

$ grep bash$ /etc/passwd
root:x:0:0:root:/root:/bin/bash
linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
$

8) Searching pattern recursively

‘-r’ option in grep command is used to search pattern recursively in folder and sub-folders.  Let’s suppose, we want to search a pattern ‘nologin’ in /etc folder recursively.

$ sudo grep -r nobody /etc
/etc/shadow:nobody:*:19101:0:99999:7:::
/etc/shadow-:nobody:*:19101:0:99999:7:::
/etc/passwd:nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
/etc/ssh/sshd_config:#AuthorizedKeysCommandUser nobody
/etc/passwd-:nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
$

Above command will search ‘nobody’ pattern in the “/etc” directory recursively.

9) Print all the empty lines of a file

Grep command can also print all the empty or blank lines from a file use the special character combination ‘^$’ , example is shown below:

$ grep '^$' /etc/sysctl.conf

output,

Print-Empty-Lines-Grep-Command

To print the line numbers of empty lines, run

$ grep -n '^$' /etc/sysctl.conf

10) Ignore letter case while searching

‘-i’ option in the grep command ignore case distinctions in patterns and data. When we use ‘-i’ then it will not discriminate upper case or lower case letters while searching.

Let’s assume we want search ‘IP_Forward’ string in sysctl.conf file, run

$ grep IP_Forward /etc/sysctl.conf
$
$ grep -i IP_Forward /etc/sysctl.conf
#net.ipv4.ip_forward=1
$

Grep command can also used to match only whole words using ‘-w’ option, example is shown below,

$ sudo sysctl -a | grep -w 'vm.swappiness'
vm.swappiness = 60
$

Above command will search and look for the lines which have exactly “vm.swappiness” word.

11) Matching multiple patterns

With the help of ‘-e’ option in grep command, we can search multiple patterns in a single command. Example is listed belpw:

$ grep -e nobody -e mail /etc/passwd
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
$
or 
$ grep -E "nobody|mail" /etc/passwd
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
$

12) Takes pattern from a file

‘-f’ option in grep command enables to take patterns from file. Example is demonstrated below:

First create a search pattern file with name “grep_pattern” in your current working directory. In my case put following content in it.

$ cat grep_pattern
^linuxtechi
root
false$
$

Now try to search using grep_pattern file.

$ grep -f grep_pattern /etc/passwd

Takes-Pattern-from-file

13) Count the number of lines that matches the pattern

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

Let’s consider we want to count the numbers of lines which ends with false word in /etc/password file, run

$ grep -c false$ /etc/passwd
6
$

14) Print N lines before & after pattern matching

Grep command can also print n number of lines before and after matching the pattern using -B and -A options respectively.

a) Print four lines before pattern matching

$ grep -B 4 "games" /etc/passwd

Print-Lines-Before-Matching-Pattern-Grep-Command

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

$ grep -A 4 "games" /etc/passwd

Print-Lines-After-Matching-Pattern-Grep-Command

c) Print Four lines around the matching pattern, use -C option

$ grep -C 4 "games" /etc/passwd

Print-Lines-Around-Matching-Pattern-Grep-Command

That’s all from this guide, I hope these examples will help you to understand how to use grep command in linux more efficiently. Please do post your queries and feedback in below comments section.

Also Read: 10 rm Command Examples for Linux Beginners

4 thoughts on “14 Grep Command Examples in Linux”

  1. Malcolm Hutchison

    “-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. SRINIVAS DARIPELLI

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

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