update file in tar

5,915

for *BSD bsdtar command option u = r = append, that is can't update file

I think they both append, man tar on Linux says

   -r, --append
          append files to the end of an archive
   …
   -u, --update
          only append files that are newer than the existing in archive

Here's a test to prove this

$ uname -s
Linux
$ tar --version
tar (GNU tar) 1.15.1

$ echo aaa > a.txt
$ echo bbb > b.txt
$ echo ccc > c.txt

$ tar cvf x.tar *txt
a.txt
b.txt
c.txt

$ echo BBB > b.txt

$ tar -uvf x.tar b.txt
b.txt

$ tar tvf x.tar
-rw-rw-r-- ian/ian           4 2012-11-08 16:43:10 a.txt
-rw-rw-r-- ian/ian           4 2012-11-08 16:43:17 b.txt
-rw-rw-r-- ian/ian           4 2012-11-08 16:43:26 c.txt
-rw-rw-r-- ian/ian           4 2012-11-08 16:48:22 b.txt

Note that the tar file now contains both old and new versions of b.txt

OK so on to the nub of your query

why tar command in Linux can update file in tar archive,but bsdtar can't.

You can install Gnu tar in BSD in /usr/local/bin/gtar.

See Package Information for gtar-1.21-static.tgz (sh)


Update:

Why can't tar update in-place?

random access vs serial media

The name "tar" is a contraction of "tape archive". The command is intended to be usable with archives stored on serial media such as tape. The tape drive cannot insert new lengths of tape into the middle of your tape, nor can it cut out existing sections of tape. Nor can it read at the same time as writing. To transform a tape with contents a.txt, b.txt, c.txt to one with a bigger b.txt, first the whole tape must be read, the contents stored (in memory or on disk) and then the tape rewound and the new contents written in full. It is faster to seek the end-of-tape mark and append the new version of b.txt.

Also, many tape drives (e.g. DAT) automatically add an end of tape mark at the end of each write operation and this cannot be prevented. Therefore rewriting even a same-size or smaller b.txt at it's existing position would leave c.txt permanently inaccessible.

filesystem issues

It isn't possible to replace content in the middle of a file with content that is of a different length. To replace a file containing "bbb" with file containing "bbbbbb" you must read the old file and write a new file. Therefore it is still easier (and may be considerably faster) to append "bbbbbb" (plus file metadata header) to the end.

Share:
5,915

Related videos on Youtube

yodabox
Author by

yodabox

Updated on September 18, 2022

Comments

  • yodabox
    yodabox over 1 year

    for Linux tar command:

    tar uf test.tar test.txt
    

    but for *BSD bsdtar command option u = r = append,that is can't update file ,only can append file and also ,I try compiled libarchive , proved bsdtar can't update file in tar.

    So, why tar command in Linux can update file in tar archive,but bsdtar can't.

    refer to: tar (Linux) tar (OpenBSD)

  • yodabox
    yodabox over 11 years
    You are right,RedGrittyBrick. tar command have same policy in Linux and *BSD. -u, --update ppend files that are newer than the existing in archive I found the diffrence between BSD and Linux for tar command, and give a test on Linux but not BSD. So why tar command keep both old and new versions of the SAME file,not just update the file in tar archive.And tar can not delete file in archive inplace.
  • user5249203
    user5249203 over 11 years
    See updated answer.