The top command in Linux is a very advanced command. The top command when used shows all the current processes of the system. This command normally shows a summary of the users’ system. And the top command also shows a list of all the running processes throughout the system. In this article, I will explain how to use the top command and I will also show you some useful examples.
A. Description
The top command in Linux is a short form of the word Table of Process. So in the long word, it is clear enough that this command works with the existent system processes.
B. Syntax
The syntax for the top command is very simple. ThatΒ is the top command followed by its various options and flags, which is given below.
top -hv|-bcEeHiOSs1 -d secs -n max -u|U user -p pids -o field -w [cols]
C. Options
Many useful options are available for the top command. I have listed some of the most used ones here. However, if you want to know more about options for the top command you can always check the man page for the top command.
man top
Useful Options
- -h: Shows help file for this command.
- -v: Shows the version and quits the program.
- -O: Shows the name for the output fields.
- -o: This gives names to the files according to which files will be sorted.
- -H: Shows individual threads.
- -n: It limits the iteration number for the top command.
Practical Examples of the βtopβ Command in Linux
The top command is very easy to use. It is one of the most important commands of the Linux CLI when it comes to system diagnosis. Here I have given some examples of the top command.
Example 1: Showing Table of Process Using “top” Command in Linux
If I want to view the system summary of all the running processes on the system I will have to use the top command. The way to do that is very simple. Follow the steps below to see how this works,
Steps to Follow >
β At first open the Ubuntu Terminal.
β Type the following command in the command prompt:
top
β Now, press the ENTER button.
Output >
The upper portion of the picture shows the total system summary and the lower portion shows the list of all running processes in the system. To halt this operation you have to press the q button.
Similar Readings
- The βtimeβ Command in Linux [4 Practical Examples]
- The βttyβ Command in Linux [4 Practical Examples]
- The βuptimeβ Command in Linux [5 Practical Examples]
- The βvmstatβ Command in Linux [6 Practical Examples]
- The βunameβ Command in Linux [11 Practical Examples]
- The βapt-getβ Command in Linux [10 Practical Examples]
Example 2: Showing Table of Process and Exiting after n Iteration
If you want to exit the top operation you have to use the option(-n). You will have to use the command then the option and the number of iterations. Now follow the steps below.
Steps to Follow >
β At first open the Ubuntu Terminal.
β Type the following command in the command prompt:
top -n 5
β Now, press the ENTER button.
Output >
As you can see that the top operation halted after just 5 iterations.
Example 3: Showing Individual Threads Using “top” Command in Linux
If you want to run the top command but only want to show a single thread at a time, you will have to use the option (-h). Now follow the steps below to see how this works.
Steps to Follow >
β At first open the Ubuntu Terminal.
β Type the following command in the command prompt:
top -h
β Now, press the ENTER button.
Output >
As you can see that the top operation only printed a single thread.
Similar Readings
- The βfreeβ Command in Linux [8 Practical Examples]
- The βgetentβ Command in Linux [11 Practical Examples]
- The βgroupaddβ Command in Linux [7 Practical Examples]
- The βaddgroupβ Command in Linux [7 Practical Examples]
- The βgroupsβ Command in Linux [6 Practical Examples]
- The βfirewall-cmdβ Command in Linux [7 Practical Examples]
Example 4: Sorting Linux Processes by PID Using the βtopβ Command
If you want to sort the process list by Process ID you will use the same top command. Follow the steps below to know how this works.
Steps to Follow >
β At first open the Ubuntu Terminal.
β Type the following command in the command prompt:
top -h
β Now, press the ENTER button.
β Now, press the M and T buttons.
Output >
As you can see that the top operation printed the process list and it is sorted by the PID.
Example 5: Sorting Linux Processes by CPU Usage
If you want to sort the process list by CPU Usage you will use the same top command. Follow the steps below to know how this works.
Steps to Follow >
β At first open the Ubuntu Terminal.
β Type the following command in the command prompt:
top -h
β Now, press the ENTER button.
β Now, press the M and P button.
Output >
As you can see that the top operation printed the process list and it is sorted by CPU Usage.
Similar Readings
- The βgroupmodβ Command in Linux [5+ Practical Examples]
- The βidβ Command in Linux [7+ Practical Examples]
- The βserviceβ Command in Linux [6 Practical Examples]
- The βsestatusβ Command in Linux [4 Practical Examples]
- The βshutdownβ Command in Linux [7 Practical Examples]
- The βfingerβ Command in Linux [6 Practical Examples]
Example 6: Showing Linux Processes for a Specific User Using the βtopβ Command
If you want to show the Linux processes of a certain user you have to use the option(-u). You will have to use the command then the option and the user name. Now follow the steps below.
Steps to Follow >
β At first open the Ubuntu Terminal.
β Type the following command in the command prompt:
top -u akash
β Now, press the ENTER button.
Output >
As you can see that the top operation printed the process list for the user akash.
Example 7: Using the βtopβ Command in Linux with Redirection
In this example, I will redirect the output of the top command to a different file. Now follow the steps below to see how this works.
Steps to Follow >
β At first open the Ubuntu Terminal.
β Type the following command in the command prompt:
top -h > File1.txt
β Now, press the ENTER button.
β Type the following command in the command prompt:
cat File1.txt
β Now, press the ENTER button.
Output >
As you can see that the top operation printed a single thread but instead of printing it on the terminal, I directed that output to File1.txt.
Similar Readings
- The βdmesgβ Command in Linux [7+ Practical Examples]
- The βinstallβ Command in Linux [6+ Practical Examples]
- The βrebootβ Command in Linux [3 Practical Examples]
- The βsyncβ Command in Linux [8 Practical Examples]
- The βchageβ Command in Linux [7 Practical Examples]
- The βenv β Command in Linux [9 Practical Examples]
Example 8: Showing Help Page for βtopβ Command in Linux
If you want to see the help page you will use the same top command in Linux. Follow the steps below to know how this works.
Steps to Follow >
β At first open the Ubuntu Terminal.
β Type the following command in the command prompt:
top
β Now, press the ENTER button.
β Now, press the h button.
Output >
As you can see that the top operation printed the help page.
Conclusion
In this article, I have tried to show you how the top command works. And I have also included some useful examples. If you go through this article properly, you should be able to use the command top properly on your own.
Similar Readings
- The “passwd” Command in Linux [7 Practical Examples]
- The βdfβ Command in Linux [11 Practical Examples]
- The βsudoβ Command in Linux [8 Practical Examples]
- The βaptβ Command in Linux [13+ Practical Examples]
- The “htop” Command in Linux [7 Practical Examples]
- The βenableβ Command in Linux [6 Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners