Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to know which command I use the most on the command line. I would like to know so I can improve my use of the command line. If I know which command I use the most, I can then read more about them try and figure out better ways to use them.

I know history keeps a list of all the previous commands I typed. How would I process it to see a list of the top 10 or 20 most used commands.

share|improve this question
    
Yes I did just post this question to answer it. It is just so interesting that I though the superuser community would really find it interesting. –  nelaar Feb 25 '11 at 12:12
1  
Nothing at all wrong with posting questions to answer yourself. It makes a handy bookmark, and if you're on the wrong track you'll soon hear about it! –  Ken Jul 8 '11 at 14:31

5 Answers 5

up vote 15 down vote accepted

I just saw this post on http://linux.byexamples.com/

Basically you use a simple one line awk script

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

A full explanation can be found at the link above.

Example of out put on my machine is:

 1  211  21.1%  ls
 2  189  18.9%  sudo
 3  58   5.8%   man
 4  52   5.2%   cd
 5  43   4.3%   ping
 6  40   4%     apropos
 7  34   3.4%   less
 8  22   2.2%   cat
 9  18   1.8%   which
10  18   1.8%   aspell
share|improve this answer
awk '{print $1}' ~/.bash_history | sort | uniq -c | sort -n

The awk command will print the first string from ~/.bash_history (not showing command options or arguments), then sort will order all lines alphabetically, then "uniq -c" will remove duplicated lines (your typed commands) and count them, and the last sort will order your commands by the count number returned by uniq.

share|improve this answer

For a more general answer, enable "process accounting" on your system. You can get not just frequency of use, but aggregate CPU, memory, and I/O stats.

share|improve this answer
    
That is very use full. –  nelaar Feb 25 '11 at 14:00

You can use the hash command in your terminal, which keeps a hash entry of every command that you use along with the number of hits and based on the hits you can sort them out and process.

Check this article for more information.

share|improve this answer

You can fashion the above awk answer from @nelaar into a nice bash script:

#!/bin/bash
HISTFILE=~/.bash_history
set -o history
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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.