Bash – Is it possible to save the output of a previous command on file

bashterminal

Quite often, I'm in the following situation: I launch a command on bash (that takes lot of time to be processes) and I get a long output, which doesn't fit into the terminal and it can't be read even using the scrolling.
The only way I have to read it is to redirect the output to a file. In order to do so, I have to relaunch the command, something that I want to avoid as I would take too much time.

I simply want to print on file the output that has been generated in the prevous command.

It there any way to do that?

Al.

PS: for example, I give

diff folder1 folder2 #the folders contain many files

wait 60 seconds, and after I decide to print the output

Best Answer

If you have a suspicion that, such a thing, i.e., a long output, to happen, start your session by executing command

script

this will log all your screen output as well as what you type in to the terminal (caveat emptor, backspaces and other normally unprintable characters will make the file harder to read, if you are not careful).

when you are done executing your long winded command, just type exit and it will tell you it saved the session output in a file called typescript. Also you can change the name of this file by running your command as

script my_output_file_name

It is a good tool for debugging scripts etc.

Related Question