Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there a way to add/update a file in a tar.gz archive? Basically, I have an archive which contains a file at /data/data/com.myapp.backup/./files/settings.txt and I'd like to pull that file from the archive (already done) and push it back into the archive once the edit has been done. How can I accomplish this? Is it problematic because of the . in the path?


3 Answers 3

up vote 7 down vote accepted

The tar file format is just a series of files concatenated together with a few headers. It's not a very complicated job to rip it apart, put your contents in, and put it back together. That being said, Jander described how tar as a program does not have the utility functions to do this and there are additional complications with compression, which has to both before and after making a change.

There are, however, tools for the job! There are at least two system out there which will allow you to to do a loopback mount of a compressed tar archive onto a folder, then make your changes in the file system. When you are done, unmount the folder and your compressed archive is ready to roll.

The one first option would be the archivemount project for FUSE. Here is a tutorial on that. Your system probably already has FUSE and if it doesn't your distribution should have an option for it.

The other option is tarfs. It's simpler to use, but I've heard it has some trouble with corrupting bzip2 archives so you might test that pretty thoroughly first.

I use AVFS, which is packaged in Ubuntu and Debian. Do you know how archivemount and tarfs compare to AVFS? –  Gilles May 13 '11 at 11:31
Honestly no I don't, although I'm curious to look into AVFS now that you mention the alternative. I've used both archivemount and tarfs but it was for a long since decommissioned project and don't know their current state. I kind of decided from that experience that the idea was wrong conceptually even if the implementation worked perfectly. –  Caleb May 13 '11 at 20:28

To pull your file from your archive, you can use tar xzf archive.tar.gz my/path/to/file.txt. Note that the directories in the file's path will be created as well. Use tar t (i.e. tar tzf archive.tar.gz) to list the files in the archive.

tar does not support "in-place" updating of files. However, you can add files to the end of an archive, even if they have the same path as a file already in the archive. In that case, both copies of the file will be in the archive, and the file added later will override the earlier one. The command to use for this is tar r (or tar u to only add files that are newer than the archive) is the command to use. The . in the path should not be a problem.

There is a catch, though: you can't add to a compressed archive. So you would have to do:

gunzip archive.tar.gz
tar rf archive.tar data/data/com.myapp.backup/./files/settings.txt
gzip archive.tar

Which is probably not what you want to hear, since it means rewriting the entire archive twice over. If it's not a very large archive, it might be better to untar the whole thing and then re-tar it after editing. Alternately, you could use an uncompressed archive.

This answer is only correct insofar as it deals with the standard tar program. Tar as a file format is actually very accessible. You could easily write a script to update the contents yourself, but there are other tools that already do this. –  Caleb May 13 '11 at 8:48
You could also do this the Unix way: gzip -dc archive.tar.gz | tar -r data/data/com.myapp.backup/./files/settings.txt | gzip >archive_new.tar.gz –  Simon Richter May 13 '11 at 11:11
IIRC you can simply concatenate two gzip'ed files. The result is a larger gzip'ed file that contains both uncompressed files concatenated. –  FUZxxl Mar 21 '13 at 20:34

on my Linux x86_64 x86_64 x86_64 GNU/Linux to perform that operation i use simply the following command:

tar cvfpz /path/archive.tar -T list.txt

This command cause a creation of a compressed archive under /path using a list of file included into list.txt file.

That overwrites an existing file /path/archive.tar, instead of adding/updating that file. –  Anthon Apr 3 at 15:29

This site is currently not accepting new answers.

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