211

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?

Nakilon
  • 109
  • 4
Bluebeep
  • 2,271

1 Answers1

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

Tom S
  • 1
user4556274
  • 4,877
  • 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 05 '16 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 '17 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 '18 at 07:04
  • 3
    @СашаЧерных None that I am aware of. That sounds like a great subject for a new question! – chili555 Jan 25 '18 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 '18 at 17:37
  • Is it possible to have tee append to a new line? I.e., to append after first inserting a line break to ensure subsequent outputs are each on new lines. – wes Oct 17 '21 at 21:40
  • @wes printf '\n' | tee -a ~/output.log – Stephen Quan Feb 23 '23 at 02:12