linux 压缩命令 tar, 7z

gz,tar

1
2
3
4
5
6
7
8
#压缩
tar -czf some.tar.gz file1 file2 folder1/*

#解压
tar -xzf some.tar.gz

# 解压某个文件/文件夹
tar -xzf some.tar.gz ./some/folder/or/file
1
2
--ignore-failed-read           Do not exit with nonzero on unreadable files.
--ignore-command-error         Ignore subprocess exit codes.

显示压缩进度

1
tar cf - /folder-with-big-files -P | pv -s $(($(du -sk /folder-with-big-files | awk '{print $1}') * 1024)) | gzip > big-files.tar.gz

多线程压缩、解压

1
2
3
4
5
# 先安装pigz
sudo pacman -S pigz
tar -I pigz -cf tarball.tgz files

tar -I pigz -xf tarball.tgz

323

You can use pigz instead of gzip, which does gzip compression on multiple cores. Instead of using the -z option, you would pipe it through pigz:

tar cf - paths-to-archive pigz > archive.tar.gz

By default, pigz uses the number of available cores, or eight if it could not query that. You can ask for more with -p n, e.g. -p 32. pigz has the same options as gzip, so you can request better compression with -9. E.g.

tar cf - paths-to-archive pigz -9 -p 32 > archive.tar.gz
pigz -dc target.tar.gz tar xf -
pigz -p 8 -dc wechat2.tgz tar xf -

xz

安装xz

1
2
3
4
5
6
# debian / ubuntu
apt-get install xz-utils


# RHEL
yum install xz

xz压缩

1
2
3
4
5
6
7
8
9
10
11
12
13
# --threads=0 尽可能使用所有进程
# 默认compress level 是 6
# -e or --extreme flag 将使用更多CPU来提高压缩率,会拖慢压缩时间
# -k 执行完不删除原来的文件
xz -z6vek --threads=0 target-file

# compress level = 0
time xz -0v linux-3.18.19.tar
# compress level = 9
time xz -9v linux-3.18.19.tar

# 压缩多个文件
xz file1.txt file2.txt file3.txt

xz解压

1
2
3
4
# -k 执行完不删除原来的文件
xz -dk file.txt.xz
# 或
unxz -k file.txt.xz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
xz -h
Usage: xz [OPTION]... [FILE]...
Compress or decompress FILEs in the .xz format.

  -z, --compress      force compression
  -d, --decompress    force decompression
  -t, --test          test compressed file integrity
  -l, --list          list information about .xz files
  -k, --keep          keep (don't delete) input files
  -f, --force         force overwrite of output file and (de)compress links
  -c, --stdout        write to standard output and don't delete input files
  -0 ... -9           compression preset; default is 6; take compressor *and*
                      decompressor memory usage into account before using 7-9!
  -e, --extreme       try to improve compression ratio by using more CPU time;
                      does not affect decompressor memory requirements
  -T, --threads=NUM   use at most NUM threads; the default is 1; set to 0
                      to use as many threads as there are processor cores
  -q, --quiet         suppress warnings; specify twice to suppress errors too
  -v, --verbose       be verbose; specify twice for even more verbose
  -h, --help          display this short help and exit
  -H, --long-help     display the long help (lists also the advanced options)
  -V, --version       display the version number and exit

With no FILE, or when FILE is -, read standard input.

Report bugs to <lasse.collin@tukaani.org> (in English or Finnish).
XZ Utils home page: <http://tukaani.org/xz/>

bzip2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$ bzip2 -h
bzip2, a block-sorting file compressor.  Version 1.0.6, 6-Sept-2010.

   usage: bzip2 [flags and input files in any order]

   -h --help           print this message
   -d --decompress     force decompression
   -z --compress       force compression
   -k --keep           keep (don't delete) input files
   -f --force          overwrite existing output files
   -t --test           test compressed file integrity
   -c --stdout         output to standard out
   -q --quiet          suppress noncritical error messages
   -v --verbose        be verbose (a 2nd -v gives more)
   -L --license        display software version & license
   -V --version        display software version & license
   -s --small          use less memory (at most 2500k)
   -1 .. -9            set block size to 100k .. 900k
   --fast              alias for -1
   --best              alias for -9

   If invoked as `bzip2', default action is to compress.
              as `bunzip2',  default action is to decompress.
              as `bzcat', default action is to decompress to stdout.

   If no file names are given, bzip2 compresses or decompresses
   from standard input to standard output.  You can combine
   short flags, so `-v -4' means the same as -v4 or -4v, &c.

7z

安装

1
2
3
4
5
6
7
8
#Ubuntu
sudo apt-get install p7zip-full

# CentOS
yum -y install p7zip

# arch
sudo pacman -S p7zip

解压

1
7z x some7zfile.7z       解压到当前目录

压缩

1
2
3
4
7z a target-name.7z target-dir/

# -mx=1 最快压缩 -mmt=线程数
7z a -mx=1 -mmt=6  -m0=LZMA2 target-name.7z target-file1 target-file2

其他技巧

使用密码

1
2
3
4
5
# 压缩
7z a secure.7z * -pSECRET

# 解压,按照提示输入密码即可
7z x secure.7z

对目录下的子文件逐个压缩

1
for i in $(ls -1); do 7z a ${i%%/}.7z ${i%%/}; done

另外一种方法:

1
\ls -1 | xargs -I% 7z a %.7z %