top -bn 5 >top.log
Where the flags:
-b – enables top to run in batch mode, so that you can redirect its output to a file or another command.
-n – specifies the number of iterations before the command terminates
In Linux and other Unix-like systems, there are three default files named below which are also identified by the shell using file descriptor numbers:
stdin or 0 – it’s connected to the keyboard, most programs read input from this file.
stdout or 1 – it’s attached to the screen, and all programs send their results to this file and
stderr or 2 – programs send status/error messages to this file which is also attached to the screen.
Therefore, I/O redirection allows you to alter the input source of a command as well as where its output and error messages are sent to. And this is made possible by the “” redirection operators |