6.1 压缩打包介绍 6.2 gzip压缩工具 6.3 bzip2压缩工具 6.4 xz压缩工具

6.1 压缩打包介绍

Linux环境常见压缩文件类型:

.zip,.gz,.bz2,.xz,
.tar.gz,.tar.bz2,.tar.xz

压缩打包的目的

  • 方便文件传输
  • 节省磁盘空间
  • 减少传输花费的时间
  • 节省带宽

6.2 gzip压缩工具

gzip是GNUzip的缩写,它是一个GNU自由软件的文件压缩程序,用于UNIX系统的文件压缩。我们在Linux中经常会用到后缀为.gz的文件,它们就是gzip格式的。

注意: gzip不能压缩目录文件
语法: gzip [options] [filename]
options:
-d:解压缩(=gunzip)
-num:指定压缩等级,此处num表示1~9数字,9压缩最好,默认为6(压缩等级越高,CPU消耗越高)

 压缩  直接压缩

[root@cham3 d6z]# find /etc/ -type f -name "*conf"  -exec cat {} >> 1.txt \;
[root@cham3 d6z]# du -sh 1.txt               文件压缩前大小
2.2M	1.txt
[root@cham3 d6z]# wc -l 1.txt                查看其内容总行数
34392 1.txt
[root@cham3 d6z]# gzip 1.txt                 压缩
[root@cham3 d6z]# ls
1.txt.gz                                     压缩后,源文件会消失
[root@cham3 d6z]# du -sh 1.txt.gz 
336K	1.txt.gz                             压缩后文件大小
[root@cham3 d6z]# gzip -d 1.txt.gz           解压文件
[root@cham3 d6z]# du -sh 1.txt
1.3M	1.txt                                解缩后文件大小
[root@cham3 d6z]# wc -l 1.txt
34392 1.txt
[root@cham3 d6z]# gzip -1 1.txt              指定压缩级别
[root@cham3 d6z]# du -sh 1.txt.gz 
392K	1.txt.gz
[root@cham3 d6z]# gunzip 1.txt.gz 
[root@cham3 d6z]# gzip -9 1.txt              指定压缩级别
[root@cham3 d6z]# du -sh 1.txt.gz 
336K	1.txt.gz
[root@cham3 d6z]# file 1.txt.gz             file 1.txt.gz   查看文件属性
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Thu Nov  9 10:41:48 2017, max compression

 指定压缩目录

[root@cham3 d6z]# gzip -c 1.txt > /tmp/1.txt.gz 
[root@cham3 d6z]# ls /tmp/1.txt.gz
/tmp/1.txt.gz
[root@cham3 d6z]# ls  压缩完成后源文件不会消失
1.txt  2.txt.gz
[root@cham3 d6z]# file /tmp/1.txt.gz
/tmp/1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Thu Nov  9 11:12:44 2017

使用zcat命令: 

[root@cham3 d6z]# zcat 2.txt.gz     可查看压缩文件内部内容

解压

直接解压

gzip -d

[root@cham3 d6z]# gzip -d 1.txt.gz           解压文件    解压后文件消失
[root@cham3 d6z]# du -sh 1.txt
1.3M	1.txt                                解缩后文件大小
[root@cham3 d6z]# wc -l 1.txt
34392 1.txt

eg2:gunzip 

[root@cham3 d6z]# gunzip 1.txt
[root@cham3 d6z]# ls
1.txt  2.txt.gz

 

6.3 bzip2 压缩工具

bzip2 是一个基于Burrows-Wheeler 变换的无损压缩软件,压缩效果比传统的LZ77/LZ78压缩算法来得好。它是一款免费软件。可以自由分发免费使用。它广泛存在于UNIX&LINUX的许多发行版本中。bzip2能够进行高质量的数据压缩。它利用先进的压缩技术,能够把普通的数据文件压缩10%至15%,压缩的速度和解压的效率都非常高!支持大多数压缩格式,包括tar、gzip 等等。

