Ranking of the most frequently used commands

Lets take a quick look at how to get the most frequently used commands on you shell. So what we need to do is this:

1
history | awk '{print $2}' | awk 'BEGIN {FS="|"}{print $1}' | sort | uniq -c | sort -n | tail | sort -nr

So, how did we arrive at this and will this always work? No it might not always work. A typical example is where HISTTIMEFORMAT variable is set. In that case, if you check history, you will see that after the number column we have time and date in the specified format, in which case, you will get wrong information from the above command. Anyways, forgetting these special cases, lets go to how we got this command:

1
history|awk '{print $2}'

will give us list of all the commands that we have used and are in history. But this will also give commands like “history|more” as one command as this does not have any space. So, we eliminate whatever is there after the “|” with awk command

1
history|awk '{print $2}'|awk -F"|" '{print $1}'

or

1
history | awk '{print $2}' | awk 'BEGIN {FS="|"}{print $1}'

and now to get all the counts, we need to sort and then count unique occurances:

1
history | awk '{print $2}' | awk 'BEGIN {FS="|"}{print $1}'|sort|uniq -c

Time for pretty display and make the display more readable to users:

add sort -n to sort with first column treated as number and then tail to display only few lines and then sort -nr to display in reverse order so that the top entry is the most used one 🙂

Enhanced by Zemanta