• Linux 基础教程 32-解压缩命令


        将文件压缩后对提升数据传输效率,降低传输带宽,管理备份数据都有非常重要的功能,因此文件压缩解压技能就成为必备技能。相对于Windows中的文件解压缩工具百花争艳,在Linux中的解压缩工具则要少很多,常用的解压缩命令主要为gzipbzip2tarzip等等。

    gzip命令

    gzip只能针对普通文件进行压缩和解压,对于文件夹,符号链接等是不支持的。基本语法如下所示:

    gzip [选项] [文件]
    

    常用参数如下所示:

    参数 说明
    -d , --decompress 解压文件
    -f , --force 压缩/解压文件强制覆盖已经存在的文件
    -l , --list 显示压缩包的信息,如压缩前大小、压缩后大小、压缩比例等
    -r , --recursive 递归压缩指定目录中的所有文件和子目录中的文件,将每个文件都压缩为一个gz文件
    -t , --test 检查压缩文件的完整性
    -v , --verbose 显示详细过程
    -V , --version 显示版本信息

    压缩速度和压缩强度

        在压缩文件时,可以根据实际需要采用不同的压缩速度和压缩强度,来调整压缩的时间和压缩比例。在gzip中共提供了9种压缩级别。

    • -1:代表压缩速度最快,但压缩强度不高
    • -9:代表压缩强度最高,但压缩速度较慢
    • -6:gzip默认值

    gzip示例如下所示:

    • 1、添加压缩文件
    [root@localhost Python-3.7.0]# du -sh Python-3.7.0
    189M    Python-3.7.0
    [root@localhost Python-3.7.0]# gzip -r Python-3.7.0
    [root@localhost Python-3.7.0]# du -sh Python-3.7.0
    64M     Python-3.7.0
    [root@localhost Python-3.7.0]# ll
    总用量 10856
    -rw-rw-r--  1 root     root    3731 6月  27 11:07 aclocal.m4.gz
    drwxr-xr-x  5 root     root      82 7月  20 12:43 build
    -rw-rw-r--  1 root     root   13214 6月  27 11:07 config.guess.gz
    -rw-r--r--  1 root     root   38624 7月  20 12:48 config.log.gz
    
    • 2、压缩tar包
    [root@localhost Python-3.7.0]# gzip Python-3.7.0.tar
    [root@localhost Python-3.7.0]# ll -h
    总用量 52M
    -rw-r--r-- 1 root root 52M 7月  24 14:13 Python-3.7.0.tar.gz
    
    • 3、解压压缩包
    gzip -d Python-3.7.0.tar.gz
    
    • 4、显示压缩包信息
    [root@localhost Python-3.7.0]# gzip -l Python-3.7.0.tar.gz
             compressed        uncompressed  ratio uncompressed_name
               53670458           190464000  71.8% Python-3.7.0.tar
    

    bzip2

    bzip2与gzip非常类似,区别在于gzip压缩和解压均是同一个命令,而bzip2压缩命令为bzip2,解压缩命令为bunzip2
    

    tar命令

    在Linux中tar命令算是用得最多的命令了,基基本语法如下所示:

    tar [选项] [文件]
    

    其常用参数如下所示:

    选项 说明
    -A , --catenate 向压缩包中添加压缩包
    -c, --create 新建压缩包
    -C, --directory=DIR 指定解压缩目录
    -d, --diff 对比压缩包与文件系统的差异
    --delete 从压缩包删除指定的文件
    -r, --append 添加文件末尾追加文件
    -t, --list 显示压缩包中的目录结构
    -u, --update 仅向压缩包中添加较新的文件
    -x, --extract 解压压缩包
    -f, --file=ARCHIVE 指定压缩文件
    -v, --verbose 显示详细过程
    -j, --bzip2 支持bzip2
    -z, --gzip 支持gzip
    --overwrite 解压时如果文件已经存在,则进行替换

    使用tar需要注意的事项如下所示:

    • 1、常用的主选项参数如下:
    -c -x -t -r -u
    以上这5个参数同时只能出现一个,不能同时出现多个
    
    • 2、辅助选项
    -f :一般情况需要将该参数放置在最后位置,后面紧跟文件名
    

    tar 示例如下所示:

    • 1、添加压缩文件到当前目录
    [root@localhost ~]# tar -cf Alltxt.tar *.txt
    
    • 2、查看压缩包内容
    [root@localhost ~]# tar -tf Alltxt.tar
    in.txt
    out.txt
    packstack-answers-20180710-091950.txt
    packstack-answers-20180710-092155.txt
    packstack-answers-20180710-100538.txt
    
    • 3、向压缩包中添加压缩包
    [root@localhost ~]# tar -cf Alltxt.tar *.txt
    [root@localhost ~]# tar -cf Allbak.tar *.bak
    [root@localhost ~]# tar -Af Alltxt.tar Allbak.tar
    [root@localhost ~]# tar -tf Alltxt.tar
    in.txt
    out.txt
    packstack-answers-20180710-091950.txt
    packstack-answers-20180710-092155.txt
    packstack-answers-20180710-100538.txt
    append.txt.bak
    
    • 4、向压缩包中添加文件
    [root@localhost ~]# tar -rf Alltxt.tar out
    [root@localhost ~]# tar -tf Alltxt.tar
    in.txt
    out.txt
    packstack-answers-20180710-091950.txt
    packstack-answers-20180710-092155.txt
    packstack-answers-20180710-100538.txt
    append.txt.bak
    out/
    out/out.txt
    out/eip.sh
    
    • 5、更新压缩包中的文件
    [root@localhost ~]# tar -uf Alltxt.tar append.txt.bak
    
    • 6、解压文件到指定目录
    [root@localhost ~]# tar -xf Python-3.7.0.tar  -C TarTest/
    
    • 7、解压压缩包中的指定文件
    [root@localhost Python-3.7.0]# tar -xf Python-3.7.0.tar Python-3.7.0/pyconfig.h
    [root@localhost Python-3.7.0]# tree Python-3.7.0
    Python-3.7.0
    └── pyconfig.h
    0 directories, 1 file
    

    zip命令

    跟bzip2类似,zip用于压缩文件,而unzip用于解压缩文件。其基本语法如下所示:

    zip [选项] [指定文件名] [压缩文件或路径]
    
    uzip [选项] [压缩包名称]
    
    • zip常用参数如下所示:
    选项 说明
    -c , --entry-comments 给压缩文件添加注释
    -d , --delete 从压缩包删除指定文件
    -D 压缩包不创建目录名称
    -f , --freshen 与参数 -u 类似,不仅更新已有文件,而且也添加压缩包没有的文件
    -i files/ --include files 仅向压缩包添加指定的文件
    -m , --move 将原始文件添加到压缩包删除原文件
    -O output-file
    -q , --quiet 静默模式
    -r , --recurse-paths 递归处理指定目录和子目录
    -T , --test 检查压缩包的完整性
    -u , --update 将较新的文件更新替换到压缩包中
    -v , --verbose 显示详细过程
    -x files/--exclude files 压缩文件时添加排除项
    -# (-0~-9) 设置压缩级别,-0:不压缩文件,-1:最快压缩速度,-9:最好压缩强度,默认为-6
    • unzip常用参数
    选项 说明
    -l 显示压缩包中的内容
    -t 检查压缩包的完整性
    -o 强制覆盖已存在的文件而不提示
    -j 不处理压缩文件中的原有目录路径
    -d exdir 指定解压目录

    zip/unzip示例如下所示:

    • 1、添加压缩包
    [root@localhost Python-3.7.0]# zip -r Python-3.7.0.zip Python-3.7.0
    
    • 2、解压压缩包
    unzip -d /tmp/ Python-3.7.0.zip
    
    • 3、检查压缩包完整性
    [root@localhost Python-3.7.0]# unzip -t Python-3.7.0.zip
    Archive:  Python-3.7.0.zip
        testing: Python-3.7.0/            OK
        testing: Python-3.7.0/install-sh   OK
        ...
        testing: Python-3.7.0/pybuilddir.txt   OK
    No errors detected in compressed data of Python-3.7.0.zip.
    
    • 4、显示压缩包内容
    [root@localhost Python-3.7.0]# unzip -l Python-3.7.0.zip
    Archive:  Python-3.7.0.zip
      Length      Date    Time    Name
    ---------  ---------- -----   ----
            0  07-20-2018 12:50   Python-3.7.0/
         7122  06-27-2018 11:07   Python-3.7.0/install-sh
       101855  06-27-2018 11:07   Python-3.7.0/setup.py
            0  07-20-2018 12:37   Python-3.7.0/.vsts/
     13965984  07-20-2018 12:50   Python-3.7.0/python
           26  07-20-2018 12:50   Python-3.7.0/pybuilddir.txt
    ---------                     -------
    186791269                     4771 files
    
    • 5、删除压缩包中指定的文件
    [root@localhost Python-3.7.0]# zip Python-3.7.0.zip -d Python-3.7.0/*.o
    deleting: Python-3.7.0/Modules/config.o
    deleting: Python-3.7.0/Modules/getpath.o
    deleting: Python-3.7.0/Modules/main.o
    

    本文同步在微信订阅号上发布,如各位小伙伴们喜欢我的文章,也可以关注我的微信订阅号:woaitest,或扫描下面的二维码添加关注:
    MyQRCode.jpg

  • 相关阅读:
    hosts 本机DNS域名解析
    五步搞定Android开发环境部署——非常详细的Android开发环境搭建教程
    OracleBulkCopy
    第三方登录(QQ登录)开发流程详解
    Asp.net MVC中Html.Partial, RenderPartial, Action,RenderAction 区别和用法
    MVC Return View() 和 Return PartialView()的区别
    如何选择Html.RenderPartial和Html.RenderAction
    C# Dictionary和Dynamic类型
    css01入门小例子
    html03表单
  • 原文地址:https://www.cnblogs.com/surpassme/p/9416455.html
Copyright © 2020-2023  润新知