【问题标题】:Updating a single file in a compressed tar更新压缩 tar 中的单个文件
【发布时间】:2010-06-02 08:55:58
【问题描述】:

给定一个压缩存档文件,例如 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 开关可能有助于避免解压整个内容,但我不确定如何准确使用它。

【问题讨论】:

    标签: linux tar


    【解决方案1】:

    嗯,我找到了答案。

    您不能将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 之前)时,它正确显示。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案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,您需要使用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
        
        

        【讨论】:

        • 我不确定您是否可以更新压缩档案(正如您在评论末尾所建议的那样)。
        猜你喜欢
        • 2012-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多