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

Was this post helpful?