o5qijw
Last Updated: August 06, 2016
·
8.921K
· chip
Chip avatar

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

Say Thanks
Respond

12 Responses
Add your response

7386
Ae32c028d069f1d646dc7e38e6e49eb9

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 ·
7402
Chip avatar

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

over 1 year ago ·
7428
Ceef79191efd23932c20696353d44bd2

My OSX required an:
export LC_ALL=C

over 1 year ago ·
7429
Ceef79191efd23932c20696353d44bd2

My OSX required an:
export LC_ALL=C

over 1 year ago ·
7430
8801730df62c4673fde3593b061e2f7a

nice post!

over 1 year ago ·
7432
Chip avatar

@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 ·
7494

cool idea!

over 1 year ago ·
7503
D2e4fccafdc13139706f8d0f4db6b13a

Nice idea, but could be easier to read:

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

over 1 year ago ·
7510
Ead6058e5fc570899ab5a9b8ecc9efb1

Great to see this has moved on xD

http://juanibiapina.com/posts/2

over 1 year ago ·
7512
Chip avatar

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

over 1 year ago ·
9172

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 ·
15576
99218a2893ac76af03ca5cecc6f5b54f

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

over 1 year ago ·
Filed Under