How to redirect the standard output (stdout) and the standard error (stderr) to multiple files

In this article I will show you how to redirect the standard output and the standard error to multiple files, with tee.

How to redirect the standard output (stdout) to multiple files:

Using tee: command | tee file1 file2

$ echo "hello" | tee f1 f2
hello

With this tee trick, you can also create multiple copies to files:

This command duplicates the /etc/passwd file to /home/razvan/pwbackup and /root/pwbackup

$ cat /etc/passwd | tee /home/razvan/pwbackup /root/pwbackup

How to redirect the standard error (stderr) to multiple files:

The tee command only accepts the standard output. To redirect the stderr to multiple files, redirect the stderr to stdout first and than use tee: command 2>&1 | tee file1 file2

$ gcc 2>&1 | tee f1 f2
gcc: fatal error: no input files
compilation terminated.

Liked it? Take a second to support Geekster on Patreon!
Tagged with: , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Support LinuxG via Patreon
Support LinuxG.net on Patreon!
Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!