压缩

   1,为什么压缩?

   当需要存储大量文件时,为了节省磁盘空间;或是要通过网络传输文件时,为了节省网络带宽,我们都需要对文件进行压缩使其体积减小。

  2,有哪些常用压缩命令和压缩格式?

  常见的压缩命令有:gzip,bz2,xz.

  他们所对应的压缩格式分别为:xx.gz, xx.bz, xx.xz


  3,常用命令的详细使用

       (1.1)、gzip

        NAME
        gzip, gunzip, zcat - compress or expand files
        
        SYNOPSIS
        gzip [ -acdfhlLnNrtvV19 ] [-S suffix] [ name ...  ]
        gunzip [ -acfhlLnNrtvV ] [-S suffix] [ name ...  ]
        zcat [ -fhLV ] [ name ...  ]

     最简单的使用就是gzip filename

     如:# gzip 123.txt

     结果就是123.txt.gz

     这个命令有如下常用选项:

     -#:指定压缩比例

     -c:输出在屏幕,这个选项很有用,但是并不是真正用于输出在屏幕,而是有更有意义的用途。gzip 压缩和解压缩时是不能保留源文件的,也就是当对test.txt压缩成test.txt.gz后,原来的test.txt就不存在了。那如何保留源文件的,那就用-c选项,将文件压缩或解压后输出在屏幕,然后重定向到指定文件中

  [root@localhost ~]# ls 123*
  123.txt
  [root@localhost ~]# gzip -c 123.txt>123.txt.gz
  [root@localhost ~]# ls 123*
  123.txt  123.txt.gz
  [root@localhost ~]#

     -d:解压缩,这个选项就等价于gunzip命令

     gzip -d 123.txt.gz = gunzip 123.txt.gz


       (1.2)、gunzip

         这个命令上面已经说过,对gzip压缩的文件解压。

       (1.3)、zcat

         这个命令和cat很类似,用于查看gzip压缩后的压缩文件

         如:zcat 123.txt.gz

      (2.1)、bzip2

      NAME
             bzip2, bunzip2 - a block-sorting file compressor, v1.0.4
             bzcat - decompresses files to stdout
             bzip2recover - recovers data from damaged bzip2 files
    
      SYNOPSIS
             bzip2 [ -cdfkqstvzVL123456789 ] [ filenames ...  ]
             bunzip2 [ -fkvsVL ] [ filenames ...  ]
             bzcat [ -s ] [ filenames ...  ]
             bzip2recover filename

       

       这个命令最基本使用为bzip2 filename

    [root@localhost ~]# ls 123*
    123.txt
    [root@localhost ~]# bzip2 123.txt
    [root@localhost ~]# ls 123*
    123.txt.bz2
    [root@localhost ~]#


       有如下常用选项

       -c:输出在屏幕,和gzip的-c选项相同

       -#:指定压缩比例

       -k:保留源文件

    [root@localhost ~]# ls 123*
    123.txt
    [root@localhost ~]# bzip2 -k 123.txt
    [root@localhost ~]# ls 123*
    123.txt  123.txt.bz2
    [root@localhost ~]#

       -d:解压缩,bzip2 -d 123.txt.bz2 相当于 bunzip 123.txt.bz2

       (2.2) bunzip

       对bzip2压缩的文件进行解压缩

        [root@localhost ~]# ls 123*
        123.txt.bz2
        [root@localhost ~]# bunzip2 123.txt.bz2
        [root@localhost ~]# ls 123*
        123.txt
        [root@localhost ~]# bzip2 123.txt
        [root@localhost ~]# ls 123*
        123.txt.bz2
        [root@localhost ~]# bzip2 -d 123.txt.bz2
        [root@localhost ~]# ls 123*
        123.txt
        [root@localhost ~]#

       (2.3) bzcat

       这个命令gzip对应的zcat一样用于查看bzip2创建的压缩文件

       

       (3.1) xz

        NAME
               xz, unxz, xzcat, lzma, unlzma, lzcat - Compress or decompress .xz and .lzma files
        
        SYNOPSIS
               xz [option]...  [file]...
        
               unxz is equivalent to xz --decompress.
               xzcat is equivalent to xz --decompress --stdout.
               lzma is equivalent to xz --format=lzma.
               unlzma is equivalent to xz --format=lzma --decompress.
               lzcat is equivalent to xz --format=lzma --decompress --stdout.
        
               When  writing  scripts that need to decompress files, it is recommended to always use
       the name xz with appropriate arguments (xz -d or xz -dc) instead of  the  names  unxzand xzcat.


   简单例子:

    [root@localhost ~]# ls 123*
    123.txt
    [root@localhost ~]# xz 123.txt
    [root@localhost ~]# ls 123*
    123.txt.xz
    [root@localhost ~]#


       与前面两个命令类似,常用选项如下

       -c:输出到屏幕

       -d: 解压缩

   (3.2) unxz

       对xz的压缩文件解压缩

       unxz 等同于 xz -d

  (3.3) xzcat

       查看xz压缩的压缩文件


