4

I am trying to tar the current directory and stream to stdout (ultimately to Amazon S3)...I have this command:

tar  -cf -  . 

but I get this error:

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

from what I can tell -f - means the file is to stdout, although -f /dev/stdout is probably more explicit.

does anyone know how to form the command correct?

5
  • 2
    Presumably you don't really want to stream the archive to your terminal - likely you will find that if you pipe it to another process it will be happy (for example, tar -cf - . | tar -tf -) Commented Aug 1, 2019 at 23:23
  • Have you tried tar -cf - . > /path/file.tar? /path/ can be anywhere so long as it is outside of ./
    – Jim L.
    Commented Aug 1, 2019 at 23:33
  • why wouldn't it work to stream to the terminal though? seems weird that this would fail but -f /dev/stdout would work Commented Aug 1, 2019 at 23:34
  • It does work on FreeBSD and Mac OS X, fwiw. But I can understand why the authors of tar might have thought it would be a relatively unlikely use case.
    – Jim L.
    Commented Aug 1, 2019 at 23:35
  • 1
    output to your terminal wiil be a bunch of gibberish at best or just crash your terminal program. if you want to save it on S3, output it to a file first, and save the file with aws s3 cp <filename> s3://<bucketname>/<prefixinbucket>. or do as i do and just backup with aws s3 sync ....
    – Skaperen
    Commented Aug 1, 2019 at 23:46

3 Answers 3

8

Like many programs, tar checks to see whether its output is going to a terminal device (tty) and modifies its behavior accordingly. In GNU tar, we can find the relevant code in 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?)")));
    }
}

You will find that once you connect stdout to something, it will happily write to it:

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

whereas

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

A gratuitous cat will work, also:

$ tar -cf - . | cat
1
  • you made me bing image search for "gratuitous cat" Commented Aug 2, 2019 at 21:51
0

Another tricky approach: use /dev/fd/1 or /dev/stdout as the output file:

$ tar -cf /dev/fd/1 .

However I am not sure about whether this solution is general.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .