28

How can I write the same content to many text files by using cat or echo in only one command?

For example I want to write "hello" to file1 and file2. I tried:

echo "hello" >> file1 file2

but it didn't work. How can I do it?

45

Use tee to read from standard input and write to standard output and files.

echo "hello" | tee -a file1 file2

-a is the short (and portable/standard) for GNU tee's --append

  • 2
    Thanks, can flip it around for heredoc: tee -a aaa bbb ccc << EOF contents ... EOF. – virtualeyes Nov 8 '16 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.