Find Top Most Used Commands On Linux or how to on AWK

The other day a fellow Linux user and I were discussing about Linux commands. He asked me which Linux command I use the most. I told him one of my most frequently used command is “sudo”. I use it everyday to install, update, remove packages and other administrative tasks on my Linux box. I guess “sudo” is the most frequently used command of many Linux users. If you ever wondered what’s your top most used commands on Linux, here is how to find them.

Find Top Most Used Commands On Linux

As you know, the history file (~/.bash_history) keeps a record of all the commands you typed in the Terminal. You can easily find which commands you use the most by using this file.

Let me show you the top 5 most used commands on my Linux box.

$ history | awk '{print $2}' | sort | uniq -c | sort -nr | head -5

Sample output:

153 sudo
118 pngquant
33 cd
30 ssh
29 exit

Let us break down the above command and see what each option does.

  • The “awk ‘{print $2}’” command prints first string from the history file without showing command options and arguments.
  • The “sort” command orders all lines alphabetically.
  • The “uniq -c” command removes the duplicate lines (typed commands) and count them.
  • And, the last “sort -nr” command displays the commands in reverse order by the count number returned by “uniq” command.

Bonus tip:

  • You can use ExplainShell to find what each part of a command does.

As you can see, “sudo” is the top most used command and I have used it 153 times. And “exit” is the least used command.

I have sorted the result in descending order (reverse) i.e largest to smallest. To display the top most used command in ascending order (smallest to largest), use this command instead:

$ history | awk {'print $2'} | sort | uniq -c | sort -n | tail -n5

Sample output:

29 exit
30 ssh
33 cd
118 pngquant
153 sudo
Find top most used commands on Linux

Find top most used commands on Linux

If you don’t want to limit the number of results, simply remove the last part of the above commands:

$ history | awk '{print $2}' | sort | uniq -c | sort -nr

Hope this helps.

Now, it’s your time. Go and find out your top most frequently used commands on your Linux box.

 

source : ostechnisx

Add a Comment