How to redirect output to a file as well as display it out
December 6th, 2007 mysurface Posted in pipeline, python, tee, Text Manipulation | Hits: 371008 | 49 Comments »
To redirect standard output to a file is easy, you just need to use the redirection symbol, for example:
echo "hello world" > test.txt
But what if I want to display it out as well as store into a file?
Answer: tee
echo "hello world" | tee test.txt
Okay it seems very easy, how about append?
To append the standard output to a file, you do this:
echo"hello world" >> test.txt
Append to file and display it out as well?
echo"hello world" | tee -a test.txt
Okay, how about dealing with standard output(stdout) and standard error(stderr)?
There are two different output stream, one is stdout and another one is stderr. Normal print usually goes to stdout and error related message will goes to stderr. Lets make a simple python script to print 1 line to stdout and 1 line to stderr.
#!/usr/bin/env python
import sys
sys.stdout.write("I am stdout\n")
sys.stderr.write("I am stderr\n")
Ok, lets save the python script as sout.py and try to redirect the output to a file.
$ ./sout.py > test.txt
I am stderr
Standard output is redirect to test.txt but stderr is print out.
What if I want stderr to be redirect and display the stdout?
./sout.py 2> test.txt
I want both stored into the file.
./sout.py 2&> test.txt
At last, I want both display and redirect to a file:
./sout.py 2>&1 | tee test.txt
Interesting isn’t it?
December 13th, 2007 at 3:12 pm
Wot if I want to redirect same output to two files without displaying it and without using like cat first file n then copy it to second?
October 21st, 2008 at 12:48 am
echo “hello” | tee -a
October 21st, 2008 at 12:49 am
echo “hello” | tee -a file1 file2
or
echo “hello” | tee -a file1 file2 > /dev/null
November 19th, 2008 at 12:23 am
I was looking for a way how to look at some php script output on screen but also to have whole output saved in a file. Your post is exactly what I needed. Great thanks for sharing this with us. :)
November 19th, 2008 at 12:47 am
[…] se deÅ¡ava, umesto da mi ceo izlaz stoji u nekom tamo log fajlu. No, zahvaljući unix tee komandi i ovom postu, danas sam nauÄio da mogu imati […]
April 18th, 2009 at 12:30 pm
Thanks! I spent hours looking for just this!
August 17th, 2009 at 7:12 pm
Very useful to me
Thanks
October 7th, 2009 at 8:51 pm
Thanks – the redirect stderr/stdout to a file AND console is what I needed. Appreciate the post.
December 4th, 2009 at 4:08 pm
Thanx mate !
January 15th, 2010 at 11:23 am
hi, what if i want out both stdout&stderr to file1 and stderr to file2 ? is it possible to do that?
February 3rd, 2010 at 8:55 am
Nice post, bravo!
March 3rd, 2010 at 11:37 pm
Have you ever piped output through firefox to a file? Would use lynx but its an unsupported browser for the webpage I’m trying to copy down.
March 30th, 2010 at 4:13 am
Erin – do you want to save the HTML, or text format the way –dump does it? Or do you want to save a screenshot? I don’t know if Firefox lets you output the page it sees like that. If you did it, let me know.
May 19th, 2010 at 12:08 pm
can i redirect standard output of one command to mail body in python sendmail script
May 20th, 2010 at 8:19 pm
Exactly what I required . Thanks a lot !
July 20th, 2010 at 2:03 pm
this is what i needed, redirect output to file and simultaneously print to command line
Thanks!
July 20th, 2010 at 2:05 pm
how do i use this in a batch file since i do not have tee?
./sout.py 2>&1 | tee test.txt
July 20th, 2010 at 2:10 pm
>>how do i use this in a batch file since i do not have tee?
>>./sout.py 2>&1 | tee test.txt
Got it..
TEE for batch files is at http://www.robvanderwoude.com/tee.php
July 28th, 2010 at 8:28 pm
Hello,
# ./script.sh | tee file.log
if [ $? -ne 0]; then
echo “Do something…”
fi
I need print log to console, save it to log AND handle the exit code !
Unfortunatelly, ‘|tee file’ overwrite exit code of my script. So, “do something” part will never happen. Any idea how to solve this ? Many thanks.
W
August 11th, 2010 at 10:34 am
spot on. my requirement to save to file and display at the same time…
Thanks a lot.
February 9th, 2011 at 1:49 pm
This web page is really a stroll-through for the entire info you wanted about this and didn’t know who to ask. Glimpse here, and you’ll definitely discover it.
February 25th, 2011 at 2:34 pm
I finally solved my previous problem – how to handle exit code.
# ./script.sh | tee file.log
if [ ${PIPESTATUS[0]} -eq 0 ]; then
echo “Do something…”
fi
August 9th, 2011 at 3:43 am
It also works if you need to capture the output from a telnet session to a file “telnet soandso | tee /anydir/output.txt”. In Unix and Linux, most Terminals don’t have a “Capture” function as they do in that other o/s. This does it.
September 5th, 2011 at 4:53 am
Thanks, I was wondering how to do this and google sent me to your page.
September 5th, 2011 at 9:18 pm
Hi,
How do I save something in a script file, then get it to display on the screen as displaying the last word in another file?
I can get all words to display on the screen and in a separate file, and I can get the last word to display on the screen and all of them to display in the file, but I cant seem to get them all to display on the screen and only one on the file. I’ve played around with tee and tail…
sort $input | tee outputfile1.txt displays all the words in the file and on the screen
November 8th, 2011 at 4:07 pm
Hi,
Very useful to me…..:)
Thanks
November 19th, 2011 at 8:57 pm
how to do it when you want to write your program
I have written a program using fork and pipes that performs 3 commands using 2 pipes but now i want to store it in a file i know it is like this ls | sort | wc > text
but how to to ii using exec?
exec(“wc”,”wc”,”>”,”text”,NULL); this didn’t work
January 18th, 2012 at 6:30 pm
Very nice post. I just stumbled upon your weblog and wanted to say that I have really loved browsing your blog posts. In any case I will be subscribing in your rss feed and I am hoping you write again soon!
February 17th, 2012 at 10:27 am
Thanks… I remembered there was tee but I also wanted to see the output in the console at the same time.
April 5th, 2012 at 2:59 am
I am trying to execute C Program
For that I have to give file as input and output should be stored in output.txt file
I am Using the following command
AIQ1 output.txt
Output in output.txt file is
cat output.txt
Program to distinguish input character
Enter any character or Number :
You Entered a Number
But I need output like this
Program to distinguish input character
Enter any character or Number :3
You Entered a Number
Here problem is The input I am giving from file is not storing in output.txt
Please solve my problem
May 24th, 2012 at 2:03 pm
Hi,
I am new to the linux and i wanted to redirect the unzipped output to an another directory.Can you please tell me what is the command to perform the above mentioned operation.
I tried the following command.
$unzip zippedfile.zip >> newdir
is that correct?
Please reply soon..
October 1st, 2012 at 2:15 am
Hi Reshma, to extract for zipped file to a particular directory yo must issue this command
unzip zippedfile.zip -d /path/to/your/directory
what you’re doing up there is unzipping the files contained by “zippedfile.zip” to the current directory and then append the output, (output for this matter is what a program prints in the screen), to a file called newdir.
Yes it actually creates a “newdir” text file if it not exist yet
December 3rd, 2012 at 4:13 pm
What if I would like to have redirection of stderr to file a, redirection of stdout to file b and display both on the screen?
December 11th, 2012 at 11:50 pm
Hello,
I need a help to redirect below mentioned commands output to my home directory or create a .txt file.How to redirect plz let me know.
below are the commands
w
date
hostname
uname -a
ps -ef |grep -i pmon
netstat -rn
bdf
bdf |wc -l
tail -100 /var/opt/resmon/log/event.log
sar 1 5
March 12th, 2013 at 5:26 am
thanks a million….just what i was searching
April 9th, 2013 at 1:05 pm
Just how would you find a way to create this type of excellent masses involving commenters to your website?
June 7th, 2013 at 10:37 pm
Hi, just wanted to mention, I enjoyed this blog post. It was helpful. Keep on posting!
May 10th, 2014 at 11:44 pm
awesome . found wat i was looking for
October 19th, 2014 at 10:44 am
I would like to thank you for the efforts you’ve put in penning this site.
I really hope to view the same high-grade blog posts from you in the future as well.
In truth, your creative writing abilities has encouraged me to get my own site now ;)
October 19th, 2014 at 4:25 pm
My spouse and I stumbled over here different web page and thought I should check things out.
I like what I see so i am just following you. Look forward to looking into your web page
for a second time.
November 25th, 2014 at 4:22 am
I visited various sites except the auxio quality for audio sohgs existing
at this site is in fzct superb.
Lookk at my web blog :: detectores de humo
April 8th, 2015 at 3:14 am
This helps to record the output, and error. I need to have the inputs to the script (readline from screen, user input) and record it, for logging/debugging purposes. Is there another command for that?
May 10th, 2015 at 1:56 pm
Regardless of the apparel ” up ” and also behaviours.
But the silent treatment – or worse negative communication – leads 85% of employees to
be dissatisfied in the workplace after only six months on the job.
Here are some off the tips and suggestions that can be
ued to motivate the calling agents:.
Feel fre to visit my web site; Motivation, Lavern,
October 30th, 2015 at 2:23 pm
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????10?????????????????????????????????????????????????????????????????????????????????????????????????100??????????????????????????????????????????????????????????????????1??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????1???????????????????????????????5????????????????????????????????????5??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
Here is my web page … ????????????????
December 21st, 2015 at 12:18 am
[…] ?? : https://linux.byexamples.com/archives/349/how-to-redirect-output-to-a-file-as-well-as-display-it-out/ […]
December 21st, 2015 at 12:19 am
[…] ?? : https://linux.byexamples.com/archives/349/how-to-redirect-output-to-a-file-as-well-as-display-it-out/ […]
December 5th, 2017 at 4:40 am
NECESITO UN PARAGOLPE DELANTERO PARA GOL NATION 2004.
December 21st, 2018 at 4:41 am
We’re a gaggle of volunteers and opening a new
scheme in our community. Your site provided us with valuable
information to work on. You have performed an impressive activity and our
whole neighborhood shall be thankful to you.
January 23rd, 2020 at 1:06 am
Why users still use to read news papers when in this technological world everything is accessible on net?