当前位置: 首页 > news >正文

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]# 

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

[root@localhost 203312032134]# tar -cvf 2135.tar *.txt
a.txt
b.txt
c.txt

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

[root@localhost 203312032134]# tar -tf 2135.tar 
a.txt
b.txt
c.txt
[root@localhost 203312032134]# 

-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

-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]# 

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

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

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

zcat、zless、zmore

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

zip/unzip
安装方式

yum install zip 
yum install unzip

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]# 

相关文章:

  • 西湖大学英语听力考试音频无线发射系统-英语听力发射系统浅析
  • docker-compose管理jenkins
  • HashMap前世今生
  • Vue从入门到实战Day03
  • 95、动态规划-编辑距离
  • [AIGC] MyBatis-Plus 动态数据源
  • MAC | linux | SSH 密钥验证
  • 【清理mysql数据库服务器二进制日志文件】
  • 高校宣讲会管理系统|基于Springboot的高校宣讲会管理系统设计与实现(源码+数据库+文档)
  • Golang使用Swag搭建api文档
  • 使用uniapp实现小程序获取wifi并连接
  • Bicycles(变形dijkstra,动态规划思想)
  • [附源码]计算机毕业设计JAVA医药管理系统
  • [附源码]计算机毕业设计基于SpringBoot+Vue的健身房会员系统的设计与实现
  • 9 特色聚类
  • python中的集合详解
  • pringboot面向爱宠人群的宠物资讯系统36as8计算机毕业设计-课程设计-期末作业-毕设程序代做
  • Flink系列之Flink中StateBackend深入剖析和应用
  • Java可变参数和集合工具类Collections的详细介绍
  • 网站构建初级教程
  • 项目管理逻辑:老板为什么赔钱的项目也做?为什么害怕你闲着?
  • Spring Boot 框架整合 MyBatis 连接数据库,详细说明
  • MySQL主从同步
  • 微服务自动化【Docker-Compose】
  • 在Postgres中分页的五种方法,从基本到异国情调
  • 工作经历分享
  • 堆(二叉堆)-优先队列-数据结构和算法(Java)
  • awk命令的使用
  • 初学Nodejs(5):npm包管理器与包的发布
  • mysql高阶语句
  • [附源码]Python计算机毕业设计Django勤工俭学管理小程序
  • 0115 查找算法Day4