设为首页 收藏本站
查看: 535|回复: 0

[经验分享] Linux系统压缩及解压缩

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-2-21 09:07:07 | 显示全部楼层 |阅读模式
本帖最后由 jtyhrew222 于 2017-2-21 09:08 编辑

==============================================================================

概述:

  本篇将介绍Linux系统中的压缩和解压缩的工具,以及归档工具(tar,cpio)

    compress/uncompress:对应 .Z 结尾的压缩格式文件;

    gzip/gunzip:其对应的是 .gz 结尾的压缩格式文件;

    bzip2/bunzip2:其对应的是 .bz2 结尾的压缩格式文件;

    xz/unxz: 其对应的是 .xz 结尾的压缩格式文件;

    zip/unzip:其对应的是 .zip 结尾的压缩格式文件

==============================================================================

Linux系统解压缩

1.压缩比和常用工具

    ★压缩比:

        时间换空间(CPU的时间 --->磁盘空间)

    ★常用工具:

    早期的有compress和uncompress,其对应的是 .Z 结尾的压缩格式文件,现在适应较多的有:

        gzip/gunzip:其对应的是 .gz 结尾的压缩格式文件;

        bzip2/bunzip2:其对应的是 .bz2 结尾的压缩格式文件;

        xz/unxz: 其对应的是 .xz 结尾的压缩格式文件;

        zip/unzip:其对应的是 .zip 结尾的压缩格式文件

        tar,cpio:归档和展开归档

2.gzip和gunzip(使用最多)

    ★常用工具:

        gzip,gunzip,zcat

    ★语法:

        gzip [OPTION]... FILE ...

    ☉选项:

        -d:解压缩,相当于gunzip

        -c:将压缩或解压缩的结果输出至标准输出(gzip -c FILE > /PATH/TP/SOMEFILE.gz);

        -#:1-9,指定压缩比,值越大压缩比越大  如:gzip -9 m

        -v:显示详情

    ☉解压缩:

        guzip

    ☉zcat:

        不显式解压缩的前提下查看文本文件内容(适用于查看小文件)  如:zcat FILE > /PATH/TP/SOMEFILE

演示:

[iyunv@centos7 ~]# cp /var/log/messages /tmp/test/
[iyunv@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
[iyunv@centos7 ~]# ll -h /tmp/test/messages
-rw------- 1 root root 286K 2月  20 16:28 /tmp/test/messages

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

[iyunv@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

# 解压缩,原来的压缩文件被删除,保留解压缩后的文件
[iyunv@centos7 ~]# gunzip /tmp/test/messages.gz
[iyunv@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命令查看
[iyunv@centos7 ~]# zcat /tmp/test/messages.gz

#=====================================================================================
# 解压缩
[iyunv@centos7 ~]# gzip -d /tmp/test/messages.gz
[iyunv@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)
[iyunv@centos7 ~]# gzip -c /tmp/test/messages > /tmp/test/messages.gz
[iyunv@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

# 解压缩到标准输出
[iyunv@centos7 ~]# rm -f /tmp/test/messages
[iyunv@centos7 ~]# gzip -d -c /tmp/test/messages.gz > /tmp/test/messages
[iyunv@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



2.bzip2/bunzip2/bzcat

    ★语法:

        bzip2 [OPTION]... FILE ...

    ☉选项:

        -k:keep, 保留原文件;

        -d:解压缩;

        -#:1-9,压缩比,默认为6

    ☉bzcat:

        不显式解压缩的前提下查看文本文件内容

    注意:

        bzip2和gzip命令的使用方式基本相同,压缩或解压缩后都会删除源文件

演示:

   
# 压缩
[iyunv@centos7 ~]# bzip2 /tmp/test/messages

[iyunv@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

# 解压缩
[iyunv@centos7 ~]# bunzip2 /tmp/test/messages.bz2
[iyunv@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 选项不用指明重定向的文件,自动保留源文件在当前文件中
[iyunv@centos7 ~]# bzip2 -k /tmp/test/messages
[iyunv@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


3.xz/unxz/zxcat(压缩比最强)

    ★语法:

        xz [OPTION]... FILE ...

    ☉选项:

        -k:keep, 保留原文件;

        -d:解压缩;

        -#:1-9,压缩比,默认为6;

    ☉xzcat:

        不显式解压缩的前提下查看文本文件内容

演示:

   
[iyunv@centos7 ~]# xz /tmp/test/messages
[iyunv@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

归档工具:tar

1.归档及常用工具

    ★归档:

        归档就是将多个文件打包为单个文件以便于管理,默认的归档不会执行压缩。

    ★常用的工具:

        tar,cpio(不常用)

2.tar命令

    ★语法:

        tar [OPTION...] [FILE]...

    ☉创建归档(-c,-f 指定文件):

        tar -c -f /PATH/TO/SOMEFILE.tar  FILE... (后缀名固定以 .tar 结尾;)

        tar -cf /PATH/TO/SOMEFILE.tar  FILE... (可以合并写为-cf ,但不能写为 -fc ,因为-f 选项后带参数)

    ☉展开归档(-x,-f 指定文件):

        tar -x -f /PATH/TO/SOMEFILE.tar (展开至归档所在的文件中)

        tar xf /PATH/TO/SOMEFILE.tar -C /PATH/TO/SOMEFILE (-C :展开归档至指定文件中)

    ☉查看归档文件中的列表(-t,-f 指定文件):

        tar -tf /PATH/TO/SOMEFILE.tar

    注意:

        多个选项可以合并,但-f由于要带参数,因此要放到最右侧 如:-cf,-xf,-cf;

        选项的引导符 "-" 可省略。如:tar xf,tar zf

演示:

   
[iyunv@centos7 ~]# ls /tmp/test/tao
boot.log  fstab  issue  pacemaker.log  wpa_supplicant.log  Xorg.0.log  yum.log

# 对所有以 .log 结尾的文件进行归档
[iyunv@centos7 ~]# tar -cf /tmp/test/mylog.tar /tmp/test/tao/*.log
[iyunv@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   

# 展开归档
[iyunv@centos7 test]# tar xf mylog.tar
[iyunv@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 展开归档至指定文件中
[iyunv@centos7 test]# mkdir /tmp/newtest
[iyunv@centos7 test]# tar xf mylog.tar -C /tmp/newtest
[iyunv@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

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


  归档完成后通常需要压缩,结合此前的压缩工具,就能实现压缩多个文件了。

    ★结合压缩工具实现:归档并压缩:

    ☉-z:gzip(后缀名.tar.gz)

        tar -zcf /PATH/TO/MEFILE.tar.gz FILE...  (创建归档并压缩);

        tar -zxf /PATH/TO/SOMEFILE.tar.gz   (解压缩并展开归档,z不写也行)

    ☉-j:bzip2(后缀名.tar.bz2)

        -jcf

        -jxf

    ☉-J:xz(后缀名:.tar.xz)

        -Jcf

        -Jxf

    注意:

        展开归档可以直接使用 tar xf ,而无需为其指定对应的压缩工具选项即可

演示:

   
# 对目录进行归档并压缩
[iyunv@centos7 test]# tar zcf /tmp/test/tao.tar.gz tao
[iyunv@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

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

# 展开归档,其中 z 可省略,tar命令会自动识别其为压缩文件
[iyunv@centos7 test]# tar xf tao.tar.gz
[iyunv@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

zip/unzip

        ---最通用的压缩工具,即可以归档,又能压缩(现在不常用)

    ★创建归档(.zip后缀):

        zip file.zip  /PATH/TO/SOMEFILE

    ★解压缩

        unzip file.zip

演示:

   
# 对目录进行归档并压缩
[iyunv@centos7 test]# zip /tmp/test/tao.zip tao
  adding: tao/ (stored 0%)
[iyunv@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


[iyunv@centos7 test]# rm -fr tao

# 解压缩
[iyunv@centos7 test]# unzip tao.zip
Archive:  tao.zip
   creating: tao/
[iyunv@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
cpio 命令

    ★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





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-345016-1-1.html 上篇帖子: NFS中小企业常见的网络文件系统服务(network file system) 下篇帖子: Linux_Centos_PHP编译安装 Linux
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表