19

给定一个压缩存档文件,例如application.tar.gz其中有一个文件夹application/x/y/z.jar,我希望能够使用我的最新版本z.jar并用它更新/刷新存档。

除了以下内容之外,还有其他方法吗?

tar -xzf application.tar.gz
cp ~/myupdatedfolder/z.jar application/x/y
tar -czf application application.tar.gz

我知道-utar 中的开关可能有助于避免解压整个内容,但我不确定如何准确使用它。

4

3 回答 3

23

嗯,我找到了答案。

您不能tar -u与压缩存档一起使用。所以我使用的解决方案如下。请注意,我将文件移动到了为此目的z.jar而在当前目录中创建的文件夹。application/x/y

gzip -d application.tar.gz
tar -uf application.tar application/x/y/z.jar
gzip application.tar

当我做了一个tar -tf application.tar(更新之后,在 gzip 之前)它正确显示。

于 2010-06-02T09:50:10.680 回答
7

如果要更新的文件是文本文件。然后你可以vim直接使用编辑器打开包含文件的tarball并打开它,就像使用vim编辑器打开文件夹一样。然后修改文件并保存并退出。

但是,如果文件是二进制文件。我不知道解决方案。

于 2013-04-27T06:06:13.570 回答
3

就我而言,我必须删除该文件,然后使用以下步骤添加新文件:

我的 tar 文件

file.tar
└── foo.json
└── bar.json
└── dir
    └── zoo.json

我只想修改/更新foo.json文件而不提取和重新创建整个 tar 文件file.tar,这里是命令:

tar -x -f file.tar foo.json # extract only foo.json file to my current location
# now modify the file foo.json as you want ...
tar --delete -f file.tar foo.json # delete the foo.json file from the file.tar
tar -uf file.tar foo.json # add the specific file foo.json to file.tar

压缩文件:

如果它是压缩文件,例如file.tar.gz,您需要从压缩文件(在本例中为 gzip)中提取 tar 文件,使用gunzip file.tar.gz它将为您创建 tar 文件file.tar。那么您将能够执行上述步骤。

最后,您应该再次压缩 tar 文件,使用gzip file.tar它将为您创建名称为的压缩文件file.tar.gz

子目录:

为了处理子目录,您还必须在文件系统中保持相同的结构:

tar -x -f file.tar dir/zoo.json
# now modify the file dir/zoo.json as you want ...
tar --delete -f file.tar dir/zoo.jsonfile.tar
tar -uf file.tar dir/zoo.json

查看文件结构:

通过使用less命令,可以查看文件的结构:

less file.tar

drwxr-xr-x root/root         0 2020-10-18 11:43 foo.json
drwxr-xr-x root/root         0 2020-10-18 11:43 bar.json
drwxr-xr-x root/root         0 2020-10-18 11:43 dir/zoo.json

于 2019-12-04T10:24:00.360 回答