How to save top output to a file in Linux

Hello Campers,

I would like to save the top command output into a file. I use this command:

top -b -n 1 > file.txt

-b - forces the batch mode
-n 1 - count of executions that would be saved

Once the command is executed the output looks like below:

top - 17:06:14 up  1:27,  0 users,  load average: 0.52, 0.58, 0.59
Tasks:   4 total,   1 running,   3 sleeping,   0 stopped,   0 zombie
%Cpu(s):  3.1 us,  0.9 sy,  0.0 ni, 95.8 id,  0.0 wa,  0.2 hi,  0.0 si,  0.0 st
KiB Mem : 16721672 total,  8794144 free,  7698176 used,   229352 buff/cache
KiB Swap: 50331648 total, 50331648 free,        0 used.  8889764 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
    1 root      20   0    8892    292    260 S   0.0  0.0   0:00.04 init
    5 root      20   0    8896    200    168 S   0.0  0.0   0:00.00 init
    6 shnaider  20   0   16892   3568   3448 S   0.0  0.0   0:00.08 bash
  105 shnaider  20   0   17488   1884   1420 R   0.0  0.0   0:00.01 top

What is important for me is the CPU utilization. Especially the CPU per core/thread utilization. In order to check it you have to:

  1. execute the top command
  2. once it is executed you should click 1 number (above the Q key on keyboard) and you will see
top - 17:12:55 up  1:34,  0 users,  load average: 0.52, 0.58, 0.59
Tasks:   4 total,   1 running,   3 sleeping,   0 stopped,   0 zombie
%Cpu0  :  9.6 us,  6.8 sy,  0.0 ni, 79.7 id,  0.0 wa,  3.9 hi,  0.0 si,  0.0 st
%Cpu1  :  5.0 us,  1.7 sy,  0.0 ni, 93.4 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  : 49.7 us,  2.6 sy,  0.0 ni, 47.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  : 17.4 us,  1.6 sy,  0.0 ni, 80.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu4  : 16.8 us,  4.6 sy,  0.0 ni, 77.9 id,  0.0 wa,  0.7 hi,  0.0 si,  0.0 st
%Cpu5  : 12.5 us,  1.0 sy,  0.0 ni, 86.5 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu6  : 16.7 us,  2.3 sy,  0.0 ni, 80.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu7  : 10.2 us,  1.7 sy,  0.0 ni, 88.1 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu8  : 19.3 us,  4.7 sy,  0.0 ni, 76.1 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu9  :  5.6 us,  1.3 sy,  0.0 ni, 93.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu10 : 16.6 us,  5.3 sy,  0.0 ni, 78.1 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu11 :  6.3 us,  4.7 sy,  0.0 ni, 89.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 16721672 total,  8390248 free,  8102072 used,   229352 buff/cache
KiB Swap: 50331648 total, 50331648 free,        0 used.  8485868 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
    1 root      20   0    8892    292    260 S   0.0  0.0   0:00.04 init
    5 root      20   0    8896    200    168 S   0.0  0.0   0:00.00 init
    6 shnaider  20   0   16892   3568   3468 S   0.0  0.0   0:00.08 bash
  122 shnaider  20   0   17620   2040   1508 R   0.0  0.0   0:00.04 top

The issue is that I don’t know how to save this output to a file. Can anybody help me?

The simple way would be

top
1
W
q

and then enter your command
top -b -n 1 > file.txt
and then grab what you need from there

The ideal way would be to make a configuration file. Use the command man top for more info.

Yes, this is the way that satisfied my needs.

Thank you!