grep is one of the more important Unix/Linux command-line utilities. This tutorial will demonstrate different ways of utilizing the text-search utility.
grep stands for “Global search in Regular Expression and Print”.
grep displays the lines in a file that match with a STDIN (Input through keyboard) pattern. The pattern which is given as input through grep are regular expressions which can be used to search the file contents easily.
Syntax : grep [OPTIONS] PATTERN [FILE]
Command 1.1
$ grep 'root' /etc/passwd
The above command will search for contents having ‘root’ written inside a file ‘/etc/passwd’. If a file contains strings as ‘rootwr’, ‘ishroot’ it will match the root in ‘rootwr’ and ‘ishroot’ and displays the result. If you want to find exact word ‘root’ using grep follow below command.
Command 1.2
$ grep -w 'root' /etc/passwd
Use -w to match the exact word.
The above command will match -w exact word ‘root’. Now if a file contain strings like ‘rootwr’, ‘root’, ‘ishroot’, this command displays only for pattern ‘root’ not for ‘rootwr’ and ‘ishroot’ which were displayed in the first command 1.1.
Command 1.3
$ grep -i 'bug' /etc/passwd
Use -i to search case-insensitively.
If you are searching with this command ‘grep ‘bug’ /etc/passwd’,
It will match only for ‘bug’ string pattern which is not a case-insensitive pattern. If /etc/passwd contains ‘Bug’, ‘BuG’, ‘bGG’ this command ‘grep ‘bug’ /etc/passwd’ will display blank results.
So in order to search case-insensitively to match the pattern you need to use ‘-i’ so it will match for all the patterns for given string.
for example $ grep -i 'bug' /etc/passwd
will matches patterns for:
‘bug’
‘Bug’
‘bUg’
‘buG’
‘BUG’
‘BUg’
‘bUG’
All the patterns which evaluates from ‘bug’.
Command 1.4
$ grep -n 'root' /etc/passwd
Use -n to print line numbers of matches
now -n option displays line numbers with lines, which contains the input pattern. The above command displays the line numbers which contain the string ‘root’.
If you want to match more than one words so this can be done using following command:
Command 1.5
$ grep -E -n 'backup|root' /etc/passwd
Above command will search for both words pattern ‘backup’ and ‘root’ and will display the line numbers with lines containing the both words.
Where:
Use -E for Extended regular expressions
more help about this can be found using ‘man grep’ command.
Command 1.6
$ grep -v 'root' /etc/passwd
Use -v to print lines not containing pattern.
Above command displays lines which doesn’t contain the pattern ‘root’.
Command 1.7
$ grep -r 'root' /etc/
Use -r to recursively search a directory.
This command will search and displays for the pattern ‘root’ in all the files inside ‘/etc/’ directory.
Command 1.8
$ grep -c 'bash' /etc/passwd
Use '-c' to return a count of lines with the matching pattern.
This command displays the count of lines which contains the ‘bash’ pattern in a file /etc/passwd.
Command 1.9
$ grep -r -l 'root' /etc
Use '-l' to return the names of files that have at least one line containing the pattern.
Above command displays the names of files which contains the pattern ‘root’,
inside /etc directory. Important note here is you need to use ‘-r’ (command 1.7) option in order to traverse the /etc directory recursively.