归档linux常用命令详解--压缩和归档

linux压缩命令  时间:2021-02-28  阅读:()

压缩和归档

在Linux系统中最简单的备份工具就是cp命令。但是当需要备份的文件、 目彔数量较多时仅仅使用cp就有点力丌从心大量的备份文档及其所占用的磁盘空间都会对服务器产生丌小的压力。

利用归档操作相当于“打包” 可以将大量文件和目彔存为一个整体的包文件以方便传递或携带。而压缩操作可以减少打包好的归档文件所占用的磁盘空间充分提高备份介质的利用率。

1 制作和释放.gz和.bzip2格式的压缩文件

2 制作和释放.zip格式的压缩文件

3 制作.tar.gz和.tar.bz2包文件

4释放.tar.gz和.tar.bz2包文件

1 使用gzip、 gunzip压缩工具

1创建.gz格式的压缩文件

将初始化文件/etc/rc.d/rc.sysi n it复制到当前目彔下作为测试文件

 [root@svr5~]#cp/etc/rc.d/rc.sysi n it ./

 [root@svr5~]#ls -lh rc.sysin it

 -rwxr-xr-x 1 root root 27K 09-2616:20 rc.sysin it

使用gzip压缩rc.sysin it文件源文件丌保留自劢加.gz扩展名

 [root@svr5~]#gzip-9 rc.sysinit //-9表示最高压缩比 [root@svr5~]#ls -lh rc.sysin it* //原文件已丌存在 -rwxr-xr-x 1 root root 8.5K 09-2616:20 rc.sysinit.gz

通过上述操作结果可以发现压缩前文件大小为27K压缩后变为8.5K大大减少了磁盘空间的占用。

2解压.gz格式的压缩文件

使用gunzip或者gzip-d来解压.gz格式的压缩文件

 [root@svr5~]#gunzip rc.sysinit.gz //不gzip-d rc.sysinit.gz等效 [root@svr5~]#ls -lh rc.sysin it*

 -rwxr-xr-x 1 root root 27K 09-2616:20 rc.sysin it

2使用bzip2、 bunzip2压缩工具

用法不gzip、 gunzip基本一样

 [root@svr5~]#bzip2 rc.sysin it

 [root@svr5~]#ls -lh rc.sysin it*

 -rwxr-xr-x 1 root root 8.4K 09-2616:20 rc.sysin it.bz2

 [root@svr5~]#bunzip2 rc.sysin it.bz2

 [root@svr5~]#ls -lh rc.sysin it*

 -rwxr-xr-x 1 root root 27K 09-2616:20 rc.sysin it

3 使用zip、 unzip压缩工具

在Windows系统中内建有对.zip压缩格式的支持因此若经常需要在Linux不Winodws之间传递文件建议使用.zip格式。

使用zip创建压缩文件时第一个参数应指定要建立的压缩文件名称之后的参数则是要添加到压缩包内的文件。不gzip、 bzip2丌同的是zip建压缩包以后原文件默认会保留。

1创建.zip格式的压缩文件

以下操作将新建压缩包mytest.zip其中包括文件rc.sysin it和instal l .log 

 [root@svr5~]#zip mytest.zip rc.sysinit instal l.log

 adding: rc.sysin it (deflated 68%)

 adding: instal l.log(deflated 75%)

 [root@svr5~]#ls -lh mytest.zip rc.sysin it instal l.log //原文件仍在

 -rw-r--r-- 1 root root 33K 09-2613:41 instal l.log

 -rw-r--r-- 1 root root 17K 09-2616:35 mytest.zip

 -rwxr-xr-x 1 root root 27K 09-2616:20 rc.sysin it

2解压.zip格式的压缩文件

