• 1.10 Linux基础(十) 压缩与解压缩


    1.10.1 compress

    • compress [-dfvcVr] [-b maxbits] [file ...]
    • -d: 解压缩,相当于uncompress
    • -c: 结果输出至标准输出,不删除原文件,可以重定向到name.Z,生成压缩文件
    • -v: 显示详情
    • uncompress 解压缩
    [root@CentOS7 data]# compress m ##不保留源文件
    [root@CentOS7 data]# ll
    total 4840
    -rw-------. 1 root root 3944964 Aug  8 19:10 m.bak
    -rw-r--r--. 1 root root  994739 Aug  8 19:11 m.Z
    [root@CentOS7 data]# compress -d m.Z
    [root@CentOS7 data]# ll
    total 7724
    -rw-r--r--. 1 root root 3944964 Aug  8 19:11 m
    -rw-------. 1 root root 3944964 Aug  8 19:10 m.bak
    
    [root@CentOS7 data]# compress -c m > m.Z
    ## 保留源文件
    [root@CentOS7 data]# ll
    total 8688
    -rw-r--r--. 1 root root 3944964 Aug  8 19:11 m
    -rw-------. 1 root root 3944964 Aug  8 19:10 m.bak
    -rw-r--r--. 1 root root  994739 Aug  8 19:20 m.Z
    drwxr-xr-x. 4 root root    4096 Aug  8 19:17 scripts
    [root@CentOS7 data]# compress -d m.Z
    m already exists.
    Do you wish to overwrite m (y or n)? y
    

    1.10.2 gzip/gunzip

    gzip [OPTION]... FILE ...

    • -d: 解压缩,相当于gunzip
    • -c: 将压缩或解压缩的结果输出至标准输出
    • -#:1-9,指定压缩比,值越大压缩比越大
    • cat /etc/fstab |gzip > fstab.gz 当命令的执行结果太多,希望对命令执行结果进行压缩,如生产中对mysql进行备份mysqldump|gzip > fstab.gz
    • zcat:不显式解压缩的前提下查看文本文件内容zcat file.Z >file
    • 实例:
    [root@CentOS7 data]# gzip m
    [root@CentOS7 data]# ll
    total 4408
    -rw-r--r--. 1 root root       0 Aug  8 19:41 file
    -rw-------. 1 root root 3944964 Aug  8 19:10 m.bak
    -rw-r--r--. 1 root root  558552 Aug  8 19:22 m.gz
    drwxr-xr-x. 4 root root    4096 Aug  8 19:17 scripts
    [root@CentOS7 data]# gunzip m  ##和gzip -d一样
    [root@CentOS7 data]# ll
    total 7716
    -rw-r--r--. 1 root root       0 Aug  8 19:41 file
    -rw-r--r--. 1 root root 3944964 Aug  8 19:22 m
    -rw-------. 1 root root 3944964 Aug  8 19:10 m.bak
    drwxr-xr-x. 4 root root    4096 Aug  8 19:17 scripts
    [root@CentOS7 data]# zcat m.gz > m
    [root@CentOS7 data]# ll
    total 8264
    -rw-r--r--. 1 root root       0 Aug  8 19:41 file
    -rw-r--r--. 1 root root 3944964 Aug  8 19:45 m
    -rw-------. 1 root root 3944964 Aug  8 19:10 m.bak
    -rw-r--r--. 1 root root  558552 Aug  8 19:22 m.gz
    drwxr-xr-x. 4 root root    4096 Aug  8 19:17 scripts
    

    1.10.3 bzip2

    bzip2/bunzip2/bzcat
    bzip2 [OPTION]... FILE ...

    • -k: keep, 保留原文件
    • -d:解压缩
    • -#:1-9,压缩比,默认为9
    • bzcat:不显式解压缩的前提下查看文本文件内容
    [root@CentOS7 data]# bzip2 m
    [root@CentOS7 data]# ll
    total 8528
    -rw-r--r--. 1 root root 3944964 Aug  8 19:49 f1
    -rw-r--r--. 1 root root       0 Aug  8 19:41 file
    -rw-------. 1 root root 3944964 Aug  8 19:10 m.bak
    -rw-r--r--. 1 root root  267452 Aug  8 19:45 m.bz2
    -rw-r--r--. 1 root root  558552 Aug  8 19:22 m.gz
    drwxr-xr-x. 4 root root    4096 Aug  8 19:17 scripts
    [root@CentOS7 data]# bzcat m.bz2 > f1
    [root@CentOS7 data]# ll
    total 12384
    -rw-r--r--. 1 root root 3944964 Aug  8 19:49 f1
    -rw-r--r--. 1 root root       0 Aug  8 19:41 file
    -rw-r--r--. 1 root root 3944964 Aug  8 19:45 m
    -rw-------. 1 root root 3944964 Aug  8 19:10 m.bak
    -rw-r--r--. 1 root root  267452 Aug  8 19:45 m.bz2
    -rw-r--r--. 1 root root  558552 Aug  8 19:22 m.gz
    drwxr-xr-x. 4 root root    4096 Aug  8 19:17 scripts
    [root@CentOS7 data]# bzip2 -d m.bz2
    [root@CentOS7 data]# ll
    total 12120
    -rw-r--r--. 1 root root 3944964 Aug  8 19:49 f1
    -rw-r--r--. 1 root root       0 Aug  8 19:41 file
    -rw-r--r--. 1 root root 3944964 Aug  8 19:45 m
    -rw-------. 1 root root 3944964 Aug  8 19:10 m.bak
    -rw-r--r--. 1 root root  558552 Aug  8 19:22 m.gz
    drwxr-xr-x. 4 root root    4096 Aug  8 19:17 scripts
    [root@CentOS7 data]# bzip2 -k f1 ##保留源文件
    [root@CentOS7 data]# ll
    total 8792
    -rw-r--r--. 1 root root 3944964 Aug  8 19:49 f1
    -rw-r--r--. 1 root root  267452 Aug  8 19:49 f1.bz2
    -rw-r--r--. 1 root root       0 Aug  8 19:41 file
    -rw-------. 1 root root 3944964 Aug  8 19:10 m.bak
    -rw-r--r--. 1 root root  267452 Aug  8 19:45 m.bz2
    -rw-r--r--. 1 root root  558552 Aug  8 19:22 m.gz
    drwxr-xr-x. 4 root root    4096 Aug  8 19:17 scripts
    

    1.10.4 xz/unxz/xzcat

    • xz [OPTION]... FILE ...
    • -k: keep, 保留原文件
    • -d:解压缩
    • -#:1-9,压缩比,默认为6
    • xzcat: 不显式解压缩的前提下查看文本文件内容

    1.10.5 zip/unzip:

    • zip可以对多个文件压缩归档成单个文件

    可以压缩文件夹

    打包压缩

    • zip –r sysconfig.zip /etc/sysconfig/

    解包解压缩

    • unzip sysconfig.zip

    • cat /var/log/messages | zip messages -

    • unzip -p message > message

    1、把文件解压到当前目录下

    unzip test.zip
    

    2、如果要把文件解压到指定的目录下,需要用到-d参数。

    unzip -d /temp test.zip
    

    3、解压的时候,有时候不想覆盖已经存在的文件,那么可以加上-n参数

    unzip -n test.zip
    unzip -n -d /temp test.zip
    

    4、只看一下zip压缩包中包含哪些文件,不进行解压缩

    unzip -l test.zip
    

    5、查看显示的文件列表还包含压缩比率

    unzip -v test.zip
    

    6、检查zip文件是否损坏

    unzip -t test.zip
    

    7、将压缩文件test.zip在指定目录tmp下解压缩,如果已有相同的文件存在,要求unzip命令覆盖原先的文件

    unzip -o test.zip -d /tmp/
    
    [root@CentOS7 data]# zip -r tertdir.zip testdir/
    [root@CentOS7 data]# ll
    total 8796
    -rw-r--r--. 1 root root     978 Aug  8 19:59 tertdir.zip
    drwxr-xr-x. 3 root root      17 Aug  8 19:58 testdir
    
    [root@CentOS7 data]# zip -r sysconfig.zip /etc/sysconfig
    -rw-r--r--. 1 root root   95104 Aug  8 20:02 sysconfig.zip
    [root@CentOS7 data]# cat /var/log/messages |zip message.zip -  ##将cat 命令的执行结果放到文件名为-中,然后压缩成message.zip 
    adding: - (deflated 98%)
    [root@CentOS7 data]# ll
    -rw-r--r--. 1 root root   61943 Aug  8 20:13 message.zip
    -rw-r--r--. 1 root root  558552 Aug  8 19:22 m.gz
    drwxr-xr-x. 4 root root    4096 Aug  8 19:56 scripts
    drwxr-xr-x. 3 root root      17 Aug  8 19:58 testdir
    -rw-r--r--. 1 root root     978 Aug  8 19:59 testdir.zip
    [root@CentOS7 data]# unzip message.zip
    Archive:  message.zip
     inflating: -                       
    [root@CentOS7 data]# ll
    total 11712
    -rw-------. 1 root root 2851599 Aug  8 20:13 -  ##解压后文件-
    message.zip
    -rw-r--r--. 1 root root  558552 Aug  8 19:22 m.gz
    drwxr-xr-x. 4 root root    4096 Aug  8 19:56 scripts
    drwxr-xr-x. 3 root root      17 Aug  8 19:58 testdir
    -rw-r--r--. 1 root root     978 Aug  8 19:59 testdir.zip
    [root@CentOS7 data]# unzip -p testdir.zip > test ##压缩后更改文件名为test
    [root@CentOS7 data]# ll
    total 11712
    -rw-r--r--. 1 root root  0 Aug  8 20:39 test
    -rw-r--r--. 1 root root 978 Aug  8 19:59 testdir.zip
    

    1.10.6 tar工具

    tar(Tape ARchive,磁带归档的缩写)

    • tar [OPTION]...

    • -c:创建打包文件

    • -p:显示打包过程

    • -v:显示压缩过程

    • -x:解压缩

    • -t:预览

    (1) 创建归档

    • tar -cpvf /PATH/TO/SOMEFILE.tar FILE...

    (2) 追加文件至归档: 注:不支持对压缩文件追加

    • tar -r -f /PATH/TO/SOMEFILE.tar FILE...

    (3) 查看归档文件中的文件列表

    • tar -t -f /PATH/TO/SOMEFILE.tar

    (4) 展开归档

    • tar -x -f /PATH/TO/SOMEFILE.tar

    • tar -x -f /PATH/TO/SOMEFILE.tar -C /PATH/

    (5) 结合压缩工具实现:归档并压缩

    • -z:gzip

      • 后缀名:.tar.gz
      • 归档并压缩:tar -zvcf
      • 展开归档:tar -zxf
    • -j:bzip2

      • 后缀名:.tar.bz2
      • 归档并压缩:tar -jvcf
      • 展开归档:tar -jxf
    • -J:xz

      • 后缀名:.tar.xz
      • 归档并压缩:tar -Jvcf
      • 展开归档:tar -Jxf
    [root@CentOS7 data]# tar -zcvf testdir.tar.gz testdir
    testdir/
    testdir/di1/
    testdir/di1/dir2/
    testdir/di1/dir2/3/
    testdir/di1/dir2/4/
    testdir/di1/dir2/5/
    [root@CentOS7 data]# ll
     testdir
     testdir.tar.gz
    
    [root@CentOS7 data]# tar -jcvf testdir.tar.bz2 testdir
    [root@CentOS7 data]# ll
     testdir.tar.bz2
    [root@CentOS7 data]# tar -Jcvf testdir.tar.bz2 testdir
    testdir/
    testdir/di1/
    testdir/di1/dir2/
    testdir/di1/dir2/3/
    testdir/di1/dir2/4/
    testdir/di1/dir2/5/
    [root@CentOS7 data]# ll
     testdir.tar.xz
    [root@CentOS7 data]# tar -zvxf testdir.tar.gz 
    testdir/
    testdir/di1/
    testdir/di1/dir2/
    testdir/di1/dir2/3/
    testdir/di1/dir2/4/
    testdir/di1/dir2/5/
    [root@CentOS7 data]# ll
    testdir
    

    -T选项指定输入文件,-X选项指定包含要排除的文件列表

    tar zcvf mybackup.tgz -T /root/includefilelist -X /root/excludefilelist
    

    分割大的 tar 文件为多份小文件:

    split –b Size –d tar-file-name prefix-name
    split -b 1M –d mybackup.tgz mybackup-parts
    split -b 1M mybackup.tgz mybackup-parts
    

    合并:

    cat mybackup-parts* > mybackup.tar.gz
    

    1.10.7 cpio

    功能:复制文件从或到归档

    cpio命令是通过重定向的方式将文件进行打包备份,还原恢复的工具,它可以

    解压以“.cpio” 或者“.tar” 结尾的文件

    cpio [选项] > 文件名或者设备名

    cpio [选项] < 文件名或者设备名

    选项

    • -o 将文件拷贝打包成文件或者将文件输出到设备上

    • -i 解包,将打包文件解压或将设备上的备份还原到
      系统

    • -t 预览,查看文件内容或者输出到设备上的文件内容

    • -v 显示打包过程中的文件名称

    • -d 解包生成目录,在cpio还原时,自动的建立目录

    • -c 一种较新的存储方式

  • 相关阅读:
    【Struts2】 国际化
    【Struts2】进阶
    【Struts2】简介及入门
    【Distributed】互联网安全架构
    【Mac】 开启原生的 NTFS 硬盘格式支持
    swagger2简单使用
    自定义异常和异常处理
    enum简单使用
    Interceptor
    修改docker下mysql配置
  • 原文地址:https://www.cnblogs.com/huangsefeizhu/p/11506536.html
Copyright © 2020-2023  润新知