Ubuntu – How to make a list with most used commands in terminal

bashcommand linehistory

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.

Best Answer

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.