11

I have had a GNU screen session running for days. I find myself in the situation that I need to save the terminal contents (which I can scroll up to see) into a file. Is this possible? I estimate it to be below 5000 lines.

I found a way to set up screen to log future output to a file. But in this case, I need to also save past output (or as much of it as is present).

2 Answers 2

10

You can use hardcopy -h command to save the contents of the current scroll buffer to a file. As described in man screen:

   hardcopy [-h] [file]

   Writes out the currently displayed image to the file file, or,
   if no filename is specified, to hardcopy.n in the default
   directory, where n is the number of the current window.  This
   either appends or overwrites the file if it exists. See below.
   If the option -h is specified, dump also the contents of the
   scrollback buffer.

You said:

I estimate it to be below 5000 lines.

5000 lines is really a lot. The default length of scroll buffer in screen is just 100, not ~5000 lines. Unless you started your screen session with a larger scroll buffer setting it will not be possible to retrieve all ~5000 lines of the scroll buffer.

2
  • I'm not sure why, but I can scroll back about 1500 lines ... I did not change the size of the scrollback buffer.
    – Szabolcs
    Oct 18, 2019 at 19:36
  • 2
    The command can be entered when before ^a : has been pressed.
    – kap
    Jul 13, 2021 at 8:27
7

One way to do it is to use copy mode to copy the entire scrollback history, then dump it into a file. (There is likely a better way.)

With default keybindings, this would be something like:

  • Ctrl-A to send screen a command
  • [ to enter copy mode
  • g to go to the top
  • Space bar to mark the beginning of the scrollback buffer (where you are) as the start of the text to be copied
  • G to go to the end
  • Enter to mark the end of the text to be copied, and copy it.

Then open up vim, run :set paste to avoid issues with e.g. auto-indentation, and then use Ctrl-A ] to paste.

6
  • Couldn't you achieve the same with hardcopy? Oct 18, 2019 at 18:48
  • @ArkadiuszDrabczyk please write an answer. :) I just wrote this off the top of my head, I didn't check the documentation for the better way, but that's probably it.
    – Wildcard
    Oct 18, 2019 at 18:49
  • I posted an answer but it still doesn't answer OP's question to be honest. I'm not sure it's possible to retrieve 5000 lines of scroll buffer, at least using default settings. Oct 18, 2019 at 19:00
  • I accepted this one because harcopy is not present on this system and I do not have root access to easily install it.
    – Szabolcs
    Oct 19, 2019 at 8:19
  • 1
    @Szabolcs: hardcopy is not an external program, it's screen command Oct 19, 2019 at 11:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .