Saturday 21 May 2011

One liners

I love one liners, here is a list of a few useful one line scripts


Get all the lines which are not common to the two files.

diff file1 file2 | sed '/^[0-9][0-9]*/d; s/^. //; /^---$/d' > file3

Rename multiple files:
(this removes the extension of all .conf files in current dir)
mmv '*.conf' '#1'
Search and Replace in Multiple Files in perl(easy as PIE)
perl -p -i -e 's/<oldword>/<newword>/g' *
Remove comments and blank lines from file
egrep -v '^$|^\s*#' configfile
Find out which process is using up your memory using ps, awk, sort
ps aux | awk '{if ($5 != 0 ) print $2,$5,$6,$11}' | sort -k2n
ps ­eo user,pid,pcpu,cmd,args ­­sort ­pcpu | head ­n20
Find out Top 10 Largest File or Directory Using du, sort and head
du -sk /var/log/* | sort -r -n | head -10
Find out Top 10 Most Used Commands.
cat ~/.bash_history | tr "\|\;" "\n" | sed -e "s/^ //g" | cut -d " " -f 1 | sort | uniq -c | sort -n | tail -n 15
Search mail logs for log in attempts
for i in `ls /var/spool/mail`; do echo $i; cat /var/log/maillog* | grep -i LOGIN| grep $i | wc -l; done
for i in `ls /var/spool/mail`; do echo $i; cat /var/log/maillog* | grep -i LOGIN| grep "authid="$i | wc -­l; done
Check Top Connections
PORT=110;netstat ­antp | awk '$4 ~ /:'$PORT'$/ {c++;print $5|"cut ­f1 ­d:|sort |uniq ­c| sort ­n"} END {print c}

Possibly Related Posts

No comments:

Post a Comment