繁体   English   中英

更新压缩 tar 中的单个文件

[英]Updating a single file in a compressed tar

给定一个压缩存档文件,例如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

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

嗯,我找到了答案。

您不能将tar -u与压缩档案一起使用。 所以我使用的解决方案如下。 请注意,我z.jarz.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 之前)时,它正确显示。

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

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

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

我的 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 ,则需要使用gunzip file.tar.gz从压缩文件(在此示例中为 gzip)中提取 tar 文件,该文件将为您创建 tar 文件file.tar . 那么你就可以完成上面的步骤了。

最后,您应该使用gzip file.tar再次压缩 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM