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)

migrated from stackoverflow.com Nov 8 '12 at 9:22

This question came from our site for professional and enthusiast programmers.


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
$ 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

$ echo BBB > b.txt

$ tar -uvf x.tar 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)


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.

  • 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. – yodabox Nov 9 '12 at 7:12
  • See updated answer. – RedGrittyBrick Nov 9 '12 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.