zoukankan      html  css  js  c++  java
  • Linux 文件压缩

    压缩工具
      compress/uncompress:对应 .Z 结尾的压缩格式文件
      压缩格式:gz、bz2、xz、zip、Z
      gzip  压缩文件并删除源文件(生成.gz的文件)
      gunzip 解压缩文件(gzip -d有相同的功能)
      bzip2   压缩文件(压缩比例比gzip更高后缀为.bz2)
      bunzip2 解压缩文件(bzip -d有相同的功能)

      压缩算法不同,压缩比也会不同
    gzip
      gzip /PATH/TO/SOMEFILE 压缩完成后会删除原文件
      -d 解压缩
      -# 指定压缩比(1-9),默认是6
      -c 将压缩结果送往标准输出,可以使用重定向将其保存为压缩文件,从而保留原文件
      例子:
      gzip -c /PATH/TO/SOMEFILE > SOMEFILE.gz
    gunzip
      gunzip /PATH/TO/SOMEFILE.gz  解压完成后会删除原文件
      zcat /PATH/TO/SOMEFILE.gz      不解压的情况,查看文本文件的内容
      z系列命令,可以在不经解压的情况下,直接操作gzip压缩文件
      zcat  直接显示压缩文件的内容
      zless 直接逐行显示压缩文件的内容
      zdiff 直接报告压缩文件的差异内容
      zcmp 直接报告压缩文件的差异处
    演示:
    [root@centos7 ~]# cp /var/log/messages /tmp/test/
    [root@centos7 ~]# ll /tmp/test/
    总用量 288
    -rw-r----- 1 root root 0 2月 20 13:41 a
    -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
    -r--r----- 1 root root 0 2月 20 13:41 c
    -rwxrwxr-x 1 root root 0 2月 20 13:41 d
    -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
    -rw-r--r-- 1 root root 0 2月 20 13:41 f
    -rw-r--r-- 1 root root 0 2月 20 13:41 g
    -rw------- 1 root root 292504 2月 20 16:28 messages
    [root@centos7 ~]# ll -h /tmp/test/messages
    -rw------- 1 root root 286K 2月 20 16:28 /tmp/test/messages

    # 压缩,删除原文件,保留压缩后以.gz结尾的文件
    [root@centos7 ~]# gzip /tmp/test/messages

    [root@centos7 ~]# ll -h /tmp/test
    总用量 44K
    -rw-r----- 1 root root 0 2月 20 13:41 a
    -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
    -r--r----- 1 root root 0 2月 20 13:41 c
    -rwxrwxr-x 1 root root 0 2月 20 13:41 d
    -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
    -rw-r--r-- 1 root root 0 2月 20 13:41 f
    -rw-r--r-- 1 root root 0 2月 20 13:41 g
    -rw------- 1 root root 41K 2月 20 16:28 messages.gz

    # 解压缩,原来的压缩文件被删除,保留解压缩后的文件
    [root@centos7 ~]# gunzip /tmp/test/messages.gz
    [root@centos7 ~]# ll -h /tmp/test
    总用量 288K
    -rw-r----- 1 root root 0 2月 20 13:41 a
    -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
    -r--r----- 1 root root 0 2月 20 13:41 c
    -rwxrwxr-x 1 root root 0 2月 20 13:41 d
    -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
    -rw-r--r-- 1 root root 0 2月 20 13:41 f
    -rw-r--r-- 1 root root 0 2月 20 13:41 g
    -rw------- 1 root root 286K 2月 20 16:28 messages

    # zcat可以查看压缩的文件,不建议对大文件使用zcat命令查看
    [root@centos7 ~]# zcat /tmp/test/messages.gz

    #=====================================================================================
    # 解压缩
    [root@centos7 ~]# gzip -d /tmp/test/messages.gz
    [root@centos7 ~]# ll /tmp/test/
    总用量 288
    -rw-r----- 1 root root 0 2月 20 13:41 a
    -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
    -r--r----- 1 root root 0 2月 20 13:41 c
    -rwxrwxr-x 1 root root 0 2月 20 13:41 d
    -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
    -rw-r--r-- 1 root root 0 2月 20 13:41 f
    -rw-r--r-- 1 root root 0 2月 20 13:41 g
    -rw------- 1 root root 292504 2月 20 16:28 messages

    # 将压缩或解压缩的结果输出至标准输出(gzip -c FILE > /PATH/TP/SOMEFILE.gz)
    [root@centos7 ~]# gzip -c /tmp/test/messages > /tmp/test/messages.gz
    [root@centos7 ~]# ll /tmp/test/
    总用量 332
    -rw-r----- 1 root root 0 2月 20 13:41 a
    -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
    -r--r----- 1 root root 0 2月 20 13:41 c
    -rwxrwxr-x 1 root root 0 2月 20 13:41 d
    -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
    -rw-r--r-- 1 root root 0 2月 20 13:41 f
    -rw-r--r-- 1 root root 0 2月 20 13:41 g
    -rw------- 1 root root 292504 2月 20 16:28 messages
    -rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz

    # 解压缩到标准输出
    [root@centos7 ~]# rm -f /tmp/test/messages
    [root@centos7 ~]# gzip -d -c /tmp/test/messages.gz > /tmp/test/messages
    [root@centos7 ~]# ll /tmp/test/
    总用量 332
    -rw-r----- 1 root root 0 2月 20 13:41 a
    -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
    -r--r----- 1 root root 0 2月 20 13:41 c
    -rwxrwxr-x 1 root root 0 2月 20 13:41 d
    -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
    -rw-r--r-- 1 root root 0 2月 20 13:41 f
    -rw-r--r-- 1 root root 0 2月 20 13:41 g
    -rw-r--r-- 1 root root 292504 2月 20 16:50 messages
    -rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz

    bzip2
      比gzip有着更大压缩比的压缩工具,使用格式近似
      bzip2 /PATH/TO/SOMEFILE
      -d 解压缩
      -# 指定压缩比(1-9),默认是6
      -k 压缩解压时保留原文件
    bunzip2
      bunzip2 /PATH/TO/SOMEFILE.bz2 解压完成后会删除原文件
      bzcat /PATH/TO/SOMEFILE.bz2 不解压的情况,查看文本文件的内容
    演示:
    # 压缩
    [root@centos7 ~]# bzip2 /tmp/test/messages

    [root@centos7 ~]# ll -h /tmp/test/
    总用量 72K
    -rw-r----- 1 root root 0 2月 20 13:41 a
    -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
    -r--r----- 1 root root 0 2月 20 13:41 c
    -rwxrwxr-x 1 root root 0 2月 20 13:41 d
    -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
    -rw-r--r-- 1 root root 0 2月 20 13:41 f
    -rw-r--r-- 1 root root 0 2月 20 13:41 g
    -rw-r--r-- 1 root root 26K 2月 20 16:50 messages.bz2 #压缩后的结果
    -rw-r--r-- 1 root root 41K 2月 20 16:44 messages.gz

    # 解压缩
    [root@centos7 ~]# bunzip2 /tmp/test/messages.bz2
    [root@centos7 ~]# ll -h /tmp/test/
    总用量 332K
    -rw-r----- 1 root root 0 2月 20 13:41 a
    -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
    -r--r----- 1 root root 0 2月 20 13:41 c
    -rwxrwxr-x 1 root root 0 2月 20 13:41 d
    -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
    -rw-r--r-- 1 root root 0 2月 20 13:41 f
    -rw-r--r-- 1 root root 0 2月 20 13:41 g
    -rw-r--r-- 1 root root 286K 2月 20 16:50 messages # 解压缩后的结果
    -rw-r--r-- 1 root root 41K 2月 20 16:44 messages.gz

    # -k 选项不用指明重定向的文件,自动保留源文件在当前文件中
    [root@centos7 ~]# bzip2 -k /tmp/test/messages
    [root@centos7 ~]# ll -h /tmp/test/
    总用量 360K
    -rw-r----- 1 root root 0 2月 20 13:41 a
    -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
    -r--r----- 1 root root 0 2月 20 13:41 c
    -rwxrwxr-x 1 root root 0 2月 20 13:41 d
    -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
    -rw-r--r-- 1 root root 0 2月 20 13:41 f
    -rw-r--r-- 1 root root 0 2月 20 13:41 g
    -rw-r--r-- 1 root root 286K 2月 20 16:50 messages
    -rw-r--r-- 1 root root 26K 2月 20 16:50 messages.bz2
    -rw-r--r-- 1 root root 41K 2月 20 16:44 messages.gz

    xz
      xz /PATH/TO/SOMEFILE
      -d 解压缩
      -# 指定压缩比(1-9),默认是6
      -k 压缩解压时保留原文件
    unxz
      unxz /PATH/TO/SOMEFILE.xz 解压完成后会删除原文件
      xzdec /PATH/TO/SOMEFILE.xz 解压文件显示到终端上
      xzcat /PATH/TO/SOMEFILE.xz 不解压的情况,查看文本文件的内容

      注意:以上压缩工具只能压缩文件,不能压缩目录,如果指定目录中的所有文件/PATH/TO/*会将目录中的每个文件压缩成一个压缩文件,所以在压缩目录的时候还需要归档工具
    演示:
    [root@centos7 ~]# xz /tmp/test/messages
    [root@centos7 ~]# ll -h /tmp/test/
    总用量 96K
    -rw-r----- 1 root root 0 2月 20 13:41 a
    -rw-rw-rw- 1 root root 0 2月 20 13:41 b.danger
    -r--r----- 1 root root 0 2月 20 13:41 c
    -rwxrwxr-x 1 root root 0 2月 20 13:41 d
    -rwxrwxrwx 1 root root 0 2月 20 13:41 e.danger
    -rw-r--r-- 1 root root 0 2月 20 13:41 f
    -rw-r--r-- 1 root root 0 2月 20 13:41 g
    -rw-r--r-- 1 root root 26K 2月 20 16:50 messages.bz2
    -rw-r--r-- 1 root root 41K 2月 20 16:44 messages.gz
    -rw-r--r-- 1 root root 21K 2月 20 16:50 messages.xz

    zip  既归档又压缩的工具(window下面的.zip的文件可以直接在linux下解压)
      zip FILENAME.zip FILE1 FILE2 ... 压缩多个文件到一个目录中,压缩后不删除原文件
      unzip FILENAME.zip
      如果要压缩默认目录,要通过指定目录的所有文件/PATH/TO/*才能压缩整个目录下的文件,如果指定的是/PATH/TO/就只会压缩TO这个目录本身并不包括目录中的文件
      zip /PATH/TO/ /PATH/TO/SOMEFILE.zip 只是压缩了TO目录
      zip /PATH/TO/* /PATH/TO/SOMEFILE.zip 压缩了TO目录中的所有文件
    演示:
    # 对目录进行归档并压缩
    [root@centos7 test]# zip /tmp/test/tao.zip tao
    adding: tao/ (stored 0%)
    [root@centos7 test]# ll
    总用量 188
    -rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log
    -rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2
    -rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
    -rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz
    -rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar
    -rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log
    drwxr-xr-x 2 root root 121 2月 20 17:43 tao
    -rw-r--r-- 1 root root 7232 2月 20 18:15 tao.tar.gz
    -rw-r--r-- 1 root root 158 2月 20 18:26 tao.zip
    -rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log
    -rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log
    -rw------- 1 root root 105 2月 20 17:42 yum.log

    [root@centos7 test]# rm -fr tao

    # 解压缩
    [root@centos7 test]# unzip tao.zip
    Archive: tao.zip
    creating: tao/
    [root@centos7 test]# ll
    总用量 188
    -rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log
    -rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2
    -rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
    -rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz
    -rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar
    -rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log
    drwxr-xr-x 2 root root 6 2月 20 17:43 tao
    -rw-r--r-- 1 root root 7232 2月 20 18:15 tao.tar.gz
    -rw-r--r-- 1 root root 158 2月 20 18:26 tao.zip
    -rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log
    -rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log
    -rw------- 1 root root 105 2月 20 17:42 yum.log


    tar 归档压缩工具(archive归档本身并不意味着压缩) 
      主要参数:
      -f 指定创建或展开归档文件名(只要操作归档文件就要用-f)
      -c 创建归档文件
      -v 显示创建的详细过程
      -x 展开归档文件
      -r 将文件添加到已经存在的归档文件中(压缩后是不能用r的,只有归档的时候用)
      -z 用gzip来压缩和解压缩文件
      -j 用bz2来压缩和解压缩文件
      -J 用xz来压缩和解压缩文件
      -t 不展开归档文件情况下查看文件中的内容
      -C 展开或解压文件到指定目录(如果没指定该参数,默认是当前目录)
      --xattrs 归档时,保留文件的扩展属性信息

      -zcf 归档并调用gzip压缩
      -zxf 调用gzip解压缩并展开归档,-z选项可省略(解压时自动判断用什么工具压缩)

      -jcf 归档并调用bzip2压缩
      -jxf 调用bzip2解压缩并展开归档,-j选项可省略

      -Jcf 归档并调用xz压缩
      -Jxf 调用xz解压缩并展开归档,-J选项可省略

      例如:
      tar -cvf home.tar /home 将home目录打包成home.tar(只是打包并没有压缩)
      tar -rvf home.tar /etc 将etc目录添加到已存在的打包文件home.tar里面
      tar -czvf home.tar.gz /home 将home目录打包并压缩成home.tar.gz
      tar -cvjf home.tar.bz2 /home 将home目录打包并压缩成.bz2的格式
      tar -xzvf home.tar.gz 将home.tar.gz这个备份文件还原并解压缩
      tar -tvf home.tar | more 查看home.tar备份文件的类容,并以分屏方式显示在显示器上
      tar -cvf /dev/st0 /home/shrek 可以将st0磁带机备份到/home目录下面

      注意:f选项一定要写在最后一个;如果是解开压缩归档文件可以不指定压缩选项zjJ,tar会通过文件后缀判断是什么压缩工具压缩的
      归档演示:
    [root@centos7 ~]# ls /tmp/test/tao
    boot.log fstab issue pacemaker.log wpa_supplicant.log Xorg.0.log yum.log

    # 对所有以 .log 结尾的文件进行归档
    [root@centos7 ~]# tar -cf /tmp/test/mylog.tar /tmp/test/tao/*.log
    [root@centos7 ~]# ll /tmp/test/
    总用量 136
    -rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2
    -rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
    -rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz
    -rw-r--r-- 1 root root 40960 2月 20 17:45 mylog.tar # 归档后的文件
    drwxr-xr-x 2 root root 121 2月 20 17:43 tao

    # 展开归档
    [root@centos7 test]# tar xf mylog.tar
    [root@centos7 test]# ll
    总用量 176
    -rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log
    -rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2
    -rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
    -rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz
    -rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar
    -rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log
    drwxr-xr-x 2 root root 121 2月 20 17:43 tao
    -rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log
    -rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log
    -rw------- 1 root root 105 2月 20 17:42 yum.log

    # -C 展开归档至指定文件中
    [root@centos7 test]# mkdir /tmp/newtest
    [root@centos7 test]# tar xf mylog.tar -C /tmp/newtest
    [root@centos7 test]# ll /tmp/newtest
    总用量 40
    -rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log
    -rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log
    -rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log
    -rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log
    -rw------- 1 root root 105 2月 20 17:42 yum.log

    # 查看归档文件中的文件列表
    [root@centos7 test]# tar tf mylog.tar
    boot.log
    pacemaker.log
    wpa_supplicant.log
    Xorg.0.log
    yum.log

    归档并压缩演示:
    # 对目录进行归档并压缩
    [root@centos7 test]# tar zcf /tmp/test/tao.tar.gz tao
    [root@centos7 test]# ll /tmp/test
    总用量 184
    -rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log
    -rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2
    -rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
    -rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz
    -rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar
    -rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log
    drwxr-xr-x 2 root root 121 2月 20 17:43 tao # 原文件
    -rw-r--r-- 1 root root 7232 2月 20 18:15 tao.tar.gz # 归档压缩后的文件
    -rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log
    -rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log
    -rw------- 1 root root 105 2月 20 17:42 yum.log

    # 删除原文件
    [root@centos7 test]# rm -fr tao

    # 展开归档,其中 z 可省略,tar命令会自动识别其为压缩文件
    [root@centos7 test]# tar xf tao.tar.gz
    [root@centos7 test]# ll
    总用量 184
    -rw-r--r-- 1 root root 12097 2月 20 17:42 boot.log
    -rw-r--r-- 1 root root 26074 2月 20 16:50 messages.bz2
    -rw-r--r-- 1 root root 41791 2月 20 16:44 messages.gz
    -rw-r--r-- 1 root root 21420 2月 20 16:50 messages.xz
    -rw-r--r-- 1 root root 40960 2月 20 17:51 mylog.tar
    -rw-r----- 1 root root 0 2月 20 17:42 pacemaker.log
    drwxr-xr-x 2 root root 121 2月 20 17:43 tao # 展开后的文件
    -rw-r--r-- 1 root root 7232 2月 20 18:15 tao.tar.gz
    -rw-r--r-- 1 root root 2800 2月 20 17:42 wpa_supplicant.log
    -rw-r--r-- 1 root root 18303 2月 20 17:42 Xorg.0.log
    -rw------- 1 root root 105 2月 20 17:42 yum.log

    cpio 归档工具(把文件复制为归档文件)
      cpio命令是通过重定向的方式将文件进行打包备份,还原恢复的工具,它可以解压以“.cpio”或者“.tar”结尾的文件。
      用法:
      cpio[选项] > 文件名或者设备名
      cpio[选项] < 文件名或者设备名
      选项:
      -o 将文件拷贝打包成文件或者将文件输出到设备上
      -i 解包,将打包文件解压或将设备上的备份还原到系统
      -t 预览,查看文件内容或者输出到设备上的文件内容
      -v 显示打包过程中的文件名称
      -d 解包生成目录,在cpio还原时,自动的建立目录
      -c 一种较新的存储方式
    示例:
      将etc目录备份:
      find . /etc -print |cpio -ov > etc.cpio
      内容预览
      cpio -tv < etc.cpio
      要解包文件
      cpio -iv < etc.cpio
      cpio -idv < etc.cpio

    压缩工具   compress/uncompress:对应 .Z 结尾的压缩格式文件  压缩格式:gz、bz2、xz、zip、Z    gzip       压缩文件并删除源文件(生成.gz的文件)    gunzip    解压缩文件(gzip -d有相同的功能)    bzip2      压缩文件(压缩比例比gzip更高后缀为.bz2)    bunzip2  解压缩文件(bzip -d有相同的功能)
      压缩算法不同,压缩比也会不同  gzip  gzip /PATH/TO/SOMEFILE  压缩完成后会删除原文件    -d    解压缩    -#    指定压缩比(1-9),默认是6    -c    将压缩结果送往标准输出,可以使用重定向将其保存为压缩文件,从而保留原文件  例子:  gzip -c /PATH/TO/SOMEFILE > SOMEFILE.gz  gunzip  gunzip /PATH/TO/SOMEFILE.gz  解压完成后会删除原文件  zcat /PATH/TO/SOMEFILE.gz      不解压的情况,查看文本文件的内容  z系列命令,可以在不经解压的情况下,直接操作gzip压缩文件    zcat        直接显示压缩文件的内容    zless       直接逐行显示压缩文件的内容    zdiff       直接报告压缩文件的差异内容    zcmp      直接报告压缩文件的差异处演示:    [root@centos7 ~]# cp /var/log/messages /tmp/test/    [root@centos7 ~]# ll /tmp/test/    总用量 288    -rw-r----- 1 root root      0 2月  20 13:41 a    -rw-rw-rw- 1 root root      0 2月  20 13:41 b.danger    -r--r----- 1 root root      0 2月  20 13:41 c    -rwxrwxr-x 1 root root      0 2月  20 13:41 d    -rwxrwxrwx 1 root root      0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root      0 2月  20 13:41 f    -rw-r--r-- 1 root root      0 2月  20 13:41 g    -rw------- 1 root root 292504 2月  20 16:28 messages    [root@centos7 ~]# ll -h /tmp/test/messages    -rw------- 1 root root 286K 2月  20 16:28 /tmp/test/messages
        # 压缩,删除原文件,保留压缩后以.gz结尾的文件    [root@centos7 ~]# gzip /tmp/test/messages
        [root@centos7 ~]# ll -h /tmp/test    总用量 44K    -rw-r----- 1 root root   0 2月  20 13:41 a    -rw-rw-rw- 1 root root   0 2月  20 13:41 b.danger    -r--r----- 1 root root   0 2月  20 13:41 c    -rwxrwxr-x 1 root root   0 2月  20 13:41 d    -rwxrwxrwx 1 root root   0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root   0 2月  20 13:41 f    -rw-r--r-- 1 root root   0 2月  20 13:41 g    -rw------- 1 root root 41K 2月  20 16:28 messages.gz
        # 解压缩,原来的压缩文件被删除,保留解压缩后的文件    [root@centos7 ~]# gunzip /tmp/test/messages.gz    [root@centos7 ~]# ll -h /tmp/test    总用量 288K    -rw-r----- 1 root root    0 2月  20 13:41 a    -rw-rw-rw- 1 root root    0 2月  20 13:41 b.danger    -r--r----- 1 root root    0 2月  20 13:41 c    -rwxrwxr-x 1 root root    0 2月  20 13:41 d    -rwxrwxrwx 1 root root    0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root    0 2月  20 13:41 f    -rw-r--r-- 1 root root    0 2月  20 13:41 g    -rw------- 1 root root 286K 2月  20 16:28 messages
        # zcat可以查看压缩的文件,不建议对大文件使用zcat命令查看    [root@centos7 ~]# zcat /tmp/test/messages.gz
        #=====================================================================================    # 解压缩    [root@centos7 ~]# gzip -d /tmp/test/messages.gz    [root@centos7 ~]# ll /tmp/test/    总用量 288    -rw-r----- 1 root root      0 2月  20 13:41 a    -rw-rw-rw- 1 root root      0 2月  20 13:41 b.danger    -r--r----- 1 root root      0 2月  20 13:41 c    -rwxrwxr-x 1 root root      0 2月  20 13:41 d    -rwxrwxrwx 1 root root      0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root      0 2月  20 13:41 f    -rw-r--r-- 1 root root      0 2月  20 13:41 g    -rw------- 1 root root 292504 2月  20 16:28 messages
        # 将压缩或解压缩的结果输出至标准输出(gzip -c FILE > /PATH/TP/SOMEFILE.gz)    [root@centos7 ~]# gzip -c /tmp/test/messages > /tmp/test/messages.gz    [root@centos7 ~]# ll /tmp/test/    总用量 332    -rw-r----- 1 root root      0 2月  20 13:41 a    -rw-rw-rw- 1 root root      0 2月  20 13:41 b.danger    -r--r----- 1 root root      0 2月  20 13:41 c    -rwxrwxr-x 1 root root      0 2月  20 13:41 d    -rwxrwxrwx 1 root root      0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root      0 2月  20 13:41 f    -rw-r--r-- 1 root root      0 2月  20 13:41 g    -rw------- 1 root root 292504 2月  20 16:28 messages    -rw-r--r-- 1 root root  41791 2月  20 16:44 messages.gz
        # 解压缩到标准输出    [root@centos7 ~]# rm -f /tmp/test/messages    [root@centos7 ~]# gzip -d -c /tmp/test/messages.gz > /tmp/test/messages    [root@centos7 ~]# ll /tmp/test/    总用量 332    -rw-r----- 1 root root      0 2月  20 13:41 a    -rw-rw-rw- 1 root root      0 2月  20 13:41 b.danger    -r--r----- 1 root root      0 2月  20 13:41 c    -rwxrwxr-x 1 root root      0 2月  20 13:41 d    -rwxrwxrwx 1 root root      0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root      0 2月  20 13:41 f    -rw-r--r-- 1 root root      0 2月  20 13:41 g    -rw-r--r-- 1 root root 292504 2月  20 16:50 messages    -rw-r--r-- 1 root root  41791 2月  20 16:44 messages.gz
      bzip2    比gzip有着更大压缩比的压缩工具,使用格式近似    bzip2 /PATH/TO/SOMEFILE      -d    解压缩      -#    指定压缩比(1-9),默认是6      -k    压缩解压时保留原文件  bunzip2    bunzip2 /PATH/TO/SOMEFILE.bz2     解压完成后会删除原文件    bzcat /PATH/TO/SOMEFILE.bz2        不解压的情况,查看文本文件的内容演示:    # 压缩    [root@centos7 ~]# bzip2 /tmp/test/messages
        [root@centos7 ~]# ll -h /tmp/test/    总用量 72K    -rw-r----- 1 root root   0 2月  20 13:41 a    -rw-rw-rw- 1 root root   0 2月  20 13:41 b.danger    -r--r----- 1 root root   0 2月  20 13:41 c    -rwxrwxr-x 1 root root   0 2月  20 13:41 d    -rwxrwxrwx 1 root root   0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root   0 2月  20 13:41 f    -rw-r--r-- 1 root root   0 2月  20 13:41 g    -rw-r--r-- 1 root root 26K 2月  20 16:50 messages.bz2 #压缩后的结果    -rw-r--r-- 1 root root 41K 2月  20 16:44 messages.gz
        # 解压缩    [root@centos7 ~]# bunzip2 /tmp/test/messages.bz2    [root@centos7 ~]# ll -h /tmp/test/    总用量 332K    -rw-r----- 1 root root    0 2月  20 13:41 a    -rw-rw-rw- 1 root root    0 2月  20 13:41 b.danger    -r--r----- 1 root root    0 2月  20 13:41 c    -rwxrwxr-x 1 root root    0 2月  20 13:41 d    -rwxrwxrwx 1 root root    0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root    0 2月  20 13:41 f    -rw-r--r-- 1 root root    0 2月  20 13:41 g    -rw-r--r-- 1 root root 286K 2月  20 16:50 messages  # 解压缩后的结果    -rw-r--r-- 1 root root  41K 2月  20 16:44 messages.gz
        # -k 选项不用指明重定向的文件,自动保留源文件在当前文件中    [root@centos7 ~]# bzip2 -k /tmp/test/messages    [root@centos7 ~]# ll -h /tmp/test/    总用量 360K    -rw-r----- 1 root root    0 2月  20 13:41 a    -rw-rw-rw- 1 root root    0 2月  20 13:41 b.danger    -r--r----- 1 root root    0 2月  20 13:41 c    -rwxrwxr-x 1 root root    0 2月  20 13:41 d    -rwxrwxrwx 1 root root    0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root    0 2月  20 13:41 f    -rw-r--r-- 1 root root    0 2月  20 13:41 g    -rw-r--r-- 1 root root 286K 2月  20 16:50 messages    -rw-r--r-- 1 root root  26K 2月  20 16:50 messages.bz2    -rw-r--r-- 1 root root  41K 2月  20 16:44 messages.gz
      xz    xz /PATH/TO/SOMEFILE      -d    解压缩      -#    指定压缩比(1-9),默认是6      -k    压缩解压时保留原文件  unxz    unxz /PATH/TO/SOMEFILE.xz        解压完成后会删除原文件    xzdec /PATH/TO/SOMEFILE.xz       解压文件显示到终端上    xzcat /PATH/TO/SOMEFILE.xz        不解压的情况,查看文本文件的内容以上压缩工具只能压缩文件,不能压缩目录,如果指定目录中的所有文件/PATH/TO/*会将目录中的每个文件压缩成一个压缩文件,所以在压缩目录的时候还需要归档工具演示:    [root@centos7 ~]# xz /tmp/test/messages    [root@centos7 ~]# ll -h /tmp/test/    总用量 96K    -rw-r----- 1 root root   0 2月  20 13:41 a    -rw-rw-rw- 1 root root   0 2月  20 13:41 b.danger    -r--r----- 1 root root   0 2月  20 13:41 c    -rwxrwxr-x 1 root root   0 2月  20 13:41 d    -rwxrwxrwx 1 root root   0 2月  20 13:41 e.danger    -rw-r--r-- 1 root root   0 2月  20 13:41 f    -rw-r--r-- 1 root root   0 2月  20 13:41 g    -rw-r--r-- 1 root root 26K 2月  20 16:50 messages.bz2    -rw-r--r-- 1 root root 41K 2月  20 16:44 messages.gz    -rw-r--r-- 1 root root 21K 2月  20 16:50 messages.xz
      zip  既归档又压缩的工具(window下面的.zip的文件可以直接在linux下解压)      zip FILENAME.zip FILE1 FILE2 ...        压缩多个文件到一个目录中,压缩后不删除原文件    unzip FILENAME.zip    如果要压缩默认目录,要通过指定目录的所有文件/PATH/TO/*才能压缩整个目录下的文件,如果指定的是/PATH/TO/就只会压缩TO这个目录本身并不包括目录中的文件    zip /PATH/TO/    /PATH/TO/SOMEFILE.zip  只是压缩了TO目录    zip /PATH/TO/*  /PATH/TO/SOMEFILE.zip  压缩了TO目录中的所有文件演示:    # 对目录进行归档并压缩    [root@centos7 test]# zip /tmp/test/tao.zip tao      adding: tao/ (stored 0%)    [root@centos7 test]# ll    总用量 188    -rw-r--r-- 1 root root 12097 2月  20 17:42 boot.log    -rw-r--r-- 1 root root 26074 2月  20 16:50 messages.bz2    -rw-r--r-- 1 root root 41791 2月  20 16:44 messages.gz    -rw-r--r-- 1 root root 21420 2月  20 16:50 messages.xz    -rw-r--r-- 1 root root 40960 2月  20 17:51 mylog.tar    -rw-r----- 1 root root     0 2月  20 17:42 pacemaker.log    drwxr-xr-x 2 root root   121 2月  20 17:43 tao    -rw-r--r-- 1 root root  7232 2月  20 18:15 tao.tar.gz    -rw-r--r-- 1 root root   158 2月  20 18:26 tao.zip    -rw-r--r-- 1 root root  2800 2月  20 17:42 wpa_supplicant.log    -rw-r--r-- 1 root root 18303 2月  20 17:42 Xorg.0.log    -rw------- 1 root root   105 2月  20 17:42 yum.log
        [root@centos7 test]# rm -fr tao
        # 解压缩    [root@centos7 test]# unzip tao.zip    Archive:  tao.zip       creating: tao/    [root@centos7 test]# ll    总用量 188    -rw-r--r-- 1 root root 12097 2月  20 17:42 boot.log    -rw-r--r-- 1 root root 26074 2月  20 16:50 messages.bz2    -rw-r--r-- 1 root root 41791 2月  20 16:44 messages.gz    -rw-r--r-- 1 root root 21420 2月  20 16:50 messages.xz    -rw-r--r-- 1 root root 40960 2月  20 17:51 mylog.tar    -rw-r----- 1 root root     0 2月  20 17:42 pacemaker.log    drwxr-xr-x 2 root root     6 2月  20 17:43 tao    -rw-r--r-- 1 root root  7232 2月  20 18:15 tao.tar.gz    -rw-r--r-- 1 root root   158 2月  20 18:26 tao.zip    -rw-r--r-- 1 root root  2800 2月  20 17:42 wpa_supplicant.log    -rw-r--r-- 1 root root 18303 2月  20 17:42 Xorg.0.log    -rw------- 1 root root   105 2月  20 17:42 yum.log

      tar  归档压缩工具( archive归档本身并不意味着压缩)   主要参数:    -f        指定创建或展开归档文件名(只要操作归档文件就要用-f)    -c        创建归档文件    -v        显示创建的详细过程    -x        展开归档文件    -r        将文件添加到已经存在的归档文件中(压缩后是不能用r的,只有归档的时候用)    -z        用gzip来压缩和解压缩文件    -j         用bz2来压缩和解压缩文件    -J         用xz来压缩和解压缩文件    -t         不展开归档文件情况下查看文件中的内容    -C        展开或解压文件到指定目录(如果没指定该参数,默认是当前目录)    --xattrs  归档时,保留文件的扩展属性信息
        -zcf        归档并调用gzip压缩    -zxf        调用gzip解压缩并展开归档,-z选项可省略(解压时自动判断用什么工具压缩)
        -jcf        归档并调用bzip2压缩    -jxf        调用bzip2解压缩并展开归档,-j选项可省略
        -Jcf        归档并调用xz压缩    -Jxf        调用xz解压缩并展开归档,-J选项可省略
      例如:  tar -cvf home.tar /home            将home目录打包成home.tar(只是打包并没有压缩)  tar -rvf home.tar /etc                将etc目录添加到已存在的打包文件home.tar里面  tar -czvf home.tar.gz /home      将home目录打包并压缩成home.tar.gz  tar -cvjf home.tar.bz2 /home     将home目录打包并压缩成.bz2的格式  tar -xzvf home.tar.gz                 将home.tar.gz这个备份文件还原并解压缩  tar -tvf home.tar | more            查看home.tar备份文件的类容,并以分屏方式显示在显示器上  tar -cvf /dev/st0 /home/shrek  可以将st0磁带机备份到/home目录下面
    注意:f选项一定要写在最后一个;如果是解开压缩归档文件可以不指定压缩选项zjJ,tar会通过文件后缀判断是什么压缩工具压缩的归档演示:    [root@centos7 ~]# ls /tmp/test/tao    boot.log  fstab  issue  pacemaker.log  wpa_supplicant.log  Xorg.0.log  yum.log
        # 对所有以 .log 结尾的文件进行归档    [root@centos7 ~]# tar -cf /tmp/test/mylog.tar /tmp/test/tao/*.log    [root@centos7 ~]# ll /tmp/test/    总用量 136    -rw-r--r-- 1 root root 26074 2月  20 16:50 messages.bz2    -rw-r--r-- 1 root root 41791 2月  20 16:44 messages.gz    -rw-r--r-- 1 root root 21420 2月  20 16:50 messages.xz    -rw-r--r-- 1 root root 40960 2月  20 17:45 mylog.tar   # 归档后的文件    drwxr-xr-x 2 root root   121 2月  20 17:43 tao   
        # 展开归档    [root@centos7 test]# tar xf mylog.tar    [root@centos7 test]# ll    总用量 176    -rw-r--r-- 1 root root 12097 2月  20 17:42 boot.log    -rw-r--r-- 1 root root 26074 2月  20 16:50 messages.bz2    -rw-r--r-- 1 root root 41791 2月  20 16:44 messages.gz    -rw-r--r-- 1 root root 21420 2月  20 16:50 messages.xz    -rw-r--r-- 1 root root 40960 2月  20 17:51 mylog.tar    -rw-r----- 1 root root     0 2月  20 17:42 pacemaker.log    drwxr-xr-x 2 root root   121 2月  20 17:43 tao    -rw-r--r-- 1 root root  2800 2月  20 17:42 wpa_supplicant.log    -rw-r--r-- 1 root root 18303 2月  20 17:42 Xorg.0.log    -rw------- 1 root root   105 2月  20 17:42 yum.log
        # -C 展开归档至指定文件中    [root@centos7 test]# mkdir /tmp/newtest    [root@centos7 test]# tar xf mylog.tar -C /tmp/newtest    [root@centos7 test]# ll /tmp/newtest    总用量 40    -rw-r--r-- 1 root root 12097 2月  20 17:42 boot.log    -rw-r----- 1 root root     0 2月  20 17:42 pacemaker.log    -rw-r--r-- 1 root root  2800 2月  20 17:42 wpa_supplicant.log    -rw-r--r-- 1 root root 18303 2月  20 17:42 Xorg.0.log    -rw------- 1 root root   105 2月  20 17:42 yum.log
        # 查看归档文件中的文件列表    [root@centos7 test]# tar tf mylog.tar    boot.log    pacemaker.log    wpa_supplicant.log    Xorg.0.log    yum.log
    归档并压缩演示:    # 对目录进行归档并压缩    [root@centos7 test]# tar zcf /tmp/test/tao.tar.gz tao    [root@centos7 test]# ll /tmp/test    总用量 184    -rw-r--r-- 1 root root 12097 2月  20 17:42 boot.log    -rw-r--r-- 1 root root 26074 2月  20 16:50 messages.bz2    -rw-r--r-- 1 root root 41791 2月  20 16:44 messages.gz    -rw-r--r-- 1 root root 21420 2月  20 16:50 messages.xz    -rw-r--r-- 1 root root 40960 2月  20 17:51 mylog.tar    -rw-r----- 1 root root     0 2月  20 17:42 pacemaker.log    drwxr-xr-x 2 root root   121 2月  20 17:43 tao           # 原文件    -rw-r--r-- 1 root root  7232 2月  20 18:15 tao.tar.gz    # 归档压缩后的文件    -rw-r--r-- 1 root root  2800 2月  20 17:42 wpa_supplicant.log    -rw-r--r-- 1 root root 18303 2月  20 17:42 Xorg.0.log    -rw------- 1 root root   105 2月  20 17:42 yum.log
        # 删除原文件    [root@centos7 test]# rm -fr tao        
        # 展开归档,其中 z 可省略,tar命令会自动识别其为压缩文件    [root@centos7 test]# tar xf tao.tar.gz    [root@centos7 test]# ll    总用量 184    -rw-r--r-- 1 root root 12097 2月  20 17:42 boot.log    -rw-r--r-- 1 root root 26074 2月  20 16:50 messages.bz2    -rw-r--r-- 1 root root 41791 2月  20 16:44 messages.gz    -rw-r--r-- 1 root root 21420 2月  20 16:50 messages.xz    -rw-r--r-- 1 root root 40960 2月  20 17:51 mylog.tar    -rw-r----- 1 root root     0 2月  20 17:42 pacemaker.log    drwxr-xr-x 2 root root   121 2月  20 17:43 tao           # 展开后的文件    -rw-r--r-- 1 root root  7232 2月  20 18:15 tao.tar.gz    -rw-r--r-- 1 root root  2800 2月  20 17:42 wpa_supplicant.log    -rw-r--r-- 1 root root 18303 2月  20 17:42 Xorg.0.log    -rw------- 1 root root   105 2月  20 17:42 yum.log
    cpio  归档工具(把文件复制为归档文件)cpio命令是通过重定向的方式将文件进行打包备份,还原恢复的工具,它可以解压以“.cpio”或者“.tar”结尾的文件。    用法:        cpio[选项] > 文件名或者设备名        cpio[选项] < 文件名或者设备名    选项:        -o:将文件拷贝打包成文件或者将文件输出到设备上        -i:解包,将打包文件解压或将设备上的备份还原到系统        -t:预览,查看文件内容或者输出到设备上的文件内容        -v:显示打包过程中的文件名称        -d:解包生成目录,在cpio还原时,自动的建立目录        -c:一种较新的存储方式    示例:        将etc目录备份:            find . /etc -print |cpio -ov > etc.cpio        内容预览            cpio -tv < etc.cpio        要解包文件            cpio -iv < etc.cpio            cpio -idv < etc.cpio

  • 相关阅读:
    Hive2.0函数大全(中文版)
    Centos7 安装并配置redis
    Java内部类
    beeline: 新版连接Hive server的工具
    jsoup的Document类
    Jsoup类
    jsoup的Node类
    jsoup的Element类
    Java中的多线程
    Java中的文件IO流
  • 原文地址:https://www.cnblogs.com/Link-Luck/p/9855821.html
Copyright © 2011-2022 走看看