233

How do I easily copy the GNU Screen scrollback buffer to a file? I.e., a more powerful version of the 'hardcopy' command?

In GNU screen, I can use "Ctrl + A Esc" to enter the scrollback buffer. I could then mark the entire buffer and use "Ctrl + A Ctrl + ]" to paste it into an Emacs buffer, thus saving it to a file.

However, this is tedious. Is there a Screen command that'll simply copy the scrollback buffer to a file, like 'hardcopy' does for the visible portion of the screen?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

7 Answers7

371

To write the entire contents of the scrollback buffer to a file, type

Ctrl + A and : to get to command mode, then

hardcopy -h <filename>

In older versions of screen, if you just do hardcopy -h, it just writes to the file -h. This was fixed in version 4.2.0, so hardcopy -h writes to hardcopy.N where N is the current window number.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Bill
  • 3,726
  • 1
  • 15
  • 3
  • 1
    Note that the file gets saved in the home directory -- at least it did for me. Thanks for the answer, btw! – Ritesh Nov 15 '13 at 04:52
  • 3
    @allrite: It gets saved in $PWD (if you don't give fullpath), there is no special behavior. – Pushpendre Mar 23 '14 at 00:50
  • 14
    Yes, but it gets saved in the $PWD where you *started* screen. *Not* in the screen session's $PWD. – seumasmac Dec 04 '14 at 01:20
  • 9
    Note that if you have a long scrollback buffer and not much in it, the saved file may appear empty because the first tens or hundreds of lines are blank and the content is at the end. – Terry Brown Apr 16 '15 at 13:07
  • 4
    One further note about the location where the hardcopy gets saved: If you cd to a directory and then use `screen -r` to reconnect to an existing session, the hardcopy doesn't get saved to the active directory inside screen or the one you were in before using `screen -r`. I think it gets saved to the directory you were in when you first started the screen session. You may be able to change this with the [hardcopydir](http://web.mit.edu/gnu/doc/html/screen_17.html#SEC101) command. – Matthias Fripp Jul 30 '15 at 06:38
  • Just a comment - not everything that runs inside screen is a shell (or has a PWD). Screen supports serial consoles where a local directory makes no sense, so it's possible that there's only one directory involved - the one that the screen process started in. – davidA Mar 13 '17 at 21:57
  • 2
    If you're not sure about the directory, create a new window with Ctrl+A+C. It opens in the directory where the file is saved. – Seppo Enarvi Jul 06 '17 at 13:01
  • Note that unlike the methods which involve the copy buffer, the `hardcopy` command dumps the scrollback exactly as it appears, including breaking long lines for the current terminal width. This may or may not be what you want. – mhsmith Mar 14 '18 at 18:53
  • 1
    In addition to the tip from @SeppoEnarvi, screen's working can be determined with a good ol' `pwd` command to the parent shell: **Ctrl+a :** `!pwd` . – Murphy's Prophet Jan 07 '19 at 13:45
  • 1
    This strips ANSI color codes before saving to file. Is there any way to preserve them? – user643011 Apr 14 '19 at 23:27
  • Thanks @Murphy'sProphet. I tried various methods but couldn't find the log file. This command saves my day. – Shaohua Li Nov 04 '21 at 12:24
27

Press Ctrl+A :bufferfile /tmp/somefile.txt ENTER, and then Ctrl+A >

This will write the current contents of the buffer to the named file.

Roshan
  • 1,096
  • 7
  • 11
  • 1
    I tried this, and the "ctrl-a >" says "Nothing happened". The first step did say "Bufferfile is now '/tmp/foo.txt'", but doesn't appear to even create that file. –  May 05 '11 at 13:27
  • 2
    Are you sure you had copied something into the screen buffer using Ctrl+A [? It sounds like your buffer might be empty - it won't copy the entire scrollback buffer, just the portion you selected using Ctrl+A [ – Roshan May 05 '11 at 20:33
  • 2
    Ah, I was looking for a shortcut to copy the entire buffer without using ctrl-a [ –  May 07 '11 at 01:30
14

TL;DR: ^A:writebuf <filename>

The OP seems to want a way to use the selected portion of the buffer you get when doing a ^A[ , selecting text using space as the start and finish, and then instead of using ^A] to paste, save the resulting selected portion of the buffer to a file.

This worked:

^A:writebuf <filename>

Note: one 'f' in writebuf

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kevin Mullet
  • 151
  • 1
  • 3
6

Try hardcopy -h to include the whole buffer.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 2
    This just makes a hardcopy in the file ~/-h –  Jun 02 '11 at 20:15
  • 2
    @barrycarter: That may have changed in more recent versions. As of screen 4.03.01, `hardcopy -h` writes to `hardcopy.N`, where `N` is the current window number. – Keith Thompson Nov 30 '16 at 01:03
6

This worked for me:

Enter edit mode (~) and type:

:hardcopy -h buff_file

It created a huge file, of which 98% was empty, but my logs were fully present in remaining 2%.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
freeninza
  • 61
  • 1
  • 2
-1

Ctrl + A, :, and issue the command 'log on'.

Or set it as the default in your .screenrc file as 'deflog on'.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
brianray
  • 1,139
  • 1
  • 12
  • 16
  • 2
    I think this just starts logging everything after I run that command. I don't want to log everything (too verbose). I just want to capture the scrollback buffer as a file. –  Feb 24 '11 at 00:09
-3

Do Ctrl + A, H.

That saves the current screen into a hard copy file, e.g., hardcopy.0 for screen 0. It seems to be a quicker way than going Ctrl + A, : and typing the hardcopy command.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jeff
  • 1
  • 1
  • 3
    Unfortunately, that just saves the VISIBLE portion of the screen, not the scrollback buffer. –  Dec 03 '13 at 14:32