Linux中,向.tar.gz/.gz文件中添加一个文件,如何做?

 凌微茵_686 发布于 2022-10-31 08:16

描述问题

需求: 对.zip .gz .tar.gz文件添加一个/几个新文件进去

在Windows上,操作极其简单,借助WinRAR,学习成本为0

在Linux上, 我

  1. 先是man tar man gzip

  2. 搜索 update append (gzip的搜索,直接没有, 关键词不对?)

  3. 然而没有找到支持压缩文件的选项(仅仅是对归档文件的支持)

注意到我需要:

  1. .zip .gz .tar.gz

  2. append/delete
    因此有6个需求

解决方法是?

上下文环境

Linux(仅仅使用发行版内置命令, 不借助任何外部工具, 换句话来说: 命令大概是gzip tar ...这些)
仅仅使用bash命令行(无GUI)

重现

查看man命令即可

相关代码

man的一部分信息

 -A, --catenate, --concatenate
           append tar files to an archive   是archive!

     -c, --create
           create a new archive

     -d, --diff, --compare
           find differences between archive and file system

     --delete
           delete from the archive (not on mag tapes!)

     -r, --append
           append files to the end of an archive

     -t, --list
           list the contents of an archive

     --test-label
           test the archive volume label and exit

     -u, --update
           only append files newer than copy in archive

报错信息

上述选项,对.tar.gz使用时会报错
比如: 
tar rzv -f zipfile.tar.gz new-file
tar: Cannot update compressed archives

相关截图

已经尝试哪些方法仍然没有解决(附上相关链接)

Google了

  1. append file to .tar.gz

  2. append file to .gz

  3. append dir to .tar.gz

附:
不成熟的吐槽

Linux是不是把事情搞复杂了?
还是我搜索/探索姿势不对? 
为啥有时候在Win上极其简单的一件事情,到了Linux上就要上升到折腾地步?  (得到一种莫名其妙的自我感动? )
3 个回答
  • 只用targzip的话,.tar.gz好像只能先解压缩再压缩...

    话说难道我们用的google不是同一个?为啥我搜到了个解决方案:

    http://unix.stackexchange.com...

    里面提到使用FUSE或者tarfs就可以了。

    2022-11-12 01:43 回答
  • 目前所知,压缩后的文件是不可追加的。
    可以先追加到tar文件,再压缩也行吧?
    tar -rvf ttt.tar aaa.txt

    2022-11-12 01:43 回答
  • tar: Cannot update compressed archives
    

    这个报错其实已经说明问题了--不能对压缩的’包‘进行更新操作。所以,可行的办法是将tar.gz 先解压成 tar,然后更新,最后重新压缩(可以写个脚本)。

    关于‘Win上极其简单的一件事情’,只是因为 Win 上的软件通常封装的比较好而已,把“解压-更新-压缩”的过程隐藏起来了而已。这里面孰优孰劣要看具体情况,winrar 如题主所说学习成本为零,但是缺点是不可不可编程(也可能是我孤陋寡闻)。试想,如果有每天有大量的压缩包需要更新,那是不是要专门找个人在 winrar 里点点点呢?

    2022-11-12 01:43 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有