注意: bzip2不可以压缩目录文件
语法: bzip2 [options] [filename]
options:
-d:解压缩
-z:压缩(=bzip2,所以可以不带该参数直接使用)

如果没有安装请先安装

[root@cham002 dabao]# yum -y install bzip2-x86_64 
已加载插件:fastestmirror


bzip2的使用方法同gzip。

压缩

[root@cham3 d6z]# bzip2 1.txt
[root@cham3 d6z]# ls
1.txt.bz2  2.txt.gz
[root@cham3 d6z]# du -sh 1.txt.bz2 
172K	1.txt.bz2                 较gzip压缩程度更高

解压

[root@cham3 d6z]# bzip2 -d 1.txt.bz2 
[root@cham3 d6z]# ls
1.txt  2.txt.gz
[root@cham3 d6z]# du -sh *.txt
1.3M	1.txt
1.3M	2.txt
——————————————————————————————————————————————————————
[root@cham3 d6z]# bzip2 -z 1.txt
[root@cham3 d6z]# ls
1.txt.bz2  2.txt


[root@cham3 d6z]# bunzip2 1.txt.bz2   方法2
[root@cham3 d6z]# ls
1.txt  2.txt
[root@cham3 d6z]# !du
du -sh *.txt
1.3M	1.txt
1.3M	2.txt

说明: 同gzip,该命令也可指定目录进行压缩和解压。

案例

情景: 查看某目录文件时里面有一个文件1.txt,使用cat等命令查看其内容时出现如下提示:"1.txt" may be a binary file. See it anyway? 。此时按‘y’,屏幕会出现一堆乱码,赶紧按q退出,然后查找器原因:使用file命令!

[root@cham3 d6z]# ls
1.txt  2.txt.gz  3.txt
[root@cham3 d6z]# less 1.txt
"1.txt" may be a binary file.  See it anyway? 
[root@cham3 d6z]# file 1.txt
1.txt: bzip2 compressed data, block size = 900k

由file命令查看该文件信息后得知其为‘.bz2’压缩文件,即使用bzcat命令即可查看,并更改其文件名为正确格式以防再次被误导。

[root@cham3 d6z]# bzcat 1.txt
由于内容太多,在此不做演示
[root@cham3 d6z]# mv 1.txt 1.txt.bz2

6.4 xz压缩工具

xz是一种压缩文件格式,采用LZMA SDK压缩,目标文件较gzip压缩文件(.gz或·tgz)小30%,较·bz2小15%。

 注意: xz不可用于压缩目录文件
语法: xz [options] [filename]
options:
-d:解压缩
使用方法同gzip、bzip2,压缩程度更高。

压缩

[root@cham3 d6z]# xz 1.txt
[root@cham3 d6z]# du -sh 1.txt.xz 
40K	1.txt.xz
[root@cham3 d6z]# file 1.txt.xz    查看压缩文件类型
1.txt.xz: XZ compressed data
[root@cham3 d6z]#xzcat 2.txt.xz 查看压缩文件内容  “内容过多不复制”

解压

[root@cham3 d6z]# xz -d 1.txt.xz 
[root@cham3 d6z]# ls
1.txt  2.txt.gz  3.txt
[root@cham3 d6z]# du -sh 1.txt 
2.0M	1.txt
————————————————————————————————————————————————————————
[root@cham3 d6z]# unxz 1.txt.xz 
[root@cham3 d6z]# ls
1.txt  2.txt.gz  3.txt

 同gzip、bzip2,该命令也可指定目录进行压缩和解压。

[root@cham3 d6z]# xz -c 1.txt > /tmp/1.txt.xz
[root@cham3 d6z]# ls /tmp/1.txt.
1.txt.bz2  1.txt.gz   1.txt.xz   
[root@cham3 d6z]# ls /tmp/1.txt.xz
/tmp/1.txt.xz
[root@cham3 d6z]# xz -d -c /tmp/1.txt.xz > ./4.txt
[root@cham3 d6z]# ls
1.txt  2.txt.gz  3.txt  4.txt

 

猜你喜欢

转载自my.oschina.net/u/3708120/blog/1796415