Input/Output redirection

Everybody knows and uses output redirection to get the output of a command to a file.

With > you redirect STDOUT, with 2> you redirect STDERR and with &> you redirect both at the same time.

ls > output.txt
ls 2> error.txt
ls &> output_and_error.txt

But how do you get the output written to the screen AND to a file?

“tee” answers this question. This little program will accept text from STDIN and put in on STDOUT and write it to a file.
This is how you use it:

ls | tee output.txt

2 Comments

  1. DenRaf’s blog » Blog Archive » Terminal logging said:

    [...] tee. Explained here by my colleague raskas. This is nice, but you aren’t able to do manual [...]

  2. Raskas' blog ยป Input/Output redirection, appending said:

    [...] a previous post I wrote about output redirection of STDOUT, STDERR and both to a file. Off course you can do the [...]