There are times when you want the output from a particular command that you’ve run on a Linux terminal in a file. There are even times when you want an accurate log of the entire interactive session you are going to run. While the solution to the former is as simple as piping the output of the command to a file, to go around doing the latter is a bit difficult, unless you are pretty OK with manually copying and pasting everything that appears on the terminal in a file.
If you are not, though, and are looking for a better, more effective, more accurate, and less tiresome solution, then look at “script“. According to the manpage of script(1), it makes a typescript log of a terminal session. Be it a command that is run in the session or an entire interactive session, script logs every detail into a file.
A few examples on how to use it.
ayaz$ script -a file.out
Script started, file is file.out
ayaz$ echo "Now, run any commands you want to run."
Now, run any commands you want to run.
ayaz$ echo "And when you want to exit, type exit."
And when you want to exit, type exit.
Script done, file is file.out
ayaz$ script -a -c "ls -l" file.out
ayaz$ script -a -c "id" file.out