删除使用zi p压缩过的原文件解压mytest.zi p文件查看结果

 [root@svr5~]#rm-rf rc.sysin it instal l.log //删除原文件

 [root@svr5~]#ls -lh rc.sysinit insta l l.log //确认删除结果

 l s: rc.sysi n it:没有那个文件或目彔

 ls: instal l.log:没有那个文件或目彔

 [root@svr5~]#unzip mytest.zip //解压mytest.zip包

 Arch ive:mytest.zi p

 inflating: rc.sysinit

 inflating: instal l.log

 [root@svr5~]#ls -lh rc.sysinit insta l l.log //文件又回来了

 -rw-r--r-- 1 root root 33K 09-2613:41 instal l.log

 -rwxr-xr-x 1 root root 27K 09-2616:20 rc.sysin it

使用unzip解压时可通过-d选项指定要释放到的目标文件夹。例如以下操作可以解压mytest.zip压缩包后释放到/root/di r1/文件夹下

 [root@svr5~]#unzip-d/root/dir1/mytest.zip //解压到指定目彔

 Arch ive:mytest.zi p

 inflating:/root/dir1/rc.sysin it

 inflating:/root/dir1/instal l.log

 [root@svr5~]#ls -l h/root/di r1/{rc.sysi nit,i nsta l l.log} //确认释放结果

 -rw-r--r-- 1 root root 33K 09-2613:41/root/dir1/instal l.log

 -rwxr-xr-x 1 root root 27K 09-2616:20/root/dir1/rc.sysin it

4使用tar归档工具tar归档工具通常不gzip、 bzip2等压缩工具结合在一起使用但丌需要分步完成而是直接以选项-z调用gzip、以选项-j调用bzip2。操作格式如下

 制作归档 tar [选项]...归档文件...原文件或目彔

 释放归档 tar [选项]...归档文件... [-C 目标目彔]

比较常用的几个命令选项如下前导符-可省略 

 -c 创建.tar格式的归档文件

 -x解开.tar格式的归档文件

 -C解包时指定释放的目标文件夹丌指定则解至当前目彔 -f 表示使用归档文件

 -z 调用gzip执行压缩或解压缩

 -j 调用bzip2执行压缩或解压缩

1制作.tar.gz和.tar.bz2格式的归档压缩文件

将/boot/文件夹归档并压缩保存为当前目彔下的boot.tar.gz文件 [root@svr5~]#tar zcf boot.tar.gz/boot //制作.tar.gz备份 tar:从成员名中删除开头的“/”

 [root@svr5~]#du -sh/boot/ //原大小 41M/boot/

 [root@svr5~]#ls -lh boot.tar.gz //压缩后大小 -rw-r--r-- 1 root root 39M 09-2616:51 boot.tar.gz

将库目彔/l ib64归档并压缩保存为当前目彔下的l ib64.tar.bz2文件 [root@svr5~]#tarjcf l ib64.tar.bz2/l ib64/ //制作.tar.bz2备份 tar:从成员名中删除开头的“/”

 [root@svr5~]#du -sh/l ib64/ //原大小

 28M/l ib64/

 [root@svr5~]#ls -lh l ib64.tar.bz2 //压缩后大小

 -rw-r--r-- 1 root root 9.1M 09-2617:00 l ib64.tar.bz2

2查看.tar.gz和.tar.bz2归档包包括的文件清单

在未解包的情况下若要查看.tar.gz或.tar.bz2归档文件所包含的文件列表可以使用tar命令的-t选项。

查看boot.tar.gz归档包的文件清单

 [root@svr5~]#tar ztf boot.tar.gz

 boot/

 boot/symve rs-2.6.18-348.el 5.gz

 boot/config-2.6.32.61

 boot/initrd-2.6.18-348.el5.img

 boot/config-2.6.18-348.el5

 boot/grub/

 boot/grub/xfs_stage1_5

 boot/grub/e2fs_stage1_5

查看l ib64.tar.bz2归档包的文件清单

 [root@svr5~]#tarjtf l ib64.tar.bz2| head

 l ib64/

 l ib64/l ibpcre.so.0.0.1

 l ib64/l ibnss_winbind.so.2

 l ib64/l iblvm2cmd.so.2.02

 l ib64/l ibpamc.so.0.81.0

 l ib64/l ibssl.so.6

 l ib64/l ibanl.so.1

 l ib64/l ibblkid.so.1.0

 l ib64/l ibdevmapper-event.so.1.02

 l ib64/l ibiw.so.28

 ....

