
有沒有辦法找出史上最常用的 10 個指令?這裡的top 10是指我使用最多的指令,也就是使用次數比其他指令多的指令。
答案1
一條線:
history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n10
輸出範例:
1 211 21.1% ls
2 189 18.9% sudo
3 58 5.8% man
4 52 5.2% cd
5 43 4.3% ping
6 40 4% apropos
7 34 3.4% less
8 22 2.2% cat
9 18 1.8% which
10 18 1.8% aspell
這網站提供了更多資訊。
答案2
我自己有一個小腳本來檢查我最近使用的前 N 個命令是什麼:
mylast () {
re='^[0-9]+$'
if ! [[ $1 =~ $re ]] ; then
echo "error: $1 not a number" >&2
else
history | awk '{a[$2]++} END {for (i in a) print a[i], i}' | sort -rn | head -n $1
fi
}
所以說它mylast 10
顯示了前 10 名。
這是透過遍歷歷史記錄並將第二個欄位儲存到 in 中來完成的awk
,以便可以透過陣列追蹤計數。
範例輸出:
$ mylast 5
248 git
107 python
93 grep
71 awk
52 less