I don’t remember where I encountered shell history (bash, zsh etc.), commands but I’d like to share them with you as well:

$ history | awk '{print $2}' | sort | uniq -c | sort -nr | head -10

What these commands do, let’s look at them each:

  • The awk '{print $2}' prints first fields from the history
  • The famous | transfers or make one command’s output to the input of the next.
  • The sort orders all lines alphabetically
  • The uniq -c removes the duplicate lines (typed commands) and count them
  • The sort -nr displays the commands in reverse order by the count number
  • And the head -10 displays only first 10 commands.

And the result will be like:

   1157 vi
   1032 cd
   863 nvim
   539 la
   384 git
   345 rm
   293 sudo
   264 make
   241 tmux
   240 python

So it means that I am using vi command most: 1157 times.

There are some other alternatives for shell shitory command:

#1

History with percentage detail:

$ 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

The output will be like:

     1	1157  11.5296%     vi
     2	1032  10.284%      cd
     3	863   8.5999%      nvim
     4	539   5.3712%      la
     5	384   3.82661%     git
     6	345   3.43797%     rm
     7	293   2.91978%     sudo
     8	264   2.63079%     make
     9	241   2.40159%     tmux
    10	240   2.39163%     python

#2

History with bar graph:

$ history | tr -s ' ' | cut -d ' ' -f3 | sort | uniq -c | sort -n | tail | perl -lane 'print $F[1], "\t", $F[0], " ", "▄" x ($F[0] / 12)'

The output:

tmux	239 ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
python	240 ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
make	263 ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
sudo	293 ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
rm	345 ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
git	383 ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
la	539 ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
nvim	859 ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
cd	1028 ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
vi	1157 ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄

All done!