Linux系统压缩和解压缩工具gzip、bzip2、xz以及tar打包工具总结

在Linux系统使用过程中,对于一些不是常用的文件,利用cpu的时间资源对文件进行压缩可以节省一定的磁盘空间,对系统中某个目录经常会有备份的需求,可以使用Linux系统中的tar打包工具实现,文件的压缩对于大量的网络文件传输可以节约带宽,同时也能降低服务器的负载,我们经常需要从网站下载软件包到本地主机,这些包基本都是打包压缩的,要想使用安装,需要我们解压缩他们,掌握Linux系统中的压缩解压缩以及打包工具的使用是系统运维工程师基础必备技能;

1)压缩工具

2)打包工具

1.压缩工具:

     (1)压缩原理:

     这里简单叙述一下,我们知道文本文件中有大量的重复字符串,单词和文字对于单独重复的字符串和单词,我们可以使用一个标记符号代表,这样只存储一个标记字符;这只是一个大致的原理,当然不同的压缩工具使用的算法也就不同,同时后面新出的压缩算法更加优异合理,对CPU的资源占用也不是很高,实现快速压缩;同时有个问题需要注意,压缩和解压缩都需要消耗大量的CPU时间资源,尤其是对大文件的操作,更需要考虑系统的负载状况,所以对于系统中的文件压缩要充分考虑清楚,有没有必要浪费CPU的时间资源来换取节约磁盘空间,同时还有一点需要注意,对于二进制格式的数据流文件压缩效果不是很好比如说MP3格式的音乐等,它们受到自身数据格式影响;

    (2)compress/uncompress:

    这是一种早期的Linux系统中的压缩和解压缩工具,压缩比不是很好,所以很少在使用;压缩后的文以.Z    结尾;

 

    (3)gzip/gunzip/zcat:

   

     压缩:gzip

     语法:

       gzip [option] /path/to/file

    常见选项:

       -#:指定压缩比,默认6,范围(1-9),数字越大,压缩比越高;

       -d:相当于命令gunzip,解压文件;

       -c:默认情况下压缩完文件后会把原文件删除,如果想保留可以使用如下格式:

      gzip -c /path/to/sourcefile > /path/to/sourcefile.gz

      -r:可以递归对目录下的所有文件进行压缩;

    解压缩:gunzip

    语法:

     gunzip [option] /path/to/file.gz

    常见选项:

         -r:可以递归对目录下的所有压缩文件进行解压缩,默认删除原压缩文件;

    查看压缩:zcat 正常情况下不能对压缩文件进行查看,打开也是一堆乱码;实际工作原理是:不解压文                   件到磁盘,只是临时解压在内存中,提供压缩文件查看, 

        语法:

     zcat /path/to/file

        *注意:使用查看压缩文件命令时,尤其是大文件,解压到内存中,是相当占内存


