Home Linux Commands Mastering Linux Command-Line Productivity: Find Top Most Used Commands

Mastering Linux Command-Line Productivity: Find Top Most Used Commands

Empowering Your Linux Journey: Discover Your Top Used Commands for Enhanced Productivity

By sk
4.6K views

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

Introduction

The Linux terminal is a powerful tool that allows users to interact with their systems and perform a wide range of tasks. As we use the Linux terminal, we learn many commands that help us do things. It can be helpful to know the which commands we use the most. This can help us learn new commands and fix problems.

There are a few ways to find the most used Linux commands. One way is to use the history command. This command shows us a list of all the commands we have used in the current session.

We can also combine the history command with other commands, such as sort and uniq, to count how many times each command was used.

Furthermore, we can use third-party tools like muc or a custom script to track your command usage in Linux.

Regardless of the chosen method, deciphering the top Linux commands empowers users to enhance their command-line proficiency and streamline their interactions with the Linux environment.

At the end, we have also included a simple Python script to determine the most or least frequently used Linux commands.

Related Read: 15 Essential Linux Commands Every Beginner Should Know

Find Top Most Used Commands in Linux

As you may know, the history file (~/.bash_history) maintains a record of all the commands you execute in the Terminal. You can easily determine which commands you use most frequently by referring to 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:

228 sudo
51 nala
38 ls
25 ip
19 cd
Find 5 Top Most Used Commands in Linux
Find 5 Top Most Used Commands in Linux

As you can see in the output, "sudo" is the top most used command and I have executed it 228 times. And "cd" is the least used command.

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

  1. history: This command displays a list of previously executed commands in your terminal session. It typically shows a sequential list of command numbers followed by the commands themselves.
  2. | (Pipe): The pipe symbol | is used to take the output of one command and use it as the input to the next command. In this case, it takes the output of the history command and passes it as input to the next command in the pipeline.
  3. awk '{print $2}': This awk command is used to extract the second column (i.e., the command itself) from each line of input. The output will consist of just the commands you've executed, one command per line.
  4. | (Pipe): Similar to before, this pipe symbol passes the output of the awk command to the next command in the pipeline.
  5. sort: This command sorts the list of commands alphabetically. It arranges the commands in ascending order.
  6. | (Pipe): Another pipe to pass the sorted list of commands to the next command.
  7. uniq -c: The uniq command is used to remove duplicate lines from the sorted list of commands. The -c option makes uniq display the count of occurrences for each unique line. So, it will count how many times each unique command appears.
  8. | (Pipe): Another pipe to pass the output of uniq -c to the next command.
  9. sort -nr: This command sorts the list of unique commands and their counts in reverse numerical order (-nr). This means that the commands you've used the most will appear at the top.
  10. | (Pipe): Another pipe to pass the sorted list to the final command.
  11. head -5: Finally, the head command is used to display the first 5 lines of the sorted list. These will be the top 5 most frequently executed commands from your command history.

So, when you run this entire command, you'll get a list of the top 5 most frequently executed commands from your command history, along with the number of times each command has been executed.

Heads Up: You can use ExplainShell to find what each part of a command does.

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

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

Here is the another version of same command which shows little bit extra details:

$ history | 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 -n5

Sample Output:

1	228  45.2381%   sudo
2	51   10.119%    nala
3	38   7.53968%   ls
4	25   4.96032%   ip
5	19   3.76984%   cd

This command is a complex one-liner for analyzing your command history and producing a summary of the most frequently executed commands, along with the percentage of each command's occurrence.

Display Most Used Commands in Fish

The aforementioned commands are Bash-specific. If you're on Fish shell, run:

$ history | cut -d ' ' -f 1 | sort | uniq -c | sort -nr | head -5

Visualize Most Used Commands using muc

muc (stands for Most Used Commands) is a command line tool designed to visualize the commands you use most frequently in your Linux or Unix terminal. It works by analyzing your command history, which is stored in the $HISTFILE environment variable.

It offers features like customizing the appearance of the output and setting the number of commands to display.

