Linux top command with examples

In this article, we will see Linux top command with examples.

We can use Linux top command to see processes with consumed resources like memory and cpu utilization. Linux top is one of most often used command when troubleshooting performance issue in Linux/Unix.

Linux top command syntax:

top -hv|-bcEHiOSs1 -d secs -n max -u|U user -p pid -o fld -w [cols]

Command Actıon
b Enables / dısables bold hıghlıghtıng on top menu
z Cycles the color scheme
l Cycles the load average headıng
m Cycles the memory average headıng
t Task ınformatıon headıng
Shıft+F Customızes sortıng and dısplay fıelds
-o Sorts by column (can prepend wıth – or + to sort ascendıng or
descendıng)
-u Shows only processes from a specıfıed user
-d Updates the delay tıme of top
-O Returns a lıst of columns whıch top can apply sortıng

Linux top command produces following columns:
PID: Shows task’s unique process id.
PR: Stands for priority of the task.
SHR: Represents the amount of shared memory used by a task.
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.

Linux top command examples

1. List all running processes:

top

To quit from windows, press q from your keyboard.

Linux top command with examples

2. To display processes for specific user in Linux:
Following command lists processes for the user david.

top -u david

3. Highlight Running Processes in Top: Press ‘z‘ option in running top command will display running process in color which may help us to identified running process easily

4. Shows Absolute Path of Processes: Press ‘c‘ option in running top command, it will display absolute path of running processes.

5. If you want sort all Linux processes by memory and CPU usage, then press M and P keys(both are capital letter or use Shift+P key

6. If we want to sort all Linux processes by running time, then press M and N keys on keyboard.


7. If we want to sort all Linux processes by PID, then press M and N keys on keyboard.

8. Toggle the display of idle processes – ‘i’
To find list of idle/sleeping processes in Linux using top command with i option.

9. Forest mode – ‘V’
Pressing ‘V’ will display the processes in a parent child hierarchy. It looks something like this –

10. Lets say we want to monitor only few processes based on a certain filter criteria. Press ‘n’ and enter the number of processes you wish to display.

Following displays only 10 processes.

11. How to change refresh top command refresh time in Linux.

By default screen refresh interval is set to 3.0 seconds, the same can be changed by pressing the ‘d‘ option in running the top command to set desired interval time.

12. How to save Linux top command output to a file.

top -n 1 -b > topcommand_output.txt

13. Exit Top Command After Specific Repetition:

The output of the top command keeps refreshing until you press ‘q‘. With the below command, it will automatically exit after 5 repetitions.

top -n 5