What's the best way to save your terminal output to a file?

  • I'm not talking about redirection command > file.txt
  • Not the history history > file.txt , I need the full terminal text
  • Not with hotkeys !

Something like terminal_text > file.txt

Best Answer

You can use script . It will basically save everything printed on the terminal in that script session.

From man script .

script makes a typescript of everything printed on your terminal.
It is useful for students who need a hardcopy record of an
interactive session as proof of an assignment, as the typescript file
can be printed out later with lpr(1).

You can start a script session by just typing script in the terminal, all the subsequent commands and their outputs will all be saved in a file named typescript in the current directory. You can save the result to a different file too by just starting script like.

script output.txt

To logout of the script session (stop saving the contents), just type exit .

Let me give you an example

$ script output.txt
Script started, file is output.txt

$ ls
output.txt  testfile.txt  foo.txt

$ exit
Script done, file is output.txt

Now if i read the file.

$ cat output.txt

Script started on Mon 20 Apr 2015 08:00:14 AM BDT
$ ls
output.txt  testfile.txt  foo.txt
$ exit

Script done on Mon 20 Apr 2015 08:00:21 AM BDT

script also has many options e.g. running quietly -q ( --quiet ) without showing/saving program messages, it can also run a specific command -c ( --command ) rather than a session, it also has many other options. Check man script to get more ideas.