Linux 如何更新 tar(不附加)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8497439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:38:06  来源:igfitidea点击:

How to update tar (NOT append)

linuxbashcompressiontar

提问by

I want to update an existing tar file with newerfiles.

我想用较新的文件更新现有的 tar文件。

At GNU, I read:

在 GNU,我读到:

4.2.3 Updating an Archive

In the previous section, you learned how to use ‘--append' to add a file to an existing archive. A related operation is ‘--update' (‘-u'). The ‘--update' operation updates a tar archive by comparing the date of the specified archive members against the date of the file with the same name. If the file has been modified more recently than the archive member, then the newer version of the file is added to the archive (as with ‘--append').

4.2.3 更新档案

在上一节中,您学习了如何使用“--append”将文件添加到现有存档中。一个相关的操作是'--update' ('-u')。 “--update”操作通过将指定存档成员的日期与同名文件的日期进行比较来更新 tar 存档。如果该文件的修改时间比存档成员的修改时间要晚,则该文件的较新版本将添加到存档中(与 '--append' 一样)

However, When I run my tar update command, the files are appended even though their modification dates are exactly the same. I want to ONLY append where modification dates of files to be tarred are newer than those already in the tar...

但是,当我运行我的 tar update 命令时,即使它们的修改日期完全相同,也会附加文件。我只想在要压缩的文件的修改日期比 tar 中已有的文件更新日期的地方追加...

tar -uf ./tarfile.tar /localdirectory/ >/dev/null 2>&1

Currently, every time I update, the tar doubles in size...

目前,每次更新时,焦油的大小都会增加一倍......

采纳答案by fge

Warning! When speaking about "dates" it means anydate, and that includes the access time.

警告!当谈到“日期”时,它意味着任何日期,包括访问时间。

Should your files have been accessed in any such way (a simple ls -lis enough) then tar is right to do what it does!

如果您的文件以任何此类方式被访问(一个简单的ls -l就足够了),那么 tar 做它所做的事情是正确的!

You need to find another way to do what you want. Probably use a sentinel file and see if its modification date is less than the files you wish to append.

你需要找到另一种方式来做你想做的事。可能使用哨兵文件并查看其修改日期是否小于您希望附加的文件。

回答by j?rgensen

The update you describe implies that the file within the archive is replaced. If the new copy is smaller than what's in the archive, it could be directly rewritten. If the new copy however is larger, tar would have to zero the existing archive entry and append. Such updates would leave runs of '\0's or other unused bytes, so any normal computer user would want that such sections are removed, which would be done by "moving up" bytes comprising the archive contents towards the start of the file (think C's memmove).

您描述的更新意味着存档中的文件已被替换。如果新副本小于存档中的副本,则可以直接重写。但是,如果新副本更大,则 tar 必须将现有存档条目归零并附加。这样的更新会留下 '\0' 或其他未使用的字节,因此任何普通的计算机用户都希望删除这些部分,这可以通过将包含存档内容的字节“向上移动”到文件的开头来完成(想想 C 的记住)。

Such an in-place move operation however, which would involve seek-read-seek-write cycles, is costly, especially when you look at it in the context of tapes?— which tar was designed for originally?—, i.e. devices with a seek performance that is not comparable to harddisks. You'd wear out the tape rather quickly with such move ops. Oh and of course, WORM devices don't support this move op either.

然而,这种就地移动操作会涉及寻道-读-寻-写循环,成本很高,尤其是当您在磁带环境中查看它时?——最初为哪种 tar 设计?——,即具有寻求无法与硬盘相比的性能。使用此类移动操作,您会很快磨损磁带。哦,当然,WORM 设备也不支持此移动操作。

回答by physkets

You may simply create (instead of update) the archive each time:

您每次都可以简单地创建(而不是更新)存档:

tar -cvpf tarfile.tar *

tar -cvpf tarfile.tar *

This will solve the problem of your archive doubling in size each time. But of course, it is generating the whole archive every time.

这将解决您的存档每次都增加一倍的问题。但当然,它每次都会生成整个档案。

回答by Zbyszek

If you do not want to use the "-P" switch tar -u... works correctly if the current directory is the parentdirectory of the one we are going to update, and the path to this directory in the tar command will notbe an absolute path.
For exmple:
We want to update catalog /home/blabla/Dir. We do it like that:
cd /home/blabla
tar -u -f tarfile.tar Dir
In general, the update must be made from the same place as the creation, so that the paths agree.
It is also possible:
cd /home/blabla/Dir
tar-u -f /path/to/tarfile.tar .

如果您不想使用“-P”开关 tar -u... 如果当前目录是我们要更新的目录的目录,则tar -u... 可以正常工作,并且 tar 命令中该目录的路径不会成为绝对路径。
例如:
我们要更新目录/home/blabla/Dir。我们这样做:
cd /home/blabla
tar -u -f tarfile.tar Dir
通常,更新必须与创建的位置相同,以便路径一致。
也可以:
cd /home/blabla/Dir
tar-u -f /path/to/tarfile.tar 。