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/

Convert Unix Timestamp (aka Epoch):-

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

Downloading a URL:-

exec 5<>/dev/tcp/
echo -e "GET / HTTP/1.0\n" >&5
cat <&5

Sending Data over network:-

cat /etc/passwd > /dev/tcp/

TCP Port Checker:-

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


cat < /dev/tcp/localhost/25

Smallest Port Scanner:-

# Usage:>$ 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


“!$” 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…..