現在のディレクトリの内容をstdoutとしてtar

現在のディレクトリの内容をstdoutとしてtar

現在のディレクトリをtarしてstdout(最終的にAmazon S3)にストリーミングしようとしています...次のコマンドがあります。

tar  -cf -  . 

ただし、次のエラーが発生します。

tar:ターミナルにアーカイブ内容の書き込みを拒否しました(-fオプションはありませんか?)tar:回復不能エラー:すぐに終了します。

私が知っている限り、-f - はファイルが標準出力であることを意味しますが、-f /dev/stdoutより明示的かもしれません。

コマンドを正しく設定する方法を知っている人はいますか?

ベストアンサー1

多くのプログラムと同様に、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?)")));
    }
}

stdoutを接続するとわかります。到着何か、それは幸せに書くでしょう:

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

しかし、

$ tar -cf - . | tar -tf -
./
./001.gif
./02.gif
./1234.gif
./34.gif

おすすめ記事