解压、压缩、打包
在Linux中,压缩文件的扩展名大多有*.tar
、*.tar.gz
、*.tgz
、*.gz
、*.Z
、*.bz2
、*.xz
等。Linux中有多个工具可以对文件进行压缩,所以扩展名的作用主要是为了识别该压缩文件是通过哪个工具进行压缩的。下面是扩展名的一些描述:
扩展名 | 描述 |
---|---|
*.Z | compress 程序压缩的文件(已经不流行,基本用不到) |
*.zip | zip 程序压缩的文件 |
*.gz | gzip 程序压缩的文件 |
*.bz2 | bzip2 程序压缩的文件 |
*.xz | xz 程序压缩的文件 |
*.tar | tar 程序打包的数据,并没有压缩 |
*.tar.gz | tar 程序打包的文件,并经过 gzip 的压缩 |
*.tar.bz2 | tar 程序打包的文件,并经过 bzip2 的压缩 |
*.tar.xz | tar 程序打包的文件,并经过 xz 的压缩 |
下面主要介绍 gzip
、bzip2
、xz
、tar
这4个工具(指令)。
gzip(压缩解压)
简介与语法
gzip可以说是应用最广的压缩指令,他可以解开compress
、zip
、gzip
等软件所压缩的文件,其压缩文件的档名为*.gz
。下面是主要的语法:
# 压缩文件
gzip [-cdtv#] 档名
选项与参数:
-c:将压缩的数据输出到屏幕上,可透过数据流重导向来处理
-d:解压
-t:用来检验一个压缩文件的一致性,查看文件是否有错误
-v:查看压缩比等信息
-#:#表示数字,代表压缩等级,-1 最快,压缩比最低,-9 最慢,压缩比最高,预设为 -6(压缩比=源文件/压缩文件)
# 查看压缩文件
zcat 档名.gz
案例操作
- 找出
/etc
下(不含子目录)容量最大的文件,并将它复制到/tmp
,然后用gzip
压缩
[root@instance-d619ad0f ~]# ls -ldS /etc/*
-rw-r--r--. 1 root root 670293 Jun 7 2013 /etc/services
-rw-r--r--. 1 root root 27726 Jun 4 19:43 /etc/ld.so.cache
-rw-r--r--. 1 root root 12288 Jun 4 19:38 /etc/aliases.db
-rw-r--r--. 1 root root 8892 Jun 10 2014 /etc/nanorc
...
[root@instance-d619ad0f tmp]# cp /etc/services .
[root@instance-d619ad0f tmp]# gzip -v services
services: 79.7% -- replaced with services.gz
压缩时,源文件本身会被压缩,所以压缩完成后,源文件就不存在了。
- 读取上面压缩文件
services.gz
的文件内容
# 可以通过 zcat/zmore/zless 读取压缩文件的内容
[root@instance-d619vf0h tmp]# zcat services.gz
...
- 解压文件
services.gz
[root@instance-d619ad0f tmp]# ls services*
services.gz
[root@instance-d619ad0f tmp]# gzip -d services.gz
[root@instance-d619ad0f tmp]# ls services*
services
解压后,压缩文件会被替换成源文件
- 将文件
services
使用最佳的压缩比压缩,并保留源文件
[root@instance-d619ad0f tmp]# gzip -cv -9 services > services1.gz
services: 79.8%
[root@instance-d619ad0f tmp]# gzip -v services
services: 79.7% -- replaced with services.gz
[root@instance-d619ad0f tmp]# ls -l /etc/services services*
-rw-r--r--. 1 root root 670293 Jun 7 2013 /etc/services
-rw-r--r-- 1 root root 135489 Aug 20 11:06 services1.gz
-rw-r--r-- 1 root root 136088 Aug 20 11:03 services.gz
- 在
services.gz
文件中,搜索关键词http
,并显示关键词所在的行号
[root@instance-d619ad0f tmp]# zgrep -n 'http' services.gz
14:# http://www.iana.org/assignments/port-numbers
89:http 80/tcp www www-http # WorldWideWeb HTTP
...
bzip2(压缩解压)
简介与语法
gzip
取代了compress
并提供了更好的压缩比,而bzip2
的压缩比比gzip
更高,且用法基本上与gzip
相同。下面是主要的语法:
# 压缩文件
bzip2 [-cdkvz#] 档名
选项与参数:
-c:降压所的过程产生的数据输出到屏幕上
-d:解压
-k:保留源文件
-v:显示压缩比等信息
-z:压缩的参数(有默认值,可以不加)
-#:与 gzip 相同,设定压缩比,-9最佳最慢,-1最差最快
# 查看压缩文件
bzcat 档名.bz2
案例操作
- 将
gzip
范例中的/tmp/services
以bzip2
压缩
[root@instance-d619ad0f tmp]# bzip2 -v services
services: 5.409:1, 1.479 bits/byte, 81.51% saved, 670293 in, 123932 out.
[root@instance-d619ad0f tmp]# ls -l services*
-rw-r--r-- 1 root root 135489 Aug 20 11:06 services1.gz
-rw-r--r-- 1 root root 123932 Aug 20 11:03 services.bz2
- 读取压缩文件
services.bz2
的内容
[root@instance-d619ad0f tmp]# bzcat services.bz2
...
- 解压文件
services.ba2
[root@instance-d619ad0f tmp]# bzip2 -d services.bz2
[root@instance-d619ad0f tmp]# ls services*
services services1.gz
- 用最佳压缩比压缩文件
services
,并保留源文件
# 方式一
[root@instance-d619ad0f tmp]# bzip2 -9 -kv services
services: 5.409:1, 1.479 bits/byte, 81.51% saved, 670293 in, 123932 out.
# 方式二
[root@instance-d619ad0f tmp]# bzip2 -9 -cv services > services1.bz2
services: 5.409:1, 1.479 bits/byte, 81.51% saved, 670293 in, 123932 out.
xz(压缩解压)
简介与语法
xz
是比bzip2
压缩比更好的一款工具,相对应的,压缩相同文件所花费时间也是gzip
、bzip2
、xz
这三者中最久的,其压缩文件的档名为*.xz
。下面是主要的语法:
# 压缩文件
xz [-dtlkc#] 档名
选项与参数:
-d:解压
-t:测试压缩文件的完整性,查看是否有错误
-l:列出压缩文件的相关信息
-k:保留源文件
-c:将压缩数据输出到屏幕
-#:与 gzip 相同,设定压缩比,-9最佳最慢,-1最差最快
# 查看压缩文件
xcat 档名.xz
案例操作
- 使用
xz
压缩将services
[root@instance-d619ad0f tmp]# xz -v services
services (1/1)
100 % 97.3 KiB / 654.6 KiB = 0.149
[root@instance-d619ad0f tmp]# ls -l services*
-rw-r--r-- 1 root root 123932 Aug 20 15:40 services1.bz2
-rw-r--r-- 1 root root 135489 Aug 20 11:06 services1.gz
-rw-r--r-- 1 root root 123932 Aug 20 11:03 services.bz2
-rw-r--r-- 1 root root 99608 Aug 20 11:03 services.xz
- 列出压缩文件
services.xz
的信息,然后读出该文件的内容
[root@instance-d619ad0f tmp]# xz -l services.xz
Strms Blocks Compressed Uncompressed Ratio Check Filename
1 1 97.3 KiB 654.6 KiB 0.149 CRC64 services.xz
[root@instance-d619vf0h tmp]# xzcat services.xz
...
- 解压文件
services.xz
[root@instance-d619ad0f tmp]# xz -d services.xz
[root@instance-d619ad0f tmp]# ls services*
services services1.bz2 services1.gz services.bz2
- 压缩文件
services
,并保留源文件
[root@instance-d619ad0f tmp]# xz -k services
[root@instance-d619ad0f tmp]# ls -l services*
-rw-r--r-- 1 root root 670293 Aug 20 11:03 services
-rw-r--r-- 1 root root 123932 Aug 20 15:40 services1.bz2
-rw-r--r-- 1 root root 135489 Aug 20 11:06 services1.gz
-rw-r--r-- 1 root root 123932 Aug 20 11:03 services.bz2
-rw-r--r-- 1 root root 99608 Aug 20 11:03 services.xz
tar(打包)
简介与语法
虽然gzip
、bzip2
、xz
都能够对目录进行压缩,但他们对目录的压缩是指将目录内的所有文件分别进行压缩,并不是像Windows
中的WinRAR
一样可以把好多数据压缩成一个文件。所以Linux
中压缩时可以通过打包指令tar
来打包压缩文件。
tar
可以将多个目录或文件打包成一个大文件,同时还可以通过gzip
/bzip2
/xz
的支持,将文件同时进行压缩。下面是主要的语法:
# 打包与压缩
tar [-z|-j|-J] [cv] [-f 代建立的新档名] filename...
# 查看档名
tar [-z|-j|-J] [tv] [-f 已存在的tar档名]
# 解压
tar [-z|-j|-J] [xv] [-f 已存在的tar档名] [-C 目录]
选项与参数
-c:建立打包文件,可搭配-v来查看过程中被打包的档名
-t:查看打包文件的内容含有哪些档名
-x:解打包或解压缩,可以搭配 -C 解压/解打包到特定目录
(注意:-c、-t、-x不可同时出现在一串指令列中)
-z:通过 gzip 压缩/解压,档名为 *.tar.gz
-j:通过 bzip2 压缩/解压,档名为 *.tar.bz2
-J:通过 xz 压缩/解压,档名为 *.tar.xz
(注意:-z、-j、-J不可同时出现在一串指令列中)
-v:在压缩/解压过程中,将正在处理的文件名显示到屏幕上
-f filename:-f 后要立即接要被处理的档名,建议-f单独写一个选项(以防忘记)
-C 目录:用于解压,可以指定解压到某个特定的目录
-p:保留备份数据的原本权限与属性,常用于备份重要的配置文件
-P:保留绝对路径,允许备份数据含有根目录
--exclude=FILE:在打包过程中,不要讲文件 FILE 打包
案例操作
- 分别使用
-z
、-j
、-J
来压缩/etc
,并查看所使用的时间
[root@instance-d619ad0f ~]# time tar -zpc -f etc.tar.gz /etc
tar: Removing leading `/' from member names
real 0m1.817s
user 0m1.348s
sys 0m0.068s
[root@instance-d619ad0f ~]# time tar -jpc -f etc.tar.bz2 /etc
tar: Removing leading `/' from member names
real 0m3.508s
user 0m3.290s
sys 0m0.059s
[root@instance-d619ad0f ~]# time tar -Jpc -f etc.tar.xz /etc
tar: Removing leading `/' from member names
real 0m16.277s
user 0m15.364s
sys 0m0.219s
[root@instance-d619ad0f ~]# ll etc*
-rw-r--r-- 1 root root 9301856 Aug 20 16:50 etc.tar.bz2
-rw-r--r-- 1 root root 10744695 Aug 20 16:50 etc.tar.gz
-rw-r--r-- 1 root root 6969436 Aug 20 16:51 etc.tar.xz
压缩的时候出现了警告tar: Removing leading `/' from member names
,主要是告诉使用者压缩的时候移除了开头的根目录/
。去掉根目录主要是为了安全。如果没有拿掉根目录,解压后的档名是绝对路径,即压缩之前文件放在哪就会解压到哪。如果某个文件做过修改,这样就会覆盖文件恢复为压缩前的数据。
- 查看tar文件的数据内容
[root@instance-d619ad0f ~]# tar -jtv -f etc.tar.bz2
...
drwxr-xr-x root/root 0 2018-05-30 19:38 etc/ppp/
-rwxr-xr-x root/root 1687 2018-01-03 00:29 etc/ppp/ipv6-down
-rwxr-xr-x root/root 386 2018-01-03 00:29 etc/ppp/ip-down
-rwxr-xr-x root/root 3182 2018-01-03 00:29 etc/ppp/ipv6-up
-rwxr-xr-x root/root 6426 2018-01-03 00:29 etc/ppp/ip-up.ipv6to4
-rwxr-xr-x root/root 430 2018-01-03 00:29 etc/ppp/ip-up
-rwxr-xr-x root/root 3214 2018-01-03 00:29 etc/ppp/ip-down.ipv6to4
...
etc/ppp
、etc/ppp/ipv6-down
这些前面并没有根目录/
,这和前面的警告信息tar: Removing leading `/' from member names
相对应。
- 压缩文件时保留根目录,并查看备份文件的内容
[root@instance-d619ad0f ~]# tar -jpPc -f etc.and.root.tar.bz2 /etc
[root@instance-d619ad0f ~]# tar -jtvf etc.and.root.tar.bz2
...
drwxr-xr-x root/root 0 2018-05-30 19:38 /etc/ppp/
-rwxr-xr-x root/root 1687 2018-01-03 00:29 /etc/ppp/ipv6-down
-rwxr-xr-x root/root 386 2018-01-03 00:29 /etc/ppp/ip-down
-rwxr-xr-x root/root 3182 2018-01-03 00:29 /etc/ppp/ipv6-up
-rwxr-xr-x root/root 6426 2018-01-03 00:29 /etc/ppp/ip-up.ipv6to4
-rwxr-xr-x root/root 430 2018-01-03 00:29 /etc/ppp/ip-up
-rwxr-xr-x root/root 3214 2018-01-03 00:29 /etc/ppp/ip-down.ipv6to4
...
- 解压备份数据
etc.tar.bz2
# 解压到当前目录
[root@instance-d619ad0f ~]# tar -jx -f etc.tar.bz2
[root@instance-d619ad0f ~]# ls
etc etc.and.root.tar.bz2 etc.tar.bz2 etc.tar.gz etc.tar.xz
# 解压到 /tmp 目录
[root@instance-d619ad0f ~]# tar -jx -f etc.tar.bz2 -C /tmp
[root@instance-d619ad0f ~]# ls -ld etc /tmp/etc
drwxr-xr-x 87 root root 4096 Aug 13 13:47 etc
drwxr-xr-x 87 root root 4096 Aug 13 13:47 /tmp/etc
- 解压单一的文件
# 查找文件
[root@instance-d619ad0f ~]# tar -jtv -f etc.tar.bz2 | grep 'shadow'
---------- root/root 480 2018-07-06 16:14 etc/gshadow
---------- root/root 636 2018-06-30 17:38 etc/shadow-
---------- root/root 758 2018-08-13 13:47 etc/shadow
---------- root/root 470 2018-06-04 19:39 etc/gshadow-
# 解压文件
[root@instance-d619ad0f ~]# tar -jxv -f etc.tar.bz2 etc/shadow
etc/shadow
# 查看文件
[root@instance-d619ad0f ~]# ll etc
total 4
---------- 1 root root 758 Aug 13 13:47 shadow
- 打包某目录,但不包含该目录下的某个文件
# 打包压缩 /etc /root 两个目录并存在 /root 目录下,文档名为system.tar.bz2, 压缩包内不包含 /root目录下etc开头的文档以及压缩包本身
[root@instance-d619ad0f ~]# tar -jc -f /root/system.tar.bz2 --exclude=/root/etc* \
> --exclude=/root/system.tar.bz2 /etc /root
tar: Removing leading `/' from member names
[root@instance-d619ad0f ~]# ll
total 44568
drwxr-xr-x 2 root root 4096 Aug 21 11:49 etc
-rw-r--r-- 1 root root 9301403 Aug 20 17:22 etc.and.root.tar.bz2
-rw-r--r-- 1 root root 9301856 Aug 20 16:50 etc.tar.bz2
-rw-r--r-- 1 root root 10744695 Aug 20 16:50 etc.tar.gz
-rw-r--r-- 1 root root 6969436 Aug 20 16:51 etc.tar.xz
-rw-r--r-- 1 root root 9306299 Aug 21 11:55 system.tar.bz2
- 只打包比
/etc/passwd
新的文件
[root@instance-d619ad0f ~]# ll /etc/passwd
-rw-r--r-- 1 root root 1139 Jul 6 16:55 /etc/passwd
[root@instance-d619ad0f ~]# tar -jcv -f /root/etc.newer.then.passwd.tar.bz2 --newer-mtime='2018/07/06' /etc/*
...
/etc/yum/vars/
tar: /etc/yum/vars/infra: file is unchanged; not dumped
tar: /etc/yum/vars/contentdir: file is unchanged; not dumped
/etc/yum/pluginconf.d/
tar: /etc/yum/pluginconf.d/langpacks.conf: file is unchanged; not dumped
tar: /etc/yum/pluginconf.d/versionlock.list: file is unchanged; not dumped
tar: /etc/yum/pluginconf.d/fastestmirror.conf: file is unchanged; not dumped
tar: /etc/yum/pluginconf.d/versionlock.conf: file is unchanged; not dumped
tar: /etc/yum/version-groups.conf: file is unchanged; not dumped
/etc/yum/fssnap.d/
tar: /etc/yum.conf: file is unchanged; not dumped
/etc/yum.repos.d/
tar: /etc/yum.repos.d/epel-testing.repo: file is unchanged; not dumped
tar: /etc/yum.repos.d/CentOS-Debuginfo.repo: file is unchanged; not dumped
...
[root@instance-d619ad0f ~]# tar -jtv -f etc.newer.then.passwd.tar.bz2 | grep -v '/$'
-rw-r--r-- root/root 603 2018-07-06 16:14 etc/group
---------- root/root 480 2018-07-06 16:14 etc/gshadow
-rw-r--r-- root/root 1139 2018-07-06 16:55 etc/passwd
-rw-r--r-- root/root 1145 2018-07-06 16:26 etc/passwd-
---------- root/root 758 2018-08-13 13:47 etc/shadow
-rw-r--r-- root/root 328 2018-08-21 13:38 etc/sshd.deny.hosteye
总结
- 目前比较流行的压缩工具有
gzip
、bzip2
、xz
。 - 压缩率比较:
xz
<bzip2
<gzip
(相同源文件,压缩率越小,压缩文件越小) - 压缩时间:
xz
>bzip2
>gzip
tar
用于打包文件,支持用gzip
、bzip2
、xz
压缩打包文件