zoukankan      html  css  js  c++  java
  • 6.2 gzip:压缩或解压文件

    gzip命令

      用于将一个大的文件通过压缩算法(Lempel-Ziv coding(LZ77))变成一个小的文件。gzip命令不能直接压缩目录,因此目录需要先用tar打包成一个文件,然后tar再调用gzip进行压缩。
     
    -d    解开压缩文件
    -v    显示指令执行的过程
    -l    列出压缩文件的内容信息
     
    -c    将内容输出到标准输出,不改变原始文件
    -r    对目录下的所有文件递归进行压缩操作
    -数字<1-9>    指定压缩率,默认为6,值越大压缩率越高
    -t    测试,检查压缩文件是否完整

    把目录下的每个文件都压缩成单独的.gz文件

    [root@cs6 html]# ls
    10.html  1.html  2.html  3.html  4.html  5.html  6.html  7.html  8.html  9.html  oldboy
    [root@cs6 html]# gzip *.html
    [root@cs6 html]# ls
    10.html.gz  2.html.gz  4.html.gz  6.html.gz  8.html.gz  oldboy
    1.html.gz   3.html.gz  5.html.gz  7.html.gz  9.html.gz
     
    不解压显示上一个例子中每个压缩文件的信息。
    [root@cs6 html]# gzip -l *.gz
             compressed        uncompressed  ratio uncompressed_name
                     28                   0   0.0% 10.html
                     27                   0   0.0% 1.html
                     27                   0   0.0% 2.html
                     27                   0   0.0% 3.html
                     27                   0   0.0% 4.html
                     27                   0   0.0% 5.html
                     27                   0   0.0% 6.html
                     27                   0   0.0% 7.html
                     27                   0   0.0% 8.html
                     27                   0   0.0% 9.html
    [root@cs6 html]# ls
    10.html.gz  2.html.gz  4.html.gz  6.html.gz  8.html.gz  oldboy
    1.html.gz   3.html.gz  5.html.gz  7.html.gz  9.html.gz

    解压文件,并显示解压过程

    [root@cs6 html]# gzip -dv *.gz        #<==使用-d参数解压文件,使用-v参数显示解压过程。
    10.html.gz:      0.0% -- replaced with 10.html
    1.html.gz:      0.0% -- replaced with 1.html
    2.html.gz:      0.0% -- replaced with 2.html
    3.html.gz:      0.0% -- replaced with 3.html
    4.html.gz:      0.0% -- replaced with 4.html
    5.html.gz:      0.0% -- replaced with 5.html
    6.html.gz:      0.0% -- replaced with 6.html
    7.html.gz:      0.0% -- replaced with 7.html
    8.html.gz:      0.0% -- replaced with 8.html
    9.html.gz:      0.0% -- replaced with 9.html
    [root@cs6 html]# ls
    10.html  1.html  2.html  3.html  4.html  5.html  6.html  7.html  8.html  9.html  oldboy
    [root@cs6 html]#

    压缩解压保留源文件

    [root@cs6 html]# cp /etc/services .
    [root@cs6 html]# ll -h services
    -rw-r--r--. 1 root root 626K May 13 00:29 services
    [root@cs6 html]# gzip -c services >services.gz    #<==使用-c 选项与输出重定向符号将输出定向到services.gz。
    [root@cs6 html]# ll -h services*
    -rw-r--r--. 1 root root 626K May 13 00:29 services
    -rw-r--r--. 1 root root 125K May 13 00:30 services.gz        #<==使用-d选项解压。
    [root@cs6 html]# gzip -dc services.gz >services2
    [root@cs6 html]# diff services services2            #<==对比源文件和解压后的文件,没有差别。
    [root@cs6 html]# ll -h services*
    -rw-r--r--. 1 root root 626K May 13 00:29 services
    -rw-r--r--. 1 root root 626K May 13 00:31 services2
    -rw-r--r--. 1 root root 125K May 13 00:30 services.gz

    经验技巧

        虽然上面使用重定向符号解决了保留源文件的问题,但是使用起来还是不太方便,因此这里告诉大家一个好方法:gzip套件包含了许多可以“在原地”处理压缩文件的实用程序。zcat、zgrep、zless、zdiff等实用程序的作用分别与cat、grep、less和diff相同,但是它们操作的是压缩的文件。比如:
     
    [root@cs6 html]# zcat services.gz |head
    # /etc/services:
    # $Id: services,v 1.48 2009/11/11 14:32:31 ovasik Exp $
    #
    # Network services, Internet style
    # IANA services version: last updated 2009-11-10
    #
    # Note that it is presently the policy of IANA to assign a single well-known
    # port number for both TCP and UDP; hence, most entries here have two entries
    # even if the protocol doesn't support UDP operations.
    # Updated from RFC 1700, ``Assigned Numbers'' (October 1994).  Not all ports
    [root@cs6 html]# zcat services.gz >services  #也可以直接解压出来重定向到文件。
    [root@cs6 html]#
  • 相关阅读:
    复合文档(Compound Document)读写栗子
    JavaScript修改IE注册表
    mysql_real_connect 端口号说明
    _beginthreadex创建线程,立即执行?
    Access 是/否 字段
    JavaScript格式化日期输出
    STM32-串行SPI nor
    全球唯一标识符:GUID在线生成
    如何交叉编译Python到ARM-Linux平台(转)
    CMOS Sensor的调试经验分享(转)
  • 原文地址:https://www.cnblogs.com/wenyule/p/12214036.html
Copyright © 2011-2022 走看看