压缩

1,什么是文件压缩
将多个文件和目录组成一个特殊的文件
2,为什么要对文件进行压缩
1,压缩前与压缩后。大小不一样。
2压缩后传输会快很多。
3,windows 和Linux压缩如何互通。
.zip
.tar.gz
4.Linux下常见压缩包类型
格式
.zip
.gz
.bz2
.tar.gz
.tar.bz2


yum install gzip -y
gzip
使用gzip方式进行压缩文件 ( 只能压缩文件,并且文件被压缩后,源文件没有了)

[root@xuliangwei ~]# yum install gzip -y
[root@xuliangwei ~]# gzip file #对文件进行压缩
[root@xuliangwei ~]# zcat file.gz #查看gz压缩后的文件
[root@xuliangwei ~]# gzip -d file.gz #解压gzip的压缩包

#使用场景:当需要让某个文件不生效时
[root@xuliangwei ~]# gzip CentOS-Vault.repo --> CentOS-Vault.repo.gz
[root@xuliangwei ~]# zcat CentOS-Vault.repo.gz --> 查看不想解压的压缩包文件内容


zip, 使用zip命令可以对文件进行压缩打包,解压则需要使用unzip命令

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

[root@xuliangwei ~]# yum install zip unzip -y
[root@db01-172 ~]# which zip
/usr/bin/zip
[root@db01-172 ~]# which unzip
/usr/bin/unzip

#1.压缩文件为zip包
[root@xuliangwei ~]# zip filename.zip filename

#2.压缩目录为zip包
[root@xuliangwei ~]# zip -r dir.zip dir/

#3.查看zip压缩包是否是完整的
[root@xuliangwei ~]# zip -T filename.zip
test of filename.zip OK

#4.不解压压缩查看压缩包中的内容
[root@xuliangwei ~]# unzip -l filename.zip
[root@xuliangwei ~]# unzip -t filename.zip

#5.解压zip文件包, 默认解压至当前目录
[root@xuliangwei ~]# unzip filename.zip

#6.解压zip内容至/opt目录
[root@xuliangwei ~]# unzip filename.zip -d /opt/


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

#语法:tar [-zjxcvfpP] filename
c #创建新的归档文件
x #对归档文件解包
t #列出归档文件里的文件列表
v #输出命令的归档或解包的过程
f #指定包文件名,多参数f写最后

z #使用gzip压缩归档后的文件(.tar.gz)
j #使用bzip2压缩归档后的文件(.tar.bz2)
J #使用xz压缩归档后的文件(tar.xz)
C #指定解压目录位置
X #排除多个文件(写入需要排除的文件名称)
h #打包软链接
–exclude #在打包的时候写入需要排除文件或目录
————————————————————————————

#常用打包与压缩组合
czf #打包tar.gz格式
cjf #打包tar.bz格式
cJf #打包tar.xz格式

zxf #解压tar.gz格式
jxf #解压tar.bz格式
xf #自动选择解压模式 《===
tf #查看压缩包内容
1.将文件或目录进行打包压缩

#1.以gzip归档方式打包并压缩
tar czf test.tar.gz test/ test2/

#2.以bz2方式压缩
yum install bzip2 -y
tar cjf test.tar.bz2 dir.txt dir/

#3.打包链接文件,打包链接文件的真实文件
[root@xuliangwei ~]# cd /
[root@xuliangwei /]# tar czfh local.tar.gz etc/rc.local

#4.打包/tmp下所有文件
[root@xuliangwei ~]# cd /
[root@xuliangwei /]# find tmp/ -type f | xargs tar czf tmp.tar.gz
[root@db01-172 ~]# tar czf tmp2.tar.gz $(find /tmp/ -type f)
——————————————————

tar 
	czf	#tar.gz    
	cjf	#tar.bz2
tar
	tf	浏览包内容

tar
	xf	智能解压
	tar  xf xx.tar.gz -C

————————————————————
2.排除文件, 并打包压缩

#1.排除单个文件
[root@xuliangwei /]# tar czf etc.tar.gz --exclude=etc/services etc/

#2.排除多个文件
[root@xuliangwei /]# tar czf oo2.tar.gz --exclude=etc/sysconfig/network-scripts/ifcfg-lo --exclude=etc/sysconfig/network-scripts/ifcfg-eth0 /etc

#3.将需要排除的文件写入文件中
[root@db01-172 opt]# cat ~/pc.txt
etc/sysconfig/network-scripts/ifcfg-eth0
etc/services
etc/rc.local
#指定需要排除的文件列表, 最后进行打包压缩
[root@db01-172 ~]# tar czfX oo3.tar.gz pc.txt /etc/
tar: 从成员名中删除开头的“/”

[root@db01-172 ~]# cp oo3.tar.gz /opt/
[root@db01-172 ~]# cd /opt/
[root@db01-172 opt]# rm -rf etc/
[root@db01-172 opt]# tar xf oo3.tar.gz
[root@db01-172 opt]# find etc/ -type f |grep “ifcg*”
etc/sysconfig/network-scripts/ifcfg-lo
[root@db01-172 opt]# find etc/ -type f |grep “services”
[root@db01-172 opt]# find etc/ -type f |grep “rc.local”
etc/rc.d/rc.local

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值