45

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.

3
  • kitty is one terminal program that can do this, unfortunately fonts look terrible (no subpixel rendering)
    – jberryman
    Jun 21, 2019 at 17:49
  • Thanks, @jberryman. Good to know. Jun 21, 2019 at 19:24
  • XTerm can also do it: unix.stackexchange.com/a/602960
    – Quasímodo
    Aug 13, 2020 at 19:58

3 Answers 3

63

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)

10
  • 11
    Highlighting can be done more easily: Edit -> Select All.
    – egmont
    Oct 29, 2015 at 7:29
  • 4
    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. Feb 8, 2017 at 15:17
  • 2
    Warning: Don't try this with a large scollback (many megabytes) as it might hang X (happened to me).
    – Kevin Cox
    Mar 22, 2017 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?!" Mar 22, 2017 at 21:51
  • 1
    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, 2020 at 13:29
8

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

7
  • 8
    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. Oct 27, 2015 at 17:43
  • 1
    This is very good to know though. I'll give it a shot in the future. Oct 27, 2015 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 Oct 27, 2015 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, 2018 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. Sep 11, 2018 at 17:22
5

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).

1
  • 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, 2022 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.