首頁·終端機命令

zip

壓縮與傳輸

用法

zip -r <archive.zip> <path>

和 tar 不同,它自己就帶壓縮,也是 Windows 不裝任何東西就能開啟的那種格式;要放整個目錄必須加 `-r`,否則只把目錄這一條記進去、裡面的東西一個也沒有,而 `-e` 設的密碼是老式 ZipCrypto,很弱。

常用選項

選項含義
-rRecurse into directories. Without it you archive the directory entry and nothing inside.
-x '*/node_modules/*'Exclude a pattern. Quote it so the shell leaves it alone.
-ePrompt for a password. This is legacy ZipCrypto, which is weak.
-1 / -9Fastest / smallest.
-jStore bare file names and drop the directory structure.
-uAdd only files that are new or changed.
-s 100mSplit the archive into 100 MB volumes.

範例

zip -r site.zip site -x '*/node_modules/*'

The archive a Windows colleague can open, without the dependency tree.

zip -j photos.zip ~/Pictures/*.jpg

One flat archive of files from several places.

打包和壓縮是兩件事:tar 打包,gzip 壓小,.tar.gz 是兩樣一起。

怎麼看

  • 方括號 [ ] 表示這部分可以不寫。
  • 省略號 … 表示可以寫多個。
  • 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。

常見問題

Q. zip 是做什麼的?

和 tar 不同,它自己就帶壓縮,也是 Windows 不裝任何東西就能開啟的那種格式;要放整個目錄必須加 `-r`,否則只把目錄這一條記進去、裡面的東西一個也沒有,而 `-e` 設的密碼是老式 ZipCrypto,很弱。

Q. 怎麼寫?

zip -r <archive.zip> <path> —— 方括號表示可以省略的部分。

Q. 值得記的選項有幾個?

這裡列了 7 個,完整列表在 man zip。這條命令屬於壓縮與傳輸。

相關命令

man 手冊: man zip