一、常见压缩及打包工具
compress/uncompress # 基本被淘汰,很少使用
压缩后文件格式:file.z
gzip/gunzip # 早期主流压缩工具,目前也在使用
压缩后文件格式:file.gz
bzip2/bunzip2 # 早期主流压缩工具,目前也在使用,压缩效率及压缩比优于gzip
压缩后文件格式:file.bz2
zip/unzip # 早期主流压缩工具,不仅能压缩还能打包,较为方便,目前使用较广
压缩后文件格式:file.zip
xz/unxz # 目前最为主流的压缩工具,效率高,压缩比大
压缩后文件格式:file.xz
tar # 目前最为主流的归档工具,可调用压缩工具实现压缩归档
打包后文件格式:file.tar
压缩打包后文件格式:file.tar.xz 、file.tar.bz2 、file.tar.gz
二、gzip的使用
格式:gzip 选项 /path/to/file # 将指定文件压缩,压缩完成后删除原文件,只保留压缩文件
gunzip /文件 # 解压缩文件
压缩后文件格式:file.gz
常用选项:
-t # 测试压缩后文件的完整性
-n # 指明压缩比(1~9),数值越大,压缩后体积越小,默认6
-r # 递归压缩,当压缩对向为目录时,递归压缩目录中的文件
-c # 压缩后将压缩内容输出为标准输出,且不删除源文件
-d # 解压缩
zcat /path/to/file # 自动调用gzip工具查看压缩文件内容
示例:
[root@localhost ~]# ll -h data/
total 12K
-rw-r--r--. 1 root root 938 Mar 2 14:54 test_file01
-rw-r--r--. 1 root root 938 Mar 2 14:54 test_file02
-rw-r--r--. 1 root root 938 Mar 2 14:54 test_file03
[root@localhost ~]# gzip -9 -r data/*
[root@localhost ~]# ll -h data/
total 12K
-rw-r--r--. 1 root root 443 Mar 2 14:54 test_file01.gz
-rw-r--r--. 1 root root 443 Mar 2 14:54 test_file02.gz
-rw-r--r--. 1 root root 443 Mar 2 14:54 test_file03.gz
[root@localhost ~]# ll
total 8
drwxr-xr-x. 2 root root 119 Mar 3 00:20 data
-rw-r--r--. 1 root root 443 Mar 2 14:54 test_file01.gz
-rw-r--r--. 1 root root 938 Mar 2 14:54 test_file02
[root@localhost ~]# gzip -t test_file01.gz
[root@localhost ~]# gzip -c test_file02 > test_file02.gz
[root@localhost ~]# ll -h
total 12K
drwxr-xr-x. 2 root root 119 Mar 3 00:20 data
-rw-r--r--. 1 root root 443 Mar 2 14:54 test_file01.gz
-rw-r--r--. 1 root root 938 Mar 2 14:54 test_file02
-rw-r--r--. 1 root root 443 Mar 3 00:25 test_file02.gz
三、bzip的使用
格式:bzip2 选项 /path/to/file #压缩指定文件,压缩后删除源文件
bunzip2 /path/to/file # 解压缩文件
压缩后文件格式:file.bz2
常用选项:
-d # 解压缩文件
-k # 压缩后保留源文件
-n # 指明压缩比(1~9),数值越大,压缩后体积越小
-t # 检测压缩后文件的完整性
bzcat /path/to/file # 自动调用bzip工具查看压缩文件内容
示例:
[root@localhost ~]# ll -h
total 4.0K
drwxr-xr-x. 2 root root 119 Mar 3 00:20 data
-rw-r--r--. 1 root root 938 Mar 2 14:54 test_file02
[root@localhost ~]# bzip2 -k -9 test_file02
[root@localhost ~]# ll -h
total 8.0K
drwxr-xr-x. 2 root root 119 Mar 3 00:20 data
-rw-r--r--. 1 root root 938 Mar 2 14:54 test_file02
-rw-r--r--. 1 root root 470 Mar 2 14:54 test_file02.bz2
[root@localhost ~]# bzcat test_file02.bz2
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
四、zip的使用
格式:zip /path/to/package.zip /path/to/file或/path/*
unzip /path/to/package.zip
将指定目录下的所有文件压缩归档为指定的压缩包
示例:
[root@localhost ~]# ll
total 0
drwxr-xr-x. 2 root root 95 Mar 3 00:40 data
[root@localhost ~]# ll -h data/
total 12K
-rw-r--r--. 1 root root 938 Mar 2 14:54 file01
-rw-r--r--. 1 root root 938 Mar 2 14:54 file02
-rw-r--r--. 1 root root 938 Mar 2 14:54 file03
[root@localhost ~]# zip data.zip data/*
adding: data/file01 (deflated 56%)
adding: data/file02 (deflated 56%)
adding: data/file03 (deflated 56%)
[root@localhost ~]# ll
total 4
drwxr-xr-x. 2 root root 95 Mar 3 00:40 data
-rw-r--r--. 1 root root 1711 Mar 3 00:40 data.zip
[root@localhost ~]# unzip data.zip
Archive: data.zip
replace data/file01? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
inflating: data/file01
inflating: data/file02
inflating: data/file03
五、xz:目前主流压缩工具,压缩比大,效率高
格式:xz 选项 /path/to/file # 压缩指定文件,压缩后删除源文件
unxz /path/to/file # 解压缩文件
压缩格式为:file.xz 、file.lzma 、file.raw
常用选项:
-t # 测试压缩文件是否完整
-d # 解压缩文件,相当于unxz
-c # 压缩后转换为标准输出
-k # 保留源文件
-n # 指明压缩比(1~9),数值越大,压缩后体积越小
-F {auto|xz|lzma|raw} # 指明压缩后文件后缀名,auto为自动指定
xzcat /文件 # 查看xz工具压缩的压缩文件
示例:
[root@localhost data]# ll -h
total 12K
-rw-r--r--. 1 root root 938 Mar 2 14:54 file01
-rw-r--r--. 1 root root 938 Mar 2 14:54 file02
-rw-r--r--. 1 root root 938 Mar 2 14:54 file03
[root@localhost data]# xz -9 -c file01 > file01.xz
[root@localhost data]# ll
total 16
-rw-r--r--. 1 root root 938 Mar 2 14:54 file01
-rw-r--r--. 1 root root 504 Mar 3 00:50 file01.xz
-rw-r--r--. 1 root root 938 Mar 2 14:54 file02
-rw-r--r--. 1 root root 938 Mar 2 14:54 file03
[root@localhost data]# xz -9 -F lzma file02
[root@localhost data]# ll
total 16
-rw-r--r--. 1 root root 938 Mar 2 14:54 file01
-rw-r--r--. 1 root root 504 Mar 3 00:50 file01.xz
-rw-r--r--. 1 root root 458 Mar 2 14:54 file02.lzma
-rw-r--r--. 1 root root 938 Mar 2 14:54 file03
[root@localhost data]# xz -9 -F lzma -k file03
[root@localhost data]# ll
total 20
-rw-r--r--. 1 root root 938 Mar 2 14:54 file01
-rw-r--r--. 1 root root 504 Mar 3 00:50 file01.xz
-rw-r--r--. 1 root root 458 Mar 2 14:54 file02.lzma
-rw-r--r--. 1 root root 938 Mar 2 14:54 file03
-rw-r--r--. 1 root root 458 Mar 2 14:54 file03.lzma
六、tar归档工具,可调用压缩工具实现压缩归档
格式: tar 选项 归档包名称 目标归档文件
常用选项:
-cf # 将指定的多个目标文件归档为指定的归档包文件
-xf /归档包名 -C /指定目录 # 将指定的归档包展开到指定目录
无-C指定的话默认为展开到当前目录
-tf /归档包 # 查看归档包里的文件列表
-tvf /归档包 # 查看归档包里的文件列表(详细信息)
-j # 调用bzip2压缩工具,压缩比高,压缩耗时高
-J # 调用xz压缩工具,压缩比小,压缩耗时低
-z # 调用gzip压缩工具,压缩比相对较高,压缩速度快
-v #显示打包或展开过程
示例:归档及展开
[root@localhost ~]# ll
total 0
drwxr-xr-x. 2 root root 48 Mar 3 01:02 data
[root@localhost ~]# tar -cvf data.tar data/
data/
data/file01
data/file02
data/file03
[root@localhost ~]# ll
total 12
drwxr-xr-x. 2 root root 48 Mar 3 01:02 data
-rw-r--r--. 1 root root 10240 Mar 3 01:02 data.tar
[root@localhost ~]# tar -tvf data.tar
drwxr-xr-x root/root 0 2020-03-03 01:02 data/
-rw-r--r-- root/root 938 2020-03-02 14:54 data/file01
-rw-r--r-- root/root 938 2020-03-02 14:54 data/file02
-rw-r--r-- root/root 938 2020-03-02 14:54 data/file03
[root@localhost ~]# rm -rf data
[root@localhost ~]# ll
total 12
-rw-r--r--. 1 root root 10240 Mar 3 01:02 data.tar
[root@localhost ~]# tar -xvf data.tar -C ./
data/
data/file01
data/file02
data/file03
[root@localhost ~]# ll
total 12
drwxr-xr-x. 2 root root 48 Mar 3 01:02 data
-rw-r--r--. 1 root root 10240 Mar 3 01:02 data.tar
示例:压缩归档及解压展开
压缩归档:
[root@localhost ~]# ll
total 0
drwxr-xr-x. 2 root root 48 Mar 3 01:02 data01
drwxr-xr-x. 2 root root 48 Mar 3 01:02 data02
drwxr-xr-x. 2 root root 48 Mar 3 01:02 data03
drwxr-xr-x. 2 root root 6 Mar 3 01:06 dir0
[root@localhost ~]# tar -jcvf data01.tar.bz2 data01
data01/
data01/file01
data01/file02
data01/file03
[root@localhost ~]# tar -Jcvf data02.tar.xz data02
data02/
data02/file01
data02/file02
data02/file03
[root@localhost ~]# tar -zcvf data03.tar.gz data03
data03/
data03/file01
data03/file02
data03/file03
[root@localhost ~]# ll
total 12
drwxr-xr-x. 2 root root 48 Mar 3 01:02 data01
-rw-r--r--. 1 root root 754 Mar 3 01:07 data01.tar.bz2
drwxr-xr-x. 2 root root 48 Mar 3 01:02 data02
-rw-r--r--. 1 root root 636 Mar 3 01:08 data02.tar.xz
drwxr-xr-x. 2 root root 48 Mar 3 01:02 data03
-rw-r--r--. 1 root root 608 Mar 3 01:08 data03.tar.gz
drwxr-xr-x. 2 root root 6 Mar 3 01:06 dir0
解压展开:
[root@localhost ~]# ll
total 12
drwxr-xr-x. 2 root root 48 Mar 3 01:02 data01
-rw-r--r--. 1 root root 754 Mar 3 01:07 data01.tar.bz2
drwxr-xr-x. 2 root root 48 Mar 3 01:02 data02
-rw-r--r--. 1 root root 636 Mar 3 01:08 data02.tar.xz
drwxr-xr-x. 2 root root 48 Mar 3 01:02 data03
-rw-r--r--. 1 root root 608 Mar 3 01:08 data03.tar.gz
drwxr-xr-x. 2 root root 6 Mar 3 01:06 dir0
[root@localhost ~]# tar -jxvf data01.tar.bz2 -C dir0/
data01/
data01/file01
data01/file02
data01/file03
[root@localhost ~]# tar -Jxvf data02.tar.xz -C dir0/
data02/
data02/file01
data02/file02
data02/file03
[root@localhost ~]# tar -zxvf data03.tar.gz -C dir0/
data03/
data03/file01
data03/file02
data03/file03
[root@localhost ~]# ll dir0/
total 0
drwxr-xr-x. 2 root root 48 Mar 3 01:02 data01
drwxr-xr-x. 2 root root 48 Mar 3 01:02 data02
drwxr-xr-x. 2 root root 48 Mar 3 01:02 data03