windows
下我们接触最多的压缩文件就是.rar
格式, 但Linux
有自己所特有的压缩工具。
如果希望windows和Linux互相能使用的压缩工具, 建议.zip
格式
压缩的好处主要有:
节省磁盘空间占用率
节省网络传输带宽消耗
网络传输更加快捷
Linux系统常见的后缀名所对应的压缩工具
.gz gzip //压缩工具压缩的文件
.bz2 bzip2 //压缩工具压缩的文件
.tar tar //tar没有压缩功能,只是把一个目录合并成一个文件
.tar.gz //先使用tar打包,然后使用gzip压缩归档
.tar.bz2 //先使用tar打包,然后使用bzip压缩归档注意:
1.Linux下常用压缩文件以.tar.gz结尾.
2.Linux下压缩文件必须带后缀.
1.ZIP压缩工具
zip
是压缩工具,unzip
是解压缩工具
[root@xuliangwei ~]# zip filename.zip filename
[root@xuliangwei ~]# zip -r dir.zip dir/
[root@xuliangwei ~]# unzip filename.zip
2.TAR压缩工具
tar
是linux
下最常用的压缩与解压缩, 支持文件和目录的压缩归档
语法:tar [-zjxcvfpP] filename
c
x
t
v
f
C
z
j
J
X
h
--hard-dereference
--exclude
czf
cjf
cJf
zxf
jxf
xf
tf
1.将文件或目录进行打包压缩
tar czf test.tar.gz test/ test2/
tar cjf test.tar.bz2 dir.txt dir/
[root@xuliangwei ~]# cd /
[root@xuliangwei /]# tar czfh local.tar.gz etc/rc.local
[root@xuliangwei ~]# cd /
[root@xuliangwei /]# find tmp/ -type f | xargs tar czf tmp.tar.gz
[root@xuliangwei /]# tar czf tmp.tar.gz | xargs find tmp/ -type f
[root@xuliangwei /]# tar czf tmp.tar.gz $(find tmp/ -type f)
2.排除文件, 并打包压缩
//排除单个文件
[root@xuliangwei /]
//排除多个文件
[root@xuliangwei /]
//将需要排除的文件写入文件中
[root@xuliangwei /]
etc/services
etc/rc.local
etc/rc.d/rc.local
//指定需要排除的文件列表, 最后进行打包压缩
[root@xuliangwei /]
3.查看压缩文件
[root@xuliangwei /]# tar tf test.tar.gz
4.解压缩文件
[root@xuliangwei /]# tar xf test.tar.gz
[root@student ~]# tar xf /etc/local.tar.gz -C /tmp
注意: 不管是打包还是解包,原文件是不会被删除的,但会覆盖当前已经存在的文件或者目录。
3.TAR实践案例
基础环境准备
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
案例1 mysql
物理备份及恢复
[root@localhost ~]
[root@localhost ~]
案例2 mysql
物理备份及恢复
[root@localhost ~]
[root@localhost mysql]
[root@localhost mysql]
[root@localhost mysql]
案例3 host A /etc
(海量小文件) --> host A /tmp
[root@localhost ~]
案例4 host A /etc
(海量小文件) --> host B /tmp
//常规方法
[root@localhost ~]
//建议方法:
//接收B主机, 需要监听端口
[root@hostB ~]
[root@hostB ~]
//发送方A主机
[root@hostA ~]
tar: Removing leading `/' from member names
转 xuliangwei.com
评论