每天回顾linux命令(zip)

zip命令
zip命令可以用来解压缩文件,或者对文件进行打包操作。zip是个使用广泛的压缩程序,文件经它压缩后会另外产生具有“.zip”扩展名的压缩文件。注意新安装的环境可能没有该命令。注意要用which命令看看是否已经安装了该命令。

语法

zip (选项) (参数)

选项
不加参数:直接压缩

root@35d388cf883b:/home/wzm/testgzip# zip redis.zip  redis.conf
  adding: redis.conf (deflated 65%)
root@35d388cf883b:/home/wzm/testgzip# ls
aaa  redis.conf  redis.zip

-A:调整可执行的自动压缩文件。(默认带上)
-b 工作目录:指定暂时存放的文件的目录。如果不带上,暂时存放的文件的目录为当前工作目录

root@35d388cf883b:/home/wzm/testgzip# zip -b ../ redis.conf.zip redis.conf
  adding: redis.conf (deflated 65%)
root@35d388cf883b:/home/wzm/testgzip# ls
aaa  redis.conf  redis.conf.zip

-c:为每个被压缩的文件加上注释,ps:笔者没有找出注释在哪里。。。
-d:从压缩文件内删除指定的文件。

root@35d388cf883b:/home/wzm# zip testgzip.zip testgzip/ -r
  adding: testgzip/ (stored 0%)
  adding: testgzip/aaa (deflated 65%)
  adding: testgzip/testzip (stored 0%)
  adding: testgzip/redis.conf (deflated 65%)
root@35d388cf883b:/home/wzm# ls
go  redis-4.0.2  redis-4.0.2.tar.gz  testgzip  testgzip.zip
root@35d388cf883b:/home/wzm# zip testgzip.zip -d testgzip/aaa
deleting: testgzip/aaa
root@35d388cf883b:/home/wzm# cp testgzip.zip  testgzip
root@35d388cf883b:/home/wzm# cd testgzip
root@35d388cf883b:/home/wzm/testgzip# ls
aaa  redis.conf  testgzip.zip  testzip
root@35d388cf883b:/home/wzm/testgzip# unzip testgzip.zip
Archive:  testgzip.zip
   creating: testgzip/
 extracting: testgzip/testzip
  inflating: testgzip/redis.conf
root@35d388cf883b:/home/wzm/testgzip# cd testgzip
root@35d388cf883b:/home/wzm/testgzip/testgzip# ls
redis.conf  testzip
root@35d388cf883b:/home/wzm/testgzip/testgzip#

-D:压缩文件内不建立目录名称(压缩目标不能有子目录)

root@35d388cf883b:/home/wzm# zip  testgzip.zip testgzip -D

zip error: Nothing to do! (testgzip.zip)
root@35d388cf883b:/home/wzm# ls
go  redis-4.0.2  redis-4.0.2.tar.gz  testgzip
root@35d388cf883b:/home/wzm# zip  testgzip.zip testgzip
  adding: testgzip/ (stored 0%)
root@35d388cf883b:/home/wzm# ls
go  redis-4.0.2  redis-4.0.2.tar.gz  testgzip  testgzip.zip
root@35d388cf883b:/home/wzm#

-u:更新比较新的文件导压缩文件内(重复执行一个命令会默认带上-u)

root@35d388cf883b:/home/wzm/testgzip# zip testzip.zip testgzip -u
updating: testgzip/ (stored 0%)
root@35d388cf883b:/home/wzm/testgzip# zip testzip.zip testgzip
updating: testgzip/ (stored 0%)
root@35d388cf883b:/home/wzm/testgzip#

-f:在-u的基础上,但不仅更新已经有的文件,如果某些文件原来不存在于压缩文件内,使用该参数会一并将其家人压缩文件中。当加上文件的时候,也会自动加上

