Linux入门之压缩与归档
LInux中的有很多对文件压缩和归档的工具,而且其压缩的算法也在不段的更新和突破,但是作为对数据的稳定一般常用的工具不会太多,下面看看一些压缩工具吧,这里我在RHEL5 (red hat 5)或CentOS 6版本的系统上列举这些工具
compress
这是一个很老旧的工具了,一般只有系统内核上的弄些不常改变的文件使用此工具压缩,但是这个工具对于小型数据的压缩效果还是不会很差的。
#使用rpm测试软件包是否已经安装
[root@localhost ~]# rpm -qa | grep compress [root@localhost ~]# rpm -qa | grep ncompress
这里我们发现,因为这个工具过于老旧,所以对于现在版本的系统来说,默认是不会安装这种过时的工具的。那么现在我们来安装一下。
安装方法一般常用三种(rpm包安装,yum仓库安装,源码包编译安装):
yum仓库安装:
#首先,需要在/etc/yum.repos.d目录下添加一个后缀为.repo的文件,然后写入特定的格式
[root@localhost ~]# cat /etc/yum.repos.d/my.repo [base] #repo仓库的ID 名称,必须唯一 name=baseiso #repo仓库的描述信息 baseurl=file:///mnt/iso/Server #这里是本地安装,那么源文件地址就是本地的挂载目录 enabled=1 #是否启用,1表示启用,0为禁用,启用才能用啊 gpgcheck=0 #是否为包的信息做验证,这里是光盘挂载的,所以不验证
#下面通过yum进行安装
[root@localhost ~]# yum install ncompress -y Loaded plugins: katello, product-id, security, subscription-manager Updating certificate-based repositories. Unable to read consumer identity Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package ncompress.i386 0:4.2.4-47 set to be updated --> Finished Dependency Resolution .............................................................. #此处信息过长我略过 Installed: ncompress.i386 0:4.2.4-47 Complete! #显示这3行信息表示已经安装成功
#通过rpm软件包管理器进行安装
[root@localhost ~]# rpm -qa | grep ncompress #查看是否已经安装 ncompress-4.2.4-47 #这里显示已经安装了一个版本
#现在卸载掉
[root@localhost ~]# rpm -e ncompress
#cd(切换)到软件包所在目录路径
[root@localhost ~]# cd /mnt/iso/Server/
#通过find 命令查找是否存在ncompress软件包
[root@localhost Server]# find ./ -name "ncom*\.rpm" ./ncompress-4.2.4-47.i386.rpm
#可能有些系统安装时选择最小化安装,没有find这个命令,也可用下面的命令查看
[root@localhost Server]# ls -ld ./ncom*\.rpm -r--r--r-- 213 root root 23896 Jan 18 2007 ./ncompress-4.2.4-47.i386.rpm [root@localhost Server]# ls | grep '^ncom.*\.rpm$' ncompress-4.2.4-47.i386.rpm
#现在总算是找到一个版本的软件包,下面开始安装
[root@localhost Server]# rpm -ivh ./ncompress-4.2.4-47.i386.rpm warning: ./ncompress-4.2.4-47.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186 Preparing... ########################################### [100%] 1:ncompress ########################################### [100%]
#因为yum仓库是依赖于rpm软件管理器存在来安装的,所有直接使用rpm安装速度会特别快,因为一般情况不会去查找有依赖关系的包
#再次查看nocompress已经安装了
[root@localhost Server]# rpm -qa | grep "^ncom.*" ncompress-4.2.4-47
命令格式:
compress [option] /path/to/file
option:
-d : 解压缩,后面跟压缩文件
-c : 结果输出至标准输出,不删除源文件
-v : 显示过程信息
#使用内置解压缩
uncompress /path/to/file.Z
#使用zcat工具查看压缩文件数据并重定向一个新文件
zcat /path/to/file.Z > /path/to/file
注意:默认情况下使用compress工具里的命令压缩和解压缩都会删除源文件
压缩测试:
#拷贝一个日志文件到自定义的目录中
[root@localhost test3]# cp /var/log/messages ./messages
#对messages文件进行压缩
[root@localhost test3]# compress messages
#查看当前目录下的文件
[root@localhost test3]# ls messages.Z
#发现只剩下压缩文件了,源文件被自动删除
#下面使用解压命令
[root@localhost test3]# compress -d messages.Z
#同样发现只剩下解压出来的文件,而压缩文件被删除
[root@localhost test3]# ls messages
#使用-c选项将数据压缩并重定向一个新文件中
[root@localhost test3]# compress -c messages > messages.Z
#现在可以发现文件都被保留了
[root@localhost test3]# ls messages messages.Z
#同样使用-c选择配合-d进行解压数据重定向到m1文件中
[root@localhost test3]# compress -c -d messages.Z > m1
#使用zcat工具读取压缩文件数据重定向到m2文件中
[root@localhost test3]# zcat messages.Z > m2
#查询刚才生成的所有文件
[root@localhost test3]# ls -l total 68 -rw-r--r-- 1 root root 14099 Aug 15 14:51 m1 -rw-r--r-- 1 root root 14099 Aug 15 14:52 m2 -rw------- 1 root root 14099 Aug 15 14:41 messages -rw-r--r-- 1 root root 3467 Aug 15 14:48 messages.Z
#发现源文件大小未改变,说明三种解压方法都是可行的
gzip
这算是目录linux中用的最广泛的工具了,因为历史比较久,所有相对的压缩算法等处理对文件是很稳定的
命令用法:
gzip [option] /path/to/file
option:
-d :解压缩,相当于调用gunzip命令
-c :将压缩或解压缩的结果进行标准输出
-# :压缩的比例等级,1~9,等级越高,压缩比越大,不指定默认为6
这里同样支持zcat命令进行重定向
zcat /path/to/file.gz > /path/to/file
压缩测试:
#直接使用命令压缩
[root@localhost test3]# gzip messages
#发现源文件被删除了
[root@localhost test3]# ls messages.gz messages.Z
#使用 -d 选项进行解压缩
[root@localhost test3]# gzip -d messages.gz [root@localhost test3]# ls messages messages.Z
#这里发现gzip默认同样不会保留源文件
#使用-c重定向来保流源文件,-9表示最大压缩比例
[root@localhost test3]# gzip -c -9 messages > messages.gz [root@localhost test3]# ls messages messages.gz messages.Z
#同样使用-d配合-c选项来保存原有压缩文件
[root@localhost test3]# gzip -d -c messages.gz > m1
#.gz文件支持zcat查看压缩文件
[root@localhost test3]# zcat messages.gz > m2 [root@localhost test3]# ls m1 m2 messages messages.gz messages.Z
#以上方法都会保留原文件进行压缩和解压缩
bzip2
这是一个相对于gzip工具较新的工具,它的压缩算法更加先进,压缩比例更高了
用法:bzip2 [option] /path/to/file
option:
-k : keep,表示处理时保存原文件
-d :解压缩
-# :压缩比,1~9,默认为6
注意:这里.bz文件就不要用zcat去查看了,有自带的专有工具查看:
bzcat /path/to/file.bz2
压缩测试:
#保留源文件进行压缩
[root@localhost test3]# bzip2 -k messages [root@localhost test3]# ls messages messages.bz2 messages.gz messages.Z
#删除源文件
[root@localhost test3]# rm -f messages
#保留源文件解压
[root@localhost test3]# bzip2 -k -d messages.bz2
#想要自定义路径或文件名,那就用下面的方法吧,bzip2同样提供了-c选项
[root@localhost test3]# bzcat messages.bz2 > m1 [root@localhost test3]# bzip2 -c m1 > m1.bz2 [root@localhost test3]# ls m1 m1.bz2 messages messages.bz2 messages.gz messages.Z
xz
这决定是一个够新的工具,而且其压缩算法最先进,压缩比也特别高;同样因为工具较新,默认情况下,此类版本系统默认是不会安装这个工具的,那么安装方法可以参照以上ncompress工具的方法进行安装
命令用法:
xz [option] /path/to/file
option:
-k :处理时保留原文件
-d :解压文件,相当于unxz (-dc 同样进行标准输出)
-# : 压缩比例,默认6,1~9
注意:一家工具一套模式,同样,.xz的压缩文件也只有使用xzcat工具去查看:
xzcat /path/to/file.xz
压缩测试:
#删除刚才解压出的m1文件,方便下面的测试
[root@localhost test3]# rm -f m1
#保留原文件进行压缩
[root@localhost test3]# xz -k messages
#显示出.xz结尾的为压缩文件
[root@localhost test3]# ls m1.bz2 messages messages.bz2 messages.gz messages.xz messages.Z
#删除原文件
[root@localhost test3]# rm -f messages
#配合-k使用-d进行保留压缩文件解压
[root@localhost test3]# xz -k -d messages.xz [root@localhost test3]# ls m1.bz2 messages messages.bz2 messages.gz messages.xz messages.Z
#当然xz也提供了-c选项来可以重定向一个指定的路径
[root@localhost test3]# xz -dc messages.xz > m1
#删除m1文件
[root@localhost test3]# rm -f m1
#使用提供的xzcat特定工具进行解压缩
[root@localhost test3]# xzcat messages.xz > m1
比较一下压缩比例:
[root@localhost test3]# ls -l total 80 -rw------- 1 root root 14099 Aug 15 14:41 messages #原始文件大小 140099字节 -rw------- 1 root root 1237 Aug 15 14:41 messages.bz2 #bzip2 压缩为 1237字节 -rw-r--r-- 1 root root 1302 Aug 15 15:07 messages.gz #gzip 压缩为 1302字节 -rw------- 1 root root 1140 Aug 15 14:41 messages.xz #xz 压缩为 1140字节 -rw-r--r-- 1 root root 3467 Aug 15 15:45 messages.Z #compress 压缩为3467字节
解析:从这里看出越新的工具的压缩比越大,压缩的算法也会越高
zip
以上的命令都是对单个或多个文件进行压缩,却无法压缩整个目录,这时就要借助于zip工具,可以同时支持目录和文件的压缩
用法:
压缩用法
zip [option] /path/to/d_file /path/to/s_file
-r:对目录递归压缩
解压缩
unzip /path/to/file.zip
cat /var/log/messages | zip messages -
unzip -p /path/to/file.zip > /path/to/file #读取压缩文件数据写入指定文件
压缩测试:
#备份目录,注意第一个参数是打包的路径,而第二个参数才是源文件
[root@localhost test3]# zip -r ./sysconfig /etc/sysconfig/ adding: etc/sysconfig/ (stored 0%) adding: etc/sysconfig/ipmi (deflated 70%) adding: etc/sysconfig/autofs (deflated 57%) adding: etc/sysconfig/network (deflated 18%) adding: etc/sysconfig/networking/ (stored 0%) adding: etc/sysconfig/networking/devices/ (stored 0%) adding: etc/sysconfig/networking/profiles/ (stored 0%) adding: etc/sysconfig/networking/profiles/default/ (stored 0%)
#查看已经压缩的文件
[root@localhost test3]# ls -l total 108 -rw-r--r-- 1 root root 100994 Aug 15 16:13 sysconfig.zip
#解压目录
[root@localhost test3]# unzip sysconfig.zip Archive: sysconfig.zip creating: etc/sysconfig/ inflating: etc/sysconfig/ipmi inflating: etc/sysconfig/autofs inflating: etc/sysconfig/network
#这时发现居然解压出一个etc目录,说明压缩时保留sysconfig的父目录但不会压缩etc下的其它文件
[root@localhost test3]# ls -l total 116 drwxr-xr-x 3 root root 4096 Aug 15 16:15 etc -rw-r--r-- 1 root root 100994 Aug 15 16:13 sysconfig.zip
#把文本日志文件通过管道传给zip标准输入做压缩,- 表示管道传来的数据,messages.zip表示自定义的路径或文件名
[root@localhost test3]# cat /var/log/messages | zip messages.zip - adding: - (deflated 91%) [root@localhost test3]# ls -l messages.zip /var/log/messages -rw-r--r-- 1 root root 1568 Aug 15 16:20 messages.zip -rw------- 1 root root 16330 Aug 15 16:08 /var/log/messages
#-p选项类似zcat类的工具,对单个文本文件压缩后的文件进行内容读取并重定向
[root@localhost test3]# unzip -p messages.zip > zip.log
#查看刚才处理的文件,发现zip压缩比也不错
[root@localhost test3]# ls -l total 144 drwxr-xr-x 3 root root 4096 Aug 15 16:15 etc -rw-r--r-- 1 root root 1568 Aug 15 16:20 messages.zip -rw-r--r-- 1 root root 100994 Aug 15 16:13 sysconfig.zip -rw-r--r-- 1 root root 16330 Aug 15 16:24 zip.log
总结特点
compress工具虽然已经老化,但是在系统内核等文件还是会见到此类压缩文件,这时别的工具是替代不了的
gzip虽然压缩比比不是后两者,但是此算法会综合考虑系统的性能,还是相当稳定的
bzip2在目前来看,已经相当成熟,对于中小型文件件决定是一个不错的选择
xz的压缩比过高,已经不适合压缩很小的文件了,要知道某些很小的文件进行高压可能数据变形
zip命令对于小文件压缩比还是不错的,而且还能对目录进行压缩,对应备份整个目录是不错的选择
愿你享受你的生活.....................................................................