Lan*_*nes 138 tar directory compression
所以我需要用最大压缩率压缩一个目录。
我该怎么做xz?我的意思是我也需要,tar因为我不能只用xz. 是否有一个oneliner可以生产例如foo.tar.xz?
bsd*_*bsd 175
使用最近的 GNU tarbash 或派生 shell:
XZ_OPT=-9 tar cJf tarfile.tar.xz directory
Run Code Online (Sandbox Code Playgroud)
tar 的小写 j 开关使用 bzip,大写 J 开关使用 xz。
在XZ_OPT环境变量让xz无法通过调用应用程序,如传递选项tar。  
这是现在最大的。
请参阅man xz您可以设置的其他选项(-e/--extreme 可能会给您一些数据集的额外压缩好处)。
XZ_OPT=-e9 tar cJf tarfile.tar.xz directory
Run Code Online (Sandbox Code Playgroud)
        Sha*_*dur 102
假设xz遵守标准的命令行标志集 - 包括压缩级别标志,您可以尝试:
tar -cf - foo/ | xz -9 -c - > foo.tar.xz 
Run Code Online (Sandbox Code Playgroud)
        Eva*_* Jr 18
XZ_OPT=-9e tar cJf tarfile.tar.xz directory
Run Code Online (Sandbox Code Playgroud)
甚至比
XZ_OPT=-9 tar cJf tarfile.tar.xz directory
Run Code Online (Sandbox Code Playgroud)
        小智 12
如果您有 16 GiB 的 RAM(并且没有其他运行),您可以尝试:
tar -cf - foo/ | xz --lzma2=dict=1536Mi,nice=273 -c - > foo.tar.xz 
Run Code Online (Sandbox Code Playgroud)
这将需要 1.5 GiB 进行解压缩,大约是压缩的 11 倍。针对较少的内存量进行相应调整。
这将只有当数据实际上是大的,并且在任何情况下,它不会帮助帮助THAT多,但仍...
如果您要压缩二进制文件,请将 --x86 添加为第一个 xz 选项。如果您正在播放“多媒体”文件(未压缩的音频或位图),您可以尝试使用 --delta=dist=2 (有值的实验,可以尝试的好值是 1..4)。
如果您喜欢冒险,可以尝试使用更多 LZMA 选项,例如
--lzma2=dict=1536Mi,nice=273,lc=3,lp=0,pb=2
Run Code Online (Sandbox Code Playgroud)
(这些是默认设置,你可以尝试0到4之间的值,并且lc+lp不能超过4)
为了查看默认预设如何映射到这些值,您可以查看源文件 src/liblzma/lzma/lzma_encoder_presets.c。但是没有什么有趣的(-e 将合适的长度设置为 273 并调整深度)。
您可以尝试不同的选项,对我来说 -4e 效果更好
tar cf - wam_GG_${dir}.nc | xz -4e > wam_GG_${dir}.nc.tar.xz 
Run Code Online (Sandbox Code Playgroud)
我通过运行测试:
$ tar -cf - wam_GG.nc | xz -4e > wam_GG.nc.xz
$ tar -cf - wam_GG.nc | xz -9e > wam_GG.nc.xz.2
Run Code Online (Sandbox Code Playgroud)
因此,选项 -4e 似乎比 -9e 好一点。
$ ll wam_GG.nc.xz*
-rw-rw-r--. 1 504 504 2707596 Jan 16  2015 wam_GG.nc.xz
-rw-rw-r--. 1 504 504 2708416 Jan 16  2015 wam_GG.nc.xz.2
Run Code Online (Sandbox Code Playgroud)
        tar --help :  -I, --use-compress-program=PROG  
tar -I 'xz -9' -cvf foo.tar.xz foo/  
tar -I 'gzip -9' -cvf foo.tar.gz foo/    
Run Code Online (Sandbox Code Playgroud)
还可以使用外部压缩器进行压缩:
tar -I 'lz4 -9' -cvf foo.tar.lz4 foo/
tar -I 'zstd -19' -cvf foo.tar.zst foo/
Run Code Online (Sandbox Code Playgroud)
解压外部压缩机:
tar -I lz4 -xvf foo.tar.lz4  
tar -I zstd -xvf foo.tar.zst  
Run Code Online (Sandbox Code Playgroud)
列出存档外部压缩器:
tar -I lz4 -tvf foo.tar.lz4
tar -I zstd -tvf foo.tar.zst
Run Code Online (Sandbox Code Playgroud)
        小智 5
在 xz-utils v5.2.0 版本的多核计算机中,检查:
-T, --threads=NUM   use at most NUM threads; the default is 1; set to 0
Run Code Online (Sandbox Code Playgroud)
如果您希望使用最大核心数和最大压缩:
export XZ_DEFAULTS="-9 -T 0 "
Run Code Online (Sandbox Code Playgroud)
或者将 -T 设置为您希望使用的核心数。
然后:
tar cJf target.tar.xz source
Run Code Online (Sandbox Code Playgroud)
这对于选择压缩级别也可能有用: