在Linux中,压缩文件的扩展名主要是:[*.tar,*.tar.gz,*.tgz,*.gz,*.Z,*.bz2],虽然,咱们知道,在LInux中,文件的扩展名没有什么做用,可是因为在Linux中支持的压缩命令很是的多,为了在解压缩的时候知道用那种方式来解压缩,就须要带上特定的扩展名。linux
扩展名对应的压缩方式:spa
◇*.Z:compress程序压缩的文件 code
◇*.gz:gzip程序压缩的文件server
◇*.bz2:bzip2程序压缩的文件blog
◇*.tar:tar程序打包的数据,并无压缩过ip
◇*.tar.gz:tar程序打包的文件,而且通过gzip程序的压缩开发
◇*.tar.bz2:tar程序打包的文件,而且通过bzip2程序的压缩it
其中compress是比较老的一款压缩软件,已经被gzip取代了,用gzip也能够解开compress压缩的文件。io
可是因为这些压缩方式都只能针对单个文件。这时候打包软件[tar]就显得很重要了。class
用法:compress [-rcV] 文件或目录 ← 压缩命令
默认状况下,压缩后的文件后缀为.Z,而且压缩前的源文件会消失。
-r:能够连同目录下的文件也同时给与压缩
-c:将压缩数据输出成为standard output(输出到屏幕)
-v:能够秀出压缩后的文件资讯以及压缩过程当中的一些档名变化
默认状况下,咱们的Centos中并没用安装compress这个软件,咱们能够用下面的命令来在线安装
yum install ncompress
实例:
[root@localhost stu]# ll 总用量 12 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:45 lin -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:45 lin1 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:46 lin2 [root@localhost stu]# compress -v lin lin: -- replaced with lin.Z Compression: 27.95% [root@localhost stu]# ll 总用量 12 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:45 lin1 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:46 lin2 -rw-r--r--. 1 fuwh fuwh 2064 7月 26 10:45 lin.Z
若是想要压缩后的文件还保留的话,能够用[-c]选项
[root@localhost stu]# ll 总用量 12 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:45 lin1 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:46 lin2 -rw-r--r--. 1 fuwh fuwh 2064 7月 26 10:45 lin.Z [root@localhost stu]# compress -c lin1 > lin11.Z [root@localhost stu]# ll 总用量 16 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:45 lin1 -rw-r--r--. 1 root root 2064 7月 26 10:58 lin11.Z -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:46 lin2 -rw-r--r--. 1 fuwh fuwh 2064 7月 26 10:45 lin.Z [root@localhost stu]#
若是要解压缩的话,就用uncompress命令
[root@localhost stu]# ll 总用量 16 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:45 lin1 -rw-r--r--. 1 root root 2064 7月 26 10:58 lin11.Z -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:46 lin2 -rw-r--r--. 1 fuwh fuwh 2064 7月 26 10:45 lin.Z [root@localhost stu]# uncompress lin11.Z [root@localhost stu]# ll 总用量 16 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:45 lin1 -rw-r--r--. 1 root root 2865 7月 26 10:58 lin11 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:46 lin2 -rw-r--r--. 1 fuwh fuwh 2064 7月 26 10:45 lin.Z [root@localhost stu]#
使用gzip压缩的文件扩展名为.gz,还能够解开copmress压缩的文件。
使用方法:gzip [-cdtv#] 文件名
-c:将压缩的数据输出到屏幕上
-d:解压缩的参数,也可使用gunzip命令
-t:能够用来检验压缩档的一致性,看看文件有无错误
-v:能够显示出文件的压缩比率
-#:压缩等级,-1最快,可是压缩比最差,-9最慢,可是压缩比最好,默认是-6
zcat能够用来读取压缩后的文件内容
实例:
[root@localhost stu]# ll 总用量 16 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:45 lin -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:45 lin1 -rw-r--r--. 1 root root 2865 7月 26 10:58 lin11 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:46 lin2 [root@localhost stu]# gzip -v lin lin: 45.1% -- replaced with lin.gz [root@localhost stu]# ll 总用量 16 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:45 lin1 -rw-r--r--. 1 root root 2865 7月 26 10:58 lin11 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:46 lin2 -rw-r--r--. 1 fuwh fuwh 1596 7月 26 10:45 lin.gz [root@localhost stu]# gzip -c lin2 > lin2.gz [root@localhost stu]# ll 总用量 20 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:45 lin1 -rw-r--r--. 1 root root 2865 7月 26 10:58 lin11 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:46 lin2 -rw-r--r--. 1 root root 1597 7月 26 11:13 lin2.gz -rw-r--r--. 1 fuwh fuwh 1596 7月 26 10:45 lin.gz [root@localhost stu]# zcat lin.gz ■◇□※ ■terterm 连不上的缘由:防火墙未关闭 ....后面内容省略.... [root@localhost stu]# gunzip lin2.gz gzip: lin2 already exists; do you wish to overwrite (y or n)? y [root@localhost stu]# gzip -d lin.gz [root@localhost stu]# ll 总用量 16 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:45 lin -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:45 lin1 -rw-r--r--. 1 root root 2865 7月 26 10:58 lin11 -rw-r--r--. 1 root root 2865 7月 26 11:13 lin2 [root@localhost stu]#
bzip2的压缩比率比gzip还要好,用法和gzip基本上同样。压缩的后缀名为.bz2。
用法:bzip2 [-cdkzv#] 文件名
-c:将压缩的过程产生的数据输出到屏幕上
-d:解压缩的参数,也可使用bunzip2来解压缩。
-k:保留原始文件
-z:压缩的参数
-v:能够显示压缩比率
-#:与gzip同样压缩比参数
可使用bzcat来读取压缩后的文件。
实例:
[root@localhost stu]# ll 总用量 16 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:45 lin -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:45 lin1 -rw-r--r--. 1 root root 2865 7月 26 10:58 lin11 -rw-r--r--. 1 root root 2865 7月 26 11:13 lin2 [root@localhost stu]# bzip2 -zv lin lin: 1.694:1, 4.722 bits/byte, 40.98% saved, 2865 in, 1691 out. [root@localhost stu]# ll 总用量 16 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:45 lin1 -rw-r--r--. 1 root root 2865 7月 26 10:58 lin11 -rw-r--r--. 1 root root 2865 7月 26 11:13 lin2 -rw-r--r--. 1 fuwh fuwh 1691 7月 26 10:45 lin.bz2 [root@localhost stu]# bzip2 -cvk lin2 > lin2.bz2 lin2: 1.694:1, 4.722 bits/byte, 40.98% saved, 2865 in, 1691 out. [root@localhost stu]# ll 总用量 20 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:45 lin1 -rw-r--r--. 1 root root 2865 7月 26 10:58 lin11 -rw-r--r--. 1 root root 2865 7月 26 11:13 lin2 -rw-r--r--. 1 root root 1691 7月 26 11:27 lin2.bz2 -rw-r--r--. 1 fuwh fuwh 1691 7月 26 10:45 lin.bz2 [root@localhost stu]# bzip2 -d lin.bz2 [root@localhost stu]# ll 总用量 20 -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:45 lin -rw-r--r--. 1 fuwh fuwh 2865 7月 26 10:45 lin1 -rw-r--r--. 1 root root 2865 7月 26 10:58 lin11 -rw-r--r--. 1 root root 2865 7月 26 11:13 lin2 -rw-r--r--. 1 root root 1691 7月 26 11:27 lin2.bz2 -rw-r--r--. 1 root root 0 7月 26 11:27 lin3 [root@localhost stu]#
前面的压缩命令都是对单个文件的压缩,tar命令主要用来将多个文件或目录打包成一个大文件。
使用方法:
tar [-j | -z] [cv] [-f 建立的档名] filename <-- 打包与压缩
tar [-j | -z] [tv] [-f 建立的档名] <-- 查看档名
tar [-j | -z] [xv] [-f 建立的档名] [-C 目录] 解压缩
-c:建立打包文件,可搭配-v来查看过程当中被打包的档名
-t:查看打包文件的内容含有哪些档名
-x:解打包或解压缩的功能,可搭配-C在特定目录中解开,须要注意,-c,-t,-x不能一块儿出现
-j:经过bizp2的支持来进行压缩/解压缩,此时的档名为*.tar.bz2
-z:经过gzip的支持来进行压缩/解压缩,此时的档名为*.tar.gz
-v:在压缩/解压缩的过程当中,将正在处理的档名显示出来
-f filename:-f后面要马上接要被处理的档名。因此若是和其余选项一块儿写的时候,须要写在最后面。
-C 目录:在解压缩的时候指定解压缩的目录
例子:
[root@localhost fuwh]# ll 总用量 8 drwxrwxr-x. 2 fuwh fuwh 4096 7月 26 11:55 stu drwxrwxr-x. 3 fuwh fuwh 4096 7月 25 10:27 stu2 [root@localhost fuwh]# tar -zcv -f stu2.tar.gz stu2 stu2/ stu2/stu22/ stu2/stu22/aa.txt [root@localhost fuwh]# tar -jcv -f stu2.tar.bz2 stu stu/ stu/lin stu/lin11 stu/lin1 stu/lin2 stu/lin3 [root@localhost fuwh]# ll 总用量 16 drwxrwxr-x. 2 fuwh fuwh 4096 7月 26 11:55 stu drwxrwxr-x. 3 fuwh fuwh 4096 7月 25 10:27 stu2 -rw-r--r--. 1 root root 2439 7月 26 11:57 stu2.tar.bz2 -rw-r--r--. 1 root root 165 7月 26 11:57 stu2.tar.gz [root@localhost fuwh]#
一般,咱们在网上下一些开发用的软件,都是tar.gz格式的。好比:
这个时候,咱们就能够用 tar -zxvf 文件名 来解压缩了。
[root@localhost data]# ll 总用量 53424 -rw-r--r--. 1 fuwh fuwh 54703519 7月 26 12:02 server-jre-8 u141-linux-x64.tar.gz [root@localhost data]# tar -zxvf server-jre-8u141-linux-x64 .tar.gz jdk1.8.0_141/ jdk1.8.0_141/THIRDPARTYLICENSEREADME.txt jdk1.8.0_141/lib/ jdk1.8.0_141/lib/jexec 。。。中略。。。 [root@localhost data]# ll 总用量 53428 drwxr-xr-x. 7 uucp 143 4096 7月 12 20:45 jdk1.8.0_141 -rw-r--r--. 1 fuwh fuwh 54703519 7月 26 12:02 server-jre-8 u141-linux-x64.tar.gz [root@localhost data]#