Question: I’m trying to capture the output of the top command into a file. When I execute top > output.txt, the output.txt file contains lot of junk characters. What is the best method to capture the output of the top command into a readable text file?
Answer: Use the top command batch mode operation option ( -b ) to capture the top command output into a file.
If you try to redirect the top command output to a text file as shown below, you’ll notice that the output file contains lot of junk characters.
When you try to view the output file using less command, you’ll notice that the output file is created with lot of junk characters.
$ top -n 1 > top-output.txt $ less top-output.txt "top-output.txt" may be a binary file. See it anyway?
Note: Option -n 1 indicates that only one iteration of the top command should be executed.
To avoid this problem and get a readable top command output, use option -b in the top command. Execute top command in batch mode as shown below.
$ top -n 1 -b > top-output.txt
$ less top-output.txt
top - 16:56:36 up 246 days, 11:14, 3 users, load average: 0.00, 0.00, 0.00
Tasks: 168 total, 1 running, 167 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 8306856k total, 7940744k used, 366112k free, 285136k buffers
Swap: 8385920k total, 104k used, 8385816k free, 7391824k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 15 0 2064 592 512 S 0.0 0.0 0:02.24 init
2 root RT -5 0 0 0 S 0.0 0.0 0:00.47 migration/0
3 root 35 19 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0
4 root RT -5 0 0 0 S 0.0 0.0 0:00.00 watchdog/0
5 root RT -5 0 0 0 S 0.0 0.0 0:00.61 migration/1
About option -b and -n from the top command man page:
-b : Batch mode operation
Starts top in "Batch mode", which could be useful for sending out-
put from top to other programs or to a file. In this mode, top
will not accept input and runs until the iterations limit youâve
set with the â-nâ command-line option or until killed.
-n : Number of iterations limit as: -n number
Specifies the maximum number of iterations, or frames, top should
produce before ending.
You can also use this method to redirect the output of top command to another program as shown below.
$ top -n1 -b | head
top - 16:58:36 up 246 days, 11:14, 3 users, load average: 0.00, 0.00, 0.00
Tasks: 169 total, 1 running, 168 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 8306856k total, 7941612k used, 365244k free, 285144k buffers
Swap: 8385920k total, 104k used, 8385816k free, 7392088k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 15 0 2064 592 512 S 0.0 0.0 0:02.24 init
2 root RT -5 0 0 0 S 0.0 0.0 0:00.47 migration/0
3 root 39 19 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0
Get free Unix tutorials, tips and tricks straight to your email in-box.
If you enjoyed this article, you might also like..


My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about
{ 1 trackback }
{ 5 comments… read them below or add one }
If i need a top output sorted by memory utilization by processes into a text file for every 15 mins. how can we do this
Madhan you could put this script to your cron or whatever.
top -n1 -b | sed -n ‘8,$-1p’ | sort -r -n +9 -9 > /var/log/memusg.log
@Madhan
This may be the one you are looking for…
top -n1 -b | sed -n ‘8,$p’ | sort -r -n -k 10,10
Hi nardi,
It is throwing an error as follows:
[root@madhan ~]# top -n1 -b | sed -n ‘8,$-1p’ | sort -r -n +9 -9 > /var/log/memusg.log
sed: -e expression #1, char 4: unknown command: `-’
sathiyaMoorthy : it is working.
Thanks a lot to all
@Madhan Sry. I was trying if sed can handle something like “last but one” notation and sent wrong “version” of command. SathiyaMoorthy corrected this mistake.