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: https://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/
txh for music)))
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’
to append with tee, write: tee -a filename.txt
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.
en güzel film sitesi..
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";
}
}
ttf-gentium is now ttf-sil-gentium in the repository
blog enter spolow
Another alternative is: sudo sh -c “echo localhost 127.0.0.1 > filename.txt”
There’s a typo in the second command. You meant a pipe, not a redirect.
Great! very handy.
hi
my problem is:
how to write the output of the following command into a file called o.txt using C++?
I was looking for some simple examples of using tee. This post gave me some basic understanding to start playing around! Thanks again!
i’m learning how to use the line command
I was looking for some simple examples of using tee. This post gave me some basic understanding to start playing around! Thanks again!
[…] 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 […]
very helpful..keep up the good work !!
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
ulen nedır bu cektıgımız yokmu bı acıklaması
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.
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!
very good
adult camtocam
Using “tee” to write to files and the terminal | Ubuntu Blog
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.
[…] and zu verwenden tee […]
[…] and use tee […]