• Linux-文件压缩解压


    文件压缩解压

    打包:是将多个文件变成一个总的文件,它的学名叫存档、归档。

    • 压缩:是将一个大文件(通常指归档)压缩变成一个小文件。

    我们常常使用 tar 将多个文件归档为一个总的文件,称为 archive 。然后用 gzip 或 bzip2 命令将 archive 压缩为更小的文件。

    tar
    创建一个 tar 归档。
    tar -cvf 打包后的文件夹名.tar 要打包的文件

    [root@localhost home]# cd 203312032134/
    [root@localhost 203312032134]# touch a.txt
    [root@localhost 203312032134]# touch b.txt
    [root@localhost 203312032134]# touch c.txt
    [root@localhost 203312032134]# tar -cvf 2135.tar *.txt
    a.txt
    b.txt
    c.txt
    [root@localhost 203312032134]# ls
    2135.tar  a.txt  b.txt  c.txt
    [root@localhost 203312032134]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    常用参数
    -cvf 表示 create(创建)+ verbose(细节)+ file(文件),创建归档文件并显示操作细节;
    tar -cvf 2135.tar *.txt

    [root@localhost 203312032134]# tar -cvf 2135.tar *.txt
    a.txt
    b.txt
    c.txt
    
    • 1
    • 2
    • 3
    • 4

    -tf 显示归档里的内容,并不解开归档;
    tar -tf 2135.tar

    [root@localhost 203312032134]# tar -tf 2135.tar 
    a.txt
    b.txt
    c.txt
    [root@localhost 203312032134]# 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    -rvf 追加文件到归档
    tar -rvf 2135.tar d.txt

    [root@localhost 203312032134]# touch d.txt
    [root@localhost 203312032134]# tar -rvf 2135.tar d.txt
    d.txt
    [root@localhost 203312032134]# tar -tf 2135.tar 
    a.txt
    b.txt
    c.txt
    d.txt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    -xvf 解开归档
    tar -xvf 2135.tar

    [root@localhost 203312032134]# ls
    2135.tar  a.txt  b.txt  c.txt  d.txt
    [root@localhost 203312032134]# rm *.txt
    rm: remove regular empty file ‘a.txt’? y
    rm: remove regular empty file ‘b.txt’? y
    rm: remove regular empty file ‘c.txt’? y
    rm: remove regular empty file ‘d.txt’? y
    [root@localhost 203312032134]# ls
    2135.tar
    [root@localhost 203312032134]# tar -xvf 2135.tar 
    a.txt
    b.txt
    c.txt
    d.txt
    [root@localhost 203312032134]# ls
    2135.tar  a.txt  b.txt  c.txt  d.txt
    [root@localhost 203312032134]# 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    gzip / gunzip
    “压缩/解压”归档,默认用 gzip 命令,压缩后的文件后缀名为 .tar.gz 。
    gzip 2135.tar

    [root@localhost 203312032134]# gzip 2135.tar 
    [root@localhost 203312032134]# ls
    2135.tar.gz  a.txt  b.txt  c.txt  d.txt
    
    • 1
    • 2
    • 3

    tar 归档+压缩
    可以用 tar 命令同时完成归档和压缩的操作,就是给 tar 命令多加一个选项参数,使之完成归档操作后,还是调用 gzip 或 bzip2 命令来完成压缩操作。
    tar -zcvf 2142.tar.gz *.txt 压缩

    [root@localhost 203312032134]# tar -zcvf 2142.tar.gz *.txt
    a.txt
    b.txt
    c.txt
    d.txt
    [root@localhost 203312032134]# ls
    2135.tar.gz  2142.tar.gz  a.txt  b.txt  c.txt  d.txt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    tar -zxvf 2142.tar.gz 解压

    [root@localhost 203312032134]# ls
    2135.tar.gz  2142.tar.gz  a.txt  b.txt  c.txt  d.txt
    [root@localhost 203312032134]# rm -f *.txt
    [root@localhost 203312032134]# ls
    2135.tar.gz  2142.tar.gz
    [root@localhost 203312032134]# tar -zxvf 2142.tar.gz 
    a.txt
    b.txt
    c.txt
    d.txt
    [root@localhost 203312032134]# ls
    2135.tar.gz  2142.tar.gz  a.txt  b.txt  c.txt  d.txt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    zcat、zless、zmore

    压缩文件的内容使用 zcat、zless、zmore 进行查看。

    zip/unzip
    安装方式

    yum install zip 
    yum install unzip
    
    • 1
    • 2

    zip 压缩后的文件名.zip 想要压缩的目标文件
    unzip 想要解压的文件夹

    [root@localhost 203312032134]# zip 1.zip *.txt
      adding: a.txt (stored 0%)
      adding: b.txt (stored 0%)
      adding: c.txt (stored 0%)
      adding: d.txt (stored 0%)
    [root@localhost 203312032134]# ls
    1.zip  a.txt  b.txt  c.txt  d.txt
    [root@localhost 203312032134]# rm -f *.txt
    [root@localhost 203312032134]# unzip 1.zip
    Archive:  1.zip
     extracting: a.txt                   
     extracting: b.txt                   
     extracting: c.txt                   
     extracting: d.txt                   
    [root@localhost 203312032134]# ls
    1.zip  a.txt  b.txt  c.txt  d.txt
    [root@localhost 203312032134]# 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    2022年全国职业院校技能大赛:网络系统管理项目-模块B--Windows样题5
    QSS(Qt样式表)概念
    网工内推 | 运维专场,厂商、软考证书优先,五险一金,节日福利
    Matlab 点云迭代加权最小二乘法(IRLS)
    为提高 SDLC 安全,GitHub 发布新功能|GitHub Universe 2022
    【云岚到家】-day02-2-客户管理-认证授权
    react面试题总结
    冒泡排序Java代码实现
    Redis如何实现多可用区?
    异常数据检测 | Python基于奇异谱分析时间序列插补预测
  • 原文地址:https://blog.csdn.net/qq_37200262/article/details/128166860