归档

   归档也就是打包,通常将大量零散文件打包成一个文件,方便存盘和传输

   linux中文件归档常用的命令是tar,这个命令非常强大,集成打包,解包,压缩,解压缩等功能与一身,所以掌握好这个命令就可以玩转对文件的打包和压缩操作。


NAME
       tar - manual page for tar 1.23
SYNOPSIS
       tar [OPTION...] [FILE]...
DESCRIPTION
       GNU  ‘tar’  saves  many  files  together  into a single tape or disk archive, and can
       restore individual files from the archive.
EXAMPLES
       tar -cf archive.tar foo bar
              # Create archive.tar from files foo and bar.
       tar -tvf archive.tar
              # List all files in archive.tar verbosely.
       tar -xf archive.tar
              # Extract all files from archive.tar.
    Main operation mode:
       -A, --catenate, --concatenate
              append tar files to an archive
              追加文件到一个打包文件之中
       -c, --create
              create a new archive
              创建一个新的打包文件
       -d, --diff, --compare
              find differences between archive and file system
       --delete
              delete from the archive (not on mag tapes!)
       -r, --append
              append files to the end of an archive
       -t, --list
              list the contents of an archive
              列出一个归档包文件中包含的所有文件
       --test-label
              test the archive volume label and exit
       -u, --update
              only append files newer than copy in archive
       -x, --extract, --get
              extract files from an archive
              对现有打包文档进行解包
       -C, --directory=DIR
              change to directory DIR
              指定路径
       -f, --file=ARCHIVE
              use archive file or device ARCHIVE
              指定文件
       -j, --bzip2
              filter the archive through bzip2
              调用bzip2命令对文档包压缩或者解压
        -J, --xz
              filter the archive through xz
              调用xz命令对文档包压缩或者解压
       -p, --preserve-permissions
              extract  information  about  file permissions (default for superuser)
       -v, --verbose
              verbosely list files processed
              显示过程
       -z, --gzip
              filter the archive through gzip
              调用gzip命令对文档包压缩或者解压


  用法:

       创建归档

           tar [选项] 包文件名  文件列表

        [root@localhost ~]# ls
        1.txt  2.txt  3.txt  4.txt
        [root@localhost ~]# tar -cf test.tar 1.txt 2.txt 3.txt 4.txt
        [root@localhost ~]# ls
        1.txt  2.txt  3.txt  4.txt  test.tar
        [root@localhost ~]#

       解开归档

           tar [选项]  包文件名  解开后存放路径

        [root@localhost ~]# mkdir demo
        [root@localhost ~]# tar -xf test.tar -C demo/
        [root@localhost ~]# ls demo/
        1.txt  2.txt  3.txt  4.txt
        [root@localhost ~]#


       常用选项说明

       -c:用于创建打包文档

       -x:用于对归档文件解包

       所以-c和-x是二选一

       -f:指定文件,当创建归档时,用于指定打包之后的文件名,当解包是,用于指定将要解开的归档文件,值得注意的是,当将多个选项缩写时,-f一定要放最后。

       -C:在对归档文件解包时,指定解开之后文件的存放位置,默认为当前目录

       

       -z:创建归档文件后调用gzip命令对其压缩

    [root@localhost ~]# ls
    1.txt  2.txt  3.txt  4.txt  demotxt
    [root@localhost ~]# tar -czf test.tar.gz 1.txt 2.txt 3.txt 4.txt
    [root@localhost ~]# ls
    1.txt  2.txt  3.txt  4.txt  demo  test.tar.gz
    [root@localhost ~]# tar -xzf test.tar.gz -C demo/
    [root@localhost ~]# ls demo/
    1.txt  2.txt  3.txt  4.txt
    [root@localhost ~]#


       在上面的操作中,对test.tar.gz解压展开式,可以不指定-z选项,因为系统会自动-z选项

       

       -j:创建归档文件后调用bzip2命令对其压缩

       -J:创建归档文件后调用xz命令对其压缩

       这两个选项和上面-z类似,使用相同


【【干货】】

       在日常使用中,我们很少只对一个普通文件进行压缩,也很少对文档只打包不压缩。通常都是对文件即打包又压缩。而且通常在网上下载的文档都是xx.tar.gz或者xx.tar.bz2。所以就在这里列出这些常规操作

       创建xx.tar.gz

    tar -czf xx.tar.gz filename ...

       展开xx.tar.gz

    tar -xf  xx.tar.gz [ -C SavePath ]

       

       创建xx.tar.bz2

    tar -cjf xx.tar.bz2 filename ...

       展开xx.tar.bx2

    tar -xjf xx.tar.bz2 [ -C SavePath ]

       

       创建xx.tar.xz

    tar -cJf xx.tar.xz filename ...

       展开xx.tar.xz

    tar -xJf xx.tar.xz [ -C SavePath ]