首页·终端命令

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