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?

  • 2
    +1. This would make working on OS X so easy. Currently, I have to hand clean a directory of all the hidden files like .DS_Store and then re-tar the directory. – user56041 Jan 1 '15 at 19:30

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.

  • 1
    I use AVFS, which is packaged in Ubuntu and Debian. Do you know how archivemount and tarfs compare to AVFS? – Gilles 'SO- stop being evil' 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
  • I took a look at how archivemount works. It does not modify the existing .tar.gz file, but when you unmount, it renames the original and creates a brand new tar file in its place. If you're looking for a good user experience, it's great, but if you're looking to avoid rewriting the entire archive for efficiency reasons, it won't help you. – Jander Mar 16 '16 at 18:24
  • @Jander That's for pointing that out. I'm pretty sure it applies to all the compressed archive mounting options though. The problem is the individual parts of the file are not compressed, the overall file internal headers and all is compressed and in order for other thing to extracti is as expected gzip has to be run over the whole data set. There isn't a way to inject something in the middle of this without extracting it and rerunning the compression and rewriting the whole data file. Hence my previous comment about it being the wrong solution conceptually. Archives are archives. – Caleb Mar 16 '16 at 19:06

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
  • 25
    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
  • 2
    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
  • Once the file has been unzipped, isn't it possible to use the --update flag? – pietro Oct 6 '17 at 10:13
  • @FUZxxl +1 for mentionning gzip concatenation – Johan Boulé Jan 18 '18 at 11:41

Tar was originally meant to be used for tapes, so "replacing" files is not really in the design. However, you can use --delete to delete the file from the original tar and then -u to update the tar and re-add the file. It may take a bit, as tar needs to reorganize the archive internally.

  • Welcome to StackExchange! Your answer only contains a suggestion already made in another answer. Please read the other answers to a question before answering, to avoid creating duplicates. – JigglyNaga Jul 29 '16 at 19:56
  • 3
    I edited my answer a bit. I don't think the other answer mentions --delete – rleibman Jul 29 '16 at 20:28

For tar.gz files, I'm afraid that this is not possible, according to my tests:

$ tar rvf backup.tar.gz db_bkp_today.sql 
tar: Cannot update compressed archives
tar: Error is not recoverable: exiting now

But being just Tape Archive (tar), I experimented positive results:

$ gzip -d backup.tar.gz
$ tar uvf backup.tar db_bkp_today.sql 
$ tar tvf backup.tar | grep sql
-rw-r--r-- ivanleoncz/ivanleoncz 14779319 2020-05-11 13:40 db_bkp_today.sql

Taken from man tar:

>  -r, --append
    Append files to the end of an archive.
    Arguments have the same meaning as for -c (--create).
>  -u, --update
    Append files which are newer than the corresponding copy in the archive. 
    Arguments have the same meaning as  with -c and -r options.

Using -u, has the same behavior of using -r, so the file was added to the TAR.

The practical way that I see for adding/updating a compressed Tape Archive (tar.gz), would ironically be:

  1. uncompress the tar.gz : gzip -d backup.tar.gz
  2. add/update a file on the tar file : tar uvf backup.tar db_bkp_today.sql
  3. compress the tar : compress the TAR (with maximum compression if you want) : gzip -9 backup.tar

on my Linux 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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.