2、zip/unzip命令

zip程序位于/usr/bin目录中,可将文件压缩成.zip文件以节省硬盘空间,而当需要的时候又可将压缩文件解开。unzip命令用于将压缩文件解压。

1)用zip命令压缩文件或文件夹

在Linux下输入man zip,系统显示zip的帮助文档。

zip命令的使用形式为:zip [选项] 压缩后文件名 待压缩文件或文件夹。

其中:参数选项表明要完成的操作类型,压缩后的文件名是某个合法的文件名,其后缀为zip,待压缩文件或文件夹指明须要进行压缩的文件或文件夹,可以是多个文件或文件夹。zip命令的参数选项如下:

注意:使用zip命令可以将许多文件压缩成一个文件,这与gzip是有区别的。

下面给出使用该命令压缩文件或文件夹的例子:

//显示当前目录下所有文件和文件夹
#ll
总用量 9
drwx------    1 root     root         4096  10月 15 19:45 gzip
drwx------    1 root     root            0  10月 12 21:25 smart
-rwx------    1 root     root           26  10月 13 22:51 smart.txt
-rwx------    1 root     root          226  10月 13 22:51 tar_create.txt
-rwx------    1 root     root         7433  10月 13 22:51 tar.txt
drwx------    1 root     root         4096  10月 13 22:13 xplns
 
//将当前目录下的所有文件和文件夹全部压缩成gong.zip压缩文件,-r表示递归压缩子目录下所有文件
# zip -r gong.zip ./*
adding: gzip/ (stored 0%)
adding: gzip/gong.tar.gz (stored 0%)
adding: smart/ (stored 0%)
adding: smart/smartsuite-2.1-2.i386.rpm (deflated 5%)
adding: smart.txt (stored 0%)
adding: tar_create.txt (deflated 44%)
adding: tar.txt (deflated 68%)
adding: xplns/ (stored 0%)
adding: xplns/xplns-cat-3.3.1-1.i386.rpm.gz (deflated 0%)
adding: xplns/xplns-elm-3.3.1-1.i386.rpm.gz (deflated 0%)
adding: xplns/xplns-img-3.3.1-1.i386.rpm.gz (deflated 0%)
 
//再显示当前目录下所有文件,可以发现压缩文件gong.zip已经生成
#ll
总用量 1621
-rwx------1 root  root  3301222 10月 15 19:49 gong.zip
drwx------1 root  root  4096 10月 15 19:45 gzip
drwx------1 root  root     0  10月 12 21:25 smart
-rwx------1 root  root    26  10月 13 22:51 smart.txt
-rwx------1 root  root   226  10月 13 22:51 tar_create.txt
-rwx------1 root  root  7433 10月 13 22:51 tar.txt
drwx------1 root  root  4096 10月 13 22:13 xplns

用带-v参数选项可以查看zip文件的内容,这点类似于带t参数选项的tar命令。只不过前者用于显示压缩的zip文件的内容,后者用于显示tar文件的内容。与带t参数选项的tar命令类似,带v参数选项的zip命令实际上并不解压缩文件。用带-v参数选项的zip命令举例如下:

//显示当前目录下所有文件,从显示结果可以看出,当前目录下只有一个gong.zip文件
# ll 
总用量 1612
-rwx------    1 root     root      3301222  10月 15 19:49 gong.zip
 
//查看gong.zip文件的内容
# zip -v gong.zip
zip info: local extra (21 bytes) != central extra (13 bytes): gzip/
zip info: local extra (21 bytes) != central extra (13 bytes): gzip/gong.tar.gz
zip info: local extra (21 bytes) != central extra (13 bytes): smart/
zip info: local extra (21 bytes) != central extra (13 bytes): smart.txt
zip info: local extra (21 bytes) != central extra (13 bytes): tar_create.txt
zip info: local extra (21 bytes) != central extra (13 bytes): tar.txt
zip info: local extra (21 bytes) != central extra (13 bytes): xplns/

用带-d参数选项的zip命令可以从zip压缩文件中删除某个文件,而使用带-m的zip命令可以向zip压缩文件添加某个文件,对带-d和-m参数选项的zip命令举例如下:

# zip -v gong.zip    //显示压缩文件gong.zip的文件内容
zip info: local extra (21 bytes) != central extra (13 bytes): free.txt
zip info: local extra (21 bytes) != central extra (13 bytes): smart.txt
zip info: local extra (21 bytes) != central extra (13 bytes): tar_create.txt
zip info: local extra (21 bytes) != central extra (13 bytes): tar.txt
 
//删除压缩文件中smart.txt文件
# zip -d gong.zip smart.txt
deleting: smart.txt
 
//再显示压缩文件内容,可以发现文件删除成功
# zip -v gong.zip
zip info: local extra (21 bytes) != central extra (13 bytes): free.txt
zip info: local extra (21 bytes) != central extra (13 bytes): tar_create.txt
zip info: local extra (21 bytes) != central extra (13 bytes): tar.txt
 
//向压缩文件中gong.zip中添加rpm_info.txt文件
# zip -m gong.zip ./rpm_info.txt
 adding: rpm_info.txt (deflated 75%)
 
//再显示压缩文件内容,可以发现文件添加成功
# zip -v gong.zip
zip info: local extra (21 bytes) != central extra (13 bytes): free.txt
zip info: local extra (21 bytes) != central extra (13 bytes): tar_create.txt
zip info: local extra (21 bytes) != central extra (13 bytes): tar.txt
zip info: local extra (21 bytes) != central extra (13 bytes): rpm_info.txt

2)用unzip命令解压缩文件

unzip命令用于扩展名为zip的压缩文件的解压缩,同时,Windows下用压缩软件Winzip压缩的文件在Linux系统下也可以用unzip命令解压缩。unzip命令的语法如下:

unzip [参数选项] 压缩文件名.zip。

所有参数选项如下:

unzip命令的常用用法有:

简单解压缩文件:将压缩文件解压缩到当前目录下。

//显示当前目录下所有文件,从显示结果可以发现当前目录下只有一个文件gong.zip
# ll
总用量 7
-rwx------    1 root     root        13512  10月 15 20:44 gong.zip
 
//将gong.zip解压缩到当前目录
# unzip gong.zip
Archive:  gong.zip
inflating: free.txt
inflating: tar_create.txt
inflating: tar.txt
inflating: rpm_info.txt
 
//再显示当前目录下所有文件,从显示结果可以发现,gong.zip压缩文件中所有文件已经成功解压缩到当前目录
# ll
总用量 32
-rwx------    1 root     root          230  10月 15 21:03 free.txt
-rwx------    1 root     root        13512  10月 15 20:44 gong.zip
-rwx------    1 root     root        40833  10月 15 21:03 rpm_info.txt
-rwx------    1 root     root          226  10月 15 21:03 tar_create.txt
-rwx------    1 root     root         7433  10月 15 21:03 tar.txt

解压缩文件到指定目录:将压缩文件解压缩到指定的目录下,如果已有相同的文件存在,unzip命令不覆盖原来的文件。

//解压缩当前目录下的text.zip压缩文件,-n参数选项指明不覆盖原有文件,-d /tmp指明将文件解压缩到/tmp目录下
#unzip -n text.zip -d /tmp 
//查看压缩文件目录:类似于带-v参数的zip命令,只显示压缩文件内容,并不实际解压缩文件。
#unzip -v text.zip  //显示当前目录下text.zip压缩文件内容


相关内容

    暂无相关文章