3

I can easily redirect both standard and error output of a bash script to the file, while discarding an output to console with simple exec statement:

exec &>>/var/log/backup.log

But how do I use exec to write to two files, appending to first and rewriting the second? Probably some tee magic should be used. And also some way to mute the console should be found.

The reason for this is a backup script of mine. I want to append to the main log the events of latest backup and just write only these events to the current backup log (clearing its previous content), which should be rewritten each backup session.

6

It can be done, you need process substitution. Redirect the streams into a subprocess that calls tee and redirects the rest into the overwritten file.

exec &> >(tee -a backup.log > overwritten.log)

Note that this will overwrite the file only once in entire script, because the stream remains open until the script exits or another redirect is established.

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.