unzip用于在一个ZIP存档压缩文件中进行陈列/检测/提取文件(list, test and extract compressed files in a ZIP archive);unzip命令是Linux系统中用于解压缩ZIP格式压缩文件的常用工具。它能够快速、方便地将ZIP文件解压到当前目录或指定目录。
Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]
-opts:
modifiers:
## 以简单短名称显示
[root@node2 Desktop]# unzip -l filetxt1.zip
Archive: filetxt1.zip
Length Date Time Name
--------- ---------- ----- ----
37 03-06-2025 12:21 file4.txt
28 03-02-2025 11:44 file5.txt
28 03-02-2025 11:44 file6.txt
28 03-02-2025 11:44 file7.txt
28 03-02-2025 11:44 file8.txt
28 03-02-2025 11:44 file9.txt
--------- -------
177 6 files
[root@node2 Desktop]#
## 以详细信息显示
[root@node2 Desktop]# unzip -v filetxt1.zip
Archive: filetxt1.zip
Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----
37 Defl:N 17 54% 03-06-2025 12:21 810bfadc file4.txt
28 Defl:N 16 43% 03-02-2025 11:44 d4a2ce1c file5.txt
28 Defl:N 16 43% 03-02-2025 11:44 d4a2ce1c file6.txt
28 Defl:N 16 43% 03-02-2025 11:44 d4a2ce1c file7.txt
28 Defl:N 16 43% 03-02-2025 11:44 d4a2ce1c file8.txt
28 Defl:N 16 43% 03-02-2025 11:44 d4a2ce1c file9.txt
-------- ------- --- -------
177 97 45% 6 files
[root@node2 Desktop]#
## 直接解压,默认时解压到当前目录
[root@node2 Desktop]# unzip filetxt1.zip
Archive: filetxt1.zip
inflating: file4.txt
inflating: file5.txt
inflating: file6.txt
inflating: file7.txt
inflating: file8.txt
inflating: file9.txt
[root@node2 Desktop]#
## 指定目录解压,解压后内容放在指定目录
[root@node2 Desktop]# unzip filetxt1.zip -d dir2/
Archive: filetxt1.zip
inflating: dir2/file4.txt
inflating: dir2/file5.txt
inflating: dir2/file6.txt
inflating: dir2/file7.txt
inflating: dir2/file8.txt
inflating: dir2/file9.txt
[root@node2 Desktop]#
## 仅解压zip文件中的一个文件file8.txt
[root@node2 Desktop]# unzip filetxt1.zip file8.txt
Archive: filetxt1.zip
inflating: file8.txt
[root@node2 Desktop]#
## 仅解压zip文件中的多个文件file6.txt和file7.txt
[root@node2 Desktop]# unzip filetxt1.zip file6.txt file7.txt
Archive: filetxt1.zip
inflating: file6.txt
inflating: file7.txt
[root@node2 Desktop]#
## 模糊匹配文件进行解压
[root@node2 Desktop]# unzip filetxt1.zip *.txt
Archive: filetxt1.zip
inflating: file4.txt
inflating: file5.txt
inflating: file6.txt
inflating: file7.txt
inflating: file8.txt
inflating: file9.txt
[root@node2 Desktop]#
## zip归档压缩文件中并没有FILE7.txt,使用-C忽略大小写匹配文件
[root@node2 Desktop]# unzip -C filetxt1.zip FILE7.txt
Archive: filetxt1.zip
inflating: file7.txt
[root@node2 Desktop]#
## 解压时,指定一个不需要解压的文件file6.txt
[root@node2 Desktop]# unzip filetxt1.zip -x file6.txt
Archive: filetxt1.zip
inflating: file4.txt
inflating: file5.txt
inflating: file7.txt
inflating: file8.txt
inflating: file9.txt
[root@node2 Desktop]#
## 解压时,指定多个不需要解压的文件
[root@node2 Desktop]# unzip filetxt1.zip -x file6.txt file7.txt
Archive: filetxt1.zip
inflating: file4.txt
inflating: file5.txt
inflating: file8.txt
inflating: file9.txt
[root@node2 Desktop]#
## 解压时,模糊匹配不解压的文件
[root@node2 Desktop]# unzip filetxt1.zip -x *.txt
Archive: filetxt1.zip
[root@node2 Desktop]#
## dir1.zip中有两层目录
[root@node2 Desktop]# unzip -l dir1.zip
Archive: dir1.zip
Length Date Time Name
--------- ---------- ----- ----
0 03-07-2025 16:10 dir1/
50 03-01-2025 18:26 dir1/file2.txt
50 02-28-2025 21:47 dir1/file3.txt
50 02-28-2025 21:49 dir1/file4.txt
50 03-01-2025 15:24 dir1/file5.txt
0 03-07-2025 16:10 dir1/dir12/
50 03-01-2025 18:37 dir1/dir12/file5.txt
219712350 03-01-2025 18:37 dir1/dir12/file1.txt
219712350 03-01-2025 18:37 dir1/dir12/ln_file1.txt
976 03-07-2025 15:24 file3.txt
--------- -------
439425926 10 files
[root@node2 Desktop]#
## 解压指定目录,并指定-j不创建目录,即只解压文件,且都存放在一个目录下
## 相同名称的文件会提示是否覆盖取代
[root@node2 Desktop]# unzip -j dir1.zip -d dir2/
Archive: dir1.zip
inflating: dir2/file2.txt
inflating: dir2/file3.txt
inflating: dir2/file4.txt
inflating: dir2/file5.txt
replace dir2/file5.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
inflating: dir2/file1.txt
inflating: dir2/ln_file1.txt
replace dir2/file3.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
[root@node2 Desktop]#
## 所有文件放一个目录,解压前不同层目录的相同文件仅保留一个
[root@node2 Desktop]# ll dir2/
total 429144
-rw-r--r--. 1 root root 219712350 Mar 1 18:37 file1.txt
-rw-r--r--. 1 root root 50 Mar 1 18:26 file2.txt
-rw-r--r--. 1 root root 50 Feb 28 21:47 file3.txt
-rw-r--r--. 1 root root 50 Feb 28 21:49 file4.txt
-rw-r--r--. 1 root root 50 Mar 1 15:24 file5.txt
-rw-r--r--. 1 root root 219712350 Mar 1 18:37 ln_file1.txt
[root@node2 Desktop]#
## filetxt1.zip和filetxt2.zip压缩的文件名称是相同的
## filetxt1.zip压缩后修改文件file4.txt,然后在压缩为filetxt2.zip
[root@node2 Desktop]# unzip filetxt1.zip -d dir2/
Archive: filetxt1.zip
inflating: dir2/file4.txt
inflating: dir2/file5.txt
inflating: dir2/file6.txt
inflating: dir2/file7.txt
inflating: dir2/file8.txt
inflating: dir2/file9.txt
[root@node2 Desktop]#
## -f 参数仅对更新过的文件进行解压,因为文件存在,则会提示是否覆盖
## 可以于-o参数一起使用,则不提示直接替换
[root@node2 Desktop]# unzip -f filetxt2.zip -d dir2/
Archive: filetxt2.zip
replace dir2/file4.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: dir2/file4.txt
[root@node2 Desktop]#
## 注意-f 与-u 类似,区别在于:
## -f :系统中文件不存在,不会创建文件
## -u :系统中文件不存在,会创建文件
[root@node2 Desktop]# zip -P passwdstr filetxt3.zip file*.txt
adding: file4.txt (deflated 54%)
adding: file5.txt (deflated 43%)
adding: file6.txt (deflated 43%)
adding: file7.txt (deflated 43%)
adding: file8.txt (deflated 43%)
adding: file9.txt (deflated 43%)
[root@node2 Desktop]#
[root@node2 Desktop]# unzip filetxt3.zip ## 解压时需要输入密码才行
Archive: filetxt3.zip
[filetxt3.zip] file4.txt password:
## 或者使用明文密码
[root@node2 Desktop]# unzip -P passwdstr filetxt3.zip
Archive: filetxt3.zip
inflating: file4.txt
inflating: file5.txt
inflating: file6.txt
inflating: file7.txt
inflating: file8.txt
inflating: file9.txt
[root@node2 Desktop]#
## -p查看zip归档压缩里面的所有文件内容
## 然后通过管道(|)和grep检索有jing行的内容
[root@node2 Desktop]# unzip -p filetxt4.zip | grep jing
jingjing
jingjing
jingjing
jingjign
jingjing
[root@node2 Desktop]#