jump to navigation

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:
T-splitter used in Plumbing

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.

Comments»

1. no one in particular - May 17, 2007

There’s a typo in the second command. You meant a pipe, not a redirect.

2. carthik - May 17, 2007

Fixed, thanks to no one in particular :)

3. earobinson - May 17, 2007

tee hee, sorry couldent help my self.

4. bartaz - May 17, 2007

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

5. Steve - May 17, 2007

Great! very handy.

Thank you a lot

6. towsonu2003 - May 17, 2007

great, very helpful tip, thanks a lot :)

7. carthik - May 17, 2007

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.

8. Euan - May 19, 2007

Another alternative is: sudo sh -c “echo localhost 127.0.0.1 > filename.txt”

9. Usando tee para escribir en ficheros desde una terminal at moeblog - May 21, 2007

[...] 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/ [...]

10. Ghada - September 28, 2007

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 ??????

11. download backgammon games - November 25, 2007

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/

12. Payday Loans No Fax - January 8, 2008

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.