Can tar be forced to exit on the first error?

5

1

By default, for many kinds of errors tar prints a message to stderr and then continues on its way -- the errors it calls "recoverable" errors, typically errors that relate to a single file or archive member, like permissions problems.

Sometimes this behavior is really obnoxious. E.g., if I'm untarring an archive and the disk is full, then I may get something like this:

tar: python-lib/PyML/classifiers/ext/_cgist.so: Wrote only 2048 of 10240 bytes
tar: python-lib/PyML/classifiers/ext/_csmo.so: Cannot write: No space left on device
tar: python-lib/PyML/classifiers/ext/_csvmodel.so: Cannot write: No space left on device
tar: python-lib/PyML/classifiers/ext/_knn.so: Cannot write: No space left on device
tar: python-lib/PyML/classifiers/ext/_libsvm.so: Cannot write: No space left on device
tar: python-lib/PyML/classifiers/ext/_mylibsvm.so: Cannot write: No space left on device
tar: python-lib/PyML/classifiers/ext/cgist.py: Cannot write: No space left on device
tar: python-lib/PyML/classifiers/ext/csmo.py: Cannot write: No space left on device

that can go on for thousands of lines in a big archive. If this happens in a script, I'd much rather tar just exited promptly so I can give a prompt error to the user.

Is there any way to force tar to exit on the first error it sees? I don't see it in a scan of tar --help. Any sane recipe for a wrapper script to accomplish this purpose would also be gratefully accepted.

Greg Price

Posted 2012-05-30T01:18:17.797

Reputation: 176

Answers

4

One way is to redirect the standard error output to /dev/full, e.g.:

tar ... 2>/dev/full

This will cause tar to fail when it tries to output a warning message into stderr.

Maxim Egorushkin

Posted 2012-05-30T01:18:17.797

Reputation: 299

+1 Nice. Never even heard of /dev/full. Useful tip. – trojanfoe – 2012-05-30T08:14:30.100

Clever idea, but it doesn't work. At least not with GNU tar 1.22, the version I have on hand. Stracing the tar process confirms that it cheerfully continues on after each write to standard error fails.

Have you actually tried this idea? What version of tar? – Greg Price – 2012-05-30T21:44:23.570

I tried with other applications. tar probably just ignores the error. There need to be a pipe so that SIGPIPE is sent to tar in case of error to knock it off. – Maxim Egorushkin – 2012-05-31T08:08:05.167