首頁·終端機命令

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