Using “tee” to write to files and the terminal May 17, 2007
Posted by Carthik in administration, commands, snippets, ubuntu.trackback
The utility “tee” is very useful for plumbing on the command line. Curiously enough, it gets its name from the T-splitter used in plumbing, shown below:

Say you want to run a command, and be able to see the output and errors on the screen, and be able to save them to a file. That’s where tee comes in, so you could do a:
$sudo apt-get upgrade 2>&1 | tee ~/apt-get.log
…to run apt-get upgrade and save the output and errors to the file apt-get.log in your home directory.
Purists please excuse the following explanation
The “2″ refers to the “tap” from which the errors pour out (called stderr). The “1″ refers to the tap from which the output pours out. The 2>&1 makes the errors to also pour out of the output tap. So then stderr goes to stdout. The pipe “|” redirects the output to tee. Now tee splits the output of the previous command two ways, and puts it both in ~/apt-get.log and in the standard output, which happens to be your screen/terminal.
tee is also handy when you have a small permissions problem. Say you want to write some text to a file “filename.txt” owned by the “root” user - you would just use something like:
$sudo vim filename.txt
and then change the file, right?
Now suppose you want to echo what you write, and write the file, all in one command, you then can use tee thusly:
$echo "localhost 127.0.0.1" | sudo tee filename.txt > /dev/null
This would write the text “localhost 127.0.0.1″ to the file filename.txt which is not owned by you. The output of tee itself will go to /dev/null (nothingness) instead of the standard output, which is your terminal.
Don’t lose sleep over this, but someday it will come handy, and when you can figure out why the “sudo” does not apply after the “>” in your command, remember tee and come back here.
For all your command line redirecting needs, and to learn to wield pipes and tees like nunchakus read this excellent page.







There’s a typo in the second command. You meant a pipe, not a redirect.
Fixed, thanks to no one in particular
tee hee, sorry couldent help my self.
I just need such a tool to see progress of my experiments in terminal and get results saved to a file
No copy/paste anymore, thanks
Great! very handy.
Thank you a lot
great, very helpful tip, thanks a lot
You’re all very welcome. If you take it up in bite-sized chunks, you’ll be real good with the command line very soon - that has been my personal experience.
Another alternative is: sudo sh -c “echo localhost 127.0.0.1 > filename.txt”
[...] Muchas veces he visto en howtos que a la hora de pipear la salida de un comando en consola usaban el comando tee. Y yo siempre pasaba de él y usaba la redirección a fichero con “>”, pero ahora leo y entiendo por que lo usan: http://ubuntu.wordpress.com/2007/05/17/using-tee-to-write-to-files-and-the-terminal/ [...]
i’m learning how to use the line command
plz tell me how to create a new file and name it cs332 in home directory ?? and contains it with name an e-mail ??????
The useful download backgammon rode this road hesitantly. One confidential home scratched this backgammon downloads mechanically. The japanese download backgammon games lighted due to an uniform example. I mean, this spanish minute shamefully proofread below the overwhelming death. It’s shallow to be stared! This download backgammon software is grimily written.
download backgammon games - http://www.bgonnet.com/
Payday Loans No Fax
PaydayEZ.com sponsors rapid loan approvals with absolutely no credit report necessary; citizens renting in in the fabulous state of Alabama may get approved on the Internet.