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.
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.
[root@localhost ~]# 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 [root@localhost ~]#
“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:
[root@localhost ~]# cat test.txt | grep pawan pawan:x:1000:1000:pawan:/home/pawan:/bin/bash OR [root@localhost ~]# 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:
[root@localhost ~]# grep Pawan test.txt [root@localhost ~]# [root@localhost ~]# grep -i Pawan test.txt pawan:x:1000:1000:pawan:/home/pawan:/bin/bash [root@localhost ~]#
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:
[root@localhost ~]# 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 [root@localhost ~]#
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:
[root@localhost ~]# grep -n tcpdump test.txt 7:tcpdump:x:72:72::/:/sbin/nologin [root@localhost ~]# 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:
[root@localhost ~]# grep -E "pawan|gdm" test.txt gdm:x:42:42::/var/lib/gdm:/sbin/nologin pawan:x:1000:1000:pawan:/home/pawan:/bin/bash [root@localhost ~]# [root@localhost ~]# 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 [root@localhost ~]# OR [root@localhost ~]# 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 [root@localhost ~]#
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:
[root@localhost ~]# 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 [root@localhost ~]#
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:
[root@localhost ~]# 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 [root@localhost ~]#
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:
[root@localhost ~]# 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 [root@localhost ~]#
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:
[root@localhost ~]# grep -c wheel /etc/sudoers 3 [root@localhost ~]# [root@localhost ~]# grep -c pawan test.txt 1 [root@localhost ~]#
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.
Labor
Thank you for your advise anirudh.