• Linux常用的压缩和解压命令gzip,gunzip,tar,zip, unzip和bzip2,bunzip2


    Linux常用的压缩和解压命令

    1、压缩解压gzip和gunzip

    特点:

    • 压缩比例大概为6:1

    • 该命令只能压缩文件,不能压缩目录

    • 压缩或者解压后不保留源文件

    压缩示例:gzip 需要压缩的文件

    解压示例gunzip xx.gz 或者gzip -d xx.gz

    [root@localhost fengmin]# ll
    total 656
    -rw-r--r--. 1 root root 670293 Nov  4 10:16 services
    [root@localhost fengmin]# gzip services 
    [root@localhost fengmin]# ll
    total 136
    -rw-r--r--. 1 root root 136088 Nov  4 10:16 services.gz   # 压缩比例大概是6:1
    
    [root@localhost fengmin]# ll
    total 656
    -rw-r--r--. 1 root root 670293 Nov  4 10:16 services
    drwxr-xr-x. 2 root root     23 Nov  4 10:18 test
    [root@localhost fengmin]# gzip test   # 不能压缩目录
    gzip: test is a directory -- ignored
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2、压缩解压命令tar

    打包目录或文件,如果参数不带-z,就是只打包不压缩。压缩后的文件格式.tar或者.tar.gz

    打包格式:tar 选项[-zcf] 压缩后的文件名 目录

    ​ -c 打包

    ​ -v 显示详细的打包信息

    ​ -f 指定压缩后的文件名

    ​ -z 打包同时压缩

    ​ -x 解压

    解包格式:tar

    也可以分两步打包和压缩,tar -cvf xx.tar xx 然后再gzip xx.tar,生成的就是xx.tar.gz

    # 一步到位,打包加压缩
    [root@localhost fengmin]# ll
    total 656
    -rw-r--r--. 1 root root 670293 Nov  4 10:16 services
    drwxr-xr-x. 2 root root     23 Nov  4 10:18 test
    [root@localhost fengmin]# tar -cvzf test.tar.gz test
    test/
    test/hello.txt
    
    # 分两步,先打包再压缩
    [root@localhost fengmin]# ll
    total 656
    -rw-r--r--. 1 root root 670293 Nov  4 10:16 services
    drwxr-xr-x. 2 root root     23 Nov  4 10:18 test
    [root@localhost fengmin]# tar -cvf test.tar test/
    test/
    test/hello.txt
    [root@localhost fengmin]# ll
    total 668
    -rw-r--r--. 1 root root 670293 Nov  4 10:16 services
    drwxr-xr-x. 2 root root     23 Nov  4 10:18 test
    -rw-r--r--. 1 root root  10240 Nov  4 10:43 test.tar
    [root@localhost fengmin]# gzip test.tar 
    [root@localhost fengmin]# ll
    total 660
    -rw-r--r--. 1 root root 670293 Nov  4 10:16 services
    drwxr-xr-x. 2 root root     23 Nov  4 10:18 test
    -rw-r--r--. 1 root root    154 Nov  4 10:43 test.tar.gz
    
    # 解压
    [root@localhost fengmin]# ll
    total 1320
    -rw-r--r--. 1 root root 670293 Nov  4 10:16 services
    -rw-r--r--. 1 root root 675840 Nov  4 10:49 services.tar
    drwxr-xr-x. 2 root root     23 Nov  4 10:18 test
    -rw-r--r--. 1 root root    145 Nov  4 10:46 test.tar.gz
    [root@localhost fengmin]# tar -zxvf test.tar.gz 
    test/
    test/hello.txt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    3、压缩解压命令zip

    压缩文件或目录。linux和window都支持的格式

    zip 选项[-r] 压缩后文件名 文件或者目录

    ​ -r 压缩目录 (不带-r表示压缩文件)

    unzip xxx.zip 解压文件

    [root@localhost fengmin]# ll
    total 4
    -rw-r--r--. 1 root root 75 Nov  4 10:57 hello.txt
    drwxr-xr-x. 2 root root 19 Nov  4 10:56 test
    [root@localhost fengmin]# zip hello.zip hello.txt  # 压缩文件
      adding: hello.txt (deflated 77%)
    [root@localhost fengmin]# ll
    total 8
    -rw-r--r--. 1 root root  75 Nov  4 10:57 hello.txt
    -rw-r--r--. 1 root root 185 Nov  4 10:57 hello.zip
    drwxr-xr-x. 2 root root  19 Nov  4 10:56 test
    [root@localhost fengmin]# zip -r test.zip test/    # 压缩目录,要带上-r参数,否则就是压了一个空目录test
      adding: test/ (stored 0%)
      adding: test/hi.py (stored 0%)
    [root@localhost fengmin]# ll
    total 12
    -rw-r--r--. 1 root root  75 Nov  4 10:57 hello.txt
    -rw-r--r--. 1 root root 185 Nov  4 10:57 hello.zip
    drwxr-xr-x. 2 root root  19 Nov  4 10:56 test
    -rw-r--r--. 1 root root 308 Nov  4 10:58 test.zip
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    4、压缩解压命令bzip2和bunzip2

    和gzip命令的区别就是多了-k参数,可以保留源文件,并且这个bzip2的压缩比惊人,压缩大文件推荐使用bzip2压缩。bzip2命令也是不能压缩目录啊,只能压缩文件

    bzip2 选项[-k] [文件]

    ​ -k 产生压缩文件后保留源文件

    压缩后文件格式.bz2

    配合tar打包命令使用就是用-j参数

    解压命令bunzip2 -k xx.bz2 解压,-k代表保留源文件

    [root@localhost fengmin]# bzip2 -k hello.txt 
    [root@localhost fengmin]# ll
    total 8
    -rw-r--r--. 1 root root 75 Nov  4 10:57 hello.txt
    -rw-r--r--. 1 root root 50 Nov  4 10:57 hello.txt.bz2
    drwxr-xr-x. 2 root root 19 Nov  4 10:56 test
    [root@localhost fengmin]# bzip2 -k test/   # 不能压缩目录
    bzip2: Input file test/ is a directory
    
    [root@localhost fengmin]# ll
    total 8
    -rw-r--r--. 1 root root 75 Nov  4 10:57 hello.txt
    -rw-r--r--. 1 root root 50 Nov  4 10:57 hello.txt.bz2
    drwxr-xr-x. 2 root root 19 Nov  4 10:56 test
    [root@localhost fengmin]# tar -cjf test.tar.bz2 test/   # -j参数代表bzip2压缩。解压是tar -xjf
    [root@localhost fengmin]# ll
    total 12
    -rw-r--r--. 1 root root  75 Nov  4 10:57 hello.txt
    -rw-r--r--. 1 root root  50 Nov  4 10:57 hello.txt.bz2
    drwxr-xr-x. 2 root root  19 Nov  4 10:56 test
    -rw-r--r--. 1 root root 140 Nov  4 11:13 test.tar.bz2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    《研发效能(DevOps)工程师》课程简介(五)丨IDCF
    解决阿里云服务器使用ip访问Nginx失败的问题
    光伏发电站电能质量在线检测装置、防弧岛保护设备数据采集方案
    软考中活动图,最关键路径、早开始时间等
    pytorch 训练时raise EOFError EOFError
    Selenium获取本地已打开的浏览器页面进行跟踪和自定义日志记录
    红队常用的防守策略.
    MySQL之创建高性能的索引(十二)
    C++11新特性学习笔记
    confluent-kafka-go依赖库编译体验优化
  • 原文地址:https://blog.csdn.net/weixin_45455015/article/details/127698225