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

相关推荐

  • MySQL基础知识

    1、SQL:结构化查询语言(Structured Query Language): DDL:Data Definition Language(数据定义语言): 其语句包括动词CREATE和DROP。在数据库中创建新表或删除表(CREAT TABLE 或 DROP TABLE);为表加入索引等。DDL包括许多与人数据库目录中获得数据有关的保留字。它也是动作查询…

    2017-11-20
  • Python线程指南

    本文介绍了Python对于线程的支持,包括“学会”多线程编程需要掌握的基础以及Python两个线程标准库的完整介绍及使用示例。 注意:本文基于Python2.4完成,;如果看到不明白的词汇请记得百度谷歌或维基,whatever。 尊重作者的劳动,转载请注明作者及原文地址 >.< 1. 线程基础 1.1. 线程状态 线程有5种状态,状态转换的过程如…

    2015-03-13
  • 软件包管理和磁盘管理

    软件运行和编译 ABI :Application Binary Interface 应用程序二进制接口     Windows和Linux不兼容      PE格式   ELF格式 库级别的虚拟化:       Linu…

    2017-04-24
  • Linux基础知识(三)

    1、列出当前系统上所有已经登录的用户的用户名(多次登录的用户,只显示一次即可) [root@bogon ~]# w |grep -E ".*(pts|tty).*"|awk '{print $1}'|uniq 2、列出最后登录到当前系统的用户的相关信息 [root@bogon ~]# last -aF 3、取出当前系统…

    Linux干货 2016-10-09
  • Linux 文 本 处 理 工 具

    Linux 文 本 处 理 工 具 一.学习大纲: ◎各种文本工具来查看、分析、统计文本文件 文件内容查看工具:cat, tac,rev,more,less 文件截取:head和tail 按列抽取:cut,paste 分析文本的工具:wc , sort , uniq,diff和patch 命令使用练习题 ◎文本过滤与处理工具: grep与正则表达式…

    Linux干货 2016-08-05
  • lvs笔记之nat&dr模型简单实现

    lvs笔记之nat&dr模型简单实现 lvs笔记之nat&dr模型简单实现 lvs 集群 实现 负载均衡 nat lvs笔记之nat&dr模型简单实现 ipvsadm使用说明 lvs-nat的简单实现 踩过的坑1 lvs-dr实现 总结 ipvsadm使用说明     -A: 添加一个…

    2017-01-03