Forrest logo
back to the top tool

system:cpu:usage

Displays CPU usage as a percentage in Linux
$ top -b -n 1 | grep '%Cpu' | awk '{print $2}'
try on your machine

This command is a combination of several command-line tools used for monitoring and manipulating data.

Here is a breakdown of each component:

  1. top: This is a command-line tool for monitoring real-time resource usage on a Unix-like system. By default, it continuously displays a dynamic view of system information such as CPU usage, memory usage, and running processes. However, in this case, the -b flag is used to run top in batch mode, which means it will run for only a single iteration and then exit.

  2. -n 1: This flag is passed to top to specify the number of iterations to run. In this case, it is set to 1, which means it will only run top once and then exit.

  3. | (pipe): This is a command-line operator that allows the output of one command to be used as input for another command. It takes the output of the command on the left and feeds it as input to the command on the right.

  4. grep '%Cpu': This command filters the output of top by searching for lines that contain the string '%Cpu'. The %Cpu line in the top output shows the CPU usage summary.

  5. awk '{print $2}': awk is a powerful text processing tool that operates on columns of data. In this command, it is used to extract the second column (column number 2) from the grep output. The specific column contains the CPU usage percentage. {print $2} is the code snippet in awk to print the second column of each line.

So, overall, this command captures a snapshot of CPU usage using top, filters the output to extract only the CPU usage line using grep, and then extracts the actual CPU usage percentage using awk.

This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the top tool