Linux shell编程学习笔记80:gzip命令——让文件瘦身

0 引言

Linux shell编程学习笔记76:tar命令——快照 & 备份(上)-CSDN博客

Linux shell编程学习笔记77:tar命令——快照 & 备份(下)_linux 系统快照-CSDN博客 

Linux shell编程学习笔记78:cpio命令——文件和目录归档工具(上)-CSDN博客

Linux shell编程学习笔记79:cpio命令——文件和目录归档工具(下)-CSDN博客

中,我们分别介绍了用于文件和目录归档的tar和cpio命令,为了缩小归档文件的体积,我们还可以使用压缩软件对归档文件进行压缩。

在Windows系统中,我们常用的压缩软件有WinRAR、7-Zip、WinZip、PeaZip、Bandizip等。

在Linux系统中,常用的压缩软件有gzip,plzip、p7zip、pbzip2、ebzip等。

今天我们重点研究gzip。

1 gzip 的功能,帮助信息,格式,选项和参数说明

1.1 gzip 的功能

gzip 命令是 Linux 中流行的压缩工具,用于减小文件大小,同时保留其原始内容。它是 GNU Core Utilities 包的一部分,几乎在所有 Linux 发行版上都可用。 

1.2 gzip 的帮助信息

我们可以使用 gzip -h 命令获取帮助信息。

1.2.1 bash中的gzip帮助信息

[purpleendurer @ bash ~] gzip
gzip: compressed data not written to a terminal. Use -f to force compression.
For help, type: gzip -h
[purpleendurer @ bash ~] gzip -h
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
  -l, --list        list compressed file contents
  -L, --license     display software license
  -n, --no-name     do not save or restore the original name and time stamp
  -N, --name        save or restore the original name and time stamp
  -q, --quiet       suppress all warnings
  -r, --recursive   operate recursively on directories
  -S, --suffix=SUF  use suffix SUF on compressed files
  -t, --test        test compressed file integrity
  -v, --verbose     verbose mode
  -V, --version     display version number
  -1, --fast        compress faster
  -9, --best        compress better
    --rsyncable   Make rsync-friendly archive

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

Report bugs to <bug-gzip@gnu.org>.
[purpleendurer @ bash ~] 

 1.2.2 银河麒麟(kylin)系统中的gzip帮助信息

[purpleendurer @ kylin ~] gzip -h
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 time stamp
  -N, --name        save or restore the original name and time stamp
  -q, --quiet       suppress all warnings
  -r, --recursive   operate recursively on directories
  -S, --suffix=SUF  use suffix SUF on compressed files
  -t, --test        test compressed file integrity
  -v, --verbose     verbose mode
  -V, --version     display version number
  -1, --fast        compress faster
  -9, --best        compress better
  --rsyncable       Make rsync-friendly archive

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

Report bugs to <bug-gzip@gnu.org>.
[purpleendurer @ kylin ~] 

 

1.3 gzip 的命令格式

gzip [选项]... [文件]...

1.4 gzip 的选项和参数说明

1.4.1 gzip的选项

选项说明备注
-c, --stdout写入标准输出,保持原始文件不变
-d, --decompress解压缩
-f, --force强制覆盖输出文件并压缩链接
-h, --help提供此帮助信息
-k, --keep保留(不删除)输入文件

不是所有版本的gzip都支持此参数

已知1.5不支持,1.6支持

-l, --list列出压缩文件内容
-L, --license显示软件许可证
-n, --no-name不保存或恢复原始名称和时间戳
-N, --name保存或恢复原始名称和时间戳
-q, --quiet抑制所有警告
-r、--recursive对目录递归操作
-S, --suffix=SUF在压缩文件上使用后缀 SUF
-t, --test测试压缩文件的完整性建议同时指定-v选项
-v, --verbose详细模式,显示详细的压缩或解压缩过程
-V、--version显示版本号
-1, --fast压缩速度更快
-9, --best压缩得更好
--rsyncable制作 rsync 友好的存档

