• 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
  • 相关阅读:
    Linux 中断实验
    JSON Web Token
    Java真的不难(四十六)Spring Boot的入门
    【图像分割】基于差分进化算法优化模糊熵实现多级图像阈值分割附matlab代码
    百度迁徒数据爬虫方法
    从 HTA 中启动应用程序
    oracle 设置表空间 表空间操作
    核心容器中bean的操作
    【Java基础面试十二】、说一说你对面向对象的理解
    使用分类权重解决数据不平衡的问题
  • 原文地址:https://blog.csdn.net/qq_37200262/article/details/128166860