Score:1

GNU tar - update tar file, overwriting the original file in command line(which is possible by drag-and-drop in Archive Manager)

cn flag

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 two test1.txt and one test2.txt there in the tar, with the test2.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 two test1.txt and two test2.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?

SaveUkraine-StartPeaceTalk avatar
cn flag
related material: https://www.gnu.org/software/tar/manual/html_node/Dealing-with-Old-Files.html
SaveUkraine-StartPeaceTalk avatar
cn flag
Related: https://www.linuxquestions.org/questions/linux-newbie-8/how-to-overwrite-existing-files-by-using-tar-command-remove-extra-files-4175489175/#post5087034
WesternGun avatar
cn flag
No, it does not solve my problem. Please, if you really want to help, test it first: `-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.
ru flag
Both of you two go cool off. Comments on this question are now locked for the next week to stop you two bickering.
Score:2
cn flag

you could create a script that would create another tarball instead of updating the original and then remove the original and rename the new one as the original...

the way that the "archive manager" app is probably doing this is a bit more advanced and I wouldn't bother trying to emulate that behavior in bash, but that doesn't mean that you shouldn't try...

WesternGun avatar
cn flag
Yes this is something I want to try, thanks. I will remove the original file from the tar and append the modified one.
Score:0
cn flag

I found this paragraph in GNU tar doc, so overwriting is not supported by tar. I will remove the original file from the tar, and append the modified one.

https://www.gnu.org/software/tar/manual/html_node/how-to-update.html#how-to-update

4.2.3.1 How to Update an Archive Using ‘--update’

You must use file name arguments with the ‘--update’ (‘-u’) operation. If you don’t specify any files, tar won’t act on any files and won’t tell you that it didn’t do anything (which may end up confusing you).

To see the ‘--update’ option at work, create a new file, ‘classical’, in your practice directory, and some extra text to the file ‘blues’, using any text editor. Then invoke tar with the ‘update’ operation and the ‘--verbose’ (‘-v’) option specified, using the names of all the files in the ‘practice’ directory as file name arguments:

$ tar --update -v -f collection.tar blues folk rock classical
blues
classical
$

Because we have specified verbose mode, tar prints out the names of the files it is working on, which in this case are the names of the files that needed to be updated. If you run ‘tar --list’ and look at the archive, you will see ‘blues’ and ‘classical’ at its end. There will be a total of two versions of the member ‘blues’; the one at the end will be newer and larger, since you added text before updating it.

The reason tar does not overwrite the older file when updating it is that writing to the middle of a section of tape is a difficult process. Tapes are not designed to go backward. See section Tapes and Other Archive Media, for more information about tapes.

EDIT: --delete does not work/is very slow. It seems that delete files from tar is not very feasible, maybe by design. I think I have to extract/remove/repackage, or: https://unix.stackexchange.com/questions/68732/remove-files-from-tar-archive but I don't know how yet.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.