Linux – Can tar be forced to exit on the first error

gnulinuxshelltarunix

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.

Best Answer

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.

Related Question