Install muc in Linux

muc can be installed through different methods, such as using an AUR (Arch User Repository) helper for Arch Linux, or directly from the GitHub repository for other distributions.

For Arch Linux Users (via AUR):

You can use an AUR helper like paru or yay to install the muc-git package. For example, to install muc using paru, you can run:

$ paru -S muc-git

Alternatively, you can manually build it using git and makepkg -si.

Install muc using cargo:

You can install muc using Cargo, the Rust package manager. be sure you've installed Rust in your Linux system.

Once rust is installed, run the following command to install muc using cargo package manager

$ cargo install --git=https://github.com/nate-sys/muc

For Nix Users:

Use the outputs provided by the flake.nix inside the repository to install muc.

This can be done either with the overlays.default output for your system configuration or the package output to imperatively install it.

You can use commands like nix install github:nate-sys/muc or create an ad-hoc shell with nix shell github:nate-sys/muc.

To quickly run muc, use:

nix run github:nate-sys/muc

Display Frequently Used Linux Commands using muc

Once installed, you can run muc to visualize your most used commands.

Basic Usage:

Just type muc and press Enter.

$ muc

This will display your most used commands.

Be default, muc uses your HISTFILE to find the most frequently used commands. If it can't find this file, it will throw an error with message, "Error: "Could not find HISTFILE environment variable. Supply the file path with the --file option"".

In that case, you should pass the actual path of your HISTFILE location to muc like below.

$ muc --file ~/.bash_history

Sample Output:

[▮▮▮▮▮▮▮▮▮▮] 37.96%   238 sudo apt, nmcli, reboot ...
[▮▮▮       ] 12.60%    79 apt 
[▮▮        ] 10.85%    68 nala 
[▮         ]  6.22%    39 ls 
[▮         ]  5.42%    34 nmcli 
[▮         ]  4.94%    31 ip 
[▮         ]  3.99%    25 reboot 
[          ]  3.35%    21 nano 
[          ]  3.03%    19 docker 
[          ]  2.07%    13 rm 
...60 (9.57%) others
Total: 627 commands
Display Frequently Used Linux Commands using muc
Display Frequently Used Linux Commands using muc

As you see in the output above, muc displays your most used commands in a simple and clear format.

Heads Up: You can find your HISTFILE location using command:

$ echo $HISTFILE

Custom Number of Commands:

By default, muc will display the top 10 most used commands.

To display a custom number of top commands, use -c followed by the number:

$ muc -c 5  # Shows top 5 commands

Specify Shell:

If you're using a specific shell like oh-my-zsh or fish, you can specify it:

$ muc --shell ohmyzsh  # For oh-my-zsh
$ muc --shell fish     # For Fish shell

Customize Appearance:

You can use various options with muc to customize the output, such as changing the appearance of the bar or showing a different number of top commands.

To change the appearance of the bar in the output, use --bar parameter like below:

$ muc --bar "=,*,-,="  # Custom bar appearance

Using Regular Expressions:

For advanced users, you can parse the history file yourself using regular expressions:

$ muc --regexp "<your-regex>"

For more details, refer to the official muc GitHub repository given below.

Displaying Least Frequently Executed Commands

