Next: Overwrite Old Files, Up: Writing [Contents][Index]

#### Options Controlling the Overwriting of Existing Files

When extracting files, if `tar` discovers that the extracted file already exists, it normally replaces the file by removing it before extracting it, to prevent confusion in the presence of hard or symbolic links. (If the existing file is a symbolic link, it is removed, not followed.) However, if a directory cannot be removed because it is nonempty, `tar` normally overwrites its metadata (ownership, permission, etc.). The `--overwrite-dir` option enables this default behavior. To be more cautious and preserve the metadata of such a directory, use the `--no-overwrite-dir` option.

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. For example:

    $ ls
    blues
    $ tar -x -k -f archive.tar
    tar: blues: Cannot open: File exists
    tar: Exiting with failure status due to previous errors

If you wish to preserve old files untouched, but don’t want `tar` to treat them as errors, use the `--skip-old-files` option. This option causes `tar` to silently skip extracting over existing files.

To be more aggressive about altering existing files, use the `--overwrite` option. It causes `tar` to overwrite existing files and to follow existing symbolic links when extracting.

Some people argue that GNU `tar` should not hesitate to overwrite files with other files when extracting. When extracting a `tar` archive, they expect to see a faithful copy of the state of the file system when the archive was created. It is debatable that this would always be a proper behavior. For example, suppose one has an archive in which `usr/local` is a link to `usr/local2`. Since then, maybe the site removed the link and renamed the whole hierarchy from `/usr/local2` to `/usr/local`. Such things happen all the time. I guess it would not be welcome at all that GNU `tar` removes the whole hierarchy just to make room for the link to be reinstated (unless it also simultaneously restores the full `/usr/local2`, of course!) GNU `tar` is indeed able to remove a whole hierarchy to reestablish a symbolic link, for example, but only if `--recursive-unlink` is specified to allow this behavior. In any case, single files are silently removed.

Finally, the `--unlink-first` (`-U`) option can improve performance in some cases by causing `tar` to remove files unconditionally before extracting them.

Next: Overwrite Old Files, Up: Writing [Contents][Index]