Updating a single file in a compressed tar

If the file you want to update is text file. Then you can use vim editor directly to open the tarball that contains the file and open it, just like open folder using vim editor. Then modify the file and save it and quit.

However, if the file is a binary. I have no idea about the solution.


Well, I found the answer.

You can't use tar -u with a zipped archive. So the solution I used was the following. Note that I moved the z.jar file to a folder I created in the current directory called application/x/y for this purpose.

gzip -d application.tar.gz
tar -uf application.tar application/x/y/z.jar
gzip application.tar

When I did a tar -tf application.tar (after the update, before the gzip) it showed up properly.


in my case, I had to delete the file and then add the new file with the following steps:

my tar file

file.tar
└── foo.json
└── bar.json
└── dir
    └── zoo.json

and I wanted only to modify/update foo.json file without extracting and re-creating the whole tar file file.tar, Here are the commands:

tar -x -f file.tar foo.json # extract only foo.json file to my current location
# now modify the file foo.json as you want ...
tar --delete -f file.tar foo.json # delete the foo.json file from the file.tar
tar -uf file.tar foo.json # add the specific file foo.json to file.tar

compressed file:

if it is compressed file, like file.tar.gz, you will need to extract the tar file from the compressed file (in this example gzip) by using gunzip file.tar.gz which will create for you the tar file file.tar. then you will be able to do the above steps.

at the end you should compress the tar file again by using gzip file.tar which will create for you compressed file with the name file.tar.gz

sub directories:

in order to handle sub dirs you will have to keep the same structure also in the file system:

tar -x -f file.tar dir/zoo.json
# now modify the file dir/zoo.json as you want ...
tar --delete -f file.tar dir/zoo.json
tar -uf file.tar dir/zoo.json

view the file structure:

by using the less command, you can view the structure of the file:

less file.tar

drwxr-xr-x root/root         0 2020-10-18 11:43 foo.json
drwxr-xr-x root/root         0 2020-10-18 11:43 bar.json
drwxr-xr-x root/root         0 2020-10-18 11:43 dir/zoo.json

Tags:

Linux

Tar