Scheduling conditional statements with crontab


Recently while trying to auto-restart a daemon which is down by identifying with ps aux and if condition, it is not worked as intended with crontab.

I used following command which is perfectly running in from command line but not through crontab.

if [ `ps aux | grep nrpe | grep -v grep | wc -l` -eq 0 ]; then service nagios-nrpe-server restart ;fi

After trying with different commands following thing worked for me:

pgrep nrpe; [ $? != 0 ] && /etc/init.d/nagios-nrpe-server restart

where pgrep returns non zero exit code if the process nrep is not running and with $? is used to get the exit code of previous command (in this case pgrep) and start the process.

Lists/Remove all empty files in a directory


To list all empty files in a directory use follwoing command:

for file in `ls`; do if [[ ! -s $file ]]; then echo $file; fi; done

To remove all empty files in a directory use following commad:

for file in `ls`; do if [[ ! -s $file ]]; then echo $file; rm $file; fi; done

Username and password with wget/curl


To download a url with basic authentication we need to pass username & password.

To acheive this functionality with wget or curl observer following examples:

With curl:

$ curl -u 'username:password' ''

With wget:

$ wget --user='username' --password='password' ''

Wget with cookie:

Login to server & save cookie to a file:
$ wget --save-cookies cookiesFile.txt --post-data 'user=username&password=password'

Download a page by using saved cookie file:
$ wget --load-cookies cookiesFile.txt -p

awk print Nth line after matching a pattern


By using awk we can print only Nth line after matching a pattern.

Lets say I have a file test.txt with following content and I want to extract 5 line after matching pattern.

$ cat test.txt


Use following awk code to extract Nth line:


$ awk 'c&&!--c;/pattern/{c=N}' file

where “pattern” is your input pattern and N is a line number to extract after matching pattern.

$ awk 'c&&!--c;/1/{c=5}' test.txt



In above example I am trying to extract 5th line after matching pattern, here my input pattern is 1 and 5th line after matching pattern is 6. So our output is 6.

bash – get previous command status


To get previous command status in bash use “$?” variable.

echo $? will return zero if previous command is success & if its non zero it indicates failure.


$ ls ; echo $?

Output of the above command is list of files, directories and next line it will return number.

We can use condition for check if previous command is success or failure:

if [ $? -eq 0 ] 
    echo SUCCESS
    echo FAIL

awk – print last field


NF is built in variable in awk which stores Number of fields.

To get last field with awk use following example:

$ echo a b c | awk '{print $NF}'


By default awk will take any white space character(space, tab, …) to split.

To get last but one field with awk:

$ echo a b c | awk '{print $(NF-1)}'


To print only number of fields with awk:

$ echo a b c | awk '{print NF}'


Nmap – Scan for open ports


nmap is a network exploration tool and security/port scanner.

We can list all open ports using nmap for a given IP/Host.

Use following command to list all open ports:

$ sudo nmap -v -sS

Use following command to scan specific ports:

$ nmap -sS -O -p80,8080

To can specific range of ports(scan from port 80 to 100):

$ sudo nmap -sS -O -p80-100

mutt – send mails with attachments


By using mutt we can send mails with attachments from command line.

Install mutt:

$ apt-get install mutt

Send mail with attachment:

$ mutt -s "PFA" -a attachment.txt < body.txt


$ echo "body text here" | mutt -s "PFA" -a attachment.txt

Send mail without attachment:

$ mutt -s "PFA" < body.txt


$ echo "body text here"  | mutt -s "PFA"

awk/sed print from line number to end of file

I have file with 10 lines where content of first line in 1, second line 2, third line is 4 …… and tenth line is 10.

Here I will show some operations related to read read in lines based their line numbers.

Print from line number 5 to end of file:

$ awk 'NR>=5' inputFile
$ sed '1,4d' inputFile

Print from line number 4 to 9 or print lines between linen number 3 and 10:

$ awk 'NR>=4 && NR<=9'  inputFile
$ sed -n '4,9p' inputFile

Print all even lines numbers:

$ awk '{if(NR%2 == 0) {print $1}}' inputFile

Print all odd lines numbers:

$ awk '{if(NR%2 != 0) {print $1}}' inputFile