$ 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
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.
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.
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
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:
{ 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 directorygrep -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.