Linux运维知识之Linux系统压缩及解压缩
小标 2018-11-07 来源 : 阅读 756 评论 0

摘要:本文主要向大家介绍了Linux运维知识之Linux系统压缩及解压缩,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。

本文主要向大家介绍了Linux运维知识之Linux系统压缩及解压缩,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助。

Linux系统解压缩

概述:  本篇将介绍Linux系统中的压缩和解压缩的工具,以及归档工具(tar,cpio)compress/uncompress:对应 .Z 结尾的压缩格式文件;gzip/gunzip:其对应的是 .gz 结尾的压缩格式文件;bzip2/bunzip2:其对应的是 .bz2 结尾的压缩格式文件;xz/unxz: 其对应的是 .xz 结尾的压缩格式文件;zip/unzip:其对应的是 .zip 结尾的压缩格式文件==============================================================================Linux系统解压缩 1.压缩比和常用工具★压缩比:时间换空间(CPU的时间 --->磁盘空间)★常用工具:早期的有compress和uncompress,其对应的是 .Z 结尾的压缩格式文件,现在适应较多的有:gzip/gunzip:其对应的是 .gz 结尾的压缩格式文件;bzip2/bunzip2:其对应的是 .bz2 结尾的压缩格式文件;xz/unxz: 其对应的是 .xz 结尾的压缩格式文件;zip/unzip:其对应的是 .zip 结尾的压缩格式文件tar,cpio:归档和展开归档 2.gzip和gunzip(使用最多)★常用工具:gzip,gunzip,zcat★语法:gzip [OPTION]... FILE ...☉选项:-d:解压缩,相当于gunzip-c:将压缩或解压缩的结果输出至标准输出(gzip -c FILE > /PATH/TP/SOMEFILE.gz);-#:1-9,指定压缩比,值越大压缩比越大  如:gzip -9 m-v:显示详情☉解压缩:guzip☉zcat:不显式解压缩的前提下查看文本文件内容(适用于查看小文件)  如:zcat FILE > /PATH/TP/SOMEFILE演示:[root@centos7 ~]# cp /var/log/messages /tmp/test/
[root@centos7 ~]# ll /tmp/test/
总用量 288
-rw-r----- 1 root root      0 2月  20 13:41 a
-rw-rw-rw- 1 root root      0 2月  20 13:41 b.danger
-r--r----- 1 root root      0 2月  20 13:41 c
-rwxrwxr-x 1 root root      0 2月  20 13:41 d
-rwxrwxrwx 1 root root      0 2月  20 13:41 e.danger
-rw-r--r-- 1 root root      0 2月  20 13:41 f
-rw-r--r-- 1 root root      0 2月  20 13:41 g
-rw------- 1 root root 292504 2月  20 16:28 messages
[root@centos7 ~]# ll -h /tmp/test/messages 
-rw------- 1 root root 286K 2月  20 16:28 /tmp/test/messages

# 压缩,删除原文件,保留压缩后以.gz结尾的文件
[root@centos7 ~]# gzip /tmp/test/messages

[root@centos7 ~]# ll -h /tmp/test
总用量 44K
-rw-r----- 1 root root   0 2月  20 13:41 a
-rw-rw-rw- 1 root root   0 2月  20 13:41 b.danger
-r--r----- 1 root root   0 2月  20 13:41 c
-rwxrwxr-x 1 root root   0 2月  20 13:41 d
-rwxrwxrwx 1 root root   0 2月  20 13:41 e.danger
-rw-r--r-- 1 root root   0 2月  20 13:41 f
-rw-r--r-- 1 root root   0 2月  20 13:41 g
-rw------- 1 root root 41K 2月  20 16:28 messages.gz

# 解压缩,原来的压缩文件被删除,保留解压缩后的文件
[root@centos7 ~]# gunzip /tmp/test/messages.gz 
[root@centos7 ~]# ll -h /tmp/test
总用量 288K
-rw-r----- 1 root root    0 2月  20 13:41 a
-rw-rw-rw- 1 root root    0 2月  20 13:41 b.danger
-r--r----- 1 root root    0 2月  20 13:41 c
-rwxrwxr-x 1 root root    0 2月  20 13:41 d
-rwxrwxrwx 1 root root    0 2月  20 13:41 e.danger
-rw-r--r-- 1 root root    0 2月  20 13:41 f
-rw-r--r-- 1 root root    0 2月  20 13:41 g
-rw------- 1 root root 286K 2月  20 16:28 messages

# zcat可以查看压缩的文件,不建议对大文件使用zcat命令查看
[root@centos7 ~]# zcat /tmp/test/messages.gz

#=====================================================================================
# 解压缩
[root@centos7 ~]# gzip -d /tmp/test/messages.gz 
[root@centos7 ~]# ll /tmp/test/
总用量 288
-rw-r----- 1 root root      0 2月  20 13:41 a
-rw-rw-rw- 1 root root      0 2月  20 13:41 b.danger
-r--r----- 1 root root      0 2月  20 13:41 c
-rwxrwxr-x 1 root root      0 2月  20 13:41 d
-rwxrwxrwx 1 root root      0 2月  20 13:41 e.danger
-rw-r--r-- 1 root root      0 2月  20 13:41 f
-rw-r--r-- 1 root root      0 2月  20 13:41 g
-rw------- 1 root root 292504 2月  20 16:28 messages

# 将压缩或解压缩的结果输出至标准输出(gzip -c FILE > /PATH/TP/SOMEFILE.gz)
[root@centos7 ~]# gzip -c /tmp/test/messages > /tmp/test/messages.gz
[root@centos7 ~]# ll /tmp/test/
总用量 332
-rw-r----- 1 root root      0 2月  20 13:41 a
-rw-rw-rw- 1 root root      0 2月  20 13:41 b.danger
-r--r----- 1 root root      0 2月  20 13:41 c
-rwxrwxr-x 1 root root      0 2月  20 13:41 d
-rwxrwxrwx 1 root root      0 2月  20 13:41 e.danger
-rw-r--r-- 1 root root      0 2月  20 13:41 f
-rw-r--r-- 1 root root      0 2月  20 13:41 g
-rw------- 1 root root 292504 2月  20 16:28 messages
-rw-r--r-- 1 root root  41791 2月  20 16:44 messages.gz

# 解压缩到标准输出
[root@centos7 ~]# rm -f /tmp/test/messages
[root@centos7 ~]# gzip -d -c /tmp/test/messages.gz > /tmp/test/messages
[root@centos7 ~]# ll /tmp/test/
总用量 332
-rw-r----- 1 root root      0 2月  20 13:41 a
-rw-rw-rw- 1 root root      0 2月  20 13:41 b.danger
-r--r----- 1 root root      0 2月  20 13:41 c
-rwxrwxr-x 1 root root      0 2月  20 13:41 d
-rwxrwxrwx 1 root root      0 2月  20 13:41 e.danger
-rw-r--r-- 1 root root      0 2月  20 13:41 f
-rw-r--r-- 1 root root      0 2月  20 13:41 g
-rw-r--r-- 1 root root 292504 2月  20 16:50 messages
-rw-r--r-- 1 root root  41791 2月  20 16:44 messages.gz  2.bzip2/bunzip2/bzcat★语法:bzip2 [OPTION]... FILE ...☉选项:-k:keep, 保留原文件;-d:解压缩;-#:1-9,压缩比,默认为6☉bzcat:不显式解压缩的前提下查看文本文件内容注意:bzip2和gzip命令的使用方式基本相同,压缩或解压缩后都会删除源文件演示:# 压缩
[root@centos7 ~]# bzip2 /tmp/test/messages

[root@centos7 ~]# ll -h /tmp/test/
总用量 72K
-rw-r----- 1 root root   0 2月  20 13:41 a
-rw-rw-rw- 1 root root   0 2月  20 13:41 b.danger
-r--r----- 1 root root   0 2月  20 13:41 c
-rwxrwxr-x 1 root root   0 2月  20 13:41 d
-rwxrwxrwx 1 root root   0 2月  20 13:41 e.danger
-rw-r--r-- 1 root root   0 2月  20 13:41 f
-rw-r--r-- 1 root root   0 2月  20 13:41 g
-rw-r--r-- 1 root root 26K 2月  20 16:50 messages.bz2 #压缩后的结果
-rw-r--r-- 1 root root 41K 2月  20 16:44 messages.gz

# 解压缩
[root@centos7 ~]# bunzip2 /tmp/test/messages.bz2 
[root@centos7 ~]# ll -h /tmp/test/
总用量 332K
-rw-r----- 1 root root    0 2月  20 13:41 a
-rw-rw-rw- 1 root root    0 2月  20 13:41 b.danger
-r--r----- 1 root root    0 2月  20 13:41 c
-rwxrwxr-x 1 root root    0 2月  20 13:41 d
-rwxrwxrwx 1 root root    0 2月  20 13:41 e.danger
-rw-r--r-- 1 root root    0 2月  20 13:41 f
-rw-r--r-- 1 root root    0 2月  20 13:41 g
-rw-r--r-- 1 root root 286K 2月  20 16:50 messages  # 解压缩后的结果
-rw-r--r-- 1 root root  41K 2月  20 16:44 messages.gz

# -k 选项不用指明重定向的文件,自动保留源文件在当前文件中
[root@centos7 ~]# bzip2 -k /tmp/test/messages
[root@centos7 ~]# ll -h /tmp/test/
总用量 360K
-rw-r----- 1 root root    0 2月  20 13:41 a
-rw-rw-rw- 1 root root    0 2月  20 13:41 b.danger
-r--r----- 1 root root    0 2月  20 13:41 c
-rwxrwxr-x 1 root root    0 2月  20 13:41 d
-rwxrwxrwx 1 root root    0 2月  20 13:41 e.danger
-rw-r--r-- 1 root root    0 2月  20 13:41 f
-rw-r--r-- 1 root root    0 2月  20 13:41 g
-rw-r--r-- 1 root root 286K 2月  20 16:50 messages
-rw-r--r-- 1 root root  26K 2月  20 16:50 messages.bz2
-rw-r--r-- 1 root root  41K 2月  20 16:44 messages.gz 3.xz/unxz/zxcat(压缩比最强)★语法:xz [OPTION]... FILE ...☉选项:-k:keep, 保留原文件;-d:解压缩;-#:1-9,压缩比,默认为6;☉xzcat:不显式解压缩的前提下查看文本文件内容演示:[root@centos7 ~]# xz /tmp/test/messages
[root@centos7 ~]# ll -h /tmp/test/
总用量 96K
-rw-r----- 1 root root   0 2月  20 13:41 a
-rw-rw-rw- 1 root root   0 2月  20 13:41 b.danger
-r--r----- 1 root root   0 2月  20 13:41 c
-rwxrwxr-x 1 root root   0 2月  20 13:41 d
-rwxrwxrwx 1 root root   0 2月  20 13:41 e.danger
-rw-r--r-- 1 root root   0 2月  20 13:41 f
-rw-r--r-- 1 root root   0 2月  20 13:41 g
-rw-r--r-- 1 root root 26K 2月  20 16:50 messages.bz2
-rw-r--r-- 1 root root 41K 2月  20 16:44 messages.gz
-rw-r--r-- 1 root root 21K 2月  20 16:50 messages.xz归档工具:tar 1.归档及常用工具★归档:归档就是将多个文件打包为单个文件以便于管理,默认的归档不会执行压缩。★常用的工具:tar,cpio(不常用) 2.tar命令★语法:tar [OPTION...] [FILE]...☉创建归档(-c,-f 指定文件):tar -c -f /PATH/TO/SOMEFILE.tar  FILE... (后缀名固定以 .tar 结尾;)tar -cf /PATH/TO/SOMEFILE.tar  FILE... (可以合并写为-cf ,但不能写为 -fc ,因为-f 选项后带参数)☉展开归档(-x,-f 指定文件):tar -x -f /PATH/TO/SOMEFILE.tar (展开至归档所在的文件中)tar xf /PATH/TO/SOMEFILE.tar -C /PATH/TO/SOMEFILE (-C :展开归档至指定文件中)☉查看归档文件中的列表(-t,-f 指定文件):tar -tf /PATH/TO/SOMEFILE.tar注意:多个选项可以合并,但-f由于要带参数,因此要放到最右侧 如:-cf,-xf,-cf;选项的引导符 "-" 可省略。如:tar xf,tar zf演示:[root@centos7 ~]# ls /tmp/test/tao
boot.log  fstab  issue  pacemaker.log  wpa_supplicant.log  Xorg.0.log  yum.log

# 对所有以 .log 结尾的文件进行归档
[root@centos7 ~]# tar -cf /tmp/test/mylog.tar /tmp/test/tao/*.log
[root@centos7 ~]# ll /tmp/test/
总用量 136
-rw-r--r-- 1 root root 26074 2月  20 16:50 messages.bz2
-rw-r--r-- 1 root root 41791 2月  20 16:44 messages.gz
-rw-r--r-- 1 root root 21420 2月  20 16:50 messages.xz
-rw-r--r-- 1 root root 40960 2月  20 17:45 mylog.tar   # 归档后的文件
drwxr-xr-x 2 root root   121 2月  20 17:43 tao    

# 展开归档
[root@centos7 test]# tar xf mylog.tar 
[root@centos7 test]# ll
总用量 176
-rw-r--r-- 1 root root 12097 2月  20 17:42 boot.log
-rw-r--r-- 1 root root 26074 2月  20 16:50 messages.bz2
-rw-r--r-- 1 root root 41791 2月  20 16:44 messages.gz
-rw-r--r-- 1 root root 21420 2月  20 16:50 messages.xz
-rw-r--r-- 1 root root 40960 2月  20 17:51 mylog.tar
-rw-r----- 1 root root     0 2月  20 17:42 pacemaker.log
drwxr-xr-x 2 root root   121 2月  20 17:43 tao
-rw-r--r-- 1 root root  2800 2月  20 17:42 wpa_supplicant.log
-rw-r--r-- 1 root root 18303 2月  20 17:42 Xorg.0.log
-rw------- 1 root root   105 2月  20 17:42 yum.log

# -C 展开归档至指定文件中
[root@centos7 test]# mkdir /tmp/newtest
[root@centos7 test]# tar xf mylog.tar -C /tmp/newtest
[root@centos7 test]# ll /tmp/newtest
总用量 40
-rw-r--r-- 1 root root 12097 2月  20 17:42 boot.log
-rw-r----- 1 root root     0 2月  20 17:42 pacemaker.log
-rw-r--r-- 1 root root  2800 2月  20 17:42 wpa_supplicant.log
-rw-r--r-- 1 root root 18303 2月  20 17:42 Xorg.0.log
-rw------- 1 root root   105 2月  20 17:42 yum.log

# 查看归档文件中的文件列表
[root@centos7 test]# tar tf mylog.tar 
boot.log
pacemaker.log
wpa_supplicant.log
Xorg.0.log
yum.log  归档完成后通常需要压缩,结合此前的压缩工具,就能实现压缩多个文件了。 ★结合压缩工具实现:归档并压缩:☉-z:gzip(后缀名.tar.gz)tar -zcf /PATH/TO/MEFILE.tar.gz FILE...  (创建归档并压缩);tar -zxf /PATH/TO/SOMEFILE.tar.gz   (解压缩并展开归档,z不写也行)☉-j:bzip2(后缀名.tar.bz2)-jcf-jxf☉-J:xz(后缀名:.tar.xz)-Jcf-Jxf注意:展开归档可以直接使用 tar xf ,而无需为其指定对应的压缩工具选项即可演示:# 对目录进行归档并压缩
[root@centos7 test]# tar zcf /tmp/test/tao.tar.gz tao
[root@centos7 test]# ll /tmp/test
总用量 184
-rw-r--r-- 1 root root 12097 2月  20 17:42 boot.log
-rw-r--r-- 1 root root 26074 2月  20 16:50 messages.bz2
-rw-r--r-- 1 root root 41791 2月  20 16:44 messages.gz
-rw-r--r-- 1 root root 21420 2月  20 16:50 messages.xz
-rw-r--r-- 1 root root 40960 2月  20 17:51 mylog.tar
-rw-r----- 1 root root     0 2月  20 17:42 pacemaker.log
drwxr-xr-x 2 root root   121 2月  20 17:43 tao           # 原文件
-rw-r--r-- 1 root root  7232 2月  20 18:15 tao.tar.gz    # 归档压缩后的文件
-rw-r--r-- 1 root root  2800 2月  20 17:42 wpa_supplicant.log
-rw-r--r-- 1 root root 18303 2月  20 17:42 Xorg.0.log
-rw------- 1 root root   105 2月  20 17:42 yum.log

# 删除原文件
[root@centos7 test]# rm -fr tao         

# 展开归档,其中 z 可省略,tar命令会自动识别其为压缩文件
[root@centos7 test]# tar xf tao.tar.gz 
[root@centos7 test]# ll
总用量 184
-rw-r--r-- 1 root root 12097 2月  20 17:42 boot.log
-rw-r--r-- 1 root root 26074 2月  20 16:50 messages.bz2
-rw-r--r-- 1 root root 41791 2月  20 16:44 messages.gz
-rw-r--r-- 1 root root 21420 2月  20 16:50 messages.xz
-rw-r--r-- 1 root root 40960 2月  20 17:51 mylog.tar
-rw-r----- 1 root root     0 2月  20 17:42 pacemaker.log
drwxr-xr-x 2 root root   121 2月  20 17:43 tao           # 展开后的文件
-rw-r--r-- 1 root root  7232 2月  20 18:15 tao.tar.gz
-rw-r--r-- 1 root root  2800 2月  20 17:42 wpa_supplicant.log
-rw-r--r-- 1 root root 18303 2月  20 17:42 Xorg.0.log
-rw------- 1 root root   105 2月  20 17:42 yum.logzip/unzip        ---最通用的压缩工具,即可以归档,又能压缩(现在不常用)★创建归档(.zip后缀):zip file.zip  /PATH/TO/SOMEFILE★解压缩unzip file.zip演示:# 对目录进行归档并压缩
[root@centos7 test]# zip /tmp/test/tao.zip tao
  adding: tao/ (stored 0%)
[root@centos7 test]# ll
总用量 188
-rw-r--r-- 1 root root 12097 2月  20 17:42 boot.log
-rw-r--r-- 1 root root 26074 2月  20 16:50 messages.bz2
-rw-r--r-- 1 root root 41791 2月  20 16:44 messages.gz
-rw-r--r-- 1 root root 21420 2月  20 16:50 messages.xz
-rw-r--r-- 1 root root 40960 2月  20 17:51 mylog.tar
-rw-r----- 1 root root     0 2月  20 17:42 pacemaker.log
drwxr-xr-x 2 root root   121 2月  20 17:43 tao
-rw-r--r-- 1 root root  7232 2月  20 18:15 tao.tar.gz
-rw-r--r-- 1 root root   158 2月  20 18:26 tao.zip
-rw-r--r-- 1 root root  2800 2月  20 17:42 wpa_supplicant.log
-rw-r--r-- 1 root root 18303 2月  20 17:42 Xorg.0.log
-rw------- 1 root root   105 2月  20 17:42 yum.log


[root@centos7 test]# rm -fr tao

# 解压缩
[root@centos7 test]# unzip tao.zip 
Archive:  tao.zip
   creating: tao/
[root@centos7 test]# ll
总用量 188
-rw-r--r-- 1 root root 12097 2月  20 17:42 boot.log
-rw-r--r-- 1 root root 26074 2月  20 16:50 messages.bz2
-rw-r--r-- 1 root root 41791 2月  20 16:44 messages.gz
-rw-r--r-- 1 root root 21420 2月  20 16:50 messages.xz
-rw-r--r-- 1 root root 40960 2月  20 17:51 mylog.tar
-rw-r----- 1 root root     0 2月  20 17:42 pacemaker.log
drwxr-xr-x 2 root root     6 2月  20 17:43 tao
-rw-r--r-- 1 root root  7232 2月  20 18:15 tao.tar.gz
-rw-r--r-- 1 root root   158 2月  20 18:26 tao.zip
-rw-r--r-- 1 root root  2800 2月  20 17:42 wpa_supplicant.log
-rw-r--r-- 1 root root 18303 2月  20 17:42 Xorg.0.log
-rw------- 1 root root   105 2月  20 17:42 yum.logcpio 命令★cpiocpio命令是通过重定向的方式将文件进行打包备份,还原恢复的工具,它可以解压以“.cpio”或者“.tar”结尾的文件。★用法:cpio[选项] > 文件名或者设备名cpio[选项] < 文件名或者设备名★选项:-o:将文件拷贝打包成文件或者将文件输出到设备上;-i:解包,将打包文件解压或将设备上的备份还原到系统;-t:预览,查看文件内容或者输出到设备上的文件内容;-v:显示打包过程中的文件名称;-d:解包生成目录,在cpio还原时,自动的建立目录;-c:一种较新的存储方式示例:将etc目录备份:find ./etc-print |cpio -ov> etc.cpio内容预览cpio–tv < etc.cpio要解包文件cpio–iv < etc.cpiocpio–idv < etc.cpio

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注系统运维Linux频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程