How to show output on terminal and save to a file at the same time?

51

12

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

Posted 2010-07-01T18:03:05.927

Reputation: 1 353

4

Possible duplicate of Echoing output to file while seeing it in console in the same time

– 8bittree – 2015-11-24T18:09:41.720

@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 – 2015-11-30T15:28:48.497

@Excellll Age is irrelevant. I flagged this as the duplicate because I believe that the other has a superior answer.

– 8bittree – 2015-11-30T18:05:51.057

There's an excellent overview of the available options in this AskUbuntu answer.

– waldyrious – 2018-03-19T16:51:28.137

Answers

67

Use tee.

user@unknown:~$ sudo command -option | tee log

Benjamin Bannier

Posted 2010-07-01T18:03:05.927

Reputation: 13 999

4

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 – 2017-03-27T16:12:47.307

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

Posted 2010-07-01T18:03:05.927

Reputation: 6 128

5

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

Posted 2010-07-01T18:03:05.927

Reputation: 1 119