[root@xia7 dir]# ls -hl 
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# gzip messages 
[root@xia7 dir]# ls -hl
total 192K
-rw------- 1 root root 190K Aug 16 17:40 messages.gz
[root@xia7 dir]# gzip -d messages.gz 
[root@xia7 dir]# ls -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# gzip -9 messages 
[root@xia7 dir]# ls -lh
total 188K
-rw------- 1 root root 187K Aug 16 17:40 messages.gz
[root@xia7 dir]# gunzip messages.gz 
[root@xia7 dir]# ls -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# gzip -c messages >messages.gz
[root@xia7 dir]# ls -lh
total 1.6M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw-r--r-- 1 root root 190K Aug 18 10:30 messages.gz
[root@xia7 dir]# zcat messages.gz 
Aug 14 11:29:01 xia7 rsyslogd: [origin software="rsyslogd" swVersion="7.4.7" x-pid="819" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
Aug 14 11:30:01 xia7 systemd: Started Session 7 of user root.
Aug 14 11:30:01 xia7 systemd: Starting Session 7 of user root.
Aug 14 11:40:01 xia7 systemd: Started Session 8 of user root.
Aug 14 11:40:01 xia7 systemd: Starting Session 8 of user root.
Aug 14 11:50:01 xia7 systemd: Started Session 9 of user root.

(4)bzip2/bunzip2/bzcat:

 

     压缩:bzip2

     语法:

     bzip2 [option] /path/to/file

     常见选项:

       -#:指定压缩比,默认6,范围(1-9),数字越大,压缩比越高;

       -d:相当于命令bunzip2,解压文件;

       -k:压缩完后保留原文件;

 

      解压缩:bunzip2

      语法:

             bunzip2 [option] /path/to/file.bz2

      常见选项: 

            -k:解压缩完成后不删除原压缩文件;


     查看压缩:bzcat

     语法:

            bzcat /path/to/file.bz2


     解压缩:bunzip2 

     语法:

            bunzip [option] /path/to/file.bz2

    常见选项:

     -k:解压缩完成后不删除原压缩文件;


[root@xia7 dir]# ls -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# bzip2 messages 
[root@xia7 dir]# ls -lh
total 84K
-rw------- 1 root root 83K Aug 16 17:40 messages.bz2
[root@xia7 dir]# bzip2 -d  messages.bz2 
[root@xia7 dir]# ls -lh 
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# bzip2 -9 messages 
[root@xia7 dir]# ls -lh
total 84K
-rw------- 1 root root 83K Aug 16 17:40 messages.bz2
[root@xia7 dir]# bunzip2 messages.bz2 
[root@xia7 dir]# ls -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# bzip2 -k messages 
[root@xia7 dir]# ls -lh
total 1.5M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw------- 1 root root  83K Aug 16 17:40 messages.bz2
[root@xia7 dir]# bzcat messages.bz2 
Aug 14 11:29:01 xia7 rsyslogd: [origin software="rsyslogd" swVersion="7.4.7" x-pid="819" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
Aug 14 11:30:01 xia7 systemd: Started Session 7 of user root.
Aug 14 11:30:01 xia7 systemd: Starting Session 7 of user root.
Aug 14 11:40:01 xia7 systemd: Started Session 8 of user root.
Aug 14 11:40:01 xia7 systemd: Starting Session 8 of user root.
Aug 14 11:50:01 xia7 systemd: Started Session 9 of user root.

(5)xz/unxz/xzcat:

   

    压缩:xz

    语法:

        xz [option] /path/to/file

    常见选项:

        -#:指定压缩比,默认6,范围(1-9),数字越大,压缩比越高;

        -d:相当于命令unxz,解压文件;

        -k:压缩完后保留原文件;

     

        查看压缩:xzcat:

        语法:

            xzcat /path/to/file.xz

        

        解压缩:unxz

        语法:

            unxz [option] /path/to/file.xz

        常见选项:

            -k:解压缩完成后不删除原压缩文件;

[root@xia7 dir]# ll -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# xz messages 
[root@xia7 dir]# ll -lh
total 60K
-rw------- 1 root root 58K Aug 16 17:40 messages.xz
[root@xia7 dir]# xz -d messages.xz 
[root@xia7 dir]# ls -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# xz -9 messages 
[root@xia7 dir]# ls -lh
total 60K
-rw------- 1 root root 58K Aug 16 17:40 messages.xz
[root@xia7 dir]# unxz messages.xz 
[root@xia7 dir]# ls -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# xz -k messages 
[root@xia7 dir]# ls -lh
total 1.5M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw------- 1 root root  58K Aug 16 17:40 messages.xz
[root@xia7 dir]# xzcat messages.xz 
Aug 14 11:29:01 xia7 rsyslogd: [origin software="rsyslogd" swVersion="7.4.7" x-pid="819" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
Aug 14 11:30:01 xia7 systemd: Started Session 7 of user root.
Aug 14 11:30:01 xia7 systemd: Starting Session 7 of user root.
Aug 14 11:40:01 xia7 systemd: Started Session 8 of user root.
Aug 14 11:40:01 xia7 systemd: Starting Session 8 of user root.

  

(6)zip/unzip

     压缩:zip

     语法:

     zip /path/to/file.zip /opt/to/file…

     zip压缩工具可以把多个文件打包压缩成一个文件;

        

        解压缩:unzip

        语法:

            unzip /path/to/file.zip

[root@xia7 dir]# ll
total 1408
-rw-r--r-- 1 root root   13948 Aug 18 10:46 functions
-rw-r--r-- 1 root root      81 Aug 18 10:45 issue
-rw------- 1 root root 1415955 Aug 16 17:40 messages
-rw-r--r-- 1 root root    2016 Aug 18 10:45 passwd
[root@xia7 dir]# zip ceshi.zip ./*
  adding: functions (deflated 68%)
  adding: issue (deflated 2%)
  adding: messages (deflated 86%)
  adding: passwd (deflated 60%)
[root@xia7 dir]# ll -lh
total 1.6M
-rw-r--r-- 1 root root 196K Aug 18 10:48 ceshi.zip
-rw-r--r-- 1 root root  14K Aug 18 10:46 functions
-rw-r--r-- 1 root root   81 Aug 18 10:45 issue
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw-r--r-- 1 root root 2.0K Aug 18 10:45 passwd
[root@xia7 dir]# rm -f functions issue messages passwd 
[root@xia7 dir]# unzip ceshi.zip 
Archive:  ceshi.zip
  inflating: functions               
  inflating: issue                   
  inflating: messages                
  inflating: passwd                  
[root@xia7 dir]# ls
ceshi.zip  functions  issue  messages  passwd
[root@xia7 dir]#

2.打包工具:

         前面讲到的众多压缩工具一般只能对文件压缩,不能对整个目录打包压缩归档,tar

工具就能很好的实现既能打包归档同时又能压缩;

 

(1)tar工具:

      命令:tar

      语法:

          tar [option]…/path/to/file.tar /path/to/file…

          tar [option]…/path/to/file.tar

          tar [option]…/path/to/file.tar.[gz|bz2|xz] /path/to/file…

          tar [option]…/path/to/file.tar.[gz|bz2|xz] 

          tar [option]…/path/to/file.tar.[gz|bz2|xz] [-C /path/to/dir]

     常见选项:

         -c:打包归档成一个文件,相当于备份;

         -x:解包归档文件,默认当前目录;

         -C:解包到指定目录;

         -t:查看打包归档中的文件名;

         -f:跟打包文件参数,一般其他命令使用要跟-f结合,注意-f要放到选项最后;

         -z:使用gzip压缩和解压缩;

         -j:使用bzip2压缩和解压缩;

         -J:使用xz压缩和解压缩;

[root@xia7 dir]# ll
total 1408
-rw-r--r-- 1 root root   13948 Aug 18 10:46 functions
-rw-r--r-- 1 root root      81 Aug 18 10:45 issue
-rw------- 1 root root 1415955 Aug 16 17:40 messages
-rw-r--r-- 1 root root    2016 Aug 18 10:45 passwd
[root@xia7 dir]# tar cf ceshi.tar ./*
[root@xia7 dir]# ll -lh
total 2.8M
-rw-r--r-- 1 root root 1.4M Aug 18 11:14 ceshi.tar
-rw-r--r-- 1 root root  14K Aug 18 10:46 functions
-rw-r--r-- 1 root root   81 Aug 18 10:45 issue
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw-r--r-- 1 root root 2.0K Aug 18 10:45 passwd
[root@xia7 dir]# tar zcf ceshi1.tar.gz functions issue messages 
[root@xia7 dir]# tar jcf ceshi2.tar.bz2 functions issue messages passwd 
[root@xia7 dir]# tar Jcf ceshi3.tar.xz functions issue messages passwd 
[root@xia7 dir]# ll -lh
total 3.1M
-rw-r--r-- 1 root root 194K Aug 18 11:15 ceshi1.tar.gz
-rw-r--r-- 1 root root  90K Aug 18 11:15 ceshi2.tar.bz2
-rw-r--r-- 1 root root  63K Aug 18 11:16 ceshi3.tar.xz
-rw-r--r-- 1 root root 1.4M Aug 18 11:14 ceshi.tar
-rw-r--r-- 1 root root  14K Aug 18 10:46 functions
-rw-r--r-- 1 root root   81 Aug 18 10:45 issue
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw-r--r-- 1 root root 2.0K Aug 18 10:45 passwd
[root@xia7 dir]# tar tf ceshi2.tar.bz2 
functions
issue
messages
passwd
[root@xia7 dir]# tar tf ceshi3.tar.xz 
functions
issue
messages
passwd
[root@xia7 dir]# rm -f functions issue messages passwd 
[root@xia7 dir]# ll -lh
total 1.8M
-rw-r--r-- 1 root root 194K Aug 18 11:15 ceshi1.tar.gz
-rw-r--r-- 1 root root  90K Aug 18 11:15 ceshi2.tar.bz2
-rw-r--r-- 1 root root  63K Aug 18 11:16 ceshi3.tar.xz
-rw-r--r-- 1 root root 1.4M Aug 18 11:14 ceshi.tar
[root@xia7 dir]# tar xf ceshi3.tar.xz 
[root@xia7 dir]# ll -lh
total 3.1M
-rw-r--r-- 1 root root 194K Aug 18 11:15 ceshi1.tar.gz
-rw-r--r-- 1 root root  90K Aug 18 11:15 ceshi2.tar.bz2
-rw-r--r-- 1 root root  63K Aug 18 11:16 ceshi3.tar.xz
-rw-r--r-- 1 root root 1.4M Aug 18 11:14 ceshi.tar
-rw-r--r-- 1 root root  14K Aug 18 10:46 functions
-rw-r--r-- 1 root root   81 Aug 18 10:45 issue
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw-r--r-- 1 root root 2.0K Aug 18 10:45 passwd
[root@xia7 dir]# mkdir /tmp/ceshitar
[root@xia7 dir]# tar xf ceshi2.tar.bz2 -C /tmp/ceshitar/
[root@xia7 dir]# ll /tmp/ceshitar/
total 1408
-rw-r--r-- 1 root root   13948 Aug 18 10:46 functions
-rw-r--r-- 1 root root      81 Aug 18 10:45 issue
-rw------- 1 root root 1415955 Aug 16 17:40 messages
-rw-r--r-- 1 root root    2016 Aug 18 10:45 passwd
[root@xia7 dir]#

原创文章,作者:xiashixiang,如若转载,请注明出处:http://www.178linux.com/37223

(0)
xiashixiangxiashixiang
上一篇 2016-08-18
下一篇 2016-08-18

相关推荐

  • 马哥教育网络班22期+第8周课程练习

    week8: 1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。 网桥也叫桥接器,是连接两个局域网的一种存储/转发设备,它能将一个大的LAN分割为多个网段, 或将两个以上的LAN互联为一个逻辑LAN,使LAN上的所有用户都可访问服务器。 它工作于OSI(开放系统互联参考模型)参考模型第二层,即“数据链路层”。 集线器的英文称为“H…

    Linux干货 2016-10-09
  • bash脚本编程实例

    bash脚本编程实例 1.写一个脚本,判断当前系统上所有用户的shell是否为可登录的shell(即用户的shell不是/sbin/nologin),分别统计这两类用户的个数(通过字符串比较来实现) #!/bin/bash cat /etc/passwd|awk -F: ‘BEGIN{nologin=0;login=0}{if($NF==”/sbin/nol…

    Linux干货 2017-08-28
  • GNU awk

    GNU awk:      文本处理三剑客:grep,sed,awk            grep,egrep,fgrep:文本过滤工具;pattern     &nb…

    Linux干货 2017-05-22
  • 利用keepalived搭建高可用集群

    在一个系统中,常常存在一些单点服务器,为了提高整个系统的稳定性,我们常常需要对这些单点服务做高可用配置;keepalived即为一种常用的高可用配置服务; Keepalived的工作模式有以下两种: 1、 主/备:即单虚拟路径器,仅配置一个VIP; 2、 主/主:即多个虚拟路径器,配置多个VIP,每个虚拟路径器作为其中某个VIP的的master虚拟路径器; …

    Linux干货 2016-11-01
  • vsftpd权限配置

    要求 :完成vsftpd配置,   (1) 禁锢系统用户于家目录;   (2) 基于mysql进行用户认证;   (3) 不同的虚拟用户拥有不同的权限设定. 解答:  (1) 首先安装需要的系统环境和软件 yum groupinstall -y "Development&nbsp…

    Linux干货 2016-10-24
  • redis + keepalived 双主模型

    redis + keepalived 双主模型 架构图:    1.vip默认绑定在redis主上,由redis主提供服务,redis从为备用节点。(实际上提供服务的只是vip) 2.当redis主挂掉,vip会默认漂移至redis从。由redis从提供服务,redis主已经挂掉。 3.当redis主已经恢复,redis从继续提供服务和挂…

    Linux干货 2016-06-23