将当前目录中的 tar 内容复制到标准输出

Ale*_*lls 1 tar stdout

我正在尝试 tar 当前目录并流式传输到标准输出(最终到 Amazon S3)...我有这个命令:

tar  -cf -  . 
Run Code Online (Sandbox Code Playgroud)

但我收到此错误:

tar:拒绝将存档内容写入终端(缺少 -f 选项?)tar:错误不可恢复:现在退出

据我所知 -f - 表示该文件是标准输出,尽管-f /dev/stdout可能更明确。

有谁知道如何正确地形成命令?

ste*_*ver 6

像许多程序一样,tar检查其输出是否发送到终端设备 (tty) 并相应地修改其行为。在 GNU 中tar,我们可以在buffer.c以下位置找到相关代码:

static void
check_tty (enum access_mode mode)
{
  /* Refuse to read archive from and write it to a tty. */
  if (strcmp (archive_name_array[0], "-") == 0
      && isatty (mode == ACCESS_READ ? STDIN_FILENO : STDOUT_FILENO))
    {
      FATAL_ERROR ((0, 0,
                    mode == ACCESS_READ
                    ? _("Refusing to read archive contents from terminal "
                        "(missing -f option?)")
                    : _("Refusing to write archive contents to terminal "
                        "(missing -f option?)")));
    }
}
Run Code Online (Sandbox Code Playgroud)

你会发现,一旦你将 stdout 连接某个东西,它就会很高兴地向它写入:

$ tar -cf- .
tar: Refusing to write archive contents to terminal (missing -f option?)
tar: Error is not recoverable: exiting now
Run Code Online (Sandbox Code Playgroud)

然而

$ tar -cf - . | tar -tf -
./
./001.gif
./02.gif
./1234.gif
./34.gif
Run Code Online (Sandbox Code Playgroud)