【问题标题】:Compressing Directory tree into multiple tar archives into a single xz将目录树压缩成多个 tar 存档到单个 xz
【发布时间】:2017-10-11 23:18:41
【问题描述】:

我正在尝试压缩包含许多 subdir_i 的大型 rootdir 文件夹树如下所示:

./rootdir
./rootdir/subdir_1
./rootdir/subdir_2
...

我希望将其输出到单个压缩存档,但每个子目录都在它自己的 tar 存档中:

rootdir.tar.xz  # containing:
     subdir_1.tar
     subdir_2.tar

我尝试了以下方法:

for foo in `find rootdir -maxdepth 1 -name "subdir_*" -type d`
do
     tar --create --verbose --file=- --directory="rootdir" `basename ${foo}` 
     # in shorter form:  tar -cvf - -C rootdir subdir_i
done | xz -zc9 > rootdir.tar.xz 

这确实将子目录隔离到 xz 中,但内部只有一个 tar 存档,只有最后一个目录:

rootdir.tar.xz
     rootdir.tar # containing subdir_2/

但是,该存档的大小与整个 rootdir 树的压缩一致。任何想法为什么会这样以及如何让它做我想做的事(不使用中间档案)?

【问题讨论】:

    标签: bash unix compression tar xz


    【解决方案1】:

    每次在循环中运行 tar 时,您都会创建一个新的存档 -- 这就是为什么最终输出只是最后一个目录的原因。

    我不相信没有中间文件可以做任何你想做的事。你可以这样做来最小化所需的空间:

    rm rootdir.tar
    find rootdir/* -maxdepth 0 -type d | while read foo
    do
        b=$(basename "$foo")
        tar --create --file "$b".tar --directory rootdir "$b"
        tar --append --file rootdir.tar "$b".tar
        rm "$b".tar
    done
    xc -zc9 rootdir.tar
    

    【讨论】:

      猜你喜欢
      • 2011-03-04
      • 1970-01-01
      • 2018-02-24
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多