I want to update a tar file with part of the content modified, by overwriting the original file, in a script.
I tried tar -u
, -A
and -r
, but none does what I want. I also tried with --overwrite
.
An easy test:
cd ~
mkdir test
echo foo > test1.txt
mkdir test_dir
cd test_dir
echo bar > test2.txt
cd ../..
tar cvf test.tar test
tar tvf test.tar # check content
echo barbarbar >> test/test_dir/test2.txt
tar rvf test.jar test # or Avf, or uvf, it's the same: modified file does not overwrite the original file in the tar
-u --overwrite
theoretically will "update", but actually it only appends and does not append the changed file, so I see twotest1.txt
and onetest2.txt
there in the tar, with thetest2.txt
being the old one-A --overwrite
theoretically will append, but errors out:tar: test: Read error at byte 0, while reading 1024 bytes: Is a directory tar: Error is not recoverable: exiting now
-r --overwrite
theoretically will "append", but will only append, and no overwrite, so I see twotest1.txt
and twotest2.txt
in the tar.
I thought: OK then tar does not include the possibility of updating the tar in place. But then I remember that this can be achieved easily by drag the new folder to the tar file in Gnome(I think this is handled by "Archive Manager"), so it's at least possible.
How can I overwrite files in tar in command line/script?
-u
in that link will add another file with the same name; if I delete by name, there are two files with same name, how can I know for sure, the one I delete is the file I don't want? I mean, there is a reason why I ask here, because I cannot find anything that works by Googling it elsewhere.