Linux – save output from a previous terminal command using only the keyboard

consolelinuxterminal

I just spent the last two hours running a dd command (or picture any similar "difficult to re-do" scenario) from a live CD without a GUI; all I have is my trusty "multi-window" (CTRL+ALT+F#) Bash terminal.

Alas, during the command dd threw out several nasty error messages and a bit more information that I would like to keep. I have a USB drive plugged in to which I can write data, but how do I get the previous output saved as a text file after the command has already been run?

If this had been a terminal emulator inside a nice GUI, I would have simply used my mouse to select the text, copy it, and paste it into a document. And had I known the command would have produced errors, I would have piped it out to a file to begin with, but alas, the additional output came as a surprise.

How do I save text output from my previous command to a file without re-running the command? Is this even possible?

Best Answer

A linux kernel should store an on-screen log for your vts in the corresponding /dev/vcsa*[ttynum] device.

It is why the following works:

echo hey >/dev/tty2
dd bs=10 count=1 </dev/vcs2

...which prints...

hey       

The corresponding /dev/vcsa[ttynum] device will store an encoded version of the formatted text on-screen, whereas the /dev/vcs[ttynum] will be a plain dump. The vcsa[ttynum] devices will encode a pair of bytes which describe each on-screen char and its attributes, as well as a string at the head of each logical page that indicates the referenced tty's lines,columns count.

As @kasperd points out, I had it wrong before by assuming the \a BEL was encoded between every character, when in fact: The default color combination happens to coincide with the bell character.

For your purposes using the /dev/vcs[ttynum] is probably easiest. Here's a look at the differences:

echo hey >/dev/tty2
dd bs=10 count=1 </dev/vcs2 |
sed -n l

...prints...

hey       $

...and...

echo hey >/dev/tty2
dd bs=10 count=1 </dev/vcsa2 |
sed -n l

...prints...

0\200\000\004h\ae\ay\a$