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 and try to redirect the output to a file.

$ ./ > 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?

 ./ 2> test.txt

I want both stored into the file.

 ./ 2&> test.txt

At last, I want both display and redirect to a file:

 ./ 2>&1 | tee test.txt

Interesting isn’t it?

49 Responses to “How to redirect output to a file as well as display it out”

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

  2. echo “hello” | tee -a

  3. echo “hello” | tee -a file1 file2
    echo “hello” | tee -a file1 file2 > /dev/null

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

  5. […] 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 […]

  6. Thanks! I spent hours looking for just this!

  7. Very useful to me


  8. Thanks – the redirect stderr/stdout to a file AND console is what I needed. Appreciate the post.

  9. Thanx mate !

  10. hi, what if i want out both stdout&stderr to file1 and stderr to file2 ? is it possible to do that?

  11. Nice post, bravo!

  12. 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.

  13. 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.

  14. can i redirect standard output of one command to mail body in python sendmail script

  15. Exactly what I required . Thanks a lot !

  16. Joviano Dias Says:

    this is what i needed, redirect output to file and simultaneously print to command line

  17. Joviano Dias Says:

    how do i use this in a batch file since i do not have tee?

    ./ 2>&1 | tee test.txt

  18. Joviano Dias Says:

    >>how do i use this in a batch file since i do not have tee?
    >>./ 2>&1 | tee test.txt

    Got it..
    TEE for batch files is at

  19. Jan Vodolan Says:

    # ./ | tee file.log
    if [ $? -ne 0]; then
    echo “Do something…”

    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.

  20. spot on. my requirement to save to file and display at the same time…

    Thanks a lot.

  21. 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.

  22. I finally solved my previous problem – how to handle exit code.
    # ./ | tee file.log
    if [ ${PIPESTATUS[0]} -eq 0 ]; then
    echo “Do something…”

  23. 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.

  24. Thanks, I was wondering how to do this and google sent me to your page.

  25. 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

  26. Hi,
    Very useful to me…..:)

  27. 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

  28. 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!

  29. Thanks… I remembered there was tee but I also wanted to see the output in the console at the same time.

  30. 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

  31. 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 >> newdir
    is that correct?
    Please reply soon..

  32. Hi Reshma, to extract for zipped file to a particular directory yo must issue this command

    unzip -d /path/to/your/directory

    what you’re doing up there is unzipping the files contained by “” 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

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

  34. 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
    uname -a
    ps -ef |grep -i pmon
    netstat -rn
    bdf |wc -l
    tail -100 /var/opt/resmon/log/event.log
    sar 1 5

  35. urwa shabir Says:

    thanks a million….just what i was searching

  36. Just how would you find a way to create this type of excellent masses involving commenters to your website?

  37. Hi, just wanted to mention, I enjoyed this blog post. It was helpful. Keep on posting!

  38. awesome . found wat i was looking for

  39. 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 ;)

  40. 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.

  41. 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

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

  43. 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,

  44. ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????10?????????????????????????????????????????????????????????????????????????????????????????????????100??????????????????????????????????????????????????????????????????1??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????1???????????????????????????????5????????????????????????????????????5??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

    Here is my web page … ????????????????


  46. 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.

  47. Why users still use to read news papers when in this technological world everything is accessible on net?

Leave a Reply