In the previous examples, we have sorted the result in descending order (reverse) i.e largest to smallest. To display the top most used commands in ascending order (i.e. 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

This will display the least frequently used commands at the top.

Say Hello to Command Frequency Analyzer (CFA) Script

We made a simple Python script named Command Frequency Analyzer, shortly CFA, to display the top most used or least used commands in Linux.

The Command Frequency Analyzer script is a simple Python script designed for Linux and Unix users to analyze and track their command line usage.

It reads the command history file of the user's shell (Bash, Zsh, or Fish) and calculates the frequency of each command used. Users can choose to display either the most frequently or least frequently used commands.

The script is user-friendly and adaptable to different shell environments, making it an invaluable tool for anyone looking to optimize their command line efficiency or simply curious about their command line habits.

Display Most Frequently or Least Frequently Used Commands using CFA

The CFA script is available in GitHub for FREE to use.

1. Ensure Python is Installed:

This script requires Python. You can check if Python is installed by typing python --version or python3 --version in your terminal.

2. Download CFA Script:

Git clone the script to your local system using command:

$ git clone https://gist.github.com/7f93a7acb8607929c28974c9c2db6e69.git cfa

This will clone the script into a local directory called cfa.

3. CD into the cfa directory:

$ cd cfa

4. Run the Script:

Execute the script by typing the following command:

$ python3 cfa.py

Simply follow the prompts.

The script will first ask if you want to see the most used or least used commands. Respond with most or least.

Next, it will prompt for the number of commands to display. Enter a number of your choice (e.g., 5, 10).

Depending upon the user's choice, CFA script will analyze the shell history file and display the most or least used commands.

Sample Output for most frequently used commands:

Do you want to display the 'most' used or 'least' used commands? Enter most/least: most
Enter the number of commands to display: 5

Top 5 most used commands:
sudo: 233
nala: 51
ls: 39
ip: 25
cd: 19

Sample Output for least frequently used commands:

Do you want to display the 'most' used or 'least' used commands? Enter most/least: least
Enter the number of commands to display: 5

Top 5 least used commands:
./muc1.sh: 1
./muc.sh: 1
touch: 1
rustc: 1
source: 1
Display Most Frequently or Least Frequently Used Commands using CFA in Linux
Display Most Frequently or Least Frequently Used Commands using CFA in Linux

The CFA script will analyze the following files depending on the shell you use.

  • ~/.bash_history for BASH
  • ~/.zsh_history for ZSH
  • ~/.local/share/fish/fish_history for FISH.

If you have configured different file for your shell history, simply change its path by editing the cfa.py file.

If you want to look into the code, visit the CFA GitHub Gist link given below.

Frequently Asked Questions (FAQ)

Q: What is the purpose of finding the most used commands on Linux?

A: Finding the top most used commands on Linux can be very helpful for understanding your command line habits and improving your efficiency.

Q: How can I see my most used commands in Linux?

A: You can use the history command combined with tools like awk, sort, and uniq to list and count your most used commands.

Q: Is there a tool specifically for visualizing most used commands?

A: Yes, muc (Most Used Commands) is a tool designed for this purpose.

Q: How do I install muc in Linux?

A: You need Rust and Cargo installed for installing muc on most distributions.

Q: Can muc be customized?

A: Yes, muc allows customization like changing the appearance of the output and specifying the number of top commands to display.

Q: Is it possible to exclude certain commands from the history analysis?

A: Yes, you can use regular expressions with muc or modify the command pipeline used with history to exclude specific commands.

Q: Is this feature available on all Linux distributions?

A: The basic method using history and shell commands is available on most Unix-like systems. muc is also available for various distributions.

Q: What is Command Frequency Analyzer (CFA)?

A: Command Frequency Analyzer (CFA) is a Python script that analyzes your shell command history to identify the most or least frequently used commands.

Q: Which shells does CFA support?

A: It supports Bash, Zsh, and Fish shells.

Q: How do I run the CFA script?

A: Download the script and run it using command: python3 cfa.py

Q: Do I need any special libraries?

A: No, it uses standard Python libraries.

Q: Can I specify the number of commands to display in CFA script?

A: Yes, the script prompts you to enter the number of commands you want to see.

Q: How does it determine the shell type?

A: It checks the SHELL environment variable to determine your current shell.

Q: Will it work on any Linux distribution?

A: As long as Python is installed and the history file is in the standard location, it should work on any Linux distribution.

Conclusion

Whether you're a beginner or an experienced user, knowing your most used commands is a valuable step towards mastering the Linux command line.

Discovering the top most used Linux commands provides useful insights into our command-line habits, enabling us to learn new commands, troubleshoot issues, and enhance our overall Linux experience.

By using tools like the history command or muc, you can easily see which commands you use the most.

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

Related Read: Apply Tags To Linux Commands To Easily Retrieve Them From History

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More