Linux Commands

How to Filter Top Output to Specific Processes

The top is a Linux command-line utility that allows you to monitor running processes and resource usage in your system. Understanding what is going on in your Linux system in real-time can help you locate resource-intensive processes and take necessary actions.

This guide will discuss using the top command in Linux to find specific information about running processes in your system.

Basic Top Usage

To use the top utility in Linux, launch a terminal session and enter the command top.

$ top

An example output of the top command is:

The output is divided into two portions:

The upper portion shows the resource usage such as Memory, CPU usage, and disk usage.

The lower portion shows information about the running processes. Such data include the Process ID, user, and process name.

How to Filter Specific Processes

To filter the top output to a specific process, press the O key and enter the entry as COMMAND=name, where the name refers to the process name.

For example, to filter for the system:

COMMAND=systemd

Press ENTER, and the top utility will filter the processes to systemd only.

You can also highlight the specific process while keeping other processes in view. To do this, press the L key and search for a string.

For example:

How to Filter User Processes

The Top output utility also allows us to filter processes spawned by a specific user. To do this, we use the -u flag followed by the username.

For example,

top -u ubuntu

The command displays only the processes owned by the ubuntu user.

How to Sort Processes by %CPU

We can also sort the top output by the CPU utilization percentage. To do this, press the SHIFT + P command.

The output is sorted in ascending order.

How to Change Refresh Interval

By default, the top will refresh the output every 3 seconds. To modify this value, press the d key while the top is running.

How to Show the Path to a Binary

To show the path to the command that initiated a specific process, press the c key:

How to Save The Output to a File

We can also save the output of the file by using redirection symbols. For example, to export the top output for 10 minutes, use the command:

top -n 3 -b > top_output.log

The -n specifies the number of iterations top should produce before terminating.

The -b flag starts top in batch mode, which continuously processes the output until the maximum iterations are met (specified in -n flag).

Final Say

The top is a good utility for system administrators and anyone who wants to know what processes use system resources. To learn more about top, consider the documentation.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list