Linux top sorting

Why We Use Top Command in Linux?

Among the most elementary command to monitor processes on Linux is the top command. As the name implies, it reveals the top processes based on specific criteria such as CPU usage or memory usage.

The procedures are listed out in a list with multiple columns for information such as procedure name, PID, user, CPU usage, memory usage.

Aside from the list of procedures, the top command also shows short stats regarding average system load, CPU usage, and RAM usage on the top.

The top command enables users to monitor processes and system resource usage on Linux. It’s among the most useful tools in a system admin’s toolbox, and it comes pre-installed on each distribution. Unlike other commands such as ps, it’s interactive, and you can navigate through the list of procedures, kill a process, etc.

Running the command with No options

$ top

An example output:

linux top

Getting the help for top command

The top command help will provide you a list of alternatives you can use to customize the output and the functions you can perform when you’re operating the top command.

In order to get the help while still the top command is running, press the ‘h’ key:

linux top help

How to quit the top command?

After executing the top command, you may simply press the ‘q’ button to quit this. If you press ‘q’ after above help command, it will quit help. Again press ‘q’ to quit top.

How to check the version of top?

To check which version of top you have, type the following command:

$ top –v
Going through the column heading of top command

The column headings in the process list are as follows:

PIDProcess ID
USERThe owner of this process.
PRProcess priority
NIThe process nice value
VIRTNumber of virtual memory used by the process.
RESNumber of resident memory used by the process.
SHRNumber of memory used by the process.
SStatus of this process.
% CPUThe share of CPU time used by the process since the last upgrade
%MEMThe share of physical memory used.
Display processes for a Particular User

If you want the top command to display processes being conducted by a specific user, you can use the following syntax:

$ top -u [username]

For example, the following command will display all of the processes being run by root:

$ top -u root
Sort by Memory/Cpu/Process ID/Running Time

To obtain the process consuming the most CPU or memory, only type the list.

Press the capital ‘M’ key to sort the process list by memory usage. Processes with the most memory are shown first and rest in order.

Here are additional options to sort by CPU usage, Process ID, and Running Time –

  • Press ‘P‘ – sorted by CPU usage.
  • Press ‘N’ – for sorting by process id.
  • Press ‘T’ – to sort from the running time.
How to Reverse the sorting order

By default, the sorting is done in descending order. Pressing ‘R’ shall reverse the sorting order of the currently sorted column

This is the output sorted in ascending order of CPU usage. Processes consuming the least amount of CPU are shown first. An example output is shown below:

Linux top sorting

Set Interval for Refreshing the Display

By default, the top command output is refreshed every 3 seconds. To be able to change this period, hit the ‘d’ key while the top command is operating.

You may then enter the new time, in seconds, and the newest output is refreshed on the screen.

How to kill the process?

If you would like to kill a process, simply press ‘k’ when the top is running. This will bring up a prompt, which will request the process ID of the process and press enter.

Next, input the sign using which the procedure ought to be killed. If you leave this blank, the top utilizes a SIGTERM, which enables processes to terminate gracefully. If you wish to kill a process successfully, you can type in SIGKILL here. You may also type in the sign number here. By way of instance, the amount for SIGTERM is 15 and SIGKILL is 9.

How to sort by columns using top command

By default, the processes list is sorted by the %CPU column. You can change the sort column by pressing on the following:

  • P: The %CPU column.
  • M: The %MEM column.
  • N: The PID column.
  • T: The TIME+ pillar.
See Processes for a Single User

Press “u” to find the processes for one user. You’ll be prompted for the name or UID

Type the title of the UID of the person you want to track.

How to only see active tasks?

Press I to see only active tasks.

View the Absolute Path of processes

If you would like to observe the complete path of the running processes, hit the ‘c’ key while the top command is operating. In the following output, you can see that the command now shows the path of the running processes in the Control column:

Linux top abs path

Automatically Exit top Controller after a Particular Number of Refreshes

As stated earlier, you can stop the top command manually via ‘q’ or ctrl+c. If you want the top command to run until a specific number of output refreshes, you can use the following command:

$ top –n5


The top command should exit after five refreshes.