1.4.2 gzip的参数

文件:表示要压缩或解压缩的一个或多个文件。 

2 gzip使用实例

2.1 创建演示文件和目录

我们使用 echo 命令和输出重定向创建文件f1.txt 和 f2.txt,使用mkdir命令创建目录d1,再使用 echo 命令和输出重定向在目录d1下面创建文件fa.txt 和 fb.txt
 

[purpleendurer @ bash ~ ] echo "f1" > f1.txt
[purpleendurer @ bash ~ ] echo "f2" > f2.txt
[purpleendurer @ bash ~ ] mkdir d1
[purpleendurer @ bash ~ ] echo "d1fa" > d1/fa.txt
[purpleendurer @ bash ~ ] echo "d1fb" > d1/fb.txt
[purpleendurer @ bash ~ ] ls -R
.:
Code  d1  f1.txt  f2.txt

./Code:

./d1:
fa.txt  fb.txt
[purpleendurer @ bash ~ ] 

 

2.2 压缩单个文件

2.2.1 压缩单个文件后删除源文件  

例如:使用gzip压缩当前目录下文件f1.txt的命令 :gzip f1.txt 

[purpleendurer @ bash ~ ] ls
Code  d1  f1.txt  f2.txt
[purpleendurer @ bash ~ ] gzip f1.txt
[purpleendurer @ bash ~ ] ls
Code  d1  f1.txt.gz  f2.txt
[purpleendurer @ bash ~ ] 

执行压缩命令后,我们会发现在当前目录下,被压缩的源文件f1.txt不见了,多了一个压缩后的目标文件 f1.txt.gz。

注意:在默认情况下,gzip压缩指定的文件,并为它加上 .gz 扩展名。源文件会被删除。 

2.2.2 压缩单个文件后保留源文件  

如果我们希望压缩文件后仍然保留源文件,该如何操作呢?

例如使用gzip压缩当前目录下文件f2.txt,压缩后不删除f2.tx

如果我们使用的gzip支持-k 选项,那么可以使用 -k 选项,命令为 :

gzip -k f2.txt 

如果我们使用的gzip不支持-k 选项,那么可以使用-c选项结合输出重定向来操作,命令为:

gzip -c f2.txt > f2.txt.gz

