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: https://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. quarratly - November 2, 2008

txh for music)))

13. bodhi.zazen - December 3, 2008

Careful with that last command :

echo “localhost 127.0.0.1” | sudo tee filename.txt > /dev/null

will over write, and not append “filename.txt”

to append : sudo bash -c “echo “It works” >> filemane.txt’

14. Eamonn O'Connell - February 9, 2009

to append with tee, write: tee -a filename.txt

15. John-John Tedro - March 7, 2009

bodhi.zazen: You lost the point of using tee, the author mentioned if you want to write to a file which you do not own – and uses sudo with tee.
I do it all the time sins I don’t like switching to the user or root when i just need to append one file.

In light of this, it is worth mentioning that you can specify which user to create the file as by adding the switch ‘-u ‘ to sudo.

16. Divx İzle - November 17, 2009

en güzel film sitesi..

17. noon89 - May 15, 2010

hi
my problem is:
how to write the output of the following command into a file called o.txt using C++?

the command: system(line.c_str());

explaining few things :
“line” is a string variable of a command read from a file “c.txt” containing some commands like:
ls -l
ps
who

thank you for your help
god bless you
note: the code/ “the given solution” should be inside a “.cpp” file
for more details i added the function :
void printFile(string f)
{
ifstream myfile;//creating a file
ofstream ofile (“o.txt”);
string line;
myfile.open(f.c_str());//opening the selected file in the terminal

while(myfile)
{
getline(myfile,line);
system(line.c_str());

if (ofile.is_open())
{ofile <<line<<endl;//write the command in o.txt
//system(line.c_str()); I need "this output" in o.txt ???
//the output of the selected command
}
else cout << "Unable to open file";

}
}

18. travesti - June 29, 2010

ttf-gentium is now ttf-sil-gentium in the repository

19. travesti - June 29, 2010

blog enter spolow

20. sikiş izle - September 26, 2010

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

21. antalya ilaçlama - September 27, 2010

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

22. porno sikiş - September 27, 2010

Great! very handy.

23. sex sikiş - September 28, 2010

hi
my problem is:
how to write the output of the following command into a file called o.txt using C++?

24. eric - October 29, 2010

I was looking for some simple examples of using tee. This post gave me some basic understanding to start playing around! Thanks again!

25. sibel kekilli - December 10, 2010

i’m learning how to use the line command

26. izmir escort - December 26, 2010

I was looking for some simple examples of using tee. This post gave me some basic understanding to start playing around! Thanks again!

27. Use “tee” to write to files and the terminal in Ubuntu | TurboLinux Blog - June 22, 2011

[…] Here is a good tutorial show you how to Use “tee” to write to files and the terminal in Ubuntu: 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 […]

28. KodeMaaster - September 7, 2011

very helpful..keep up the good work !!

29. denise rutkowski 2011 - November 25, 2011

you have some really useful posts in here!
i have a female bodybuilding women blog check out the link below
bodybuilding blog info link click here

30. çiğli ev temizlik şirketi - November 5, 2013

ulen nedır bu cektıgımız yokmu bı acıklaması

31. nunchuk black - September 4, 2014

Group: Heavy blade Properties: High crit, keen. All
players in every faction start with this weapon and it’s actually not that bad.
You have 4 weapons of every kind – be sure to use them wisely.

32. help me write my essay - October 8, 2014

I was recommended this blog by means of my cousin. I’m not positive whether this post is written by him as nobody else realize such special
approximately my trouble. You’re wonderful! Thanks!

33. خرید کریو - December 12, 2014

very good

34. adult camtocam - March 11, 2017

adult camtocam

Using “tee” to write to files and the terminal | Ubuntu Blog

35. ทีมชาติอังกฤษ - September 7, 2019

Thank you for some other informative website. The place else could I am getting that kind
of info written in such a perfect means? I’ve a mission that I
am just now working on, and I’ve been at the glance out for such info.

36. file-permissions - Wie kann ich sed in /etc/apache2/sites-available verwenden? - January 29, 2021

[…] and zu verwenden tee […]

37. How can I use sed in /etc/apache2/sites-available? - Boot Panic - October 31, 2021

[…] and use tee […]


Leave a Reply to carthik Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: