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)
上一篇 2016-08-18 15:20
下一篇 2016-08-18 15:27

相关推荐

  • 设计模式(七)组合模式Composite(结构型)

    1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面。 例子1:就是多级树形菜单。 例子2:文件和文件夹目录 2.问题 我们可以使用简单的对象组合成复杂的对象,而这个复杂对象有可以组合成更大的对象。我们可以把简单这些对象定义成类,然后定义一些容器类来存储这些简单对象。客户端代码必须区别对象简单对象和容器对象,而实际上大多数情况下用…

    Linux干货 2015-07-01
  • 马哥教育网络班20期-第四周课程作业

    Table of Contents 1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 2、编辑/etc/group文件,添加组hadoop。 3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id号;其家目录为/home/h…

    Linux干货 2016-06-26
  • Linux主要发行版

    Redhat:三大发行版之一,由红帽公司维护,分支有fedora,centosDebian:社区维护,非商业维护,三大发行版之一,分支有Ubuntu,Mintslackware:三大发行版之一,分支有Suse,opensusearch Linux:轻量级行业新贵

    Linux干货 2018-03-03
  • 10月18日作业–hash,history

    1 生产环境发现一台服务器系统时间产生偏差,造成服务异常,请帮忙校正。 1、使用ntpdate对时间进行同步,然后hwclock -w;并再建立Crontab每小时进行一次时间同步。 2 生产有一个数据同步脚本需要执行很长时间,怎样做到无人值守,在管理工具退出的情况下,脚本依然能正常运行。 1、载光盘 2、进入/mnt/Packages 安装共享screen…

    Linux干货 2016-10-19
  • 脚本进阶笔记整理

    一、逻辑运算 变量:   本地变量、环境变量、局部变量、位置参数变量、特殊变量   变量赋值:name=value,export name=value,declare -x name=value   变量引用:$name,${name}   注意:有些时候{}不能省略,例如 &n…

    Linux干货 2017-03-26
  • python-多进程

    进程是由系统自己管理的。 1:最基本的写法 from multiprocessing import Pool def f(x):     return x*x if __name__ == '__main__': &nb…

    Linux干货 2016-11-05