If you are a Linux user, whether Ubuntu, Centos, Fedora, or any distribution, you have most likely spent a lot of time on the bash that is the default command line on Linux. You must have used many commands in the Linux bash for different purposes and you often have to repeat those commands which you have executed in the past. Luckily, Linux bash shell history feature helps you to not only get the commands that you have previously run but also can get the most used commands. The records from the .bash_history file are used for this purpose and the history file is updated every time the terminal session is closed.

In this article, we will show you two methods which you can use to see the commands you mostly used in the Terminal. We will use Ubuntu 18.04 LTS for describing the procedure mentioned in this article.

Method 1: Use the history, grep and awk commands

In this method, we will see the most used terminal commands by making use of the History command. Before this, we will see what actually the History command does. It actually shows you all of the previous commands that have been recently used.

To see how it works, type history in the Terminal:

$ history

Linux history command

You can also see a list of last x number of commands that you have run last time, use the following syntax:

$ history x

For instance, to list the last 10 number of commands, replace x with 10.

$ history 10

history 10

To search for a specific command in the history list, use the following syntax:

$ history | grep command

For instance to search for ping command in the history list, run the following command in Terminal:

$ history | grep ping

History grep command

View the most used commands in Terminal

To view the most used commands, run the following command in a Terminal:

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

most used commands

Above result shows the history list in which there are most used commands at the top and the least used at the bottom since the installation of Ubuntu 8.04 LTS. According to the above result, sudo was the top most used command and it was used 104 times, the second command was History and third was Locate.

To view the specific number of top most used commands, run the following command in a Terminal.

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

For instance, to view top 10 most used commands, replace x with the 10.

Top 10 commands

If you want to view the history list in a reverse order that is the least used at the top and the most used commands at the bottom, you can easily do that. Remove the r option for the second sort like shown in the below command.

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

Top 30 commands

You can see in the above screenshot that It has listed the history in reverse order.

You can also view the history list of only those commands that occur for once, twice or for any specific number of times. For that, use the following syntax:

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

Replace x with any desired number. For instance, to view the list of commands that only occurred once use the below command:

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

Commands run only once

Method 2: Create a function to view command statistics

There is another method using which you can view the list of commands that were run previously in the bash shell. For that, first run the following command in Terminal:

$ function zsh-stats() { fc -l 1 | 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 -n25; }

Then run the following command:

$ zsh-stats

Command stats function

It will list the commands along with the number of times they occurred and their percentage.

That is how we can see the most used terminal commands in our Ubuntu 18.04 LTS OS. We discussed above two methods using history and zsh-stats commands, you can use any one of them as per your convenience.

How to see the terminal commands you use most on Linux