5

Possible Duplicate:
sudo tar cvpzf exiting with failure due to previous error

When I run the command

tar -cvfz backup.tar.gz somedirectory

I get the following sterr

tar: Exiting with failure status due to previous errors

However when I omit the preceding hyphen from -cvfz everything seems to work just fine. Is there a known reason for why the hyphen is preventing me from tar'ing my files?

user784637
  • 10,955
  • 1
    What are the previous errors? – psusi Jan 09 '12 at 03:37
  • There's no errors before that message, it's some verbose stdout of all the files in that directory – user784637 Jan 09 '12 at 03:43
  • have you seen this ? http://askubuntu.com/questions/78043/sudo-tar-cvpzf-exiting-with-failure-due-to-previous-error – viyyer Jan 09 '12 at 03:44
  • Yeah I read that before I posted this question because it didn't mention the hyphen. Someone mentioned the hyphen on superuser but it was only as an afterthought. I wanted a more detailed explanation of why including the hyphen causes problems. – user784637 Jan 09 '12 at 03:47

1 Answers1

13

I'm not sure why tar cvfz works, when accourding to a quick scan of the man page and my experience, a hyphen is standard practice. Maybe it's a BSD compatibility thing (like ps).

At any rate, your syntax is incorrect. -f takes the next argument as the filename to compress to. In this case, that filename is z, which isn't what you expected. Here's a sample of the output so you can see what I mean:

$ tar -cvfz test.tar.gz scott
tar: test.tar.gz: Cannot stat: No such file or directory
scott/
scott/netx/
scott/netx/locks/
scott/netx/locks/netx_running
tar: Exiting with failure status due to previous errors

An ls shows a file called z. The file command reveals that it's an uncompressed tar archive. It's uncompressed because the -z argument wasn't passed. And the error message came from trying to add the non-existant file test.tar.gz to the archive.

Simply reorder the options and you'll be OK:

tar -czvf test.tar.gz some/directory

If you always make the -f the last argument, you'll be fine. By the way, remember that short options (one-letter options) that take arguments don't normally require a space between the option and the argument.

EDIT: By the way, if the form without the hyphen is a BSD-compatibility thing or something like that, then it's likely that the way that tar is invoked in such an environment is different, and doesn't require the f flag to specify the input file, but works more like cp or something. But this is just speculation based on comparison with ps (q.v.).