Chinaunix首页 | 论坛 | 博客
  • 博客访问: 140927
  • 博文数量: 70
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 770
  • 用 户 组: 普通用户
  • 注册时间: 2017-11-04 11:19
文章分类

全部博文(70)

文章存档

2018年(69)

2016年(1)

我的朋友

分类: LINUX

2018-06-24 17:04:29

6.1 压缩打包介绍
为什么要使用压缩包:
1、备份打包,节省空间
2、传输带宽得到节约,并且降低传输成本

常见的压缩文件:后缀名,Linux中后缀不再是衡量文件类型的标准。但是也有习惯性的使用。
win:.rar   ,  .zip   ,  .7z
linux:.zip  ,  .gz   ,  .bz2  ,  .xz  ,  .tar.gz   ,   .tar.bz2  ,   .tar.zx


6.2 gzip压缩工具
[root@localhost ~]# cd /tmp/
[root@localhost tmp]# mkdir gzip
[root@localhost tmp]# cd gzip/
[root@localhost gzip]# find /etc/ -type f -name "*.conf" -exec cat {} >> a.txt \;[root@localhost gzip]# 
创建一个测试用的目录,然后创建一个较大的文件。
[root@localhost gzip]# find /etc/ -type f -name "*.conf" -exec cat {} >> a.txt \;  //多次重复,文件尽量大一点。
[root@localhost gzip]# du -sh a.txt
2.2M a.txt
压缩: 将文件创建为压缩文件,原文件则消失了
[root@localhost gzip]# gzip a.txt        //压缩,直接加上需要压缩的文件
[root@localhost gzip]# ls
a.txt.gz
[root@localhost gzip]# du -sh a.txt.gz 
376K a.txt.gz
解压:
[root@localhost gzip]# gzip -d a.txt.gz 
[root@localhost gzip]# ls
a.txt
[root@localhost gzip]# du -sh a.txt         //发现跟之前的大小有出入,因为创建方式,所以文件大小之前是虚高的
1.4M a.txt                                               //但是不会删除文件中的内容的。

压缩级别:
gzip可以控制压缩级别,默认按照级别 6 压缩,选择范围:1-9,使用方法:
[root@localhost gzip]# gzip -9 a.txt     //压缩级别越高,那么耗费CPU资源就越多。一般保持默认即可。
[root@localhost gzip]# du -sh a.txt.gz 
372K a.txt.gz
不能压缩目录

查看:
[root@localhost gzip]# file a.txt.gz             //查看文件类型
a.txt.gz: gzip compressed data, was "a.txt", from Unix, last modified: Sun Jun 24 16:19:08 2018, max compression

[root@localhost gzip]# zcat a.txt.gz        //查看压缩文件的内容。

另外的创建压缩文件方式:原文件依旧存在
[root@localhost gzip]# gzip -c a.txt > /root/a.txt.gz
[root@localhost gzip]# ls
a.txt
[root@localhost gzip]# ls /root/
a.txt.gz
解压:压缩文件解压后,使原文件依旧存在,可以重命名解压后的文件名
[root@localhost gzip]# gzip -d -c /root/a.txt.gz > /tmp/gzip/b.txt
[root@localhost gzip]# ls
a.txt  b.txt
[root@localhost gzip]# ls /root/
a.txt.gz

6.3 bzip2压缩工具
第一步安装:
[root@localhost gzip]# yum -y install bzip2
[root@localhost gzip]# bzip2 a.txt                 //与gzip的使用方式差不多。
[root@localhost gzip]# ls
a.txt.bz2
[root@localhost gzip]# du -sh a.txt.bz2         //比gzip的压缩率更高。默认压缩级别:9
160K a.txt.bz2
不支持压缩目录

同样支持选项  “-c”  和  “-d”
[root@localhost gzip]# bzip2 -c a.txt > /root/a.txt.bz2
[root@localhost gzip]# bzip2 -d -c /root/a.txt.bz2 > c.txt

注意,linux中对于后缀名无要求:但是约定俗成,要遵循
[root@localhost gzip]# ls
a.txt.bz2  b.txt  c.txt
[root@localhost gzip]# mv a.txt.bz2 a.txt
[root@localhost gzip]# ls
a.txt  b.txt  c.txt
[root@localhost gzip]# file a.txt 
a.txt: bzip2 compressed data, block size = 900k

查看文件内容:
[root@localhost gzip]# bzcat a.txt.bz2


6.4 xz压缩工具
与gzip、bzip2的使用方法类似:

压缩程度更高,所以耗费CPU更多,压缩级别默认9

对于排序:但是也要注意不同的文件内容,使用不同的压缩命令效果也不同。因为文件内容重复的话,那么使用xz效果就很好,如果内容不重复,那么xz的压缩效果可能就不是那么好了
就当前文件的测试结果,默认压缩程度:  gzip < bzip2 < xz
阅读(1162) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~