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. 使用通配符批量压缩文件前,请确认不会意外地压缩不需要的文件。
相关文章
|
3月前
|
缓存 JSON 程序员
DeepSeek V4 Pro 正式版、Grok 4.6 同夜发布
DeepSeek V4 Pro 正式版(0813)与 Grok 4.6 同夜上线。跑分、价格、选型一次讲清,帮你判断主力 API 要不要换
DeepSeek V4 Pro 正式版、Grok 4.6 同夜发布
|
3月前
|
人工智能 边缘计算 安全
阿里云边缘安全加速 ESA 是什么?ESA、CDN、DCDN 三者核心区别与选型对比
阿里云边缘安全加速(ESA)是集全球加速、AI驱动安全防护与边缘计算于一体的一站式服务,依托3200+边缘节点,毫秒级响应网站、API及AI Agent请求,支持免费试用与99元/年高性价比套餐。边缘安全加速 ESA 官网:https://t.aliyun.com/U/AlRpfa
457 0
|
3月前
|
SQL 人工智能 NoSQL
DBX:仅 20MB 的全能数据库工作台,让 70+ 数据库管理变得简单高效
DBX是一款开源轻量级多数据库管理工具,单文件仅20MB,无需Java/Python/Chromium依赖,支持MySQL、达梦、Redis、ClickHouse等70+数据库。集成SQL编辑、数据浏览、AI助手、MCP协议及Docker/Web部署,兼顾高效、安全与智能。
1043 0
|
3月前
|
数据采集 人工智能 算法
45条AI引用源实测:内容平台权重分布与信息块拆解
本文拆解豆包AI的45条引用源,揭示CSDN、头条、搜狐占国内引用近半;剖析被高频引用的CSDN文章结构参数(如数字密度、列表数、H2标题),提出“平台推荐→AI抓取→被引用”链路及可复现的监测方法。
393 1
|
3月前
|
缓存 自然语言处理 区块链
投机解码原理拆解:Draft-Target双模型流水线:小模型预生成 + 主模型并行校验20.0
投机解码是一种高效的大模型推理加速技术:用轻量小模型(Draft)快速预生成多个候选Token,再由大模型(Target)一次性批量校验并修正。它不改动主模型,零精度损失,显著提升生成速度,是当前性价比最高的并行推理优化方案。
272 3
|
3月前
|
监控
阿里云轻量应用服务器不限流量是真的不限吗?无固定流量有没有流量上限?
阿里云轻量应用服务器“不限流量”仅适用于标注“无固定流量”的套餐,即流量无上限、不额外收费;含“每月XXGB”配额的套餐则超额按0.8元/GB计费。注意:200M为峰值带宽,非保障带宽,且禁止流量穿透。阿里云轻量应用服务器官网:https://t.aliyun.com/U/dwftch
|
3月前
|
缓存 安全 程序员
智谱GLM-5.3发布同基座纯靠后训练编程涨50还点亮网安技能树
智谱 8 月 14 日发布 GLM-5.3,与 5.2 同基座、纯后训练,编程内部基准提升 50%,CyberGym 拿下开源第一,两周后开源权重
智谱GLM-5.3发布同基座纯靠后训练编程涨50还点亮网安技能树
|
3月前
|
人工智能 JavaScript 开发工具
MCP Server 开发入门:手把手写一个能跑的 Server,三种协议怎么选
MCP Server 开发入门:用 uv 建工程装 SDK,FastMCP 写出 tool 和 resource 并跑通,再讲清三种传输协议怎么选。
261 0
|
3月前
|
Shell API 调度
DeepSeek Harness 一切皆插件:开源Agent框架强在哪,怎么装
DeepSeek Harness 开发者预览版 2026 年 8 月开源,口号"一切皆插件"。本文拆解插件化架构的 4 个好处,并带你跑通安装命令。
2024 3
DeepSeek Harness 一切皆插件:开源Agent框架强在哪,怎么装
|
3月前
|
人工智能
素材库加内容创作Skills开源,附工作流从0到1搭建指南
本文分享内容创作全流程系统化经验:以风格文档+素材库为双基石,结合开源Skills工作流(含SOP与代码),实现从选题到发布的自动化协同。强调“搭系统而非写提示词”,AI负责80%信息填充,人专注20%观点与真实细节。
364 0