6.5 zip压缩工具
zip目录概要
- zip支持压缩目录
- zip 1.txt.zip 1.txt 压缩文件
- zip -r 123.zip 123/ 压缩文件到指定目录下
- unzip 1.txt.zip 解压文件
- unzip 123.zip -d /root/456/ 解压文件到制定目录
- unzip -l 123.zip 查看压缩文件列表
zip压缩文件或目录
- 在linux和windows中都有一个zip压缩工具,但是linux下不支持解压windos下的rar文件(默认不支持,需要安装工具才可以)
- 安装zip包——>yum install -y zip
[root@hf-01 d6z]# mkdir -p /tmp/hf/han/hanfeng
[root@hf-01 d6z]# cd /tmp/hf
[root@hf-01 hf]# touch 1.txt
[root@hf-01 hf]# touch /tmp/hf/han/wu.txt
[root@hf-01 hf]# touch /tmp/hf/han/hanfeng/66.txt
[root@hf-01 hf]# cd /tmp/d6z
[root@hf-01 d6z]# ls /tmp
12.txt 1.txt 2.txt.bz2 d6z ha.txt.xz hf mysql.sock vim.txt
[root@hf-01 d6z]# cp -r /tmp/hf .
[root@hf-01 d6z]# ls
1.txt 4.txt 5.txt 8.xt hf
[root@hf-01 d6z]# tree hf/
hf/
├── 1.txt
└── han
├── hanfeng
│ └── 66.txt
└── wu.txt
2 directories, 3 files
[root@hf-01 d6z]# cp 1.txt hf/han/hanfeng/
[root@hf-01 d6z]# du -sh hf/
1.5M hf/
zip压缩文件
- zip 1.txt.zip 1.txt 压缩文件
[root@hf-01 d6z]# zip 1.txt.zip 1.txt
adding: 1.txt (deflated 74%)
[root@hf-01 d6z]# du -sh 1.txt.zip
388K 1.txt.zip
[root@hf-01 d6z]# ls
1.txt 1.txt.zip 4.txt 5.txt 8.xt hf
zip压缩目录
- zip -r hafe.zip 8.txt hf 压缩文件和目录
[root@hf-01 d6z]# zip -r hafe.zip 8.txt hf
zip warning: name not matched: 8.txt
adding: hf/ (stored 0%)
adding: hf/han/ (stored 0%)
adding: hf/han/hanfeng/ (stored 0%)
adding: hf/han/hanfeng/66.txt (stored 0%)
adding: hf/han/hanfeng/1.txt (deflated 74%)
adding: hf/han/wu.txt (stored 0%)
adding: hf/1.txt (stored 0%)
[root@hf-01 d6z]# ls
1.txt 8.txt hf hafe.zip
zip压缩文件后不删除之前的文件
zip解压文件
- 解压zip包——>yum install -y unzip
- unzip yasuo.zip 解压文件
- 解压文件的时候,会提示 “是否覆盖”、“是否替换”等, A 表示全部解压(因为压缩的时候不会删除源文件)
[root@localhost d6z]# unzip yasuo.zip
Archive: yasuo.zip
replace 2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
inflating: 2.txt
extracting: hf/han/hanfeng/66.txt
inflating: hf/han/hanfeng/1.txt
extracting: hf/han/wu.txt
extracting: hf/1.txt
extracting: hf/11.txt
[root@localhost d6z]#
zip解压文件到指定目录
- unzip 1.txt.zip -d lala/ 解压文件到指定目录下(若不指定目录,就会在当前目录下)
[root@localhost d6z]# mkdir lala
[root@localhost d6z]# unzip 1.txt.zip -d lala/
Archive: 1.txt.zip
inflating: /lala/1.txt
- zip解压文件不能指定文件名,压缩的时候是什么文件名,解压的时候还是这个文件名
- zip的压缩文件,是无法查看的
查看zip压缩包的文件列表
- unzip -l yasuo.zip 查看压缩文件列表
[root@localhost d6z]# unzip -l yasuo.zip
Archive: yasuo.zip
Length Date Time Name
--------- ---------- ----- ----
1283485 11-09-2017 16:02 2.txt
0 11-10-2017 10:40 hf/
0 11-10-2017 10:41 hf/han/
0 11-10-2017 10:45 hf/han/hanfeng/
0 11-10-2017 10:41 hf/han/hanfeng/66.txt
1283485 11-10-2017 10:45 hf/han/hanfeng/1.txt
0 11-10-2017 10:41 hf/han/wu.txt
0 11-10-2017 10:40 hf/1.txt
0 11-10-2017 10:40 hf/11.txt
--------- -------
2566970 9 files
[root@localhost d6z]#
总结
- gzip、bzip2、xz这三种压缩工具是可以指定解压文件的目录和名称,而zip只可以指定目录,却不能指定解压文件名称
- zip压缩工具可以压缩文件和目录,但gzip、bzip2、xz这三种却不可以压缩目录
6.6 tar打包
tar目录概要
- tar -cvf 123.tar 123
- tar -cvf aming.tar 1.txt 123
- tar -xvf aming.tar
- tar -tf aming.tar
- tar -cvf aming.tar –exclude 1.txt –exclude 2 123
网络小知识
- 带宽的单位:比特(Byte)
- 1Byte=8bit;1兆字节=8兆比特
- 100兆网卡跑满,应该是12.5兆字节每秒(这是传输速度)——>这是理论值
tar打包目录
- 打包对于文件大小无太大变化,但是多少会存在一些微小的变化
- 如果每个小文件小于4K(小于一个块的大小),文件只有1K、2K、3K(未超过4K),有一万个类似的文件,就需要4000K(40兆),但打包这些小文件就会相加,最后有可能只有十几兆
- tar -cvf hf.tar hf/ 把hf目录打包成hf.tar,其中 / 符号可省略
- c 表示创建,v 表示可视化,看到创建的过程,f 后跟tar打包的名称——>v可省略,但cf必须有
[root@hf-01 d6z]# ls
1.txt 1.txt.zip 4.txt 5.txt 8.xt hafe.zip hf
[root@hf-01 d6z]# tar -cvf hf.tar hf/
hf/
hf/han/
hf/han/hanfeng/
hf/han/hanfeng/66.txt
hf/han/hanfeng/1.txt
hf/han/wu.txt
hf/1.txt
[root@hf-01 d6z]#
- 若已经打包,再次去重新打包,会直接覆盖之前的打包文件,并且没有任何提示
[root@hf-01 d6z]# tar -cf hf.tar hf/
[root@hf-01 d6z]# ls
1.txt 1.txt.zip 4.txt 5.txt 8.xt hafe.zip hf hf.tar
[root@hf-01 d6z]#
tar解包
- tar -xvf hf.tar 把hf.tar包解开——>默认覆盖之前的物件,无任何提示
[root@hf-01 d6z]# tar -xvf hf.tar
hf/
hf/han/
hf/han/hanfeng/
hf/han/hanfeng/66.txt
hf/han/hanfeng/1.txt
hf/han/wu.txt
hf/1.txt
[root@hf-01 d6z]# ls
1.txt 1.txt.zip 4.txt 5.txt 8.xt hafe.zip hf hf.tar
- 解包后会发现打包的的依旧存在
tar打包目录和文件
- tar -cvf han.tar hf/ 1.txt 4.txt 把文件和目录一起打包成han.tar
[root@hf-01 d6z]# tar -cvf han.tar hf/ 1.txt 4.txt
hf/
hf/han/
hf/han/hanfeng/
hf/han/hanfeng/66.txt
hf/han/hanfeng/1.txt
hf/han/wu.txt
hf/1.txt
1.txt
4.txt
[root@hf-01 d6z]# ls
1.txt 1.txt.zip 4.txt 5.txt 8.xt hafe.zip han.tar hf hf.tar
tar查看打包文件中的文件列表
- tar -tf han.tar 查看han.tar中的文件列表
[root@hf-01 d6z]# tar -tf han.tar
hf/
hf/han/
hf/han/hanfeng/
hf/han/hanfeng/66.txt
hf/han/hanfeng/1.txt
hf/han/wu.txt
hf/1.txt
1.txt
4.txt
tar过滤指定的文件,不去打包它们
- tar -cvf han.tar –exclude hanfeng hf/ 1.txt 4.txt 过滤hanfeng文件目录,然后打包hf、1.txt、4.txt文件成han.tar
- –exclude 后面跟过滤到的文件或目录
- 选项 f 后,必须跟tar包的名称
- 重复的打包名称,会直接覆盖掉之前的打包文件
[root@hf-01 d6z]# tar -cvf han.tar --exclude hanfeng hf/ 1.txt 4.txt
hf/
hf/han/
hf/han/wu.txt
hf/1.txt
1.txt
4.txt
- 过滤掉文件、目录和*.txt文件
- 过滤”*.txt”文件必须加”“
- –exclude支持过个文件过滤
[root@hf-01 d6z]# tar -cvf han.tar --exclude hanfeng --exclude wu.txt --exclude "*.txt" hf/ 1.txt 4.txt
hf/
hf/han/
6.7 打包并压缩
打包并压缩目录概要
- tar -zcvf 123.tar.gz 123 把目录和文件打包成123.tar.gz
- tar -zxvf 123.tar.gz 把文件和目录解包
- tar -jcvf 123.bz2 123 把目录和文件打包成123.tar.bz2
- tar -jxvf 123.bz2 把文件和目录解包
- tar -Jcvf 123.xz 123 把目录和文件打包成123.tar.xz
- tar -Jxvf 123.xz 把文件和目录解包
- tar -tf 123.bz2 或 tar -tf 123.gz 或 tar -tf 123.xz 查看文件目录
tar打包成gzip,解包
- tar -zcvf gurui.tar.gz hf/ 1.txt 4.txt
[root@hf-01 d6z]# tar -czvf gurui.tar.gz hf/ 1.txt 4.txt
hf/
hf/han/
hf/han/hanfeng/
hf/han/hanfeng/66.txt
hf/han/hanfeng/1.txt
hf/han/wu.txt
hf/1.txt
1.txt
4.txt
[root@hf-01 d6z]# ls
1.txt 1.txt.zip 4.txt 5.txt 8.xt gurui.tar.gz hafe.zip han.tar hf hf.tar
[root@hf-01 d6z]# du -sh gurui.tar.gz 查看打包文件的大小
1.2M gurui.tar.gz
[root@hf-01 d6z]# du -sh hf/ 1.txt 4.txt
1.5M hf/
1.5M 1.txt
1.5M 4.txt
- tar解gzip包
- tar -zxvf gurui.tar.gz
[root@hf-01 d6z]# tar -zxvf gurui.tar.gz
hf/
hf/han/
hf/han/hanfeng/
hf/han/hanfeng/66.txt
hf/han/hanfeng/1.txt
hf/han/wu.txt
hf/1.txt
1.txt
4.txt
[root@hf-01 d6z]# ls
1.txt 4.txt 8.xt gurui.tar.gz hafe.zip hf
1.txt.zip 5.txt gurui.tar.bz2 gurui.tar.xz han.tar hf.tar
tar打包成bz2
- tar -cjvf gurui.tar.bz2 hf/ 1.txt 4.txt
[root@hf-01 d6z]# tar -cjvf gurui.tar.bz2 hf/ 1.txt 4.txt
hf/
hf/han/
hf/han/hanfeng/
hf/han/hanfeng/66.txt
hf/han/hanfeng/1.txt
hf/han/wu.txt
hf/1.txt
1.txt
4.txt
[root@hf-01 d6z]# ls
1.txt 4.txt 8.xt gurui.tar.gz han.tar hf.tar
1.txt.zip 5.txt gurui.tar.bz2 hafe.zip hf
[root@hf-01 d6z]# du -sh gurui.tar.bz2
432K gurui.tar.bz2
- tar解bzip包
- tar -xjvf gurui.tar.bz2
[root@hf-01 d6z]# tar -xjvf gurui.tar.bz2
hf/
hf/han/
hf/han/hanfeng/
hf/han/hanfeng/66.txt
hf/han/hanfeng/1.txt
hf/han/wu.txt
hf/1.txt
1.txt
4.txt
tar打包成xz
- tar -cJvf gurui.tar.xz hf/ 1.txt 4.txt
[root@hf-01 d6z]# tar -cJvf gurui.tar.xz hf/ 1.txt 4.txt
hf/
hf/han/
hf/han/hanfeng/
hf/han/hanfeng/66.txt
hf/han/hanfeng/1.txt
hf/han/wu.txt
hf/1.txt
1.txt
4.txt
[root@hf-01 d6z]# du -sh gurui.tar.xz
60K gurui.tar.xz
- tar解xz包
- tar -xJvf gurui.tar.xz
[root@hf-01 d6z]# tar -xJvf gurui.tar.xz
hf/
hf/han/
hf/han/hanfeng/
hf/han/hanfeng/66.txt
hf/han/hanfeng/1.txt
hf/han/wu.txt
hf/1.txt
1.txt
4.txt
tar查看文件目录
- tar -tf 123.bz2 查看文件目录
- tar -tf 123.gz 查看文件目录
- tar -tf 123.xz 查看文件目录
[root@hf-01 d6z]# tar -tf gurui.tar.gz
hf/
hf/han/
hf/han/hanfeng/
hf/han/hanfeng/66.txt
hf/han/hanfeng/1.txt
hf/han/wu.txt
hf/1.txt
1.txt
4.txt
总结
- 我们会发现,压缩的越小,花费的时间越多