How to add/update a file to an existing tar.gz archive?
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?
+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.
5 Answers 5
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.
Linux add to archive command
The simplest way to add a file to an already existing archive is the ‘ —append ’ (‘ -r ’) operation, which writes specified files into the archive whether or not they are already among the archived files.
When you use ‘ —append ’, you must specify file name arguments, as there is no default. If you specify a file that already exists in the archive, another copy of the file will be added to the end of the archive. As with other operations, the member names of the newly added files will be exactly the same as their names given on the command line. The ‘ —verbose ’ (‘ -v ’) option will print out the names of the files as they are written into the archive.
‘ —append ’ cannot be performed on some tape drives, unfortunately, due to deficiencies in the formats those tape drives use. The archive must be a valid tar archive, or else the results of using this operation will be unpredictable. See section Tapes and Other Archive Media.
To demonstrate using ‘ —append ’ to add a file to an archive, create a file called ‘rock’ in the ‘practice’ directory. Make sure you are in the ‘practice’ directory. Then, run the following tar command to add ‘rock’ to ‘collection.tar’:
$ tar --append --file=collection.tar rock
If you now use the ‘ —list ’ (‘ -t ’) operation, you will see that ‘rock’ has been added to the archive:
$ tar --list --file=collection.tar -rw-r--r-- me/user 28 1996-10-18 16:31 jazz -rw-r--r-- me/user 21 1996-09-23 16:44 blues -rw-r--r-- me/user 20 1996-09-23 16:44 folk -rw-r--r-- me/user 20 1996-09-23 16:44 rock
This document was generated on March 24, 2021 using texi2html 5.0.
This post and this website contains affiliate links. See my disclosure about affiliate links.
how to add new files into an archive file (tar, gz, zip) in linux (also delete files)
There are several programs, both command line and GUI based that will allow you to view and extract the contents of an archive. But sometimes, you will need to modify an existing archive file, such as add files to the archive, delete an existing file in the archive or rename a file in the archive.
Most of the GUI applications, like Ark support adding and deleting files using the drag and drop. This is accomplished by extracting the archive in memory and performing the modification operation. The archive is then re-archived from memory and saved to the disk. A GUI interface may not always be available and you may want to modify the archive from command line without extracting it completely.
You can always do it the long way. First extracting the archive to the hard disk and then modify the contents and re-create the archive from the modified content. This requires space on the disk and extra time for extraction and archiving.
Most of the widely used archive formats are usually the combination of two different process: archiving and compression. There are a couple of caveats that you should be aware of while modifying the archives.
compressed archive: You can directly modify an archive but not a compressed archive. Modifying the compressed file without uncompressing it first will corrupt the file. For example, this means that you can modify a .tar file, but not a .tar.gz file. The .tar.gz should first be uncompressed to .tar.
file position: There is really no way to “replace” a file inside the archive while also maintaining its position. You can always extract the whole archive to a temp location, modify or replace the file and then archive it back up. But we are only dealing with the scenario of doing the operations in place without extracting the entire archive.
Add files to archive
tar archive
Assuming that you have a archive with .tar extension, you can use the -r (or –append) option of the tar command to add/append a new file to the end of the archive.
bash$ tar rvf /path/to/archive.tar /path/to/newfile.txt
You can use the -v option to have a verbose output to verify the operation. The other option that can be used with the tar command is -u (or –update).
The update refreshes the files that are newer than the ones in the archive, so it works slightly different than the –append (or -r) option but provides the same functionality for the addition of a new file.
bash$ tar uvf /path/to/archive.tar /path/to/newfile.txt
Another feature of the archive is that it allows for multiple files with the same name. If you append a file with the same name, then the file is just appended to the end of the archive without deleting the previous one. But when you extract the archive, it will extract the latest version (by its order inside the archive) of the file.
zip archive
The zip archive can be modified using the zip command. The -r option of the zip command allows you to add new files to the archive.
bash$ zip -rv zipfile.zip newfile.txt newfile1.txt
where the zipfile.zip is the name of an existing zip file and the newfile.txt and newfile1.txt are the files that you want to add to the zip archive.
jar archive
In case of the jar files, you can use the jar command. The –update (or -u) option allows you to add or update files into a jar archive.
bash$ jar -uvf jarfile.jar newfile.txt
Again, you can add multiple files in a single command just like the zip command.
Delete files from archive
tar archive
You can use the –delete option of the tar command to delete existing files from inside an archive.
bash$ tar -dvf archive.tar filename.txt
zip archive
You can use the –delete (or -d) option of the zip command to delete files from the archive. The zip command take multiple arguments for file names and supports regular expressions in the argument. This allows for deletion of multiple files with a single command.
bash$ zip -d zipfile.zip filename.doc \*.txt
The above command will delete the file named filename.doc and also all the files with an extension of .txt.
jar archive
Unfortunately there is no option with the jar command to delete the files. But jar files are essentially zip files with .jar extension, hence you can use the zip command to delete the files, just as you would to a zip file.
bash$ zip -d jarfile.jar file1.txt file2.txt
Renaming files inside the archive essentially amounts to deleting the existing file and adding the new file. However, this will also cause the file to lose its position inside the tar archive. If the order of the files is important, then you should extract the archive and rename the file on the disk first.