35

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.

51

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)

| improve this answer | |
  • 8
    Highlighting can be done more easily: Edit -> Select All. – egmont Oct 29 '15 at 7:29
  • 3
    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 8 '17 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 '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
4

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

| improve this answer | |
  • 7
    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

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.