L38.linux命令每日一练 -- 第六章 文件备份与压缩命令 -- zip和unzip

6.3 zip:打包和压缩文件

6.3.1 命令详解

【命令星级】 ★★★★☆

【功能说明】

​ zip压缩格式是Windows与Linux等多平台通用的压缩格式。和gzip命令相比,zip命令压缩文件不仅不会删除源文件,而且还可以压缩目录。

【语法格式】

zip [option] [file]
zip [选项] [文件]

​ **说明:**在zip命令及后面的选项里,每个元素之间都至少要有一个空格。

【选项说明】

​ 表6-3针对该命令的参数选项进行了说明。

​ 表6-3 zip命令的参数选项及说明

在这里插入图片描述

6.3.2 使用范例

​ **范例6-13:**压缩文件。

[root@centos7 /tmp]# cd /tmp/
[root@centos7 /tmp]# cp /etc/services .
[root@centos7 /tmp]# ll -h services 
-rw-r--r--. 1 root root 655K Oct 25 16:17 services
[root@centos7 /tmp]# zip services.zip ./services 	#格式:zip 压缩包名 被压缩的文件。
  adding: services (deflated 80%)	#deflated压缩率。
[root@centos7 /tmp]# ll -h services*
-rw-r--r--. 1 root root 655K Oct 25 16:17 services
-rw-r--r--. 1 root root 134K Oct 25 16:17 services.zip

​ **范例6-14:**压缩目录。

[root@centos7 /tmp]# cd /
[root@centos7 /]# zip tmp.zip ./tmp/	#这样只是压缩目录这一个文件,目录下的文件没有压缩。
  adding: tmp/ (stored 0%)
[root@centos7 /]# zip -r tmp.zip ./tmp/	#使用-r选项递归压缩。
updating: tmp/ (stored 0%)
  adding: tmp/.XIM-unix/ (stored 0%)
  adding: tmp/.X11-unix/ (stored 0%)
  adding: tmp/.ICE-unix/ (stored 0%)
  adding: tmp/.font-unix/ (stored 0%)
  adding: tmp/.Test-unix/ (stored 0%)
  adding: tmp/ks-script-s1kcev (deflated 58%)
  adding: tmp/services (deflated 80%)
  adding: tmp/services.zip (stored 0%)

​ **范例6-15:**排除压缩。

[root@centos7 /]# zip -r tmp.zip /tmp/ -x /tmp/services.zip	#-x指定不压缩的文件。
updating: tmp/ (stored 0%)
updating: tmp/.XIM-unix/ (stored 0%)
updating: tmp/.X11-unix/ (stored 0%)
updating: tmp/.ICE-unix/ (stored 0%)
updating: tmp/.font-unix/ (stored 0%)
updating: tmp/.Test-unix/ (stored 0%)
updating: tmp/ks-script-s1kcev (deflated 58%)
updating: tmp/services (deflated 80%)

6.4 unzip:解压zip文件

6.4.1 命令详解

【命令星级】 ★★★★☆

【功能说明】

​ unzip命令可以解压zip命令或其他压缩软件压缩的zip格式的文件。

【语法格式】

unzip [option] [file]
unzip [选项] [压缩文件]

​ **说明:**在unzip命令及后面的选项里,每个元素之间都至少要有一个空格。

【选项说明】

​ 表6-4针对该命令的参数选项进行了说明。

​ 表6-4 unzip命令的参数选项及说明

在这里插入图片描述

6.4.2 使用范例

​ **范例6-16:**查看压缩文件。

[root@centos7 /]# unzip -l tmp.zip 	#使用-l选项可以查看压缩包内的文件列表。
Archive:  tmp.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  10-25-2020 16:30   tmp/
        0  10-20-2020 16:16   tmp/.XIM-unix/
        0  10-20-2020 16:16   tmp/.X11-unix/
        0  10-20-2020 16:16   tmp/.ICE-unix/
        0  10-20-2020 16:16   tmp/.font-unix/
        0  10-20-2020 16:16   tmp/.Test-unix/
      836  10-20-2020 16:30   tmp/ks-script-s1kcev
   670293  10-25-2020 16:17   tmp/services
   136227  10-25-2020 16:17   tmp/services.zip
---------                     -------
   807356                     9 files

​ **范例6-17:**常规解压文件的例子。

[root@centos7 /]# unzip tmp.zip 
 	#在跟下直接解压文件,因为源文件还存在,因此会出现下面的提示。
Archive:  tmp.zip
replace tmp/ks-script-s1kcev? [y]es, [n]o, [A]ll, [N]one, [r]ename: N	#输入N取消。
#是否替换文件,y是 n否 A所有文件都替换 N所有文件都不替换 r重命名。
[root@centos7 /]# unzip -v tmp.zip 	#-v解压时显示一些信息。
Archive:  tmp.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
       0  Stored        0   0% 10-25-2020 16:30 00000000  tmp/
       0  Stored        0   0% 10-20-2020 16:16 00000000  tmp/.XIM-unix/
       0  Stored        0   0% 10-20-2020 16:16 00000000  tmp/.X11-unix/
       0  Stored        0   0% 10-20-2020 16:16 00000000  tmp/.ICE-unix/
       0  Stored        0   0% 10-20-2020 16:16 00000000  tmp/.font-unix/
       0  Stored        0   0% 10-20-2020 16:16 00000000  tmp/.Test-unix/
     836  Defl:N      354  58% 10-20-2020 16:30 0eadde5e  tmp/ks-script-s1kcev
  670293  Defl:N   136061  80% 10-25-2020 16:17 32c44632  tmp/services
  136227  Stored   136227   0% 10-25-2020 16:17 daeeaa1f  tmp/services.zip
--------          -------  ---                            -------
  807356           272642  66%                            9 files

[root@centos7 /]# unzip -o tmp.zip 	#-o解压时不提示是否覆盖。
Archive:  tmp.zip
  inflating: tmp/ks-script-s1kcev    
  inflating: tmp/services            
 extracting: tmp/services.zip 

​ **范例6-18:**指定解压目录解压文件。

[root@centos7 /]# unzip -d /tmp tmp.zip 	#可以使用-d选项接目录来指定解压目录。
Archive:  tmp.zip
   creating: /tmp/tmp/
   creating: /tmp/tmp/.XIM-unix/
   creating: /tmp/tmp/.X11-unix/
   creating: /tmp/tmp/.ICE-unix/
   creating: /tmp/tmp/.font-unix/
   creating: /tmp/tmp/.Test-unix/
  inflating: /tmp/tmp/ks-script-s1kcev  
  inflating: /tmp/tmp/services       
 extracting: /tmp/tmp/services.zip   
[root@centos7 /]# tree /tmp	#解压成功。
/tmp
├── ks-script-s1kcev
├── services
├── services.zip
└── tmp
    ├── ks-script-s1kcev
    ├── services
    └── services.zip

1 directory, 6 files

猜你喜欢

转载自blog.csdn.net/qq_25599925/article/details/125686645