首页·终端命令

unzip

压缩与传输

用法

unzip [-d <dir>] <archive.zip>

解开 .zip;不给 `-d` 就直接倒在当前目录里,所以先用 `-l` 看看包内有没有一层顶层文件夹,旧版 Windows 打的包要靠 `-O CP932` 才不乱码,而 `-P` 写在命令行里的密码会留在 shell 历史和 ps 里。

常用选项

选项含义
-lList the contents without extracting.
-d <dir>Extract there instead of into the current directory.
-oOverwrite existing files without asking.
-nNever overwrite: skip files that already exist.
-jFlatten the paths into one directory.
-qQuiet, for scripts.
-O CP932Interpret file names in a legacy code page (Debian/Ubuntu Info-ZIP only; stock macOS unzip rejects -O).
-P <password>Password on the command line, where ps and your shell history can see it.

示例

unzip -l archive.zip

Check whether it has a top-level folder before extracting.

unzip archive.zip -d /tmp/out

Extract somewhere harmless.

unzip -O CP932 japanese.zip

Fixes mojibake from an older Windows zip, where the patched Info-ZIP is available.

打包和压缩是两件事:tar 打包,gzip 压小,.tar.gz 是两样一起。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. unzip 是做什么的?

解开 .zip;不给 `-d` 就直接倒在当前目录里,所以先用 `-l` 看看包内有没有一层顶层文件夹,旧版 Windows 打的包要靠 `-O CP932` 才不乱码,而 `-P` 写在命令行里的密码会留在 shell 历史和 ps 里。

Q. 怎么写?

unzip [-d <dir>] <archive.zip> —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 8 个,完整列表在 man unzip。这条命令属于压缩与传输。

相关命令

man 手册: man unzip