首页 > 百科知识 正文

Linux怎么解压gz文件和怎么创建gz压缩文件(linux tgz文件怎么解压缩)

时间:2023-11-19 06:51:06 阅读:845 作者:哭到半夜

请关注本头条号,每天坚持更新原创干货技术文章

如需学习视频,请查看本头条号简介,免费在线观看学习视频

1. gzip简介

gzip是linux系统中比较常用的压缩和解压工具,只支持单文件。可以支持目录递归压缩,可以指定压缩率等等。如果要打包多个文件,还要结合tar工具。

Linux怎么解压gz文件和怎么创建gz压缩文件(linux tgz文件怎么解压缩)-第1张

gzip简介

gzip使用案例:

Linux怎么解压gz文件和怎么创建gz压缩文件(linux tgz文件怎么解压缩)-第2张

gzip使用案例

2. 准备测试环境

产生两个后缀名为txt的文件,一个10M,另一个15M,用于测试。

[root@zcwyou gzip]# dd if=/dev/zero of=10M.txt bs=1M count=10 [root@zcwyou gzip]# dd if=/dev/zero of=15M.txt bs=1M count=15

产生一个gziptest的目录,并创建一个1.txt的文件

[root@zcwyou gzip]# cd gziptest/ [root@zcwyou gziptest]# touch 1.txt [root@zcwyou gziptest]# cd ../

看看当前目录的情况:

[root@zcwyou gzip]# ll -h

total 25M

-rw-r--r--. 1 root root 10M Oct 22 01:41 10M.txt

-rw-r--r--. 1 root root 15M Oct 22 01:41 15M.txt

drwxr-xr-x. 2 root root 19 Oct 22 01:51 gziptest

3. 压缩指定文件

压缩文件名以.txt结尾的文件

[root@zcwyou gzip]# gzip *.txt

执行结果如下:

[root@zcwyou gzip]# ll -h

total 28K

-rw-r--r--. 1 root root 10K Oct 22 01:41 10M.txt.gz

-rw-r--r--. 1 root root 15K Oct 22 01:41 15M.txt.gz

drwxr-xr-x. 2 root root 19 Oct 22 01:51 gziptest

[root@zcwyou gzip]# ll -h gziptest/

total 0

-rw-r--r--. 1 root root 0 Oct 22 01:56 1.txt

可以看到只压缩了当前目录的txt文件,并且删除了源文件。不压缩子目录下的文件。

4. 解压文件

加选项-d

选项-v表示输出执行的详细信息

[root@zcwyou gzip]# gzip -dv *.gz

10M.txt.gz: 99.9% -- replaced with 10M.txt

15M.txt.gz: 99.9% -- replaced with 15M.txt

gzip: gziptest is a directory -- ignored

提示忽略目录。

查看文件

[root@zcwyou gzip]# ll -h

total 25M

-rw-r--r--. 1 root root 10M Oct 22 01:41 10M.txt

-rw-r--r--. 1 root root 15M Oct 22 01:41 15M.txt

drwxr-xr-x. 2 root root 19 Oct 22 01:51 gziptest

发现文件已经被解压并且删除了源文件。

5. 压缩文件包括子目录

使用选项-r,压缩当前目录下的文件以及子目录里的文件。

[root@zcwyou gzip]# gzip -rv *

10M.txt: 99.9% -- replaced with 10M.txt.gz

15M.txt: 99.9% -- replaced with 15M.txt.gz

gziptest/1.txt: 0.0% -- replaced with gziptest/1.txt.gz

检查结果:

[root@zcwyou gzip]# ll -h

total 28K

-rw-r--r--. 1 root root 10K Oct 22 01:41 10M.txt.gz

-rw-r--r--. 1 root root 15K Oct 22 01:41 15M.txt.gz

drwxr-xr-x. 2 root root 22 Oct 22 02:24 gziptest

Linux怎么解压gz文件和怎么创建gz压缩文件(linux tgz文件怎么解压缩)-第3张

linux gzip压缩文件包括子目录

[root@zcwyou gzip]# ll -h gziptest/

total 4.0K

-rw-r--r--. 1 root root 26 Oct 22 01:56 1.txt.gz

发现子目录里的文件也被压缩了。

6. 测试压缩文件

使用选项-t

[root@zcwyou gzip]# gzip -t 10M.txt.gz

没有输出证明压缩文件没有问题。

7. 列出压缩文件相关信息

使用选项-l

[root@zcwyou gzip]# gzip -l 10M.txt.gz

compressed uncompressed ratio uncompressed_name

10216 10485760 99.9% 10M.txt

Linux怎么解压gz文件和怎么创建gz压缩文件(linux tgz文件怎么解压缩)-第4张

linux使用gzip列出压缩文件相关信息

8. 指定压缩率

压缩率从1-9。默认值为6,数值越高压缩率越高。

或者使用名字参数

--best使用最高压缩率9,最好的压缩率。

--fast使用最小压缩率1,提高压缩速度。

9. 解压缩

使用选项-d

oucanrong@zcwyou:~/xinshiji_ou$ gzip -d *.gz

Linux怎么解压gz文件和怎么创建gz压缩文件(linux tgz文件怎么解压缩)-第5张

linux使用gzip解压缩

总结:gzip是一个常见的压缩工具,运维人员一定要掌握它的使用方法。

点击了解更多,快速查看更多的技术文章列表。

,

版权声明:该问答观点仅代表作者本人。如有侵犯您版权权利请告知 cpumjj@hotmail.com,我们将尽快删除相关内容。