How to see the Terminal commands you use most often in Debian 10

When working on a Terminal, you often need to reuse the commands that you have executed previously or you may even want to find the statistics of the most used commands . Most of the users know how to browse the bash history by using the arrows keys to scroll back to previous commands. But fewer users know that they can do a lot more with bash history than just using the up and down arrow keys.

In this article we will see two different methods by which you can see the commands that you have used most often in Terminal. We will use Debian 10 to describe the procedure mentioned in this article.

Method 1: Using the history command to view commands statistic

In this method, we will use the history command to view the most used Terminal commands. In Linux, there is a history file usually located in ~/.bash_history that stores a history of all commands that the user has executed in previous sessions. The history file is updated every time the session is closed.

It facilitates a user to not only get the commands that they have executed previously but also can search out the most used commands. With history command, you can even list out the most used commands.

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

$ history

The output will be similar to the following. You can see that it has listed all the previously executed commands of a current user session in an order with the most recent command at the bottom.

Bash history command

To see a list of last x number of commands that you have executed previously, type history followed by x:

$ history x

For instance, to list the last 6 number of commands, replace x with 6 in the above command.

History x

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

$ history | grep command

For instance to search for network command in the history list, execute the following command in Terminal:

$ history | grep network

grep command from history

1. View the list of most used commands

We have seen the usage of history command. Now we will use the history command to view the most used commands in Terminal. To do so, execute the following command in a Terminal:

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

View the list of most used commands

From the above output, you can see the history list in which there are most recent commands at the top and the least used at the bottom since the installation of your OS. According to the above result, history command was the top most used command and it was used 13 times, the second command was ip and third was ping. Similarly, the least used command was systemctl, it was used one time.

2. View the specific number of most used commands

We can also view only the specific number of top most used commands. To do so, 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 only the top 4 most used commands, replace x with the 4.

View the specific number of most used commands

3. View the list of most used commands in a reverse order

It is also possible to view the history list in a reverse order that is the recent one at the bottom and earlier ones at the top. To do so, use the same above command but without using r option for the second sort as shown in the below command.

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

Now you will see the history in a reverse order.

View the list of most used commands in a reverse order

4. View the list of most used commands by occurance

To view the history list of only the commands that occur for once, twice or any specific number of time, 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 twice, replace x with 2 in the above syntax:

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

View the list of most used commands by occurance

Here are some more options you can use with history command:

5. Delete a specific entry from the history

To delete a specific line from your shell history, execute the below command.

$ history -d <line number>

For instance, to delete line number 19 from the list, replace <line number> by 19.

Delete a specific entry from the history

The above command will delete the entry corresponding to line number 19.

6. Exit without saving history

If you want to exit the Terminal without saving any history, use the below command:

$ kill -9 $$

7. Clear entire history

You can even clear the entire history for the current user session. Use the below command to do so:

$ history -c

Method 2 # Create a function to view command statistic

There is another method you can use to view the list of most executed commands in the Terminal. To use it, first execute the following command in Terminal to create a function:

$ 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 execute the following command to call this function:

$ zsh-stats

You will see the output similar to this.

Create a function to view command statistic

In the above output, you can see the four columns displayed as output to the above command. The first column displays the index number, the second and third displays the frequency and percentage of occurrence of most used commands correspondingly, and the last one displays the command name.

So that is how we can see the most used Terminal commands in our Debian 10 OS. We discussed above two methods that is the history command and zsh-stats function, you can use any method as per your convenience. However, it is to be noted that both the above methods show the history of commands for only a current user session.