Grep Command Examples.

The grep is a Linux utility command which will help you in searching the contents within the file. It is the most useful when you are debugging through a large message file. It can be helpful in the scripts too.

Below are the few examples and the tricks for grep command.

Grep Command to highlight Color

Below command will search for “DB Error” in the /var/log/messages and highlight it with red color so you can quickly identify.

[root@server ~]# grep --color "DB Error" /var/log/messages

Print 3 lines before and after the pattern match in grep

If you want to print the lines before/after the pattern matches then you can use -A (after) or -B (before) option. Below command will print 3 lines before and after match.

[root@server ~]# grep "DB Error" /var/log/messages -A3 -B3

History Command Examples In Linux

To check history of the commands which has been executed.

[root@server ~]# history
    1  whoami
    2  clear
    3  ls
    4  whoaami
    5  ls -ltr
    6  top -c

To check last 5 commands in history use history 5

[root@server ~]# history 5
   92  clear
   93  whoami
   94  top -c
   95  history
   96  history 5

To clear history use history -c

[root@server ~]# history -c
[root@server ~]#
[root@server ~]# history
    1  history

To delete a particular command in history use history -d

[root@server ~]# history 5
1 history
2 whoami
3 clear
4 hisry 5
5 history 5

here I'm deleting the command which I fired at 4th Number
[root@server ~]# history -d 4
[root@server ~]# history
1 history
2 whoami
3 clear
4 history 5
5 history -d 4
6 history

To run a command at particular number in history you can use ! followed by the number. Example you want to run the command at number 2 the run !2

[root@server ~]# !2
whoami
root

TEE Command in Linux

TEE command is useful when you want to redirect the standard output to a file. Tee command should be already installed on your Linux Machine.

How to use Tee Command

[root@server ~]# cat file-1.txt | tee file-2.txt
a
b
c

This will overwrite all the contents of the file-2.txt with file-1.txt.
if want to keep the contents of the file-2.txt intact so you need -a option to append. As shown below.

[root@server ~]# cat file-1.txt | tee -a file-2.txt
one
word

Redirecting out of a command to a file. Most of us have habit of using >> to redirect output of a command to a file. But that’s work all the time where there is a error in the script.

So, to redirect output of a command to a file you need to use below command.
Example, my script has error but when I use >> to redirect output it won’t work.

[root@server scripts]# ./servicestatus.sh >> error.log
./servicestatus.sh: line 1: !/bin/bash: No such file or directory
[root@server scripts]#

[root@server scripts]# ls -al error.log
-rw-r--r-- 1 root root 0 May 29 06:05 error.log
[root@server scripts]#

My error log file is blank, so here I will use TEE command which works perfect. It will re-direct stdout and stderr into a file.

[root@server scripts]# ./servicestatus.sh 2>&1 | tee error.log
./servicestatus.sh: line 1: !/bin/bash: No such file or directory
[root@server scripts]#

[root@server scripts]# ls -al error.log
-rw-r--r-- 1 root root 66 May 29 06:08 error.log
[root@server scripts]#

[root@server scripts]# cat error.log
./servicestatus.sh: line 1: !/bin/bash: No such file or directory
[root@server scripts]#

Dig Command Examples

Simple Dig Command

[root@server ~]# dig justgeek.net

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-9.P2.el7 <<>> justgeek.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 53092
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;justgeek.net.                  IN      A

;; ANSWER SECTION:
justgeek.net.           267     IN      A       104.28.12.34
justgeek.net.           267     IN      A       104.28.13.34
justgeek.net.           267     IN      A       172.67.130.28

;; Query time: 10 msec
;; SERVER: 108.61.10.10#53(108.61.10.10)
;; WHEN: Wed May 27 07:13:17 UTC 2020
;; MSG SIZE  rcvd: 89

How to check A record of the website using DIG command

[root@server ~]# dig redhat.com +noall +answer

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-9.P2.el7 <<>> redhat.com +noall +answer
;; global options: +cmd
redhat.com. 3547 IN A 209.132.183.105

Check MX record using dig command

[root@server ~]# dig redhat.com MX +noall +answer

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-9.P2.el7 <<>> redhat.com MX +noall +answer
;; global options: +cmd
redhat.com. 600 IN MX 10 us-smtp-inbound-2.mimecast.com.
redhat.com. 600 IN MX 10 us-smtp-inbound-1.mimecast.com.
[root@server ~]#

How to check NS record of the website using DIG command.

[root@server ~]# dig justgeek.net NS +noall +answer

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-9.P2.el7 <<>> justgeek.net NS +noall +answer
;; global options: +cmd
justgeek.net. 86400 IN NS kia.ns.cloudflare.com.
justgeek.net. 86400 IN NS jerome.ns.cloudflare.com.
[root@server ~]#