linux常用命令详解--压缩和归档.docx_第1页
linux常用命令详解--压缩和归档.docx_第2页
linux常用命令详解--压缩和归档.docx_第3页
linux常用命令详解--压缩和归档.docx_第4页
linux常用命令详解--压缩和归档.docx_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

压缩和归档在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.sysinit复制到当前目录下,作为测试文件:1 rootsvr5 # cp /etc/rc.d/rc.sysinit ./2 rootsvr5 # ls -lh rc.sysinit3 -rwxr-xr-x 1 root root 27K 09-26 16:20 rc.sysinit使用gzip压缩rc.sysinit文件,源文件不保留,自动加 .gz 扩展名:4 rootsvr5 # gzip -9 rc.sysinit / -9 表示最高压缩比5 rootsvr5 # ls -lh rc.sysinit* /原文件已不存在6 -rwxr-xr-x 1 root root 8.5K 09-26 16:20 rc.sysinit.gz通过上述操作结果可以发现,压缩前文件大小为27K,压缩后变为8.5K,大大减少了磁盘空间的占用。2)解压 .gz 格式的压缩文件使用gunzip或者gzip -d来解压 .gz 格式的压缩文件:7 rootsvr5 # gunzip rc.sysinit.gz /与gzip -d rc.sysinit.gz等效8 rootsvr5 # ls -lh rc.sysinit*9 -rwxr-xr-x 1 root root 27K 09-26 16:20 rc.sysinit2使用bzip2、bunzip2压缩工具用法与gzip、gunzip基本一样:10 rootsvr5 # bzip2 rc.sysinit11 rootsvr5 # ls -lh rc.sysinit*12 -rwxr-xr-x 1 root root 8.4K 09-26 16:20 rc.sysinit.bz213 rootsvr5 # bunzip2 rc.sysinit.bz214 rootsvr5 # ls -lh rc.sysinit*15 -rwxr-xr-x 1 root root 27K 09-26 16:20 rc.sysinit3使用zip、unzip压缩工具在Windows系统中内建有对.zip压缩格式的支持,因此若经常需要在Linux与Winodws之间传递文件,建议使用.zip格式。使用zip创建压缩文件时,第一个参数应指定要建立的压缩文件名称, 之后的参数则是要添加到压缩包内的文件。与gzip、bzip2不同的是,zip建压缩包以后,原文件默认会保留。1)创建 .zip 格式的压缩文件以下操作将新建压缩包mytest.zip,其中包括文件rc.sysinit和install.log:16 rootsvr5 # zip mytest.zip rc.sysinit install.log17 adding: rc.sysinit (deflated 68%)18 adding: install.log (deflated 75%)19 rootsvr5 # ls -lh mytest.zip rc.sysinit install.log /原文件仍在20 -rw-r-r- 1 root root 33K 09-26 13:41 install.log21 -rw-r-r- 1 root root 17K 09-26 16:35 mytest.zip22 -rwxr-xr-x 1 root root 27K 09-26 16:20 rc.sysinit2)解压 .zip 格式的压缩文件删除使用zip压缩过的原文件,解压mytest.zip文件,查看结果:23 rootsvr5 # rm -rf rc.sysinit install.log /删除原文件24 rootsvr5 # ls -lh rc.sysinit install.log /确认删除结果25 ls: rc.sysinit: 没有那个文件或目录26 ls: install.log: 没有那个文件或目录27 rootsvr5 # unzip mytest.zip /解压mytest.zip包28 Archive: mytest.zip29 inflating: rc.sysinit30 inflating: install.log31 rootsvr5 # ls -lh rc.sysinit install.log /文件又回来了32 -rw-r-r- 1 root root 33K 09-26 13:41 install.log33 -rwxr-xr-x 1 root root 27K 09-26 16:20 rc.sysinit使用unzip解压时,可通过-d选项指定要释放到的目标文件夹。例如,以下操作可以解压mytest.zip压缩包后释放到/root/dir1/文件夹下:34 rootsvr5 # unzip -d /root/dir1/ mytest.zip /解压到指定目录35 Archive: mytest.zip36 inflating: /root/dir1/rc.sysinit37 inflating: /root/dir1/install.log38 rootsvr5 # ls -lh /root/dir1/rc.sysinit,install.log /确认释放结果39 -rw-r-r- 1 root root 33K 09-26 13:41 /root/dir1/install.log40 -rwxr-xr-x 1 root root 27K 09-26 16:20 /root/dir1/rc.sysinit4使用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文件:41 rootsvr5 # tar zcf boot.tar.gz /boot /制作.tar.gz备份42 tar: 从成员名中删除开头的“/”43 rootsvr5 # du -sh /boot/ /原大小44 41M /boot/45 rootsvr5 # ls -lh boot.tar.gz /压缩后大小46 -rw-r-r- 1 root root 39M 09-26 16:51 boot.tar.gz将库目录/lib64归档并压缩,保存为当前目录下的lib64.tar.bz2文件:47 rootsvr5 # tar jcf lib64.tar.bz2 /lib64/ /制作.tar.bz2备份48 tar: 从成员名中删除开头的“/”49 rootsvr5 # du -sh /lib64/ /原大小50 28M /lib64/51 rootsvr5 # ls -lh lib64.tar.bz2 /压缩后大小52 -rw-r-r- 1 root root 9.1M 09-26 17:00 lib64.tar.bz22)查看 .tar.gz 和 .tar.bz2归档包包括的文件清单在未解包的情况下,若要查看 .tar.gz 或 .tar.bz2 归档文件所包含的文件列表,可以使用tar命令的 -t 选项。查看 boot.tar.gz归档包的文件清单:53 rootsvr5 # tar ztf boot.tar.gz54 boot/55 boot/symvers-2.6.18-348.el5.gz56 boot/config-2.6.32.6157 boot/initrd-2.6.18-348.el5.img58 boot/config-2.6.18-348.el559 boot/grub/60 boot/grub/xfs_stage1_561 boot/grub/e2fs_stage1_5查看 lib64.tar.bz2归档包的文件清单:62 rootsvr5 # tar jtf lib64.tar.bz2 | head63 lib64/64 lib64/libpcre.so.0.0.165 lib64/libnss_winbind.so.266 lib64/liblvm2cmd.so.2.0267 lib64/libpamc.so.0.81.068 lib64/libssl.so.669 lib64/libanl.so.170 lib64/libblkid.so.1.071 lib64/libdevmapper-event.so.1.0272 lib64/libiw.so.2873 .3)释放 .tar.gz 和 .tar.bz2格式的归档压缩文件将归档包boot.tar.gz释放到/tmp/目录下,确认释放结果:74 rootsvr5 # tar zxf boot.tar.gz -C /tmp/75 rootsvr5 # ls /tmp/boot/76 config-2.6.18-348.el5 initrd-2.6.32.61.img vmlinux-2.6.32.61.bz277 config-2.6.32.61 symvers-2.6.18-348.el5.gz vmlinuz-2.6.18-348.el578 grub ystem.map-2.6.18-348.el5 vmlinuz-2.6.32.6179 initrd-2.6.18-348.el5.img System.map-2.6.32.61将归档包lib64.tar.bz2释放到/tmp/目录下,确认释

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论