[purpleendurer @ bash ~ ] ls
Code  d1  f1.txt.gz  f2.txt
[purpleendurer @ bash ~ ] gzip -k f2.txt
gzip: invalid option -- 'k'
Try `gzip --help' for more information.
[purpleendurer @ bash ~ ] gzip -c f2.txt > f2.txt.gz
gzip: f2.txt : No such file or directory
[purpleendurer @ bash ~ ] ls
Code  d1  f1.txt.gz  f2.txt  f2.txt.gz
[purpleendurer @ bash ~ ] 

在上面的操作中,我们先尝试了命令: gzip -k f2.txt ,但是很不幸的是,我们使用的gzip不支持-k选项。

接着我们尝试了命令:gzip -c f2.txt > f2.txt.gz,虽然我们收到了出错提示信息:

gzip: f2.txt : No such file or directory

但是,我们使用ls命令会发现压缩文件f2.txt.gz。

要想避免收到上面所说的出错提示信息。

我们可以使用命令:

gzip  < f1.txt > f2.txt.gz

[purpleendurer @ bash ~ ] ls
Code  d1  f1.txt  f2.txt
[purpleendurer @ bash ~ ] gzip  < f1.txt > f2.txt.gz
[purpleendurer @ bash ~ ] ls
Code  d1  f1.txt  f2.txt  f2.txt.gz
[purpleendurer @ bash ~ ] 

2.3 压缩多个文件

压缩当前目录下的文件f1.txt f2.txt,命令为:

gzip f1.txt f2.txt

[purpleendurer @ bash ~ ] ls
Code  d1  f1.txt  f2.txt  f2.txt.gz
[purpleendurer @ bash ~ ] gzip f1.txt f2.txt
gzip: f2.txt.gz already exists; do you wish to overwrite (y or n)? y
[purpleendurer @ bash ~ ] ls
Code  d1  f1.txt.gz  f2.txt.gz
[purpleendurer @ bash ~ ] 

在执行压缩命令的过程中,由于当前目录下已经存在文件f2.txt.gz,所以会提示是否覆盖该文件。

2.4 递归压缩目录树下的文件

为了实现递归压缩,我们要指定 -r 选项。

为了演示效果,我们在目录d1下再创建目录d2,在目录d2下创建文件fc.txt和fd.txt

[purpleendurer @ bash ~ ] mkdir d1/d2
[purpleendurer @ bash ~ ] echo "d2fc" > d1/d2/fc.txt
[purpleendurer @ bash ~ ] echo "d2fd" > d1/d2/fd.txt
[purpleendurer @ bash ~ ] ls -R
.:
Code  d1  f1.txt  f2.txt

./Code:

./d1:
d2  fa.txt  fb.txt

./d1/d2:
fc.txt  fd.txt
[purpleendurer @ bash ~ ] 

然后我们将目录d1及子目录下的文件进行压缩

[purpleendurer @ bash ~ ] ls -R
.:
Code  d1  f1.txt  f2.txt

./Code:

./d1:
d2  fa.txt  fb.txt

./d1/d2:
fc.txt  fd.txt
[purpleendurer @ bash ~ ] gzip -rv d1
d1/d2/fd.txt:   -40.0% -- replaced with d1/d2/fd.txt.gz
d1/d2/fc.txt:   -40.0% -- replaced with d1/d2/fc.txt.gz
d1/fb.txt:      -40.0% -- replaced with d1/fb.txt.gz
d1/fa.txt:      -40.0% -- replaced with d1/fa.txt.gz
[purpleendurer @ bash ~ ] ls -R
.:
Code  d1  f1.txt  f2.txt

./Code:

./d1:
d2  fa.txt.gz  fb.txt.gz

./d1/d2:
fc.txt.gz  fd.txt.gz
[purpleendurer @ bash ~ ] 

为了更好地看到命令执行的过程和效果,我们指定了-v选项。

2.4 指定压缩速度

我们可以为gzip指定一个压缩速度,其速度范围值为1——9。

1 是最快的压缩,压缩率最低

9 是最慢的压缩,压缩率最高

默认是 6

2.4.1 汉字

我们先拿汉字试一下。

[purpleendurer @ bash ~ ] echo "LZ77 的基本原理:LZ77 以经常出现的字母组合(或较长的字符串)构建字典中的数据项,并且使用较短的数字(或符号)编码来代替比较复杂的数据 项。数据压缩时,将从待压缩数据中读入的源数据与字典中的数据项进行匹配,从中检索出相应的代码并输出。从而实现数据的压缩。" > f3.txt
[purpleendurer @ bash ~ ] gzip -1 -cv < f3.txt > f3-1.gz
 29.6%
[purpleendurer @ bash ~ ] gzip -4 -cv < f3.txt > f3-4.gz
 29.6%
[purpleendurer @ bash ~ ] gzip -9 -cv < f3.txt > f3-9.gz
 29.6%
[purpleendurer @ bash ~ ] gzip -cv < f3.txt > f3-9.gz
 29.6%
[purpleendurer @ bash ~ ] 

 

我们先把一段汉字存放在文件 f3.txt中,然后分别指定压缩速度 1、4、9和默认(6)来进行压缩。

从命令执行信息来看,压缩率没有差别。 

2.4.2 英文

我们再用常用英文单词试一下:

[purpleendurer @ bash ~ ] echo "I, you, he, she, it, we, they,be, have, do, go, make, see, take, get, give,person, time, day, year, way, man, woman, child, government, company,good, bad, big, little, new, old, happy, sad, beautiful, ugly,very, really, so, too, then, now, well, just, also, still,a, an, the,in, on, at, with, for, about, from, over, under, between,and, but, or, so, because, if, when, while, although, before, after,one, two, three, four, five, ten, hundred, thousand, million,hello, goodbye, please, thank you, sorry, excuse me, yes, no,I, you, he, she, it, we, they,be, have, do, go, make, see, take, get, give,person, time, day, year, way, man, woman, child, government, company,good, bad, big, little, new, old, happy, sad, beautiful, ugly,very, really, so, too, then, now, well, just, also, still,a, an, the,in, on, at, with, for, about, from, over, under, between,and, but, or, so, because, if, when, while, although, before, after,one, two, three, four, five, ten, hundred, thousand, million,hello, goodbye, please, thank you, sorry, excuse me, yes, no" > f4.txt
[purpleendurer @ bash ~ ] gzip -1 -cv < f4.txt > f4-1.gz
 70.7%
[purpleendurer @ bash ~ ] gzip -4 -cv < f4.txt > f4-4.gz
 71.0%
[purpleendurer @ bash ~ ] gzip -9 -cv < f4.txt > f4-9.gz
 71.0%
[purpleendurer @ bash ~ ] gzip -cv < f4.txt > f4-9.gz
 71.0%
[purpleendurer @ bash ~ ] 

我们先把常用英文单词存放在文件 f4.txt中,然后分别指定压缩速度 1、4、9和默认(6)来进行压缩。

从4开始,压缩率没有变化。所以一般使用默认(6)就可以了。

 2.5 测试压缩文件的完整性

我们可以使用选项-t或 --test来测试压缩文件的完整性。

例如:测试文件f3.txt.gz的完整性

[purpleendurer @ bash ~ ] gzip -cv < f3.txt > f3.txt.gz
 29.6%
[purpleendurer @ bash ~ ] gzip -t f3.txt.gz
[purpleendurer @ bash ~ ] gzip -tv f3.txt.gz
f3.txt.gz:       OK
[purpleendurer @ bash ~ ] 

 

可以看到, 执行命令:gzip -t f3.txt.gz 没反馈信息。

执行命令 gzip -tv f3.txt.gz 才看到反馈信息:

f3.txt.gz:       OK

注意:为了看到测试结果,建议同时指定-v选项。

2.6 查看压缩文件的详细信息

我们可以指定 -l 选项可以查看 .gz 文件的详细信息。

例如,查看f3.txt.gz的详细信息

[purpleendurer @ bash ~ ] gzip -l f3.txt.gz
         compressed        uncompressed  ratio uncompressed_name
                279                 371  29.6% f3.txt
[purpleendurer @ bash ~ ] 

2.7 解压文件

我们可以使用选项 -d 或--decompress 来对压缩文件进行 解压缩。

例如,对压缩文件 f3.txt.gz 进行 解压缩

[purpleendurer @ bash ~ ] ls
Code  f3-1.gz  f3-4.gz  f3-9.gz  f3.txt
[purpleendurer @ bash ~ ] gzip -c < f3.txt >  f3.txt.gz
[purpleendurer @ bash ~ ] ls
Code  f3-1.gz  f3-4.gz  f3-9.gz  f3.txt  f3.txt.gz
[purpleendurer @ bash ~ ] gzip -dv f3.txt.gz
gzip: f3.txt already exists; do you wish to overwrite (y or n)? n
        not overwritten
[purpleendurer @ bash ~ ] gzip -dfv f3.txt.gz
f3.txt.gz:       29.6% -- replaced with f3.txt
[purpleendurer @ bash ~ ] ls
Code  f3-1.gz  f3-4.gz  f3-9.gz  f3.txt
[purpleendurer @ bash ~ ] 

 

我们执行命令gzip -dv f3.txt.gz时,由于当前目录中存在文件f3.txt,会要求我们确定是否覆盖原有文件:

gzip: f3.txt already exists; do you wish to overwrite (y or n)? 

我们输入n后,反馈信息:

        not overwritten

没有覆盖原有文件。

如果我们不想接收和处理是否覆盖原有文件的信息,我们可以使用 -f选项强制覆盖原有文件。

所以我们执行命令 gzip -dfv f3.txt.gz 时 会直接覆盖原有文件f3.txt:

f3.txt.gz:       29.6% -- replaced with f3.txt 

2.8 查看版本信息

 我们可以使用-V选项查看当前使用的gzip版本信息。

2.8.1 测试用的bash中的gzip版本信息

[purpleendurer @ bash ~ ] gzip -V
gzip 1.5
Copyright (C) 2007, 2010, 2011 Free Software Foundation, Inc.
Copyright (C) 1993 Jean-loup Gailly.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Written by Jean-loup Gailly.
[purpleendurer @ bash ~ ] 

 

2.8.2 银河麒麟(kylin)系统中的gzip版本信息

[purpleendurer @ kylin ~] gzip -v
gzip: compressed data not written to a terminal. Use -f to force compression.
For help, type: gzip -h
[purpleendurer @ kylin ~] gzip -V
gzip 1.6
Copyright (C) 2007, 2010, 2011 Free Software Foundation, Inc.
Copyright (C) 1993 Jean-loup Gailly.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Written by Jean-loup Gailly.
[purpleendurer @ kylin ~] 

 

看来gzip的1.5版本不支持-k选项,而1.6版本支持-k选项。

2.9 查看软件许可证信息

我们可以使用-L 或 --license 选项 查看软件许可证信息。

2.9.1 测试用的bash中的gzip软件许可证信息

[purpleendurer @ bash ~ ] gzip -L
gzip 1.5
Copyright (C) 2007, 2010, 2011 Free Software Foundation, Inc.
Copyright (C) 1993 Jean-loup Gailly.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.
[purpleendurer @ bash ~ ] 

显示的信息跟版本信息相似。

如果我们同时查看gzip版本和软件许可证信息会如何显示呢?

[purpleendurer @ bash ~ ] gzip -VL
gzip 1.5
Copyright (C) 2007, 2010, 2011 Free Software Foundation, Inc.
Copyright (C) 1993 Jean-loup Gailly.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Written by Jean-loup Gailly.
[purpleendurer @ bash ~ ] 

 

显示的是软件许可证信息。

2.9.2 银河麒麟(kylin)系统中的gzipgzip软件许可证信息

[purpleendurer @ kylin ~] gzip -L
gzip 1.6
Copyright (C) 2007, 2010, 2011 Free Software Foundation, Inc.
Copyright (C) 1993 Jean-loup Gailly.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.
[purpleendurer @ kylin ~] 

3  LZ77编码

gzip 使用 LZ77  编码来压缩和解压文件。

LZ 编码由以色列研究者 Jacob Ziv 和 Abraham Lempel 提出,是无损压缩的核心思想。LZ 是一个系列的算法,而其中最基本的就是两人在 1977年所发表的论文《A Universal Algorithm for Sequential Compression》 中提出的 LZ77 算法。

LZ77的核心思想:利用数据的重复结构信息来进行数据压缩。

而Huffman等基于统计的数据压缩编码需要先对数据进行字符频率统计,然后进行压缩。

LZ77 的基本原理:LZ77 以经常出现的字母组合(或较长的字符串)构建字典中的数据项,并且使用较短的数字(或符号)编码来代替比较复杂的数据项。数据压缩时,将从待压缩数据中读入的源数据与字典中的数据项进行匹配,从中检索出相应的代码并输出。从而实现数据的压缩。

LZ77 编码是一种基于字典及滑动窗口的无损压缩技术,广泛应用于通信传输。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫郢剑侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值