On OSX I'm trying to create a tarball of a directory and on the fly encrypt it with gpg and keeps the output of the tar messages in a log file for later analysis.
While the tar+gpg on the fly it's pretty easy
tar zcvf - foo | gpg -e -r foo@bar.com -o foo.tgz.gpg
so far I'm failing to log the output of tar (and maybe the gpg one in case) to a log file.
I tried several combination of
tar | gpg | tee
tar | gpg 2>&1 >(tee)
tar | gpg > file.log
any help here?
Cheers Davide
2> file.log
should achieve it.
If you want to tee
the stderr
stream and also keep it going to the original destination, you can achieve that with
2> >(tee file.log >&2)
In your example:
tar zcvf - foo 2> >(tee file.log >&2) | gpg -e -r foo@bar.com -o foo.tgz.gpg
Your attempts don't succeed because the piping (|) only forwards stdout
of the previous command to the stdin
of the current one. The current command (gpg) doesn't have access to the stderr
of the previous command (tar
). That stderr
has already gone to its destination (your terminal, most likely).
2> >(command )
pipes (quite literally >()
create an unnamed, OS-level pipe underneath) stderr
to stdin
of command
. Since command
is a child process, its stderr
(#2) descriptor will point to the same file as the stderr
of the parent process.