I am unpacking tar files with the --keep-old-files option so that I don't clobber files that are already there. The annoying thing is that this causes tar to produce an error code #2 (fatal error) when a file is already there.

The reason it's important to me that this should not be considered an error is because my script logs when a file is successfully unpacked and then uses this log in order to not re-unpack that tar file.

I could just ignore this error completely, not log, and trust that no files will be overwritten but there are two problems with this: it generates a lot of error messages, and there are many files, so it's inefficient for me to open tar once for every file.

Is there any way I can use --keep-old-files, ignore the errors generated by existing files, and not ignore other errors?

1 Answer 1


This is apparently by design - from the documentation:

To be even more cautious and prevent existing files from being replaced, use the --keep-old-files (-k) option. It causes tar to refuse to replace or update a file that already exists, i.e., a file with the same name as an archive member prevents extraction of that archive member. Instead, it reports an error.

The git repository does contain a patch that introduces a --skip-old-files parameter, so this could be solved in a future GNU tar version:

+A new option --skip-old-files is introduced, which acts exactly as
+--keep-old-files, except that it does not treat existing files as
+errors.  Instead it just silently skips them.  An additional level of
+verbosity can be obtained by using the option --warning=existing-file
+together with this option.

The patch hasn't made it into an official release yet, though.


You must log in to answer this question.

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