Best Practices for Sorting with the Top Command in Linux/Unix

The top command is a powerful utility in Linux and Unix-like systems for monitoring system processes and resource usage. It displays a dynamic, real-time view of a running system, including the processes that are consuming the most resources.

Here are some examples of how you can sort and filter the output of the top command:

1. Sorting by CPU Usage

To sort the processes by CPU usage in descending order, you can use the following key combination in the top interface:

  • Press 1 to enable sorting by %CPU (CPU usage percentage).
  • Press r to reverse the sort order (from ascending to descending).

Alternatively, you can launch top with specific sorting options using the -o option:

top -n 10 -o %CPU

This command runs top, displays only one update (-n 10), and sorts by CPU usage (-o %CPU).

top sort by cpu

2. Sorting by Memory Usage

To sort the processes by memory usage in descending order, you can use the following key combination in the top interface:

  • Press 2 to enable sorting by %MEM (memory usage percentage).
  • Press r to reverse the sort order.

Alternatively, you can launch top with specific sorting options using the -o option:

top -n 10 -o %MEM

3. Sorting by Running Time

To sort the processes by the amount of time they have been running, you can use the following key combination in the top interface:

  • Press 3 to enable sorting by TIME+ (running time).
  • Press r to reverse the sort order.

Alternatively, you can launch top with specific sorting options using the -o option:

top -b -n 1 -o TIME+

4. Sorting by Nice Value

To sort the processes by their nice value (which affects scheduling priority), you can use the following key combination in the top interface:

  • Press 4 to enable sorting by NICE (nice value).
  • Press r to reverse the sort order.

Alternatively, you can launch top with specific sorting options using the -o option:

top -b -n 1 -o NICE

5. Sorting by Virtual Memory Size

To sort the processes by virtual memory size, you can use the following key combination in the top interface:

  • Press 5 to enable sorting by VIRT (virtual memory size).
  • Press r to reverse the sort order.

Alternatively, you can launch top with specific sorting options using the -o option:

top -b -n 1 -o VIRT

6. Sorting by Resident Set Size

To sort the processes by resident set size (the non-swapped physical memory a process has used), you can use the following key combination in the top interface:

  • Press 6 to enable sorting by RES (resident set size).
  • Press r to reverse the sort order.

Alternatively, you can launch top with specific sorting options using the -o option:

top -b -n 1 -o RES

7. Sorting by Shared Memory Size

To sort the processes by shared memory size, you can use the following key combination in the top interface:

  • Press 7 to enable sorting by SHR (shared memory size).
  • Press r to reverse the sort order.

Alternatively, you can launch top with specific sorting options using the -o option:

top -b -n 1 -o SHR

8. Sorting by Start Time

To sort the processes by start time, you can use the following key combination in the top interface:

  • Press 8 to enable sorting by START (start time).
  • Press r to reverse the sort order.

Alternatively, you can launch top with specific sorting options using the -o option:

top -b -n 1 -o START

9. Sorting by PID

To sort the processes by their process ID (PID), you can use the following key combination in the top interface:

  • Press 9 to enable sorting by PID.
  • Press r to reverse the sort order.

Alternatively, you can launch top with specific sorting options using the -o option:

top -b -n 1 -o PID

10. Sorting by Command Name

To sort the processes by the command name, you can use the following key combination in the top interface:

  • Press 0 to enable sorting by COMMAND (command name).
  • Press r to reverse the sort order.

Alternatively, you can launch top with specific sorting options using the -o option:

top -b -n 1 -o COMMAND

Conclusion

The top command provides a wealth of information about system processes and resource usage. You can use the above examples to sort the processes according to various criteria. Remember that the top command updates the process list continuously, so the output will change over time.

This entry was posted in System, Top Command and tagged , , , . Bookmark the permalink.