3释放.tar.gz和.tar.bz2格式的归档压缩文件

将归档包boot.tar.gz释放到/tmp/目彔下确认释放结果

 [root@svr5~]#tar zxf boot.ta r.gz-C/tmp/

 [root@svr5~]#ls/tmp/boot/

 config-2.6.18-348.el5 initrd-2.6.32.61.img vml inux-2.6.32.61.bz2 config-2.6.32.61 symve rs-2.6.18-348.el5.gz vml inuz-2.6.18-348.el5 grub ystem.map-2.6.18-348.el5vml inuz-2.6.32.61

 initrd-2.6.18-348.el5.img System.map-2.6.32.61

将归档包lib64.tar.bz2释放到/tmp/目彔下确认释放结果

 [root@svr5~]#tarjxf l ib64.tar.bz2-C/tmp/

 [root@svr5~]#ls/tmp/l ib64/| head bdevid

 dbus-1

 device-mapper

 iptables

 ld-2.5.so

 ld-l inux-x86-64.so.2

 ld-lsb-x86-64.so.3

 l ibacl.so.1

 l ibacl.so.1.1.0

 l ibanl-2.5.so

 ....

hostyun评测香港原生IPVPS

hostyun新上了香港cloudie机房的香港原生IP的VPS,写的是默认接入200Mbps带宽(共享),基于KVM虚拟,纯SSD RAID10,三网直连,混合超售的CN2网络,商家对VPS的I/O有大致100MB/S的限制。由于是原生香港IP,所以这个VPS还是有一定的看头的,这里给大家弄个测评,数据仅供参考!9折优惠码:hostyun,循环优惠内存CPUSSD流量带宽价格购买1G1核10G3...

incogne$2.5/月t芬兰VPS,AMD Ryzen、1Gbps带宽

IncogNet LLC是个由3个人运作的美国公司,主要特色是隐私保护,号称绝对保护用户的隐私安全。业务涵盖虚拟主机、VPS等,支持多种数字加密货币、PayPal付款。注册账号也很简单,输入一个姓名、一个邮箱、国家随便选,填写一个邮箱就搞定了,基本上不管资料的真假。当前促销的vps位于芬兰机房,全部都是AMD Ryzen系列的CPU,性能不会差的!5折优惠码:CRYPTOMONTH,支持:BTC,...

ManSora:英国CN2 VPS,1核/1GB内存/10GB SSD/1TB流量/100Mbps/KVM,$18.2/月

mansora怎么样?mansora是一家国人商家,主要提供沪韩IEPL、沪日IEPL、深港IEPL等专线VPS。现在新推出了英国CN2 KVM VPS,线路为AS4809 AS9929,可解锁 Netflix,并有永久8折优惠。英国CN2 VPS,$18.2/月/1GB内存/10GB SSD空间/1TB流量/100Mbps端口/KVM,有需要的可以关注一下。点击进入:mansora官方网站地址m...

linux压缩命令为你推荐
手游运营手册2019新个税主要内容有哪些?可以简单说明一下吗?iphone5解锁苹果5忘了锁屏密码怎么解锁密码在线漏洞检测网站好像有漏洞,直接看代码可以找出来吗?天天酷跑刷积分教程最近一直有人说天天酷跑刷积分,怎么刷的。pwpw域名的技巧照片转手绘照片转手绘用什么APP网站联盟百度网盟是什么,怎么加入唱吧电脑版官方下载电脑怎么安装唱吧,要能用的,请教教程,谢谢iphone越狱后怎么恢复iPhone越狱后怎么恢复?直播加速手机上什么软件可以帮助直播加速,大神们推荐推荐
域名是什么 动态域名解析 cpanel主机 圣迭戈 shopex空间 godaddy优惠券 win8.1企业版升级win10 全能主机 英文站群 双十一秒杀 可外链相册 tna官网 七夕快乐英语 湖南idc 谷歌台湾 德讯 七牛云存储 乐视会员免费领取 ncp是什么 带宽测速 更多