These are commands I type in the terminal

echo -e "First Line" | tee ~/output.log
echo -e "Second Line" | tee ~/output.log

When I look in the file output.log I only see 'Second Line'. How can I make sure that tee appends (rather than wipes out the file)?

I'd like to be able to see this in the file:

First Line
Second Line

Should I be approaching this another way?


1 Answer 1

echo -e "First Line" | tee ~/output.log
echo -e "Second Line" | tee -a ~/output.log

From man tee:

   Copy standard input to each FILE, and also to standard output.

   -a, --append
          append to the given FILEs, do not overwrite

Note: Using -a still creates the file mentioned.

  • 19
    For the benefit of the searchers, the -a modifier is for 'append', or add to the end. Without -a, the tee command overwrites the file.
    – chili555
    Aug 5, 2016 at 16:20
  • 1
    Does tee still create the file if it doesn't exist when the "-a" option is included?
    – Bryson S.
    Aug 11, 2017 at 20:10
  • @chili555: is it possible appending to the beginning of file, not to the end, and doesn't overwrite a file? Thanks. Jan 25, 2018 at 7:04
  • 3
    @СашаЧерных None that I am aware of. That sounds like a great subject for a new question!
    – chili555
    Jan 25, 2018 at 14:13
  • 2
    @Саша Черных 'cat source.file destination.file | tee destination.file' will append source.file at the beginning of destination.file. The only catch with this approach is that tee will print to stdout both files.
    – Bruno9779
    Apr 16, 2018 at 17:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .