文章标题

来源:互联网 发布:网络教育课程是啥子 编辑:程序博客网 时间:2024/05/21 04:43

6.5 zip压缩工具
1.zip支持压缩目录,Linux下默认不能解压windows下的.rar文件,需要安装一个工具

2.初次使用需要安装: yum install -y zip

3.语法:zip 2.txt.zip 2.txt

4.各压缩工具 的压缩能力 ,取决于文件内容,不同的文件内容压缩的结果对比不一样。

5.压缩目录需要加上-r 选项,也可以同时压缩文件
这里写图片描述

6.zip工具压缩完后,原来的文件不会删除
这里写图片描述
7.解压缩命令unzip 这个也是需要安装的
yum install -y unzip

8.在使用unzip时 会出现以下提示 因为原文件没有删除
这里写图片描述
replace:取代 、替换
ename :重命名
A :表示全部

9.zip解压也可以指定解压到什么地方。不指定地方就在当前目录下。使用-d选项
这里写图片描述
解压时不能重命名 即使指定了它的新名字 也会自己生成一个新名字的目录。文件名不能改

10.zip压缩后的文件是不能查看里面的内容
只能查看文件列表-l 选项
这里写图片描述


6.6 tar打包
1.在传输一个目录或者多个目录加文件时,不使用打包工具会传输很慢,比较耗费带宽。

2.带宽:带宽就是单位时间内的最大数据流量,也可以说是单位时间内最大可能提供多少个二进制位传输。例如100M带宽所指的是100Mbps
bps是指(bits per second 比特/秒)。
一字节=8bp (比特)
100Mbps除以8就是,1秒钟最多能达到12.5MB的速度。

3.打包目录
这里写图片描述

这里写图片描述
目录文件一起打包

-c 创建
v 可视化
f 后面跟tar名字 可以指定打包名
如果已经存在打包后的文件 再次打包该文件不会有提示 ,直接覆盖旧文件

4.解包时 也会覆盖原来的文件或者目录 不会有任何提示
这里写图片描述

5.查看打包后的文件列表
这里写图片描述

6.打包过滤-exclude后面 指定某些目录或者文件不进行打包

tar包名字一定要和-f 连在 一起
例:1
tar -cvf aminglinux.tar –exclude aming1 aminglinux 3.txt 4.txt
过滤aming1子目录

2
tar -cvf aminglinux.tar –exclude aming1 –exclude 2.txt aminglinux 3.txt 4.txt
同时过滤aming1目录以及2.txt文件
3
tar -cvf aminglinux.tar –exclude aming1 –exclude “*.txt” aminglinux 3.txt 4.txt
同时过滤aming1目录 以及所有的txt文件


6.7 打包并压缩
1.tar在打包的时候是支持压缩的 gzip ,bzip2 ,xz ,都可以在tar中使用

-打包并使用gzip压缩
tar -czvf aminglinux.tar.gz aminglinux 3.txt 4.txt

-打包并使用bzip2压缩
tar -cjvf aminglinux.tar.bz2
aminglinux 3.txt 4.txt

-打包并使用xz压缩
tar -cJvf aminglinux.tar.xz
aminglinux 3.txt 4.txt

解包解压缩 其中的c选择换成x选项

查看文件列表 使用tar -tf


1
-cvf 创建一个tar包
-xvf 解包
-tf 查看包内容
- - exclude 排除指定的文件或者目录 支持通配

2.打包并使用gzip压缩
-zcvf
解包
-zxvf
打包并使用bzip2压缩
-jcvf
解包
-jxvf
打包并使用xz压缩
-Jcvf
解包
-Jxvf


在linux下最常见的压缩文件通常都是以.tar.gz 为结尾的,除此之外还有.tar, .gz, .bz2, .zip等等。以前也介绍过linux系统中的后缀名其实要不要无所谓,但是对于压缩文件来讲必须要带上。这是为了判断压缩文件是由哪种压缩工具所压缩,而后才能去正确的解压缩这个文件。以下介绍常见的后缀名所对应的压缩工具。

.gz gzip 压缩工具压缩的文件

.bz2 bzip2 压缩工具压缩的文件

.tar tar 打包程序打包的文件(tar并没有压缩功能,只是把一个目录合并成一个文件)

.tar.gz 可以理解为先用tar打包,然后再gzip压缩
.tar.bz2 同上,先用tar打包,然后再bzip2压缩

gzip压缩工具

语法: gzip [-d#] filename 其中#为1-9的数字

“-d” : 解压缩时使用

“-#” : 压缩等级,1压缩最差,9压缩最好,6为默认

[root@localhost ~]# [ -d test ] && rm -rf test
[root@localhost ~]# mkdir test
[root@localhost ~]# mv test.txt test
[root@localhost ~]# cd test
[root@localhost test]# ls
test.txt
[root@localhost test]# gzip test.txt
[root@localhost test]# ls
test.txt.gz
你对第一条命令也许很陌生,其实这是两条命令,[ ] 内是一个判断,”-d test” 判断test目录是否存在,’&&’ 为一个连接命令符号,当前面的命令执行成功后,后面的命令才执行。关于这两块内容阿铭会在后面详细讲解。gzip 后面直接跟文件名,就在当前目录下把该文件压缩了,而原文件也会消失。

[root@localhost test]# gzip -d test.txt.gz
[root@localhost test]# ls
test.txt
“gzip -d” 后面跟压缩文件,会解压压缩文件。gzip 是不支持压缩目录的。

[root@localhost ~]# gzip test
gzip: test is a directory – ignored
[root@localhost ~]# ls test
test/ test1 test2/ test3
[root@localhost ~]# ls test
test.txt
至于 “-#” 选项,平时很少用,使用默认压缩级别足够了。

bzip2压缩工具

语法: bzip2 [-dz] filename

bzip2 只有两个选项需要你掌握。

“-d” : 解压缩

“-z” : 压缩

压缩时,可以加 “-z” 也可以不加,都可以压缩文件,”-d” 则为解压的选项:

[root@localhost ~]# cd test
[root@localhost test]# bzip2 test.txt
[root@localhost test]# ls
test.txt.bz2
[root@localhost test]# bzip2 -d test.txt.bz2
[root@localhost test]# bzip2 -z test.txt
[root@localhost test]# ls
test.txt.bz2
bzip2 同样也不可以压缩目录:

[root@localhost test]# cd ..
[root@localhost ~]# bzip2 test
bzip2: Input file test is a directory.
tar压缩工具

tar 本身为一个打包工具,可以把目录打包成一个文件,它的好处是它把所有文件整合成一个大文件整体,方便拷贝或者移动。

语法:tar [-zjxcvfpP] filename tar 命令有多个选项,其中不常用的阿铭做了标注。

“-z” : 同时用gzip压缩

“-j” : 同时用bzip2压缩

“-x” : 解包或者解压缩

“-t” : 查看tar包里面的文件

“-c” : 建立一个tar包或者压缩文件包

“-v” : 可视化

“-f” : 后面跟文件名,压缩时跟 “-f 文件名”,意思是压缩后的文件名为filename, 解压时跟 “-f 文件名”,意思是解压filename. 请注意,如果是多个参数组合的情况下带有 “-f”,请把 “-f” 写到最后面。

“-p” : 使用原文件的属性,压缩前什么属性压缩后还什么属性。(不常用)

“-P” : 可以使用绝对路径。(不常用)

–exclude filename : 在打包或者压缩时,不要将filename文件包括在内。(不常用)

[root@localhost ~]# cd test
[root@localhost test]# mkdir test111
[root@localhost test]# touch test111/test2.txt
[root@localhost test]# echo “nihao” > !echo “nihao” > test111/test2.txt  
[root@localhost test]# ls  
test111  test.txt.bz2  
[root@localhost test]# tar -cvf test111.tar test111  
test111/  
test111/test2.txt  
[root@localhost test]# ls  
test111  test111.tar  test.txt.bz2  
首先在test目录下建立test111目录,然后在test111目录下建立test2.txt, 并写入 “nihao” 到test2.txt中,接着是用tar把test111打包成test111.tar. 请记住 “-f” 参数后跟的是打包后的文件名, 然后再是要打包的目录或者文件。tar 打包后,原文件不会消失,而依旧存在。在上例中,阿铭使用一个特殊的符号 ”!
” 它表示上一条命令的最后一个参数,比如在本例中,它表示”test111/test2.txt”. tar 不仅可以打包目录也可以打包文件,打包的时候也可以不加 “-v” 选项表示,表示不可视化。

[root@localhost test]# rm -f test111.tar
[root@localhost test]# tar -cf test.tar test111 test.txt.bz2
[root@localhost test]# ls
test111 test.tar test.txt.bz2
删除原来的test111目录,然后解包test.tar,不管是打包还是解包,原来的文件是不会删除的,而且它会覆盖当前已经存在的文件或者目录。

[root@localhost test]# rm -rf test111
[root@localhost test]# ls
test.tar test.txt.bz2
[root@localhost test]# tar -xvf test.tar
test111/
test111/test2.txt
test.txt.bz2
打包的同时使用gzip压缩

tar命令非常好用的一个功能就是可以在打包的时候直接压缩,它支持gzip压缩和bzip2压缩。

[root@localhost test]# tar -czvf test111.tar.gz test111
test111/
test111/test2.txt
[root@localhost test]# ls
test111 test111.tar.gz test.tar test.txt.bz2
“-tf” 可以查看包或者压缩包的文件列表:

[root@localhost test]# tar -tf test111.tar.gz
test111/
test111/test2.txt
[root@localhost test]# tar -tf test.tar
test111/
test111/test2.txt
test.txt.bz2
“-zxvf” 用来解压.tar.gz的压缩包

[root@localhost test]# rm -rf test111
[root@localhost test]# ls
test111.tar.gz test.tar test.txt.bz2
[root@localhost test]# tar -zxvf test111.tar.gz
test111/
test111/test2.txt
[root@localhost test]# ls
test111 test111.tar.gz test.tar test.txt.bz2
打包的同时使用bzip2压缩

和gzip压缩不同的是,这里使用 “-cjvf” 选项来压缩

[root@localhost test]# tar -cjvf test111.tar.bz2 test111
test111/
test111/test2.txt
[root@localhost test]# ls
test111 test111.tar.bz2 test111.tar.gz test.tar test.txt.bz2
同样可以使用 “-tf” 来查看压缩包文件列表

[root@localhost test]# tar -tf test111.tar.bz2
test111/
test111/test2.txt
解压.tar.bz2 的压缩包也很简单

[root@localhost test]# tar -jxvf test111.tar.bz2
test111/
test111/test2.txt
下面介绍一下 –exclude 这个选项的使用,因为在日常的管理工作中你也许会用到它。

[root@localhost test]# tar -cvf test111.tar –exclude test3.txt test111
test111/
test111/test4.txt
test111/test2.txt
test111/test5/
请注意上条命令中,test111.tar 是放到了 –exclude 选项的前面。除了可以排除文件,也可以排除目录:

[root@localhost test]# rm -f test111.tar
[root@localhost test]# tar -cvf test111.tar –exclude test5 test111
test111/
test111/test4.txt
test111/test3.txt
test111/test2.txt

原创粉丝点击