Linux命令 - zip命令
Linux中 zip 命令是个使用广泛的压缩程序,文件经它压缩后会另外产生具有".zip"扩展名的压缩文件。
1.语法:
zip [参数] [文件或目录]
2.功能:
可以压缩文件且保留源文件。
3.参数:
-A 调整可执行的自动解压缩文件。
-b<工作目录> 指定暂时存放文件的目录。
-c 替每个被压缩的文件加上注释。
-d 从压缩文件内删除指定的文件。
-D 压缩文件内不建立目录名称。
-f 此参数的效果和指定"-u"参数类似,但不仅更新既有文件,如果某些文件原本不存在于压缩文件内,使用本参数会一并将其加入压缩文件中。
-F 尝试修复已损坏的压缩文件。
-g 将文件压缩后附加在既有的压缩文件之后,而非另行建立新的压缩文件。
-h 在线帮助。
-i<范本样式> 只压缩符合条件的文件。
-j 只保存文件名称及其内容,而不存放任何目录名称。
-J 删除压缩文件前面不必要的数据。
-k 使用MS-DOS兼容格式的文件名称。
-l 压缩文件时,把LF字符置换成LF+CR字符。
-ll 压缩文件时,把LF+CR字符置换成LF字符。
-L 显示版权信息。
-m 将文件压缩并加入压缩文件后,删除原始文件,即把文件移到压缩文件中。
-n<字尾字符串> 不压缩具有特定字尾字符串的文件。
-o 以压缩文件内拥有最新更改时间的文件为准,将压缩文件的更改时间设成和该文件相同。
-q 不显示指令执行过程。
-r 递归处理,将指定目录下的所有文件和子目录一并处理。
-S 包含系统和隐藏文件。
-t<日期时间> 把压缩文件的日期设成指定的日期。
-T 检查备份文件内的每个文件是否正确无误。
-u 更换较新的文件到压缩文件内。
-v 显示指令执行过程或显示版本信息。
-V 保存VMS操作系统的文件属性。
-w 在文件名称里假如版本编号,本参数仅在VMS操作系统下有效。
-x<范本样式> 压缩时排除符合条件的文件。
-X 不保存额外的文件属性。
-y 直接保存符号连接,而非该连接所指向的文件,本参数仅在UNIX之类的系统下有效。
-z 替压缩文件加上注释。
-$ 保存第一个被压缩文件所在磁盘的卷册名称。
-<压缩效率> 压缩效率是一个介于1-9的数值。
4.常用范例:
例一:将test目录下所有文件和文件夹打包为当前目录下的 test.zip
命令:zip -q -r test.zip /usr/games/test
生成的压缩文件是在当前打包的目录下。
[root@localhost ~]# ll
总用量 11
drwxr-xr-x. 2 root root 6 5月 12 14:29 公共
drwxr-xr-x. 2 root root 6 5月 12 14:29 模板
drwxr-xr-x. 2 root root 6 5月 12 14:29 视频
drwxr-xr-x. 2 root root 6 5月 12 14:29 图片
drwxr-xr-x. 2 root root 6 5月 12 14:29 文档
drwxr-xr-x. 2 root root 6 5月 12 14:29 下载
drwxr-xr-x. 2 root root 6 5月 12 14:29 音乐
drwxr-xr-x. 2 root root 6 5月 12 14:29 桌面
-rw-------. 1 root root 1247 5月 12 14:24 anaconda-ks.cfg
-rw-r--r--. 1 root root 1526 5月 12 14:29 initial-setup-ks.cfg
[root@localhost ~]# zip -q -r test.zip /usr/games/test
[root@localhost ~]# ll
总用量 12
drwxr-xr-x. 2 root root 6 5月 12 14:29 公共
drwxr-xr-x. 2 root root 6 5月 12 14:29 模板
drwxr-xr-x. 2 root root 6 5月 12 14:29 视频
drwxr-xr-x. 2 root root 6 5月 12 14:29 图片
drwxr-xr-x. 2 root root 6 5月 12 14:29 文档
drwxr-xr-x. 2 root root 6 5月 12 14:29 下载
drwxr-xr-x. 2 root root 6 5月 12 14:29 音乐
drwxr-xr-x. 2 root root 6 5月 12 14:29 桌面
-rw-------. 1 root root 1247 5月 12 14:24 anaconda-ks.cfg
-rw-r--r--. 1 root root 1526 5月 12 14:29 initial-setup-ks.cfg
-rw-r--r--. 1 root root 1670 5月 25 16:25 test.zip
zip -q -r test.zip *
[root@localhost test]# ll
总用量 24
-rw-r--r--. 1 root root 96 5月 25 15:10 color.sh
-rwxr--r--. 1 root root 96 5月 18 10:15 lncolor
---xr--r--. 1 root root 13 5月 25 10:58 log1.txt
-rwxrw-r-x. 1 root root 592 5月 14 17:00 log2.txt
-rwxr-x--x. 1 root root 655 5月 14 17:06 log3.txt
-rwxr--r--. 1 root root 52 5月 25 10:58 log.txt
[root@localhost test]# zip -q -r test.zip *
[root@localhost test]# ll
总用量 28
-rw-r--r--. 1 root root 96 5月 25 15:10 color.sh
-rwxr--r--. 1 root root 96 5月 18 10:15 lncolor
---xr--r--. 1 root root 13 5月 25 10:58 log1.txt
-rwxrw-r-x. 1 root root 592 5月 14 17:00 log2.txt
-rwxr-x--x. 1 root root 655 5月 14 17:06 log3.txt
-rwxr--r--. 1 root root 52 5月 25 10:58 log.txt
-rw-r--r--. 1 root root 1332 5月 25 16:29 test.zip
例二:从压缩文件 test.zip 中删除文件log.txt
命令:zip -dv test.zip log.txt
[root@localhost test]# zip -dv test.zip log.txt
1>1: updating: log.txt (deflated 29%)
例三:把/games目录下面的test目录压缩为test1.zip
命令: zip -r test1.zip test
[root@localhost games]# zip -r test1.zip test
adding: test/ (stored 0%)
adding: test/color.sh (deflated 47%)
adding: test/lncolor (deflated 47%)
adding: test/log1.txt (stored 0%)
adding: test/log2.txt (deflated 73%)
adding: test/log3.txt (deflated 79%)
adding: test/log.txt (deflated 29%)
adding: test/test.zip (stored 0%)
[root@localhost games]# ll
总用量 8
-rw-r--r--. 1 root root 96 5月 25 15:16 color.sh
drwxr-xr-x. 2 root root 116 5月 25 16:33 test
-rw-r--r--. 1 root root 3016 5月 25 16:36 test1.zip
例四:把/games目录下面的test文件夹和color.sh压缩成为test2.zip
命令: zip -r test2.zip test color.sh
[root@localhost games]# zip -r test2.zip test color.sh
adding: test/ (stored 0%)
adding: test/color.sh (deflated 47%)
adding: test/lncolor (deflated 47%)
adding: test/log1.txt (stored 0%)
adding: test/log2.txt (deflated 73%)
adding: test/log3.txt (deflated 79%)
adding: test/log.txt (deflated 29%)
adding: test/test.zip (stored 0%)
adding: color.sh (deflated 47%)
[root@localhost games]# ll
总用量 12
-rw-r--r--. 1 root root 96 5月 25 15:16 color.sh
drwxr-xr-x. 2 root root 116 5月 25 16:33 test
-rw-r--r--. 1 root root 3016 5月 25 16:36 test1.zip
-rw-r--r--. 1 root root 3211 5月 25 16:40 test2.zip
例五:将test目录打包成 test3.zip
命令: zip -r test3.zip ./test
压缩的是包括test目录及以下的文件和文件夹。
[root@localhost games]# zip -r test3.zip ./test
adding: test/ (stored 0%)
adding: test/color.sh (deflated 47%)
adding: test/lncolor (deflated 47%)
adding: test/log1.txt (stored 0%)
adding: test/log2.txt (deflated 73%)
adding: test/log3.txt (deflated 79%)
adding: test/log.txt (deflated 29%)
adding: test/test.zip (stored 0%)
[root@localhost games]# ll
总用量 16
-rw-r--r--. 1 root root 96 5月 25 15:16 color.sh
drwxr-xr-x. 2 root root 116 5月 25 16:33 test
-rw-r--r--. 1 root root 3016 5月 25 16:36 test1.zip
-rw-r--r--. 1 root root 3211 5月 25 16:40 test2.zip
-rw-r--r--. 1 root root 3016 5月 25 16:42 test3.zip