Linux命令行压缩 & 解压缩 - tar

tar 为 GNU 打包工具,目前在标准 Linux 发行版上已经整合进压缩/解压缩工具 gzip、bzip2 和 xz。

  • 打包,后缀为 .tar,称为 tarfile
  • 打包 + 压缩,后缀为 .tar.gz.tar.bz2.tar.xz,称为 tarball。此为 linux 最常用的压缩、解压缩方式。

windows 中常见的压缩包有 .zip.rar,可通过 unzipunrar 两个工具进行解压。通过 apt-get install 安装即可。

  • 压缩
    • gzip,tar -zcvf xxx.tar.gz ./*
    • bzip2,tar -jcvf xxx.tar.bz2 ./*
    • xz,tar -Jcvf xxx.tar.xz ./*
    • 压缩时,排除某个目录、某个后续或某个文件,--exclude dir --exclude *.jpg --exclude a_dir/sb.bin
    • 压缩时,压缩的源目录和压缩包同一个目录的情况下,通过 --warning=no-file-changed 去除告警。
  • 解压缩,tar -xvf xxx.tar.gz
  • 查看压缩包内容,tar -tf xxx.tar.gz
  • 解压到特定目录,tar -xvf xxx.tar.gz -C /your/dir/

p4factory 为例,log 如下。

压缩前大小:

1
2
sunyongfeng@openswitch-OptiPlex-380:~/workshop/p4factory$ du -hd 0
7.0M .

压缩:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
sunyongfeng@openswitch-OptiPlex-380:~/workshop/p4factory$ 
sunyongfeng@openswitch-OptiPlex-380:~/workshop/p4factory$ tar -zcvf p4factory.tar.gz ./*
./apps/
./apps/int/
./apps/int/monitor/
./apps/int/monitor/preprocessor.py
./apps/int/monitor/monitor.py
./apps/int/monitor/client_msg_handler.py
./apps/int/monitor/topology.json
./apps/int/monitor/client/
./apps/int/monitor/client/index.html
./apps/int/monitor/client/styles/
./apps/int/monitor/client/styles/main.css
./apps/int/monitor/client/lib/
./apps/int/monitor/client/lib/cola.v1.min.js
...

压缩后大小:

1
2
sunyongfeng@openswitch-OptiPlex-380:~/workshop/p4factory$ ls -alh p4factory.tar.gz 
-rw-rw-r-- 1 sunyongfeng sunyongfeng 771K 417 14:26 p4factory.tar.gz

解压过程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
sunyongfeng@openswitch-OptiPlex-380:~/workshop/test-tar$ tar xvf ../p4factory/p4factory.tar.gz 
./apps/
./apps/int/
./apps/int/monitor/
./apps/int/monitor/preprocessor.py
./apps/int/monitor/monitor.py
./apps/int/monitor/client_msg_handler.py
./apps/int/monitor/topology.json
./apps/int/monitor/client/
./apps/int/monitor/client/index.html
./apps/int/monitor/client/styles/
./apps/int/monitor/client/styles/main.css
./apps/int/monitor/client/lib/
./apps/int/monitor/client/lib/cola.v1.min.js
...