Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Okay. If I wanted to redirect the output of a program to a file, I'd do something like this

prog > file

If I wanted to redirect both stdout and stderr to that file, then I'd do

prog > file 2>&1

This is all well and good if you want the output to go to the file. But what if you want the output to go to the file and yet still go to stdout/stderr? So, the output is saved in the file, but you can still see it on the console as the program is running. Is there a way to do that? And if so, how?

share|improve this question
add comment

1 Answer

up vote 32 down vote accepted

tee exists for this purpose; it takes a filename argument and writes the data it reads from stdin to both stdout and the file:

$ prog 2>&1 | tee file
share|improve this answer
6  
And for extra bash 4 cuteness: prog |& tee file. –  Tobu Nov 19 '10 at 1:49
1  
You can also replace 'file' with a named pipe... –  Kevin Cantu Nov 30 '10 at 2:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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