Tuesday, 23 February 2016

Some Useful One Liner in Shell scripting

 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
11 ssh
12 shutdown
15 cp
15 vncserver
22 cat
23 find
23 pwd
24 mv
25 ovc
47 grep
58 ps
67 vi
74 ll
117 ls
118 cd
Find out Top 10 Largest File or Directory Using du, sort and head
# du -sk /var/log/* | sort -r -n | head -10
1796    /var/log/audit
1200    /var/log/sa
612     /var/log/anaconda.log
512     /var/log/wtmp
456     /var/log/messages.4
92      /var/log/messages.2
76      /var/log/scrollkeeper.log
72      /var/log/secure
56      /var/log/cups
48      /var/log/messages.1

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
PID VSZ RSS COMMAND
3823 3788 484 /sbin/mingetty
3827 3788 484 /sbin/mingetty
3830 3788 484 /sbin/mingetty
3833 3788 488 /sbin/mingetty
3834 3788 484 /sbin/mingetty
3873 3788 484 /sbin/mingetty
2173 3796 568 /usr/sbin/acpid
1835 3800 428 klogd
1832 5904 596 syslogd
2054 5932 540 /usr/sbin/sdpd
2281 6448 360 gpm

Find List of Unique Words in a file Using tr, sed, uniq

cat Readme1.txt
Installtion Steps:

Run the below steps as root user:

1.Copy the output file at any temporary location.
2.Unzip the file using unzip command.
3.Copy the tar to data directory
tr -c a-zA-Z '\n' < Readme1.txt  | sed '/^$/d' | sort | uniq -i -c
1 any
1 as
1 at
1 below
1 command
2 Copy
1 data
1 directory
2 file
1 Installtion
1 location
1 output
1 root
1 Run
2 steps
1 tar
1 temporary
4 the
1 to
2 unzip
1 user
1 using
$

Display Username and UID sorted by UID Using cut, sort and tr

cut -d ':' -f 1,3 /etc/passwd | sort -t ':' -k2n - | tr ':' '\t'
root    0
daemon    1
bin    2
sys    3
sync    4
games    5
man    6
lp    7
mail    8
news    9
uucp    10
proxy    13

No comments:

Post a Comment