Last Updated: September 27, 2021
·
9.9K
· Chip Castle

Top 10 shell commands you currently use

First off, I need to credit Ben Orenstein for this idea. Since I've found it to be very useful, I thought others could benefit from it, so here goes...

From time to time, I like to analyze which unix or linux shell commands I'm using most frequently. To do this, all I need is a little awk, like the following:

history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head

So what does this do?

Basically, it parses your history looking at the 2nd column, which is the command you typed and increments it each time it is found.

Then it displays a sorted report showing the count of the command and the command, like this example on my iMac:

1705 git
1420 ack
1016 vi
501 ls
490 commit
310 cd
211 cat
202 g
191 rm
181 c

Based on this output of the top 10 most frequently used shell commands, it shows that I'm using git, ack, and vi a TON, so it would be helpful to create aliases for these commands. I generally prefer 1-character aliases if I can get away with it. Here are a few examples:

alias a="ack"
alias g="git"
alias v="vi"
alias l="ls -al"
alias c="git commit -m"

I have found that these aliases alone have sped up my workflow significantly, as well as reduce the wear and tear on my fingers.

This has become so useful that I created an alias for it as well, which I'll cover in a later tip.

Please try it out and let me know your thoughts.

For more UNIX tips, please check out
Learning the UNIX Command Line. I'll also be providing a FREE PDF of software development tips for anyone who subscribes to the list. I hope you'll find them to be helpful.

Enjoy,
Chip

11 Responses
Add your response

Nice optimalization tip! The next step for me is to find a something which can do this for the commands and key phrases I use in Vim.

over 1 year ago ·

@orangetux, Thanks! You might want to check out Drew Neil's project: https://github.com/nelstrom/vimprint

over 1 year ago ·

My OSX required an:
export LC_ALL=C

over 1 year ago ·

My OSX required an:
export LC_ALL=C

over 1 year ago ·

nice post!

over 1 year ago ·

@larrywilliamson - thanks for the suggestion, since others might run into the same issue. Here's more info in case anyone is interested:
http://stackoverflow.com/questions/28881/why-doesnt-sort-sort-the-same-on-every-machine

over 1 year ago ·

cool idea!

over 1 year ago ·

Nice idea, but could be easier to read:

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

over 1 year ago ·

@tdl - Agreed. I didn't know about the -c option for the uniq command, so thanks for sharing that!

over 1 year ago ·

Here are my 2 cents. I am using this to find out which are my most typed git commands. I am using g as an alias for hub and git.

history | awk '{print $2 " " $3}' | sort | uniq -c | sort -rn | grep " g " | head
over 1 year ago ·

Just came across this Ruby gem that will do the analysis for you: https://github.com/paulmars/huffshell

over 1 year ago ·