Linux系统之gzip命令的基本使用

简介: Linux系统之gzip命令的基本使用

在这里插入图片描述

一、gzip命令简介

gzip 是 Linux 系统中一个非常常用的命令,用来压缩文件。它使用 Lempel-Ziv 编码 (LZ77) 和霍夫曼编码的组合来减少文件大小。gzip 命令创建的文件通常以 .gz 作为扩展名。

二、gzip命令使用帮助

2.1 help帮助信息

使用--help选项,查询gzip命令的基本帮助信息。

[root@openEuler-test ~]# gzip --help
Usage: gzip [OPTION]... [FILE]...
Compress or uncompress FILEs (by default, compress FILES in-place).

Mandatory arguments to long options are mandatory for short options too.

  -c, --stdout      write on standard output, keep original files unchanged
  -d, --decompress  decompress
  -f, --force       force overwrite of output file and compress links
  -h, --help        give this help
  -k, --keep        keep (don't delete) input files
  -l, --list        list compressed file contents
  -L, --license     display software license
  -n, --no-name     do not save or restore the original name and timestamp
  -N, --name        save or restore the original name and timestamp
  -q, --quiet       suppress all warnings
  -r, --recursive   operate recursively on directories
      --rsyncable   make rsync-friendly archive
  -S, --suffix=SUF  use suffix SUF on compressed files
      --synchronous synchronous output (safer if system crashes, but slower)
  -t, --test        test compressed file integrity
  -v, --verbose     verbose mode
  -V, --version     display version number
  -1, --fast        compress faster
  -9, --best        compress better

With no FILE, or when FILE is -, read standard input.

Report bugs to <bug-gzip@gnu.org>.

2.2 选项解释

  • 语法
gzip(选项)(参数)
  • 选项解释信息
参数 描述
-c, --stdout 将结果输出到标准输出,保持原始文件不变。
-d, --decompress 解压缩。
-f, --force 强制覆盖输出文件并压缩链接。
-h, --help 显示此帮助信息。
-k, --keep 保留(不删除)输入文件。
-l, --list 列出压缩文件的内容。
-L, --license 显示软件许可证。
-n, --no-name 不保存或恢复原始文件名和时间戳。
-N, --name 保存或恢复原始文件名和时间戳。
-q, --quiet 抑制所有警告。
-r, --recursive 对目录递归操作。
--rsyncable 创建适合 rsync 的存档。
-S, --suffix=SUF 在压缩文件上使用后缀 SUF。
--synchronous 同步输出(如果系统崩溃更安全,但速度较慢)。
-t, --test 测试压缩文件的完整性。
-v, --verbose 详细模式。
-V, --version 显示版本号。
-1, --fast 更快地压缩。
-9, --best 更好地压缩。

三、gzip命令的基本使用

3.1 压缩文件

  • 我们创建测试目录与测试文件。
[root@openEuler-test ~]# mkdir /test && cd /test
[root@openEuler-test test]# ls
[root@openEuler-test test]# touch file{01..10}
[root@openEuler-test test]# ls
file01  file02  file03  file04  file05  file06  file07  file08  file09  file10
[root@openEuler-test test]# echo aatest  > /test/file01
  • 要压缩一个或多个文件,我们可以直接使用以下命令:
[root@openEuler-test test]# gzip file01
[root@openEuler-test test]# ll
total 4
-rw-r--r-- 1 root root 34 Sep 29 09:17 file01.gz
-rw-r--r-- 1 root root  0 Sep 29 09:16 file02
-rw-r--r-- 1 root root  0 Sep 29 09:16 file03
-rw-r--r-- 1 root root  0 Sep 29 09:16 file04
-rw-r--r-- 1 root root  0 Sep 29 09:16 file05
-rw-r--r-- 1 root root  0 Sep 29 09:16 file06
-rw-r--r-- 1 root root  0 Sep 29 09:16 file07
-rw-r--r-- 1 root root  0 Sep 29 09:16 file08
-rw-r--r-- 1 root root  0 Sep 29 09:16 file09
-rw-r--r-- 1 root root  0 Sep 29 09:16 file10

3.2 保留原始文件

如果我们想保留原始文件,使用-k选项。

[root@openEuler-test test]# gzip -k file02
[root@openEuler-test test]# ll
total 8
-rw-r--r-- 1 root root 34 Sep 29 09:17 file01.gz
-rw-r--r-- 1 root root  0 Sep 29 09:16 file02
-rw-r--r-- 1 root root 27 Sep 29 09:16 file02.gz
-rw-r--r-- 1 root root  0 Sep 29 09:16 file03
-rw-r--r-- 1 root root  0 Sep 29 09:16 file04
-rw-r--r-- 1 root root  0 Sep 29 09:16 file05
-rw-r--r-- 1 root root  0 Sep 29 09:16 file06
-rw-r--r-- 1 root root  0 Sep 29 09:16 file07
-rw-r--r-- 1 root root  0 Sep 29 09:16 file08
-rw-r--r-- 1 root root  0 Sep 29 09:16 file09
-rw-r--r-- 1 root root  0 Sep 29 09:16 file10

3.3 解压文件

使用-d选项,进行解压文件。

[root@openEuler-test test]# gzip -d file01.gz
[root@openEuler-test test]# ll
total 8
-rw-r--r-- 1 root root  7 Sep 29 09:17 file01
-rw-r--r-- 1 root root  0 Sep 29 09:16 file02
-rw-r--r-- 1 root root 27 Sep 29 09:16 file02.gz
-rw-r--r-- 1 root root  0 Sep 29 09:16 file03
-rw-r--r-- 1 root root  0 Sep 29 09:16 file04
-rw-r--r-- 1 root root  0 Sep 29 09:16 file05
-rw-r--r-- 1 root root  0 Sep 29 09:16 file06
-rw-r--r-- 1 root root  0 Sep 29 09:16 file07
-rw-r--r-- 1 root root  0 Sep 29 09:16 file08
-rw-r--r-- 1 root root  0 Sep 29 09:16 file09
-rw-r--r-- 1 root root  0 Sep 29 09:16 file10

3.4 查看压缩信息

查看压缩比率::使用 -l 选项可以列出每个被压缩文件的信息,包括未压缩的大小、压缩后的大小、压缩比等信息

[root@openEuler-test test]# gzip -l file01.gz
         compressed        uncompressed  ratio uncompressed_name
                 34                   7 -28.6% file01

3.5 标准输出/输入

  • 保留原始文件,把压缩流重定向到新文件
gzip -c file03 > file01.gz
  • 保留原始文件,把解压流重定向到新文件
gzip -dc file01.gz > file03

3.6 批量处理文件

可以一次性压缩多个文件

gzip file02 file03 file04

3.7 递归解压缩目录

  • 重新创建测试目录和文件
[root@openEuler-test test]# mkdir -p test01/test03
[root@openEuler-test test]# echo aa > test01/test03/aa.txt
[root@openEuler-test test]# echo bb > test01/bb.txt

递归进行压缩目录:所有test01目录下面的文件都变成了.gz,目录依然存在只是目录里面的文件相应变成了.gz.这就是压缩,和打包不同。因为是对目录操作,所以需要加上-r选项,这样也可以对子目录进行递归了。

[root@openEuler-test test]# gzip -rv test01/
test01/test03/aa.txt:    -66.7% -- replaced with test01/test03/aa.txt.gz
test01/bb.txt:    -66.7% -- replaced with test01/bb.txt.gz
[root@openEuler-test test]# ll
total 4
drwxr-xr-x 3 root root 4096 Sep 29 09:42 test01
[root@openEuler-test test]# tree ./
./
└── test01
    ├── bb.txt.gz
    └── test03
        └── aa.txt.gz

3 directories, 2 files
  • 递归进行解压目录
[root@openEuler-test test]# gzip -dr test01
[root@openEuler-test test]# tree ./
./
└── test01
    ├── bb.txt
    └── test03
        └── aa.txt

3 directories, 2 files

3.8测试压缩文件完整性

使用 -t 选项可以检查 .gz 文件是否完整且没有损坏

gzip -t file01.txt.gz

四、注意事项

  1. 使用 gzip 会替换原始文件,除非你加上 -k 选项来保留原文件。
  2. 在压缩多个文件或目录时,gzip 不支持直接压缩整个目录,需要先将目录打包成一个文件(如通过 tar)再进行压缩。
  3. 指定不同的压缩级别会影响压缩速度和最终文件大小,通常默认的6级已经足够日常使用。
  4. 当解压文件时确保有足够的磁盘空间来存放解压后的数据,因为解压后文件可能显著增大。
  5. 如果要检查 .gz 文件的完整性,可以使用 gzip -t 选项避免在解压损坏文件时丢失数据。
  6. 对于非常大的文件,考虑使用更高级别的压缩可能会大幅增加处理时间,需根据实际需求权衡。
  7. 使用通配符批量压缩文件前,请确认不会意外地压缩不需要的文件。
相关文章
|
22天前
|
数据采集 人工智能 算法
45条AI引用源实测:内容平台权重分布与信息块拆解
本文拆解豆包AI的45条引用源,揭示CSDN、头条、搜狐占国内引用近半;剖析被高频引用的CSDN文章结构参数(如数字密度、列表数、H2标题),提出“平台推荐→AI抓取→被引用”链路及可复现的监测方法。
155 1
|
21天前
|
存储 监控 API
基于 RAG + LangChain 搭建企业级私有知识库问答系统(2026 实战版)
本文是作者基于多个企业RAG知识库落地经验的实战总结,提供完整可运行代码与十年避坑指南。涵盖文档解析、混合检索、向量存储、DeepSeek接入、结果重排、拒答机制及效果评估,助你构建本地可运行、生产可扩展的企业级私有知识库系统。(239字)
314 1
|
21天前
|
缓存 安全 程序员
智谱GLM-5.3发布同基座纯靠后训练编程涨50还点亮网安技能树
智谱 8 月 14 日发布 GLM-5.3,与 5.2 同基座、纯后训练,编程内部基准提升 50%,CyberGym 拿下开源第一,两周后开源权重
智谱GLM-5.3发布同基座纯靠后训练编程涨50还点亮网安技能树
|
22天前
|
Shell API 调度
DeepSeek Harness 一切皆插件:开源Agent框架强在哪,怎么装
DeepSeek Harness 开发者预览版 2026 年 8 月开源,口号"一切皆插件"。本文拆解插件化架构的 4 个好处,并带你跑通安装命令。
1263 3
DeepSeek Harness 一切皆插件:开源Agent框架强在哪,怎么装
|
21天前
|
Ubuntu 测试技术 网络安全
【Docker项目实战篇】Docker部署PDD查看器PdfDing
【Docker项目实战篇】Docker部署PDD查看器PdfDing
152 2
【Docker项目实战篇】Docker部署PDD查看器PdfDing
|
23天前
|
Ubuntu 测试技术 网络安全
【Docker项目实战】使用Docker部署todo任务管理器
【Docker项目实战】使用Docker部署todo任务管理器
128 2
【Docker项目实战】使用Docker部署todo任务管理器
|
26天前
|
存储 Ubuntu Linux
Ubuntu系统下的用户管理
Ubuntu系统下的用户管理
152 2
Ubuntu系统下的用户管理
|
30天前
|
Web App开发 安全 网络安全
Docker一键部署 Neko,搭建属于自己的自托管虚拟浏览器
Docker一键部署 Neko,搭建属于自己的自托管虚拟浏览器
261 3
Docker一键部署 Neko,搭建属于自己的自托管虚拟浏览器
|
21天前
|
人工智能 程序员 开发工具
AI时代程序员需要关注的点(个人理解)
AI时代,经过我自己原创的项目,十几天的不断产品迭代,是对自己所有经验以及与AI结合,不断发现问题,不断要求AI去改,不断AI改,我不断抽象的过程,以及我不断精进的过程。
55 2
|
21天前
|
缓存 自然语言处理 区块链
投机解码原理拆解:Draft-Target双模型流水线:小模型预生成 + 主模型并行校验20.0
投机解码是一种高效的大模型推理加速技术:用轻量小模型(Draft)快速预生成多个候选Token,再由大模型(Target)一次性批量校验并修正。它不改动主模型,零精度损失,显著提升生成速度,是当前性价比最高的并行推理优化方案。
122 3