Most used commands from history (without perl)

mosth() { 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; }
I copied this (let's be honest) somewhere on internet and I just made it as a function ready to be used as alias. It shows the 10 most used commands from history. This seems to be just another "most used commands from history", but hey.. this is a function!!! :D
Sample Output
$ mosth
     1  86  8.6%  rpm
     2  64  6.4%  yum
     3  47  4.7%  echo
     4  36  3.6%  ip
     5  36  3.6%  cat
     6  35  3.5%  ls
     7  32  3.2%  cd
     8  27  2.7%  systemctl
     9  27  2.7%  journalctl
    10  25  2.5%  nano

1
By: nnsense
2015-05-11 17:41:55

What Others Think

> I copied this off the internet OK, so it's fair game for me to tear it to pieces :-) . First, my version of awk (GNU Awk 4.1.1) gives me 6 significant digits rather than the 1 decimal place you have. So, I'll reformat the print: { printf ("%4d %5.1f%% %s\n",CMD[a],CMD[a]/count*100,a); } That's prettier and I don't need the column command any more, . Now we have grep -v "./" Did you copy *that* from the internet? If so bad internet, naughty internet. Was the intention to filter out all commands with a path? grep -v / or just commands that were run from the current directory grep -v "\./" . Let's assume the second (that's what I commonly do) Whatever the case, it would be better to filter out the rubbish before doing the count. But, let awk do it: ... | awk '($2 !~ "^\\./"){ count++ }' . Then comes sort, nl, head. No! Why number hundreds of lines, then only keep 10? It'd be more efficient to: ... | sort -nr | head | nl head gives us 10 lines by default, so no need for any arguments. . So, the new command is: mosth() { history | awk '($2 !~ "^\\./") { CMD[$2]++; count++; } END { for (a in CMD) { printf ("%4d %5.1f%% %s\n",CMD[a],CMD[a]/count*100,a); } }' | sort -nr | head | nl; } Only a few characters shorter, but much more efficient.
flatcap · 175 weeks and 4 days ago
I always learn something, that's why we love linux after all ;)
nnsense · 175 weeks and 2 days ago
This does not work for me.
amoore2600 · 174 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: