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

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.


Solution 1:

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.

Solution 2:

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

Solution 3:

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