命令 zip 用于压缩文件使用,zip压缩格式是Windows与Linux等多平台通用的压缩格式
2020-09-27 09:27:50
卿卿
17
在 Linux 系统中,命令 zip 用于压缩文件使用,zip压缩格式是Windows与Linux等多平台通用的压缩格式。和gzip命令相比,zip命令 压缩文件后不仅不会删除源文件,而且还可以压缩目录。
语法:
zip [选项] [文件或目录]
参数说明:
-r 将指定目录下的所有文件和目录一起压缩
-x 压缩文件时排除某个文件
-q 不显示压缩信息
案例:
压缩test.py文件
[root@VM_0_5_centos ~]# cd test [root@VM_0_5_centos test]# ls test01.py test.log test.py [root@VM_0_5_centos test]# zip testpy.zip ./test.py adding: test.py (stored 0%) [root@VM_0_5_centos test]# ls test01.py test.log test.py testpy.zip [root@VM_0_5_centos test]#
压缩目录
[root@VM_0_5_centos test]# mkdir test01 [root@VM_0_5_centos test]# ls test01 test01.py test.log test.py testpy.zip [root@VM_0_5_centos test]# zip test01.zip ./test01 adding: test01/ (stored 0%) [root@VM_0_5_centos test]# ls test01 test01.py test01.zip test.log test.py testpy.zip [root@VM_0_5_centos test]#
排除压缩 (排除test.log文件)
[root@VM_0_5_centos test]# ls test01 test01.py test01.zip test.log test.py testpy.zip [root@VM_0_5_centos test]# zip -r all.zip ./* -x test.log adding: test01/ (stored 0%) adding: test01.py (stored 0%) adding: test01.zip (stored 0%) adding: test.py (stored 0%) adding: testpy.zip (stored 0%) [root@VM_0_5_centos test]# ls all.zip test01 test01.py test01.zip test.log test.py testpy.zip [root@VM_0_5_centos test]# rm -rf test01 test01.py test01.zip test.log test.py testpy.zip # 除了压缩文件,删除其他文件 [root@VM_0_5_centos test]# unzip all.zip # 解压文件看是否有test.log文件 Archive: all.zip creating: test01/ extracting: test01.py extracting: test01.zip extracting: test.py extracting: testpy.zip [root@VM_0_5_centos test]# ls all.zip test01 test01.py test01.zip test.py testpy.zip # 解压后没有test.log文件 [root@VM_0_5_centos test]#