更新压缩tar中的单个文件

Phi*_*hil 16 linux 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
Run Code Online (Sandbox Code Playgroud)

我知道-utar中的开关可能有用,以避免解开整个事情,但我不确定如何使用它.

Phi*_*hil 21

好吧,我找到了答案.

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

gzip -d application.tar.gz
tar -uf application.tar application/x/y/z.jar
gzip application.tar
Run Code Online (Sandbox Code Playgroud)

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


Con*_* He 6

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

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


dsa*_*don 5

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

\n

我的 tar 文件

\n
file.tar\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 foo.json\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 bar.json\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 dir\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 zoo.json\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
tar -x -f file.tar foo.json # extract only foo.json file to my current location\n# now modify the file foo.json as you want ...\ntar --delete -f file.tar foo.json # delete the foo.json file from the file.tar\ntar -uf file.tar foo.json # add the specific file foo.json to file.tar\n
Run Code Online (Sandbox Code Playgroud)\n

压缩文件:

\n

如果它是压缩文件,例如file.tar.gz,您将需要从压缩文件(在本例中为 gzip)中提取 tar 文件,gunzip file.tar.gz这将为您创建 tar 文件file.tar。然后您就可以执行上述步骤。

\n

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

\n

子目录:

\n

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

\n
tar -x -f file.tar dir/zoo.json\n# now modify the file dir/zoo.json as you want ...\ntar --delete -f file.tar dir/zoo.json\ntar -uf file.tar dir/zoo.json\n
Run Code Online (Sandbox Code Playgroud)\n

查看文件结构:

\n

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

\n
less file.tar\n\ndrwxr-xr-x root/root         0 2020-10-18 11:43 foo.json\ndrwxr-xr-x root/root         0 2020-10-18 11:43 bar.json\ndrwxr-xr-x root/root         0 2020-10-18 11:43 dir/zoo.json\n\n
Run Code Online (Sandbox Code Playgroud)\n