63

I am using:

user@unknown:~$ sudo command -option > log

to save the results of "command" to the file "log", but I'd like to also get the result on the terminal, is this possible?

I am using ubuntu 10.04 lts.

fazpas
  • 1,583
  • 3
  • 18
  • 18
  • 4
    Possible duplicate of [Echoing output to file while seeing it in console in the same time](http://superuser.com/questions/694064/echoing-output-to-file-while-seeing-it-in-console-in-the-same-time) – 8bittree Nov 24 '15 at 18:09
  • @8bittree This post is 3 years older and has more views and votes than the other question. The other question should be closed as a duplicate instead. – Excellll Nov 30 '15 at 15:28
  • @Excellll [Age is irrelevant](http://meta.stackexchange.com/questions/55251/opinions-on-closing-an-older-question-as-a-duplicate-of-a-newer-question). I flagged this as the duplicate because I believe that the other has a [superior](http://superuser.com/a/694098/302463) answer. – 8bittree Nov 30 '15 at 18:05
  • There's an excellent overview of the available options in [this AskUbuntu answer](https://askubuntu.com/a/731237/23900). – waldyrious Mar 19 '18 at 16:51

3 Answers3

84

Use tee.

user@unknown:~$ sudo command -option | tee log
Benjamin Bannier
  • 16,044
  • 3
  • 43
  • 41
  • 6
    Easy to remember if one thinks of the output being split by an upper case "T". E.g. https://en.wikipedia.org/wiki/Tee_(command)#/media/File:Tee.svg – Pocketsand Mar 27 '17 at 16:12
  • .. | tee -a log To keep appending to the existing log. Otherwise every new line overwrites the log. – ZixuTech Mar 30 '20 at 04:24
13

The command you're looking for is 'tee' which makes a data connection similar to a pipe-tee. it sends data two ways. So

sudo command -option | tee log

would tee the command output to both the file 'log' and to stdout, in this case, your terminal.

JRobert
  • 6,744
  • 24
  • 28
6

You can also use script [ http://linux.die.net/man/1/script ] to capture everything that occurs in your terminal session.

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

charlesbridge
  • 1,249
  • 10
  • 13