Next: , Previous: Operations, Up: Advanced tar


4.2.2 How to Add Files to Existing Archives: --append

(This message will disappear, once this node revised.)

If you want to add files to an existing archive, you don't need to create a new archive; you can use --append (-r). The archive must already exist in order to use --append. (A related operation is the --update operation; you can use this to add newer versions of archive members to an existing archive. To learn how to do this with --update, see update.)

If you use --append to add a file that has the same name as an archive member to an archive containing that archive member, then the old member is not deleted. What does happen, however, is somewhat complex. tar allows you to have infinite number of files with the same name. Some operations treat these same-named members no differently than any other set of archive members: for example, if you view an archive with --list (-t), you will see all of those members listed, with their data modification times, owners, etc.

Other operations don't deal with these members as perfectly as you might prefer; if you were to use --extract to extract the archive, only the most recently added copy of a member with the same name as four other members would end up in the working directory. This is because --extract extracts an archive in the order the members appeared in the archive; the most recently archived members will be extracted last. Additionally, an extracted member will replace a file of the same name which existed in the directory already, and tar will not prompt you about this1. Thus, only the most recently archived member will end up being extracted, as it will replace the one extracted before it, and so on.

There exists a special option that allows you to get around this behavior and extract (or list) only a particular copy of the file. This is --occurrence option. If you run tar with this option, it will extract only the first copy of the file. You may also give this option an argument specifying the number of copy to be extracted. Thus, for example if the archive archive.tar contained three copies of file myfile, then the command

     tar --extract --file archive.tar --occurrence=2 myfile

would extract only the second copy. See —occurrence, for the description of --occurrence option.

If you want to replace an archive member, use --delete to delete the member you want to remove from the archive, , and then use --append to add the member you want to be in the archive. Note that you can not change the order of the archive; the most recently added member will still appear last. In this sense, you cannot truly “replace” one member with another. (Replacing one member with another will not work on certain types of media, such as tapes; see delete and Media, for more information.)


Footnotes

[1] Unless you give it --keep-old-files option, or the disk copy is newer than the the one in the archive and you invoke tar with --keep-newer-files option