root@35d388cf883b:/home/wzm/testgzip# ls zip/
redis.conf  testzip
root@35d388cf883b:/home/wzm/testgzip# zip old.zip zip -r
  adding: zip/ (stored 0%)
  adding: zip/testzip (stored 0%)
  adding: zip/redis.conf (deflated 65%)
root@35d388cf883b:/home/wzm/testgzip# touch zip/addfile.txt
root@35d388cf883b:/home/wzm/testgzip# zip old.zip zip -r
updating: zip/ (stored 0%)
updating: zip/testzip (stored 0%)
updating: zip/redis.conf (deflated 65%)
  adding: zip/addfile.txt (stored 0%)

-F:尝试修复已损坏的压缩文件。
-g:将文件压缩后附加在已有的压缩文件之后,而非另外建立新的压缩文件。

root@35d388cf883b:/home/wzm/testgzip/ret# unzip old.zip
Archive:  old.zip
   creating: zip/
 extracting: zip/testzip
  inflating: zip/redis.conf
 extracting: zip/addfile.txt
root@35d388cf883b:/home/wzm/testgzip/ret# cd ..
root@35d388cf883b:/home/wzm/testgzip# ls
aaa  old.zip  redis.conf  ret  testzip  zip
root@35d388cf883b:/home/wzm/testgzip# zip ret/old.zip aaa -g
  adding: aaa (deflated 65%)
root@35d388cf883b:/home/wzm/testgzip# cd ret
root@35d388cf883b:/home/wzm/testgzip/ret# rm zip -r
root@35d388cf883b:/home/wzm/testgzip/ret# unzip old.zip
Archive:  old.zip
   creating: zip/
 extracting: zip/testzip
  inflating: zip/redis.conf
 extracting: zip/addfile.txt
  inflating: aaa
root@35d388cf883b:/home/wzm/testgzip/ret#

-i <范本样式>:只压缩符合条件的文件

root@35d388cf883b:/home/wzm/testgzip# zip zipi.zip zip -i *.txt -r
  adding: zip/addfile.txt (stored 0%)
root@35d388cf883b:/home/wzm/testgzip# ls zip/
addfile.txt  redis.conf  testzip
root@35d388cf883b:/home/wzm/testgzip#

-j:只保存文件名称及其内容,而不存放任何目录名称

root@35d388cf883b:/home/wzm# zip testgzip.zip testgzip/ -j

zip error: Nothing to do! (testgzip.zip)
root@35d388cf883b:/home/wzm# ll testgzip/
total 80
drwxr-xr-x 4 root root  4096 Nov 21 02:58 ./
drwxr-xr-x 5 root root  4096 Nov 21 02:10 ../
lrwxrwxrwx 1 root root    10 Nov 20 02:25 aaa -> redis.conf
-rw-r--r-- 1 root root 57764 Nov 20 01:26 redis.conf
drwxr-xr-x 3 root root  4096 Nov 21 02:31 ret/
-rw-r--r-- 1 root root     4 Nov 21 01:55 testzip
drwxr-xr-x 2 root root  4096 Nov 21 02:25 zip/
root@35d388cf883b:/home/wzm#

-J:删除压缩文件前面不必要的数据。
-k:使用 MS-DOS兼容格式的文件名称
-l:压缩文件时,把LF字符置换成LF+CR字符
-ll:压缩文件时,吧LF+CR字符置换成 LF字符

-m:将文件压缩并加入压缩文件后删除元素文件

root@35d388cf883b:/home/wzm/testgzip# ls
aaa  redis.conf  ret  testzip  zip
root@35d388cf883b:/home/wzm/testgzip# zip zipm.zip aaa -m
  adding: aaa (deflated 65%)
root@35d388cf883b:/home/wzm/testgzip# ls
redis.conf  ret  testzip  zip  zipm.zip
root@35d388cf883b:/home/wzm/testgzip#

-n <字尾字符串>:不压缩具有特定字尾字符串的文件。
-o:以压缩文件内拥有最新更改时间的文件为准,将压缩文件的更改时间设成和该文件相同

