6.1压缩打包介绍
使用压缩文件不仅可以节省磁盘空间,而且在传输时还能节省网络带宽。
Linux下常见的后缀名所对应的压缩工具:
.gz:表示由gzip压缩工具压缩的文件
.bz2:表示由bzip2压缩工具压缩的文件
.tar:表示由tar打包程序打包的文件(tar并没有压缩功能,只是把一个目录合并成一个文件)。
.tar.gz:可以理解为先由tar打包,然后由gzip压缩
.tar.bz2:可以理解为先由tar打包,然后由bzip2压缩
.tar.xz:可以理解为先由tar打包,然后由xz压缩
6.2gzip压缩工具
不支持压缩目录,命令格式:gzip[-d#] filename,其中#为1-9数字。
实例:[root@localhost test]# gzip 1.txt
[root@localhosttest]# ls
1. txt.gz 2.txt
在当前的目录下压缩该文件,原文件消失
参数:
-d:该参数在解压缩时使用。
[root@localhosttest]# gzip -d 1.txt.gz
[root@localhosttest]# ls
1. txt 2.txt
gzip –d 表示解压缩文件。
-#:表示压缩等级,1为最差,9为最好,6为默认。
zcat压缩文件名:表示查看压缩文件
实例:[root@localhost test]# zcat 1.txt.gz
sdfds
file压缩文件名:表示查看压缩文件的格式属性
实例:[root@localhost test]# file !$
file1.txt.gz
1. txt.gz: gzip compresseddata, was "1.txt", from Unix, last modified: Sun May 27 12:22:43 2018
du –sh压缩文件名:查看压缩文件大小
[root@localhosttest]# du -sh !$
du-sh 1.txt.gz
4.0K 1.txt.gz
gzip–c 1.txt > /tmp/1.txt.bz2:表示指定压缩文件路径,并且源文件存在
实例:[root@localhost test]# ls
1.txt 2.txt
[root@localhosttest]# gzip -c 1.txt > /tmp/1.txt.bz2
[root@localhosttest]# cd ..
[root@localhosttmp]# ls
1. txt.bz2
gzip–d –c /tmp/1.txt.bz2 > /tmp/test/1.txt表示:解压文件到指定路径下,并且源文件存在。
[root@localhosttmp]# gzip -d -c /tmp/1.txt.bz2 > /tmp/test/1.txt
[root@localhosttmp]# ls
1.txt.bz2
8
d6z
newdisk
systemd-private-2a0ef15f4611415292c5dad4691fc9db-chronyd.service-1LKyXo
systemd-private-2a0ef15f4611415292c5dad4691fc9db-cups.service-9K3k03
systemd-private-2a0ef15f4611415292c5dad4691fc9db-vgauthd.service-WZFAfS
systemd-private-2a0ef15f4611415292c5dad4691fc9db-vmtoolsd.service-fdgU2W
test
[root@localhosttmp]# ls /tmp/test/
1.txt 2.txt
[root@localhosttmp]# ls
1. txt.bz2
解压到了/tmp/test/目录下了,而且/tmp/目录下源文件存在。
6.3bzip2压缩工具
不能压缩目录,命令格式:bzip2 [-dz] filename ,它只有-z(压缩)和-d(解压缩)两个常用选项。压缩级别有1-9,默认级别9.加不加-z都可以压缩文件.
实例:[root@localhost test]# bzip2 1.txt
[root@localhosttest]# ls
1.txt.bz2
[root@localhosttest]# bzip2 -d 1.txt.bz2
[root@localhosttest]# ls
1. txt
相关用法和gzip相同
6.4xz压缩工具
默认级别是6,同样不能压缩目录。命令格式:xz [-dz]filename. 加不加-z都可以压缩文件.
实例:
[root@localhosttest]# xz 1.txt
[root@localhosttest]# ls
1.txt.xz
[root@localhosttest]# xz -d 1.txt.xz
[root@localhosttest]# ls
1. txt
相关用法和gzip类似
6.5zip压缩工具
可以压缩目录和文件,压缩目录时,需要指定目录下的文件,压缩后源文件存在。
先安装zip:yum install –y zip
命令格式:zip 目标文件名 需要压缩的文件或者目录。
实例:[root@localhost test]# zip 1.txt.zip 1.txt
adding: 1.txt (stored 0%)
[root@localhosttest]# ls
1. txt 1.txt.zip
如果想要一并压缩二级目录下的文件,必须加上-r选项。
实例:
[root@localhosttmp]# zip -r test.zip test/
adding: test/ (stored 0%)
adding: test/2.txt (stored 0%)
adding: test/1.txt (stored 0%)
adding: test/test1/ (stored 0%)
查看一并压缩后的:
[root@localhosttmp]# unzip -l test.zip
Archive: test.zip
Length Date Time Name
--------- ---------- ----- ----
0 05-27-2018 13:17 test/
0 05-27-2018 13:04 test/2.txt
6 05-27-2018 12:42 test/1.txt
0 05-27-2018 13:17 test/test1/
--------- -------
6 4 files
没有cat命令,不可查看文件内容,只可查看文件列表
6.6tar打包
tar本身是一个打包工具,打包不是压缩,可以把目录打包成一个文件,把所有文件整合成一个大文件,方便复制或移动管理。打包后原文件存在。
命令格式tar[-zjxcvfpP]filename tar
参数:
-z:表示同时用gzip压缩
-j:表示同时用bzip压缩
-J:表示同时用xz压缩
-x:表示解包或者解压缩
-t:表示查看tar包里面的文件
-c:表示建立一tar包或者压缩文件包
-v:表示可视化
-f:后面跟文件名,表示压缩后的文件名,或者解压文件名,需要注意的是,如果多个参数组合情况下,请把-f参数写到最后面。
--excludefilename:表示打包或压缩时,不要将filename文件包括在内。(不常用)
使用tar命令把test111打包成test111.tar:
[root@localhosttest]# tar -cvf test111.tar test111
test111/
test111/2.txt
[root@localhosttest]# ls
1.txt test1 test111 test111.tar
[root@localhosttest]#
打包后源文件存在。
tar不仅可以打包目录,也可以打包文件:
[root@localhosttest]# rm -f test111.tar
[root@localhosttest]# tar -cf test.tar test111 1.txt
[root@localhosttest]# ls
1. txt test1 test111 test.tar
查看上述命令打包好的文件:
[root@localhost test]# tar -tvftest.tar
drwxrwxrwx root/root 0 2018-05-27 13:53 test111/
-rw-r--r-- root/root 6 2018-05-27 13:53 test111/2.txt
-rwxrwxrwx root/root 6 2018-05-27 12:42 1.txt
不管是打包还是解包,原来的文件是不会删除的,而且会覆盖当前已存在的文件或者目录。
[root@localhost test]# rm -rf test111
[root@localhost test]# ls
1.txt test1 test.tar
[root@localhost test]# tar -xvftest.tar
test111/
test111/2.txt
1.txt
[root@localhost test]# ls
1. txt test1 test111 test.tar
上述命令,先删除了原来的test111目录,然后解包test.tar
--exclude用法:
[root@localhosttest]# tar -cvf test111.tar --exclude 1.txt test111
test111/
test111/2.txt
上述命令中,建立一个打包文件test111.tar,没有将/test111/1.txt打包。
除了排除文件,还可以排除目录:
[root@localhosttest]# mkdir test111/test222
[root@localhosttest]# tar -cvf test111.tar --exclude test222 test111
test111/
test111/2.txt
6.7打包并压缩
Tar命令打包的同时还可以直接压缩,它支持gzip压缩,bzip2压缩,xz压缩。使用-z选项,可以压缩成gzip格式的文件。使用-j选项,可以压缩成bzip2格式的文件。使用-J选项,可以压缩成xz格式的文件。
实例:
[root@localhosttest]# tar -czvf test111.tar.gz test111
test111/
test111/2.txt
test111/test222/
[root@localhosttest]# ls
1. txt test1 test111 test111.tar test111.tar.gz test.tar
使用-tf可以查看包或者压缩包的文件列表:
[root@localhost test]# tar -tftest111.tar.gz
test111/
test111/2.txt
test111/test222/
使用-zxvf,可以解压.tar.gz格式的压缩包:
[root@localhost test]# rm -rf test111
[root@localhost test]# ls
1.txt test1 test111.tar test111.tar.gz test.tar
[root@localhost test]# tar -zxvftest111.tar.gz
test111/
test111/2.txt
test111/test222/
[root@localhost test]# ls
1. txt test1 test111 test111.tar test111.tar.gz test.tar