Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I make a list with most used commands in terminal?

I know that this question may be unuseful for any future proposals for some of us, but even like this, the list can be useful when we don't remember a command used once or some times in the past, when we can search at the end of this list.

share|improve this question
    
Which terminal? – Elijah Lynn Jan 6 at 13:19
up vote 16 down vote accepted

We will use the records from .bash_history file to do this. The next command will give you a list of all commands in order that you used them most often:

history | awk 'BEGIN {FS="[ \t]+|\\|"} {print $3}' | sort | uniq -c | sort -nr

If you want only top 10, you must to add head at the command above:

history | awk 'BEGIN {FS="[ \t]+|\\|"} {print $3}' | sort | uniq -c | sort -nr | head

To get a specific top, for example top 5, use head with -n 5 option:

Top 5 commands

If you want the list in reverse order (top with the rarely used commands), don't use r oprion for second sort:

history | awk 'BEGIN {FS="[ \t]+|\\|"} {print $3}' | sort | uniq -c | sort -n

And finally to get a list with the commands used once for example, use grep ' 1 ' (change 1 with the desired number):

history | awk 'BEGIN {FS="[ \t]+|\\|"} {print $3}' | sort | uniq -c | grep ' 1 '

To deal with sudo commands (like sudo vim foo), instead of just {print $3} in the awk command, use:

{if($3 ~ /sudo/) print $4; else print $3}

So the entire command would look like:

history | awk 'BEGIN {FS="[ \t]+|\\|"} {if($3 ~ /sudo/) print $4; else print $3}' | sort | uniq -c | sort -nr

For example:

$ history | awk 'BEGIN {FS="[ \t]+|\\|"} {print $3}' | sort | uniq -c | sort -nr | head
    284 vim
    260 git
    187 find
    174 man
    168 echo
    149 rm
    134 awk
    115 pac
    110 sudo
    102 l

$ history | awk 'BEGIN {FS="[ \t]+|\\|"} {if($3 ~ /sudo/) print $4; else print $3}' | sort | uniq -c | sort -nr | head
    298 vim
    260 git
    189 find
    174 man
    168 echo
    153 rm
    134 awk
    115 pac
    102 l
     95 cd

You can see the jump in counts for vim, rm, etc.

share|improve this answer
1  
May want to credit your source in your answer: stackoverflow.com/a/68390/1210983 – ohaal Jun 25 '13 at 8:28
3  
@ohaal Believe or not, I didn't know about above source; sincerely this was a laboratory problem of a friend what studying at Computer Science of Iasi. – Radu Rădeanu Jun 25 '13 at 8:33
    
@muru The edit you made for sudo does not work for something like sudo -i because it gives me -i as the command – geek1011 Apr 23 at 17:31
1  
@geek1011 sudo -i starts a new shell. That history is no longer in your shell's history, but in root's. Obviously these commands don't work for a lot of edge cases; there's no way to deal with all possible commands without writing your own parser for shell language. – muru Apr 23 at 17:32
1  
@geek1011 sure, add another check, something like: if ($3 ~ /sudo/ && $4 !~ /^-/) – muru Apr 23 at 17:49

Below command will also list the top 10 most frequently used terminal commands,

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

Command to list all the commands which are most oftenly used in terminal,

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

Fish & Bash - The question doesn't specify a specific shell, just 'terminal'. Change head to whatever number you want.

Fish

history | awk '{print $1}' | sort | uniq --count | sort --numeric-sort --reverse | head -10

517 git
314 drush
197 sudo
171 cd
115 man
103 echo
 95 vi
 81 dig
 67 set
 66 ls

This Bash one seems to work and is a bit shorter and easier to understand than the other answers. I like the long options to help readability in these types of examples.

Bash

history | awk '{print $2}' | sort | uniq --count | sort --numeric-sort --reverse | head -10

73 drush
72 exit
65 sudo
46 g
40 echo
35 ll
34 tmux
30 history
30 fish
27 mux
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.