4

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.

    1 Answer 1

    7

    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.

    1

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.