欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 美景 > Linux系统之gzip命令的基本使用

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

2025/5/15 1:34:01 来源:https://blog.csdn.net/jks212454/article/details/142471757  浏览:    关键词:Linux系统之gzip命令的基本使用

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

  • 一、gzip命令简介
  • 二、gzip命令使用帮助
    • 2.1 help帮助信息
    • 2.2 选项解释
  • 三、gzip命令的基本使用
    • 3.1 压缩文件
    • 3.2 保留原始文件
    • 3.3 解压文件
    • 3.4 查看压缩信息
    • 3.5 标准输出/输入
    • 3.6 批量处理文件
    • 3.7 递归解压缩目录
    • 3.8测试压缩文件完整性
  • 四、注意事项

在这里插入图片描述

一、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 betterWith 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.gzcompressed        uncompressed  ratio uncompressed_name34                   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.gz3 directories, 2 files
  • 递归进行解压目录
[root@openEuler-test test]# gzip -dr test01
[root@openEuler-test test]# tree ./
./
└── test01├── bb.txt└── test03└── aa.txt3 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. 使用通配符批量压缩文件前,请确认不会意外地压缩不需要的文件。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词