day 14 打包压缩和解压缩

今日内容

1.什么是文件压缩

    将多个文件或目录合并成为一个特殊的文件

2.为什么要对文件进行压缩

    减小文件的体积    加快资源传输     节省网络的带宽

3.Windows的压缩包与Linux的压缩包能否互通

    Windows有:rar  zip  其实支持很多类型的压缩

    Linux:zip   tar.gz

    windows 与Linux 互通建议使用:zip 

4.Linux下压缩有哪些常见的类型

格式                压缩工具

.zip                  zip压缩工具

.gz                   gzip压缩工具,只能压缩文件,会删除原文件(通常配合tar使用)

.bz2                 bzip2压缩工具,只能压缩文件,会删除原文件(通常配合tar使用)

.tar.gz              先使用tar命令归档打包,然后使用gzip压缩

.tar.bz2            先使用tar命令归档打包,然后使用bzip压缩

5.Linux 打包工具 gzip的使用

#1.gzip打包与压缩,仅对文件有效

###  gzip  +  (filename)文件名称     打包      

###  gzip  -d   (filename.gz )新压缩文件名    解包

###  zcat   (filename.gz)    新压缩文件名      查看包文件的内容


[root@caoweiqiang ~]# yum install gzip -y       #安装gzip 的yum

[root@caoweiqiang ~]# gzip file                #对文件进行压缩

[root@caoweiqiang ~]# zcat file.gz            #查看gz 压缩后的文件

[root@caoweiqiang ~]# gzip -d file.gz        #解压gzip 的压缩包 (不指定位置会解压到当前文件)

#使用场景:当需要让某个文件快速关闭和快速启用

[root@caoweiqiang ~]# gzip Centos-Vault.repo -->Centos-Vault.repo.gz

[root@caoweiqiang ~]# zcat Centos-Vault.repo.gz    查看不想解压的文件


6.Linux zip 工具使用

#默认情况下 没有zip和unzip工具,需要进行安装

[root@caoweiqiang ~]# yum install zip unzip -y

#1.压缩文件为zip包

[root@caoweiqiang ~]# zip filename.zip file  

[root@caoweiqiang ~]# unzip -l filename.zip     #查看包里面内容


#2.压缩目录为zip的包

[root@caoweiqiang ~]# zip -r dir.zip dir/   打包dir这个目录,打包成名字是dir.zip的文件

#3.查看zip 压缩包是否是完整的

[root@caoweiqiang ~]# zip -T filename.zip

test of filename.zip OK

#4.不解压压缩查看压缩包中的内容

[root@caoweiqiang ~]# unzip -l filename.zip

[root@caoweiqiang ~]# unzip -t filename.zip    #检测文件是否都OK

#5.解压zip文件包,默认解压至当前目录

[root@caoweiqiang ~]# unzip filename.zip  

#6.解压zip内容至/opt目录

[root@caoweiqiang ~]# unzip filename.zip -d /opt/    #指定压缩包内容解压到什么位置

#打包  :[root@caoweiqiang ~]# zip -r /tmp/test.zip file dir/   (将file  还有dir/目录打包到tmp下叫test.zip)

#解压 : [root@caoweiqiang tmp]# unzip test.zip

               [root@caoweiqiang tmp]# unzip test.zip -d /opt  (解压test.zip到/opt目录下)

#7.Linux  tar  工具使用

tar 是Linux下最常用的压缩与解压缩,支持文件和目录的压缩归档

#语法:tar   [-zjxcvfpP]     filename 

c #创建新的归档文件x #对归档文件解包

x #对归档文件解包

t #列出归档文件里的文件列

f #指定包文件名,多参数f写最后

z #使用gzip压缩归档后的文件(.tar.gz)

j #使用bzip2压缩归档后的文件(.tar.bz2)

J #使用xz压缩归档后的文件(tar.xz)

C #指定解压目录位置

X #排除多个文件(写入需要排除的文件名称)

h #打包软链接--hard-dereference 

--exclude   #在打包的时候写入需要排除文件或目录


#常用大包与压缩组合

cjf #打包tar.bz格式

cJf #打包tar.xz格式     (使用较少)

zxf #解压 tar.gz格式

jxf #解压  tar.bz格式


czf   #打包tar.gz格式   (重要)

tf     #查看压缩包内容

xf    #自动选择解压模式  (重要 )

1。将文件或目录进行打包压缩

#打包  [root@caoweiqiang ~]# tar czf test.tar.gz test/ test2/   #以gzip方式压缩

           [root@caoweiqiang ~]# tar cjf test.tar.bz2 dir.txt dir/     #以bz2方式压缩

#查看包内容       

[root@caoweiqiang ~]# tar tf test.tar.gz

[root@caoweiqiang ~]# tar tf test.tar.bz2

#解压

[root@caoweiqiang ~]# tar xf test.tar.gz

[root@caoweiqiang ~]# tar xf test.tar.bz2

[root@caoweiqiang ~]# tar xf root.tar.gz -C /tmp/       (解压到指定目录)

#打包/tmp下所有文件

[root@caoweiqiang ~]# find tmp/ -type f |xargs tar czf tmp.tar.gz

tar czf  tmp.tar.gz  $(find /tmp/  -type f)

#3.打包链接文件,打包链接文件的真实文件

[root@caoweiqiang ~]# tar czfh local.tar.gz etc/rc.local

#4.排除操作

[root@caoweiqiang ~]# tar czf etc.tar.gz /etc/ --exclude=etc/services

[root@caoweiqiang ~]# tar czf etc.tar.gz /etc/ --exclude=etc/services --exclude=etc/shadow

tar命令练习

#1.环境准备

[root@xuliangwei ~]# yum install mariadb-server

[root@xuliangwei ~]# systemctl start mariadb

[root@xuliangwei ~]# mkdir /backup

#案例1.mysql备份及恢复

[root@xuliangwei ~]# tar cJf /backup/mysql.tar.xz /var/lib/mysql

[root@xuliangwei ~]# tar xf /backup/mysql.tar.xz -C /

#案例2 mysql备份及恢复

[root@xuliangwei ~]# cd /var/lib/mysql

[root@xuliangwei mysql]# tar cJf /backup/mysql.tar.xz *

[root@xuliangwei mysql]# tar tf /backup/mysql.tar.xz

[root@xuliangwei mysql]# tar xf /backup/mysql.tar.xz -C /var/lib/mysql

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。