8

I have a some empty html files that i want to write. I am trying this

cat account_settings/account_settings.html >> assets/assets.html, users/users.html

to attempt to write to the files assets.html and users.html.

How can I write to multiple files?

1
  • 3
    In zsh, cat foo > a > b works.
    – muru
    Nov 16, 2016 at 1:30

2 Answers 2

15

You can use the tee command

NAME
       tee - read from standard input and write to standard output and files

e.g.

cat account_settings/account_settings.html | tee -a assets/assets.html users/users.html

or (using input redirection)

tee -a assets/assets.html users/users.html < account_settings/account_settings.html

As noted in the manual page, tee also outputs the contents to the terminal (standard output) - if you don't want to see that, redirect stdout to null

tee -a assets/assets.html users/users.html < account_settings/account_settings.html > /dev/null
6
  • Can tee -a also be used when deleting contents truncate -s 0 tee -a assets/assets.html users/users.html
    – Le Qs
    Nov 16, 2016 at 1:22
  • @LeQs Are you trying to erase the files before writing to them? Nov 16, 2016 at 1:24
  • @ChaiT.Rex Its just a random thought that came to mind unrelated to my original question.
    – Le Qs
    Nov 16, 2016 at 1:25
  • @LeQs truncating and appending seem incompatible - what exactly are you trying to achieve? If you want to truncate multiple files, you can simply give multiple filename arguments to the truncate command e.g. truncate -s0 foo bar baz. You could do something like : | tee foo bar baz I guess. Nov 16, 2016 at 1:28
  • 1
    @LeQs does truncate -s0 users/users.html credit/credit.html not do what you want in that case? Nov 16, 2016 at 1:35
6

Just for-loop over the list of files you want

for file in assets/assets.html users/users.html
do
    cat account_settings/account_settings.html >> "$file"
done

You must log in to answer this question.

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