Ranking of the most frequently used commands

2011-01-31 334 words 2 mins read

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:

  <td>
    <div class="text codecolorer">
      history | awk '{print $2}' | awk 'BEGIN {FS="|"}{print $1}' | sort | uniq -c | sort -n | tail | sort -nr
    </div>
  </td>
</tr>
1

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:

  <td>
    <div class="text codecolorer">
      history|awk '{print $2}'
    </div>
  </td>
</tr>
1

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

  <td>
    <div class="text codecolorer">
      history|awk '{print $2}'|awk -F"|" '{print $1}'
    </div>
  </td>
</tr>
1

or

  <td>
    <div class="text codecolorer">
      history | awk '{print $2}' | awk 'BEGIN {FS="|"}{print $1}'
    </div>
  </td>
</tr>
1

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

  <td>
    <div class="text codecolorer">
      history | awk '{print $2}' | awk 'BEGIN {FS="|"}{print $1}'|sort|uniq -c
    </div>
  </td>
</tr>
1

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

author

Authored By Amit Agarwal

Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it