50

I have been looking for an answer to this and not finding anything which makes me think it's not possible but ...

Is it possible to save the current Gnome Terminal scrollback buffer to a file?

I know that I can do something like command > output.txt to redirect all output to a file, or command | tee output.txt to split the output to the terminal as well as to a file. What I am trying to do is capture this after the fact. I'd like to save the current terminal tab's scrollback contents to a file.

Karl Wilbur
  • 1,875

3 Answers3

67

After a bit of playing around I've discovered that you can:

  • triple-click the last line
  • hit shift + home
  • shift + click first line
  • copy with ctrl + shift + c (or right-click > 'Copy')

Now paste it into a text file ...or, using xsel you can shove your clipboard into a new file by just popping open a new tab and doing:

xsel -o > out.txt

(To install xsel do sudo apt-get install xsel)

WARNING

An unlimited scrollback can get huge and attempting to select all of it for copying will be slow and could even exceed memory limits. See comments below.

Karl Wilbur
  • 1,875
  • 11
    Highlighting can be done more easily: Edit -> Select All. – egmont Oct 29 '15 at 07:29
  • 5
    This is a great answer cause it makes you realize you can use triple-click the last line and shift + home or the scrollbar to go to the exact location you want for the beginning of the output. – Chef Pharaoh Feb 08 '17 at 15:17
  • 4
    Warning: Don't try this with a large scollback (many megabytes) as it might hang X (happened to me). – Kevin Cox Mar 22 '17 at 10:30
  • With many megabytes, slow processing is to be expected. Be patient and let the computer work. "Give it a second! It's going to space! Can you give it a second to get back from space?!" – Karl Wilbur Mar 22 '17 at 21:51
  • I just tried to copy a large scrollback (rsync output from backing up a couple 2TB drives overnight) using the Edit -> Select All method to capturing the output. It's been 25-30 minutes now and Gnome Terminal is still locked up. Using Byobu, I was able to start up top to see that Gnome Terminal is still running (increasing CPU time); currently using over 4.176g of RAM and slowly climbing. So, yeah, with a very large scrollback, it's gonna take a while. X is still running (and Chrome, YouTube, Nautilus, Byobu, etc.) but Gnome Terminal is gonna need a minute. I should've planned better. – Karl Wilbur Apr 30 '17 at 15:36
  • My Gnome Terminal (all 3 windows with multiple tabs) crashed when I did xsel -o > out.txt, and then Cinnamon crashed as well :/ – Bloke Oct 10 '17 at 09:14
  • Wow, Bloke. You might want to look into that a little more (hardware issues, Cinnamon bugs, etc.). I use this trick quite often now and without any trouble. I consistently have several windows open and several tabs in each window. I use Ubuntu 16.04 with Gnome 3. – Karl Wilbur Oct 13 '17 at 15:38
  • @chicks, Thanks for the edits! – Karl Wilbur May 05 '20 at 17:35
  • 2
    Just noticed that newer version (tested on 20.04 = GNOME 3.36.2) of the GNOME terminal also offer a "Copy as HTML" right-click option, which is useful if you like to preserve any colouring and highlighting. – sxc731 Dec 19 '20 at 13:29
  • Nice! Thanks, sxc731. – Karl Wilbur Dec 20 '20 at 18:05
  • 1
    @KevinCox Yup, just discovered that the hard way. I wanted to copy the scrollback (including the whole output of tree /) and it hung my laptop untill oom-killer killed the terminal. – Lampe2020 Jul 10 '23 at 18:29
  • 1
    Thanks, @Lampe2020. Yeah, you have to be careful with that. An unlimited scrollback can get huge. – Karl Wilbur Jul 10 '23 at 18:37
9

script command is appropriate when you want to save a terminal session to a file and display it later. When you call script it launches your shell, and when you are done, just type in exit. Everything will be in typescript file unless you specify otherwise.

For instance,

xieerqi:$ echo "Hello, Karl Wilbur"
Hello, Karl Wilbur

xieerqi:$ date
2015年 10月 27日 星期二 11:38:15 MDT

xieerqi:$ df
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda1      115247656 80939384  28430924  75% /
none                   4        0         4   0% /sys/fs/cgroup
udev             2914832        4   2914828   1% /dev
tmpfs             585216     1100    584116   1% /run
none                5120        0      5120   0% /run/lock
none             2926072      328   2925744   1% /run/shm
none              102400       52    102348   1% /run/user

xieerqi:$ exit
Script done, file is typescript
xieerqi@eagle:~$ cat typescript 
Script started on 2015年10月27日 星期二 11时37分55秒

xieerqi:$ echo "Hello, Karl Wilbur"
Hello, Karl Wilbur

xieerqi:$ date
2015年 10月 27日 星期二 11:38:15 MDT

xieerqi:$ df
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda1      115247656 80939384  28430924  75% /
none                   4        0         4   0% /sys/fs/cgroup
udev             2914832        4   2914828   1% /dev
tmpfs             585216     1100    584116   1% /run
none                5120        0      5120   0% /run/lock
none             2926072      328   2925744   1% /run/shm
none              102400       52    102348   1% /run/user

xieerqi:$ exit

Script done on 2015年10月27日 星期二 11时38分18秒

There may be control characters in the typescript file though ,such as from the ls comand, so use cat typescript | col -b > outputfile.txt command to redirect clean output to file

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • 9
    Thanks, but this is something that needs to be done before you have collected valuable information in your terminal. I needed an after-the-fact solution. – Karl Wilbur Oct 27 '15 at 17:43
  • 1
    This is very good to know though. I'll give it a shot in the future. – Karl Wilbur Oct 27 '15 at 22:42
  • @KarlWilbur :) I'm sure you will find it useful. I use it a lot for my programming homework. There's actually a way to dump what's on screen in TTY consoles, but gnome-terminal is a bit more tricky – Sergiy Kolodyazhnyy Oct 27 '15 at 22:50
  • @SergiyKolodyazhnyy I try to implement this into my python script, but I cant exit from script doing "os.system("exit"). However it does exit from terminal and save output to file. – mtkilic Sep 11 '18 at 15:57
  • @mtkilic The script command launches a separate process, so your python script being a parent of the subprocess is waiting for child to exit. It's meant more for interactive sessions, but if you do want to record a session in Python interpreter, start the script command first and launch interpreter second. The command should also have -c flag so you could call script -c python but double check with man script. – Sergiy Kolodyazhnyy Sep 11 '18 at 17:22
  • by the way, system() isn't recommended. Look into subprocess.call() or subprocess.Popen() with shell=False flag – Sergiy Kolodyazhnyy Sep 11 '18 at 17:23
  • This is useful - even if it didn't answer the question exactly. – Seamus Oct 13 '22 at 07:37
8

You can do this after the fact as well. You're looking for Virtual Console Memory (man vcs contains more info and a couple of examples).

In your case, if you're is working on a virtual terminal trough /dev/tty1, the following snippet will let you store the backlog in a file output-file:

cat /dev/vcs1 >output-file

Note that the device numbers of tty1 and vcs1 match.

Edit: I should note that this only works for virtual terminals and not for pseudo terminals (pseudo terminals are used to implement terminal emulators like gnome-terminal, xterm etc see man pts for more info).

Thomas Cremers
  • 181
  • 1
  • 4
  • This is potentially a very useful answer for me, except for the fact that I'm unclear on how to get the correct vcs. the tty command -> /dev/pts/0. As I read your answer, that means I should be able to get my scrollback with cat /dev/vcs0. Unfortunately, there is no vcs0*. Could you please clarify? – Seamus Oct 13 '22 at 07:03