Skip to main content

Bash basics for personal use

Replace using sed when too many slashes are there.
sed -i -e 's@/home/username/jas/@/home/username/jas/folder/@g'
sed -i -e ''

Note : In bash scripting on remote server ssh login, we could experience problems with awk print $ and "`". In such situations, use cut and there will be no such issues.

To add double quotes in a line which has words separated by comma.
sed -e 's/"//g' -e 's/[^,]*/"&"/g' -i filename

To add double quotes at end of line in a file.
sed -e 's/$/"/' -i filename

To add double quotes at beginning of a line in a file.
sed -e 's/^/"/' -i filename

To check a log file between two dates.
awk '$0 >= "Sep 26 05:01:11" && $0 <= "Sep 26 08:37:05"' logfile

If it didn't work, use the below format.
awk '$0>=from&&$0<=to' from="2007/03/20 15:13" to="2007/08/19 14:31" logfile

To pass two objects using multiple for loop from single file.
for i in `cat names | awk '{print $1}'`; do for j in `cat names | grep $i | awk '{print $2}'`; do echo $i; echo $j; echo " "; done; done
for i in `cat /home/username/etc/instance.txt | awk '{print $1}'`; do for j in `cat /home/username/etc/instance.txt | grep $i | awk '{print $2}'`; do echo $i; echo $j; echo " "; done; done

Simple while loop in bash
while true ;  do ; clear ;  ps -ef|grep sed ;  sleep 20 ; done

To remove unneeded stuff while filtering in log.
for i in `cat S1 | sed 's/'Local7.Info'/''/g; s/'%ASA-6-'/''/g' | tr  "\t" " " | strings | grep -i --color deny | grep "" | grep  "" | grep -i --color "" | cut -d "/" -f 3 | cut -d " " -f 1 | sort -n | uniq` ; do

cat $1 | sed 's/'Local7.Info'/''/g; s/'%ASA-6-'/''/g' | tr  "\t" " " | strings | grep -i --color deny | grep "" | grep  "" | grep -i --color "" | grep Si >> $1deny.txt ;

To zip file without additional disk space using gzip. 
gzip -9c console.log > console.log_$(date "+%d-%m-%y_%T").gz && > console.log &


Popular posts from this blog

Qmail cheat sheet

1) To check the mail queue in plesk from command line, you can use the command :


2) You can examine the queue with qmail-qread.


3) From the qread command you get the message id . In the above example , let us assume one of the id is 524514 . Now you can find the file holding the email in/var/qmail/queue with find command.

# find /var/qmail/queue -iname 524514


/var/qmail/queue/mess/22/524514 (mail headers)


4) From the mail header you get the IP address

vi /var/qmail/queue/mess/22/524514


Shortcut for the cool guys

find /var/qmail/queue -iname queu_id | grep mess | xargs less


4) If you wish …

Logical volume vmxxxx_img is used by another device - Error on LVM removal

Hi Folks,

I've faced error while trying to remove an LVM from the server.

The exact LVM error will be "Logical volume vmxxxx_img is used by another device" on executing the lvremove command.

Feel free to remove the following steps to remove the LVM from the server.

Note: Please remember to replace <id> with your VMID in the below section.

dmsetup ls dmsetup info -c xen-vm<id>_img dmsetup remove xen-vm<id>_img lvremove -f /dev/xen/vm<id>_img ----------------------------------------------------------

Cheat sheet for Hardware RAID health check - Megaraid, Adaptec, 3wareraid and HPraid.

Hi Folks,

In another post "raid type" I've mentioned how to find if the server is running with Software or Hardware RAID.

Here, we can go ahead and check the health of the Hardware RAID array in the server.

The first thing we need to detect is the controller in which the raid is configured and you can easily get it from the another post that I mentioned earlier. However, I'm providing it here again for your convenience.

1. Login to the server as the root user.
2. Execute the following command.

/sbin/lspci -vv | grep -i raid

3. This will show the raid controller running on the server. The different types of controller I've worked with includes

a. Megaraid which uses megacli
b. Adaptec which uses arcconf.
c. 3ware raid which uses tw_cli
d. HPraid which uses hpacucli.

How to check the Megaraid array health status?

The Megaraid is usually installed in the /opt/MegaRAID and you can execute the following command to verify the health of the array.