root@35d388cf883b:/home/wzm/testgzip/zip# stat addfile.txt
  File: 'addfile.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 70h/112d        Inode: 2602        Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-11-21 02:25:58.915893701 +0000
Modify: 2017-11-21 02:25:54.831843559 +0000
Change: 2017-11-21 02:25:54.831843559 +0000
 Birth: -
root@35d388cf883b:/home/wzm/testgzip/zip# cd ..
root@35d388cf883b:/home/wzm/testgzip# ls
aaa  redis.conf  ret  testzip  zip  zipn.zip
root@35d388cf883b:/home/wzm/testgzip# zip zipo.zip zip -r -o
  adding: zip/ (stored 0%)
  adding: zip/testzip (stored 0%)
  adding: zip/redis.conf (deflated 65%)
  adding: zip/addfile.txt (stored 0%)
root@35d388cf883b:/home/wzm/testgzip# cd ret
root@35d388cf883b:/home/wzm/testgzip/ret# ls
aaa  old.zip  zip
root@35d388cf883b:/home/wzm/testgzip/ret# rm * -r
root@35d388cf883b:/home/wzm/testgzip/ret# cp ../zipo.zip  ./
root@35d388cf883b:/home/wzm/testgzip/ret# unzip zipo.zip
Archive:  zipo.zip
   creating: zip/
 extracting: zip/testzip
  inflating: zip/redis.conf
 extracting: zip/addfile.txt
root@35d388cf883b:/home/wzm/testgzip/ret# stat zip/addfile.txt
  File: 'zip/addfile.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 70h/112d        Inode: 2617        Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-11-21 02:25:54.000000000 +0000
Modify: 2017-11-21 02:25:54.000000000 +0000

-q:不显示指令执行过程。(默认)
-r:递归处理,将制定目录下所有文件和子目录一并处理。
例子在上面例子中已经出现很多了。**
-S:包含系统和隐藏文件。 默认也带上

root@35d388cf883b:/home/wzm/testgzip/ret# zip test.zip zip -r
updating: zip/ (stored 0%)
  adding: zip/redis.conf (deflated 65%)
  adding: zip/addfile.txt (stored 0%)
  adding: zip/.testzip (stored 0%)
root@35d388cf883b:/home/wzm/testgzip/ret#

-t<日期时间>:把压缩文件的日期设成制定的日期。经过测试,发现不起作用
-T:检查备份文件内的每个文件是否正确无误。

root@35d388cf883b:/home/wzm/testgzip/ret# ls
test.zip  zip
root@35d388cf883b:/home/wzm/testgzip/ret# zip test.zip  -T
test of test.zip OK

-v:显示指令执行过程或显示版本信息

root@35d388cf883b:/home/wzm/testgzip/ret# zip test.zip  -T -v
Archive:  test.zip
    testing: zip/                     OK
    testing: zip/redis.conf           OK
    testing: zip/addfile.txt          OK
    testing: zip/.testzip             OK
No errors detected in compressed data of test.zip.
test of test.zip OK
root@35d388cf883b:/home/wzm/testgzip/ret#

-X:不保存额外的文件属性
-y:直接保存符号连接,而非该连接所指向的文件,只在linux下有效
-<压缩效率>:和gzip一样,是一个介于1~9的数值。

root@35d388cf883b:/home/wzm/testgzip/ret# zip test.zip zip -r -1
updating: zip/ (stored 0%)
updating: zip/redis.conf (deflated 60%)
updating: zip/addfile.txt (stored 0%)
updating: zip/.testzip (stored 0%)
root@35d388cf883b:/home/wzm/testgzip/ret# zip test.zip zip -r -9
updating: zip/ (stored 0%)
updating: zip/redis.conf (deflated 65%)
updating: zip/addfile.txt (stored 0%)
updating: zip/.testzip (stored 0%)
root@35d388cf883b:/home/wzm/testgzip/ret#
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值