Linux之zip命令

  • 命令概述

zip 命令用于压缩文件;

  • 命令语法
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
  • 命令参数
  • -b

    指定存放文件的目录;

    -d

    删除压缩文件内指定的文件;

    -f

    更新压缩包的文件;

    -m

    将文件压缩并加入压缩文件后,删除原始文件;

    -q

    不显示执行过程;

    -r

    递归处理,将指定目录下的所有文件和子目录一并处理;

    -v

    显示指令执行过程或显示版本信息(默认选项);

    -sf

    显示文件;

  • 命令示例

1、zip test1.zip test1.txt压缩一个名为test.zip的文件;

[root@localhost test]# zip test1.zip test1.txt   
  adding: test1.txt (deflated 31%)
[root@localhost test]# ll
total 20
-rw-r--r-- 1 root root  13 Jul 16 14:15 test1.txt
-rw-r--r-- 1 root root 177 Jul 17 14:01 test1.zip

 2、zip -r test2.zip test2-r 表示将指定目录下的所有文件和子目录一并处理;

[root@localhost test]# zip test2.zip test2  --不加r,压缩文件夹
  adding: test2/ (stored 0%)
[root@localhost test]# zip -sf test2.zip    --只对文件夹压缩
Archive contains:
  test2/
Total 1 entries (0 bytes)
[root@localhost test]# zip -r test2.zip test2 --加r,压缩文件夹
updating: test2/ (stored 0%)
  adding: test2/test1.txt (deflated 31%)
[root@localhost test]# zip -sf test2.zip  --指定目录下的所有文件和子目录一并处理
Archive contains:
  test2/
  test2/test1.txt
Total 1 entries (89 bytes)

3、zip -sf test2.zip -sf表示查看压缩内的文件(zipinfo -1 test2.zip 效果一样);

[root@localhost test]# zip -sf test2.zip  --查看压缩包内的文件
Archive contains:
  test2/
  test2/test1.txt
Total 1 entries (89 bytes)

4、zip -d test2.zip test2/test1.txt -d表示删除压缩文件内指定的文件(或者当压缩是目录时,指定目录下的文件);

[root@localhost test]# zip -d test2.zip test2/test1.txt --加-d 删除压缩内的文件
deleting: test2/test1.txt

5、zip -q test4.zip test4.txt -q 表示压缩文件或文件夹不显示执行过程;

[root@localhost test]# zip -q test4.zip test4.txt  -- 加-q 不显示执行过程
[root@localhost test]# ll
total 28
-rw-r--r-- 1 root root   0 Jul 16 14:25 test4.txt
-rw-r--r-- 1 root root 168 Jul 17 14:25 test4.zip

6、zip -m test5.zip test5.txt -m表示将文件压缩并加入压缩文件后,删除原始文件(zip -rm test5.zip test5 对文件压缩后删除需要加 -rm );

[root@localhost test]# ll
-rw-r--r-- 1 root root  50 Jul 16 14:50 test5.txt
[root@localhost test]# zip -m test5.zip test5.txt --加-m 加入压缩文件后,删除原始文件; 
  adding: test5.txt (deflated 4%)
[root@localhost test]# ll
-rw-r--r-- 1 root root 216 Jul 17 14:28 test5.zip

版权声明:本文为m0_57719465原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。