压缩和打包
  TEZNKK3IfmPf 2023年11月13日 34 0
压缩和打包

LINUX的压缩通常称之为打包压缩,所谓的打包就是将多个文件放在一起,那放在一起,文件有没有变小呀?没有,就是一个打包嘛!需要对包进行压缩之后包才会缩小。

常见的压缩格式:gz、bz2、xz、zip

压缩工具 压缩后的格式 压缩语法 解压缩语法 不解压查看
gzip .gz gzip FILE gunzip FILE zcat FILE
bzip2 .bz2 bzip2 FILE bunzip FILE bzcat FILE
xz .xz xz FILE unxz FILE xzcat FILE
zip .zip zip file.zip <FILE/DIR> unzip file.zip

gzip

gzip FILE 压缩文件

gzip –d FILE 解压缩

gzip -# FILE 指定压缩比1-9的范围,默认是6

zcat FILE 查看压缩后的内容,但并不解压缩

[root@zhanghe ~]# ll -h  /etc/man.config 
-rw-r--r--. 1 0 root 4.9K 2月  22 2013 /etc/man.config   #文件原本大小为4.9K
[root@zhanghe ~]# rm -rf /tmp/*  
[root@zhanghe ~]# cd /tmp 
[root@zhanghe tmp]# cp /etc/man.config .
[root@zhanghe tmp]# gzip man.config     #压缩 
[root@zhanghe tmp]# ls man.config.gz          #压缩后自动指定一个.gz的后缀
[root@zhanghe tmp]# ll -h man.config.gz      #压缩后2.2K 
 -rw-r--r--. 1 0 root 2.2K 8月  17 03:59 man.config.gz
[root@zhanghe tmp]# gzip -d man.config.gz   #解压缩 
[root@zhanghe tmp]# ls -lh man.config 
-rw-r--r--. 1 0 root 4.9K 8月  17 03:59 man.config  #解压缩之后自动去掉后缀 
[root@zhanghe tmp]# gzip -9 man.config   #指定压缩比
[root@zhanghe tmp]# zcat man.config    在不解压缩情况下查看被压缩的内容 

bzip2

bzip2 FILE 压缩文件

–d FILE 解压缩

-# 指定压缩比1-9

-k 保留源文件

bzcat 查看压缩后的内容,但并不解压缩

[root@zhanghe tmp]# bzip2 -9 man.config   压缩并指定压缩比
[root@zhanghe tmp]# ls -lh man.config.bz2 
-rw-r--r--. 1 0 root 2.2K 8月  17 03:59 man.config.bz2
[root@zhanghe tmp]# bzip2 -d man.config.bz2 解压缩 
[root@zhanghe tmp]# bzcat man.config.bz2    在不解压缩情况下查看被压缩的内容 
[root@zhanghe tmp]# bzip2 -k man.config 使用-K选项保留源文件
[root@zhanghe tmp]# ls man.config  man.config.bz2 

xz

xz –d FILE 或者unxz FILE 解压缩

-# 指定压缩比1-9

-k保留源文件

xzcat查看压缩后的内容,但并不解压缩

[root@zhanghe tmp]# xz man.config 
[root@zhanghe tmp]# ls man.config.xz 
[root@zhanghe tmp]# xz -d man.config.xz 
[root@zhanghe tmp]# ls man.config 
[root@zhanghe tmp]# xz -k man.config 
[root@zhanghe tmp]# ls man.config  man.config.xz 
[root@zhanghe tmp]# xzcat man.config.xz 

zip

zip <压缩后的文件名.zip> 要压缩的目录或文件

不删除源文件,即打包又压缩

unzip解压缩

[root@zhanghe tmp]# zip man.zip man.config  
[root@zhanghe tmp]# ls man.config  man.zip 
[root@zhanghe tmp]# ll -h ./ 
-rw-r--r--. 1 0 root 4.9K 8月  17 03:59 man.config 
-rw-r--r--. 1 0 root 2.3K 8月  17 04:21 man.zip  
[root@zhanghe tmp]# zip dev.zip dev/   #可以对目录使用
[root@zhanghe tmp]# ls dev  dev.zip 
[root@zhanghe tmp]# ll -dh ./* 
drwxr-xr-x. 18 0 root 5.0K 8月  17 01:31 ./dev 
-rw-r--r--.  1 0 root  158 8月  17 04:25 ./dev.zip 
[root@zhanghe tmp]# unzip dev.zip    #解压 

tar

只打包不压缩,不删除源文件,可使用通配符

-f FILE.tar 指定压缩后的文件名
-c 创建归档文件
--cattrs 归档时保留扩展属性

[root@zhanghe tmp]# tar -cf dev.tar dev/   #一般cf连用,先指定归档后的名字,后指定文件 
[root@zhanghe tmp]# ls dev  dev.tar     
[root@zhanghe tmp]# tar -xf  dev.tar     #一般与xf连用 
[root@zhanghe tmp]# ls dev  dev.tar 
[root@zhanghe tmp]# tar -tf dev.tar  #-tf不展开归档,查看归档了哪些文件

归档压缩

tar –zcf 归档并调用gzip压缩,z指的是gzip

tar –zxf解归档并调用gzip解压缩

tar –jcf 归档并调用bzip2压缩,z指的是bzip2

tar –jxf解归档并调用bzip2解压缩

tar –Jcf 归档并调用XZ压缩,z指的是XZ

tar –Jxf解归档并调用XZ解压缩

其实只要要压缩的时候指明白压缩后的格式,解压缩的时候就不用指定用什么方式解压缩,如下:

[root@zhanghe tmp]# tar -zcf dev.tar.gz  dev/
[root@zhanghe tmp]# ls
dev.tar.gz
[root@zhanghe tmp]# tar -xf dev.tar.gz      
[root@zhanghe tmp]# ls
dev  dev.tar.gz

总结

gzip,bzip2,xz,仅压缩,使用方式都是一样的,非常的简单,并且都是不保留源文件,解压缩都是使用-d的选项。

bzip2和``xz可以通过-k的选择来明确表示需要保存源文件,而且它们在压缩之后还可以通过cat命令进行查看.gzipzcat,而bzip2bzcat,而xzxzcat`。

zip,不仅压缩,还可打包,可对目录使用,无论是压缩还是解压缩都保留源文件,zip比较落后,需要我们手工指定压缩后的文件名字,解压缩使用unzip,不用指定-d的选项。

tar,仅打包不压缩,可在tar命令当中调用压缩的命令

tar的基本语法与zip相似,并且也保留源文件.

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2023年11月14日   21   0   0 文件打包
  TEZNKK3IfmPf   2023年11月14日   24   0   0 linux压缩
  TEZNKK3IfmPf   2023年11月14日   21   0   0 gziplinux压缩
  TEZNKK3IfmPf   2023年11月14日   20   0   0 linux压缩tar
  TEZNKK3IfmPf   2024年05月17日   24   0   0 打包tar
  TEZNKK3IfmPf   2023年11月12日   20   0   0 linux打包tar
  TEZNKK3IfmPf   2023年11月13日   35   0   0 压缩打包
  TEZNKK3IfmPf   2023年11月13日   40   0   0 linux压缩zip
  TEZNKK3IfmPf   2023年11月12日   22   0   0 文件打包webpack
  TEZNKK3IfmPf   2023年11月13日   44   0   0 gziplinux压缩
TEZNKK3IfmPf