Running top in batch mode

If you try to redirect the top command output to a text file as shown below, you’ll notice that the output file contains lots of junk characters and when you try to view the output file using the less command, you’ll notice that the output file is also 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
You can also use this method to redirect the output of top command to another program as shown below.
top -n1 -b | head


I am a g33k, Linux blogger, developer, student and Tech Writer for My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.