The “top” Command in Linux [8 Practical Examples]

The top command in Linux is a very advanced command. The top command when used shows all the current processes of the system. This command normally shows a summary of the users’ system. And the top command also shows a list of all the running processes throughout the system. In this article, I will explain how to use the top command and I will also show you some useful examples.

A. Description

The top command in Linux is a short form of the word Table of Process. So in the long word, it is clear enough that this command works with the existent system processes.

B. Syntax

The syntax for the top command is very simple. That is the top command followed by its various options and flags, which is given below.

top -hv|-bcEeHiOSs1 -d secs -n max -u|U user -p pids -o field -w [cols]
Note: The traditional switches `-‘ and whitespace are optional.

C. Options

Many useful options are available for the top command. I have listed some of the most used ones here. However, if you want to know more about options for the top command you can always check the man page for the top command.

man top

Useful Options

  • -h: Shows help file for this command.
  • -v: Shows the version and quits the program.
  • -O: Shows the name for the output fields.
  • -o: This gives names to the files according to which files will be sorted.
  • -H: Shows individual threads.
  • -n: It limits the iteration number for the top command.
Note: The options in Linux CLI (Command Line Interface) are all case-sensitive. So, be cautious while using them. Options starting with (- -) are long forms of options. Long Forms have the same meaning as Short Forms.

Practical Examples of the “top” Command in Linux

The top command is very easy to use. It is one of the most important commands of the Linux CLI when it comes to system diagnosis. Here I have given some examples of the top command.

Example 1: Showing Table of Process Using “top” Command in Linux

If I want to view the system summary of all the running processes on the system I will have to use the top command. The way to do that is very simple. Follow the steps below to see how this works,

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

top

➌ Now, press the ENTER button.

Output >

The upper portion of the picture shows the total system summary and the lower portion shows the list of all running processes in the system. To halt this operation you have to press the q button.

Using top command in linux to show table of processes


Similar Readings


Example 2: Showing Table of Process and Exiting after n Iteration

If you want to exit the top operation you have to use the option(-n). You will have to use the command then the option and the number of iterations. Now follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

top -n 5

➌ Now, press the ENTER button.

Output >

As you can see that the top operation halted after just 5 iterations.

using iterative top command in linux

Example 3: Showing Individual Threads Using “top” Command in Linux

If you want to run the top command but only want to show a single thread at a time, you will have to use the option (-h). Now follow the steps below to see how this works.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

top -h

➌ Now, press the ENTER button.

Output >

As you can see that the top operation only printed a single thread.

Using top command in linux to show a single thread

Note: The traditional switches `-‘ and whitespace are optional.

Similar Readings


Example 4: Sorting Linux Processes by PID Using the “top” Command

If you want to sort the process list by Process ID you will use the same top command. Follow the steps below to know how this works.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

top -h

➌ Now, press the ENTER button.

➍ Now, press the M and T buttons.

Output >

As you can see that the top operation printed the process list and it is sorted by the PID.

Sorting Linux Processes by PID

Example 5: Sorting Linux Processes by CPU Usage

If you want to sort the process list by CPU Usage you will use the same top command. Follow the steps below to know how this works.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

top -h

➌ Now, press the ENTER button.

➍ Now, press the M and P button.

Output >

As you can see that the top operation printed the process list and it is sorted by CPU Usage.

Sorting using the top command


Similar Readings


Example 6: Showing Linux Processes for a Specific User Using the “top” Command

If you want to show the Linux processes of a certain user you have to use the option(-u). You will have to use the command then the option and the user name. Now follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

top -u akash

➌ Now, press the ENTER button.

Output >

As you can see that the top operation printed the process list for the user akash.

Showing specific user processes

Example 7: Using the “top” Command in Linux with Redirection

In this example, I will redirect the output of the top command to a different file. Now follow the steps below to see how this works.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

top -h > File1.txt

➌ Now, press the ENTER button.

➍ Type the following command in the command prompt:

cat File1.txt

➎ Now, press the ENTER button.

Output >

As you can see that the top operation printed a single thread but instead of printing it on the terminal, I directed that output to File1.txt.

Redirecting output

Note: The cat command is used for viewing file content.

Similar Readings


Example 8: Showing Help Page for “top” Command in Linux

If you want to see the help page you will use the same top command in Linux. Follow the steps below to know how this works.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

top

➌ Now, press the ENTER button.

➍ Now, press the h button.

Output >

As you can see that the top operation printed the help page.

Showing help page

Conclusion

In this article, I have tried to show you how the top command works. And I have also included some useful examples. If you go through this article properly, you should be able to use the command top properly on your own.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Md. Rafsan Zani

Hello, I’m Md. Rafsan Zani. I have recently completed my Undergraduate from the Bangladesh University of Engineering and Technology (BUET). Currently, I’m pursuing higher studies abroad. I’m really interested in computer science and would like to learn a lot about the wonderful world of computers. Currently, I’m working as a Linux Content Developer Executive and find Linux really interesting. I certainly would like to learn more about Linux and implement them in my future studies. Read Full Bio

Leave a Comment