创建一个 tar 球,即时加密并在日志文件中保存 tar 消息

creating a tarball, encrypt it on the fly and keeps tar messages in a log file

在 OSX 我正在尝试创建一个 tar 目录球并即时加密 它与 gpg 并将 tar 消息的输出保存在日志文件中 稍后分析。

虽然 tar+gpg 在运行时非常简单

tar zcvf - foo | gpg -e -r foo@bar.com -o foo.tgz.gpg

到目前为止,我没有记录 tar 的输出(也许 gpg 一个在 case) 到日志文件。

我尝试了几种组合

tar | gpg | tee
tar | gpg 2>&1 >(tee)
tar | gpg > file.log

有什么帮助吗?

干杯 大卫

2> file.log 应该可以实现。 如果你想 tee stderr 流并保持它到原始目的地,你可以用

实现

2> >(tee file.log >&2)

在你的例子中:

tar zcvf - foo 2> >(tee file.log >&2) | gpg -e -r foo@bar.com -o foo.tgz.gpg

您的尝试没有成功,因为管道 (|) 仅将前一个命令的 stdout 转发到当前命令的 stdin。当前命令 (gpg) 无权访问前一个命令 (tar) 的 stderrstderr 已经到达目的地(很可能是您的终端)。

2> >(command ) 管道(字面意思是 >() 在下面创建一个未命名的 OS 级管道)stderrstdin of command .由于 command 是子进程,其 stderr (#2) 描述符将指向与父进程的 stderr 相同的文件。

问题是当在行尾使用 2> 时,您仅重定向第二个命令 (gpg) stderr。如果你想(看起来)重定向 tar 命令 stderr,你应该在管道之前添加 2> logfile,如下所示:

tar zcvf - foo 2> tar.log | gpg -e -r foo@bar.com -o foo.tgz.gpg