首頁·終端機命令

gzip

壓縮與傳輸

用法

gzip [-k] <file>

把單個檔案壓成 .gz,而且預設在成功後刪掉原檔,想留著就加 `-k` 或用 `-c` 寫到標準輸出;它從不打包多個檔案,所以前面要配 tar。

常用選項

選項含義
-kKeep the original. Without it, gzip deletes the file it just compressed.
-dDecompress, which is exactly what gunzip does.
-cWrite to stdout and leave the input alone.
-1 / -9Fastest / smallest. The default is -6.
-lShow the compressed and original sizes of a .gz file.
-tTest the integrity without unpacking.
-rWalk a directory and compress each file separately.

範例

gzip -9 access.log

Creates access.log.gz. The original access.log is gone.

gzip -c report.csv > report.csv.gz

Compress while keeping the original.

gzip -l backup.sql.gz

How much it actually saved.

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

怎麼看

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

常見問題

Q. gzip 是做什麼的?

把單個檔案壓成 .gz,而且預設在成功後刪掉原檔,想留著就加 `-k` 或用 `-c` 寫到標準輸出;它從不打包多個檔案,所以前面要配 tar。

Q. 怎麼寫?

gzip [-k] <file> —— 方括號表示可以省略的部分。

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

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

相關命令

man 手冊: man gzip