Thursday 28 February 2013

Linux For DBA

Useful Linux command




1) Find and Replace (Replace askapache with htaccess)
nice -n19 sh -c 'S=askapache && R=htaccess; find . -type f|xargs -P5 -iFF grep -l -m1 "$S" FF|xargs -P5 -iFF sed -i -e s%${S}%${R}% FF'


2) Disk Use in current Location
du -a --max-depth=1 | sort -n | cut -d/ -f2 | sed '$d' | while read i; do if [ -f $i ]; then du -h "$i"; else echo "$(du -h --max-depth=0 "$i")/"; fi; done


3) Do a search-and-replace in a file after making a backup
for file in <filename>; do cp $file{,.bak} && sed 's/old/new/g' $file.bak > $file; done


4) Delete all but the latest 5 files
ls -t | awk 'NR>5 {system("rm \"" $0 "\"")}'

5) Identify long lines in a file
awk 'length>72' file

6) Show directories in the PATH, one per line
echo src::${PATH} | awk 'BEGIN{pwd=ENVIRON["PWD"];RS=":";FS="\n"}!$1{$1=pwd}$1!~/^\//{$1=pwd"/"$1}{print $1}'

7) Internet access at Current Moment
lsof -P -i -n
netstat -lantp | grep -i stab | awk -F/ '{print $2}' | sort | uniq


watch --interval 0 'iptables -nvL | grep -v "0 0"'

8) Client Ip Enquiry
netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n

9) Remove Garbage
echo "http%3A%2F%2Fwww.google.com" | sed -e's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g' | xargs echo -e

10) Send Mail with attachment
tar cvzf - data1 data2 | uuencode data.tar.gz | mail -s 'data'

11) Email HTML content
mailx bar@foo.com -s "HTML Hello" -a "Content-Type: text/html" < body.htm

12) Send email with one or more binary attachments
echo "Body goes here" | mutt -s "A subject" -a /path/to/file.tar.gz recipient@example.com


du -h -s -x * | sort -g -b -r | less -F

13) Archive every file in /var/logs
find /var/logs -name * | xargs tar -jcpf logs_`date +%Y-%m-%e`.tar.bz2

14) Search for all files that begin with . and delete them.
find ~/Desktop/ \( -regex '.*/\..*' \) -print -exec rm -Rf {} \;

15) Copy all documents PDF in disk for your home directory
for i in `find / -name *.pdf`; do cp -v $i $HOME; done

16) simulates the DOS tree command that you might be missing on your Mac or Linux box
find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'

find . -name "*.sql" -print0 | wc -l --files0-from=-

17) Find file changed in last 7 days. Use +7 files modified before 7 days
find / -type f -mtime -7 | xargs tar -rf weekly_incremental.tar
gzip weekly_incremental.tar

18) Date function

date -d '1 day ago'; date -d '11 hour ago'; date -d '2 hour ago - 3 minute'; date -d '16 hour'

date +%s

date +%Y-%m-%d

No comments:

Post a Comment