- 20 top command examples in Linux [Cheat Sheet]
- Understanding the output of top command
- Different examples to use top command
- 1. top command to display Linux processes
- 2. top command to display output in batch mode
- 3. Set the delay time of screen updates in top command output
- 4. Set the number of iterations in top command output
- 5. Monitor specific process with top command
- 6. top command to display the process of specific user
- 7. Adjust the output width of top command
- 8. Display Mem and Swap in KB/MB/GB/TB/PB/EB with top command
- 9. Sort Processes by Memory Usage in top command output
- 10. Sort Processes by CPU Usage in top command output
- 11. Add or Remove Fields in top command output
- 12. Hide the value 0 in top command output
- 13. Toggle Alternate Display Mode in top command output
- 14. Disable bold text in top command output
- 15. Change delay time in top command output
- 16. Change memory unit type in task windows
- 17. Choose another window in top command output
- 18. Display only active tasks with top command
- 19. Change colour mapping in top command output
- 20. Scroll a window in top command output
- Conclusion
- What’s Next
- Further Reading
- top command in Linux with Examples
20 top command examples in Linux [Cheat Sheet]
top program provides a dynamic real-time view of processes running in the Linux system. It also displays the system summary information and processes or threads currently managed by the Linux kernel. The output of the top command is interactive and can be controlled by pressing keys to some extent.
Understanding the output of top command
The top command output contains the three main sections: Summary Area, Fields/Columns Headers, and Task Area.
These are the Fields/Columns Headers, which you will see on the top command output.
- PID: It is the task’s unique process ID.
- USER: It shows the owner of the tasks.
- PR: It is the priority of a task.
- NI: The nice value of a task. A negative value means higher priority, whereas a positive value means lower priority.
- VIRT: The total amount of virtual memory used by the task.
- RES: The non-swapped physical memory used by a task.
- SHR: The amount of shared memory used by a task.
- S: The process status of a task:
- D = uninterruptible sleep
- I = idle
- R = running
- S = sleeping
- T = stopped by job control signal
- t = stopped by debugger during trace
- Z = zombie
Different examples to use top command
Now, let’s have a look at some practical examples of top commands in the Linux system.
1. top command to display Linux processes
You can run top command without any options or arguments to display running processes in the system. You can press q or kill the program to exit the output mode.
Sample Output:
2. top command to display output in batch mode
-b option displays the output in batch mode or non-interactive mode. It will not accept any inputs and runs until you kill the program.
Sample Output:
3. Set the delay time of screen updates in top command output
-d option allows you to specify the delay time between screen updates. It takes an argument of time in seconds as a number.
For example, if you put the number 10 after -d option, the output will be updated every 10 seconds. Try it on your terminal to get the desired output.
4. Set the number of iterations in top command output
With -n option, you can put the limit of iterations top should produce in the output.
For example, if to put the iterations limit of 5, you can use the following command.
5. Monitor specific process with top command
Instead of viewing all the processes, you can monitor the specific process using the process ID. -p option lets you specify the process ID and view the particular process in the output.
Sample Output:
6. top command to display the process of specific user
Normally, top command displays the running processes of all users in the output. To view the process by specific users, you can use -u option followed by the user name. You can also use user ID instead of the name.
Sample Output:
7. Adjust the output width of top command
-w option allows you to adjust the output width by decreasing it. You cannot increase the width of the output.
Sample Output:
8. Display Mem and Swap in KB/MB/GB/TB/PB/EB with top command
When top command is running, you can press E option to change the unit of memory as displayed with top command. Different supported units are as shown below:
k - kibibytes -> top shows default in KB m - mebibytes -> Press E one time g - gibibytes -> Press E two time t - tebibytes -> Press E three time p - pebibytes -> Press E four time e - exbibytes -> Press E five time
Sample Output:
By default top shows Memory and Swap in KB
When E pressed one time (show in MB):
When E pressed two time (show in GB):
Similarly you can press E multiple times to change the Unit type of Memory.
9. Sort Processes by Memory Usage in top command output
You can sort the process by memory usage in the output using M key.
10. Sort Processes by CPU Usage in top command output
To sort the processes by CPU Usage, press P at the displayed output.
11. Add or Remove Fields in top command output
f key lets you select or order fields in the output. When you press f , the field management screen appears.
Select Field: Press ‘d’ or to select the field to display in the output.
Let’s add the field UID to the output.
Then press ‘q’ or to see the output.
Order Field: Navigate with Up/Dn, Right selects for move then or Left to save changes
Now, let’s order the field to a different location. Here, we are putting the field UID between USER and PR.
Press ‘q’ or to see the output.
The changes made as part of this example is temporary, and next time when you execute top command, the added/modified sections will not be visible again.
12. Hide the value 0 in top command output
You can press 0 to hide or show the value 0 in top command output. The blank field in the output contains 0, which is suppressed.
13. Toggle Alternate Display Mode in top command output
You can use A to switch between alternate display or full display mode in the output. Next you can press ‘g‘ where you will be prompted to enter a number between 1 and 4 designating the field group which should be made the current window.
14. Disable bold text in top command output
Pressing B key will disable or enable the bold text in the output. It alters both the summary area and task area for the current
window. As we can see, the bold texts are disabled in the output below.Without pressing B
After pressing B
15. Change delay time in top command output
If you press d , you will be prompted to enter the new delay time.
16. Change memory unit type in task windows
With e , you can change the memory scale in the task window, which ranges from kibibytes (KiB) to pebibytes (PiB). If the scaling range is high, you are more likely to get zero value. Such zero values are not suppressed by interactive command 0 .
k - kibibytes -> top shows default in KB m - mebibytes -> Press e one time g - gibibytes -> Press e two time t - tebibytes -> Press e three time p - pebibytes -> Press e four time e - exbibytes -> Press e five time
Press e one time (show in MB):
Press e two times (show in GB):
Press e three times (show in TB):
17. Choose another window in top command output
When you press g , you will be prompted to enter the number between 1 and 4 which allows you to choose another window or field group.
18. Display only active tasks with top command
You can view the active tasks in the system by pressing the key i . Other idled or zombied processes will not be displayed.
19. Change colour mapping in top command output
You can press Z key while top is running. After which you will be presented with the list of colors to choose with. Follow the instructions as shown below:
Here we will select 3 for yellow, and by default only Task Information is selected:
Accordingly you can choose between section 1 and section 2 and then press a to commit and add another combination. For example, here I have selected 4 = blue for S = Summary and then press a to commit and add another:
20. Scroll a window in top command output
You can use the following keys to scroll the tasks or columns in the output.
Up, PgUp: The Up arrow key moves a single line while PgUp scrolls the entire window. Down, PgDn: The Down arrow key moves a single line while PgDn scrolls the entire window. Left, Right: Move the view of displayable fields horizontally one column at a time. Home: Reposition the display to the un-scrolled coordinates. End: Reposition the display to the last displayable field or last task.
Conclusion
This is the end of our tutorial on top command examples in the Linux system. We hope you will be now able to view the Linux processes using the top command. If you still have any confusion, use the comment section below to ask the questions.
What’s Next
Further Reading
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!
top command in Linux with Examples
top command is used to show the Linux processes. It provides a dynamic real-time view of the running system. Usually, this command shows the summary information of the system and the list of processes or threads which are currently managed by the Linux Kernel. As soon as you will run this command it will open an interactive command mode where the top half portion will contain the statistics of processes and resource usage. And Lower half contains a list of the currently running processes. Pressing q will simply exit the command mode. Here,
- PID: Shows task’s unique process id.
- PR: The process’s priority. The lower the number, the higher the priority.
- VIRT: Total virtual memory used by the task.
- USER: User name of owner of task.
- %CPU: Represents the CPU usage.
- TIME+: CPU Time, the same as ‘TIME’, but reflecting more granularity through hundredths of a second.
- SHR: Represents the Shared Memory size (kb) used by a task.
- NI: Represents a Nice Value of task. A Negative nice value implies higher priority, and positive Nice value means lower priority.
- %MEM: Shows the Memory usage of task.
- RES: How much physical RAM the process is using, measured in kilobytes.
- COMMAND: The name of the command that started the process.
Examples: 1) Exit Top Command After Specific repetition: Top output keep refreshing until you press ‘q‘. With below command top command will automatically exit after 10 number of repetition.
2) Display Specific User Process
Z 3) Highlight Running Process in Top: Press ‘z‘ option in running top command will display running process in color which may help you to identified running process easily Z 4) Shows Absolute Path of Processes: Press ‘c‘ option in running top command, it will display absolute path of running pro Z 5) Kill running process: You can kill a process after finding PID of process by pressing ‘k‘ option in running top command without exiting from top window as shown below. 6) Sort by CPU Utilisation: Press (Shift+P) to sort processes as per CPU utilization. 7) Shows top command syntax:
8) Batch Mode : Send output from top to file or any other programs.
9) Secure Mode : Use top in Secure mode.
10) Command Line : The below command starts top with last closed state.
11) Delay time : It tells delay time between screen updates.