0

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?

  • 1
    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 -) – steeldriver Aug 1 at 23:23
  • Have you tried tar -cf - . > /path/file.tar? /path/ can be anywhere so long as it is outside of ./ – Jim L. Aug 1 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 – Alexander Mills Aug 1 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. Aug 1 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 Aug 1 at 23:46
4

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
1

A gratuitous cat will work, also:

$ tar -cf - . | cat
  • you made me bing image search for "gratuitous cat" – Alexander Mills Aug 2 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.