Advance Bash Scriptiong Tricks

Posted: 21st December 2010 by Babar Shafiq in Networking, Programming
Tags: , , , , ,

Here are some of my Linux bash shell findings, I hope it is good for everyone

Find some text in current folder, sub-folders and files:-

find . | xargs grep 'string_to_find'

Getting current time from some time server:-

cat < /dev/tcp/time.nist.gov/13

Convert Unix Timestamp (aka Epoch):-

date -d @1292946804
Tue Dec 21 20:53:24 2010

Downloading a URL:-

exec 5<>/dev/tcp/www.net.cn/80
echo -e "GET / HTTP/1.0\n" >&5
cat <&5

Sending Data over network:-

cat /etc/passwd > /dev/tcp/example.com/10000

TCP Port Checker:-

(echo >/dev/tcp/127.0.0.1/23) 2>/dev/null \
&& echo open || echo close

And

cat < /dev/tcp/localhost/25

Smallest Port Scanner:-

#!/bin/sh
# Usage:>$PortScanner.sh hostname_or_ip startport endport
for ((i=$2; $i <=$3; i++)); do
echo >/dev/tcp/$1/$i && echo $i/tcp Port Open;
done 2>/dev/null

Making File Backup when working on it

cp portscanner.sh{,.bak}

“!$” Reusing Last command arguments

mkdir /path/to/exampledir
cd !$

Taking Folder Backup with rsync (local)

rsync -Aax myfolder/ myfolder-dirbkp_`date +”%Y%m%d”`/

Deleting all files/Folder except some (in this example data and config folder will not delete)

ls | grep -v ‘(data)|(config)’ | xargs rm -r

Will add more tricks laters…..