#!/bin/bash
#remove the output files
rm -f /tmp/{comm_list.txt,comm_final.txt,pop_result.txt}
#list all users
USERS=$(ls /home/ | egrep -v "xx|yy|zz")
#output the command history to a text file
for i in $(echo $USERS)
do
cat /home/$i/.bash_history >> /tmp/comm_list.txt
done
cat /root/.bash_history >> /tmp/comm_list.txt
#remove the timestamp comment
grep -v "#" /tmp/comm_list.txt > /tmp/comm_final.txt
#get the top 10 popular commands
cat /tmp/comm_final.txt | cut -d ' ' -f1 | sort | uniq -c | sort -n -r | head -n 20 | cat -n > /tmp/pop_result.txt
輸出結果
1 18478 vi
2 17220 cd
3 9586 grep
4 8428 ls
5 6994 p4
6 3752 ll
7 3432 make
8 3320 fg
9 1577 rm
10 1390 pwd
11 1211 vim
12 732 ssh
13 715 cat
14 711 python
15 702 find
16 629 exit
17 620 ps
18 598 tail
19 579 cp
20 566 g++