linux(二十六)打包压缩命令gzip

2023年 10月 11日 46.8k 0

Linux中第二个常用的解压缩命令gzip。

 

gzip 命令用来压缩文件。gzip是个使用广泛的压缩程序,文件经它压缩过后,其名称后面会多处“.gz”扩展名。

 

gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用。gzip不仅可以用来压缩大的、较少使用的文件以节省磁盘空间,还可以和tar命令一起构成Linux操作系统中比较流行的压缩文件格式。据统计,gzip命令对文本文件有60%~70%的压缩率。减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间。

 

Gzip 并不能压缩目录。只能压缩文件

 

1 :参数选项

-a或——ascii:使用ASCII文字模式;

-d或--decompress或----uncompress:解开压缩文件;

-f或——force:强行压缩文件。不理会文件名称或硬连接是否存在以及该文件是否为符号连接;

-h或——help:在线帮助;

-l或——list:列出压缩文件的相关信息;

-L或——license:显示版本与版权信息;

-n或--no-name:压缩文件时,不保存原来的文件名称及时间戳记;

-N或——name:压缩文件时,保存原来的文件名称及时间戳记;

-q或——quiet:不显示警告信息;

-r或——recursive:递归处理,将指定目录下的所有文件及子目录一并处理;

-S或或----suffix:更改压缩字尾字符串;

-t或——test:测试压缩文件是否正确无误;

-v或——verbose:显示指令执行过程;

-V或——version:显示版本信息;

-:压缩效率是一个介于1~9的数值,预设值为“6”,指定愈大的数值,压缩效率就会愈高;

--best:此参数的效果和指定“-9”参数相同;

--fast:此参数的效果和指定“-1”参数相同。

 

2 :实例

(1): 把当前目录下的每个文件压缩成 .gz 文件

[root@iZuf60ynur81p6k0ysvtneZ opt]# gzip *
gzip: a is a directory -- ignored
gzip: auth is a directory -- ignored
gzip: cp is a directory -- ignored
gzip: first is a directory -- ignored
gzip: mv is a directory -- ignored
gzip: second is a directory -- ignored
gzip: third is a directory -- ignored
[root@iZuf60ynur81p6k0ysvtneZ opt]# ll
total 64
-rw-r--r-- 1 root root   31 Aug 27 14:22 123466.txt.gz
-rwx------ 1 root root  101 Aug 27 18:02 123.txt.gz
-rw-r--r-- 1 root root   24 Aug 27 11:32 2.h.gz
-rw-r--r-- 1 root root   24 Aug 27 11:32 3.c.gz
drwxr-xr-x 3 root root 4096 Aug 27 11:17 a
drwx------ 2 root root 4096 Aug 27 10:49 auth
drwxr-xr-x 2 root root 4096 Aug 27 14:20 cp
-rwxrwxrwx 1 root test   71 Aug 28 11:33 echo.sh.gz
drwxr-xr-x 2 root root 4096 Sep 10 14:50 first
-rw-r--r-- 1 root root  286 Aug 28 11:55 menu.txt.gz
-rw-r--r-- 1 root root   50 Aug 28 11:55 m.gz
drwxr-xr-x 3 root root 4096 Sep  1 10:18 mv
drwxr-xr-x 2 root root 4096 Aug 27 10:48 second
-rw-r--r-- 1 root root  135 Aug 28 10:05 test.txt.gz
drwxr-xr-x 2 root root 4096 Aug 27 10:48 third
-rw-r--r-- 1 root root  290 Aug 28 10:20 total.txt.gz

 

(2): 把上例中每个压缩的文件解压,并列出详细的信息

[root@iZuf60ynur81p6k0ysvtneZ opt]# gzip -dv *
123466.txt.gz:   0.0% -- replaced with 123466.txt
123.txt.gz:        26.5% -- replaced with 123.txt
2.h.gz:       0.0% -- replaced with 2.h
3.c.gz:       0.0% -- replaced with 3.c
gzip: a is a directory -- ignored
gzip: auth is a directory -- ignored
gzip: cp is a directory -- ignored
echo.sh.gz:       4.3% -- replaced with echo.sh
gzip: first is a directory -- ignored
menu.txt.gz:    65.2% -- replaced with menu.txt
m.gz:      -20.0% -- replaced with m
gzip: mv is a directory -- ignored
gzip: second is a directory -- ignored
test.txt.gz:        27.0% -- replaced with test.txt
gzip: third is a directory -- ignored
total.txt.gz:      35.6% -- replaced with total.txt
[root@iZuf60ynur81p6k0ysvtneZ opt]# ll
total 52
-rw-r--r-- 1 root root    0 Aug 27 14:22 123466.txt
-rwx------ 1 root root  102 Aug 27 18:02 123.txt
-rw-r--r-- 1 root root    0 Aug 27 11:32 2.h
-rw-r--r-- 1 root root    0 Aug 27 11:32 3.c
drwxr-xr-x 3 root root 4096 Aug 27 11:17 a
drwx------ 2 root root 4096 Aug 27 10:49 auth
drwxr-xr-x 2 root root 4096 Aug 27 14:20 cp
-rwxrwxrwx 1 root test   47 Aug 28 11:33 echo.sh
drwxr-xr-x 2 root root 4096 Sep 10 14:50 first
-rw-r--r-- 1 root root   25 Aug 28 11:55 m
-rw-r--r-- 1 root root  744 Aug 28 11:55 menu.txt
drwxr-xr-x 3 root root 4096 Sep  1 10:18 mv
drwxr-xr-x 2 root root 4096 Aug 27 10:48 second
-rw-r--r-- 1 root root  148 Aug 28 10:05 test.txt
drwxr-xr-x 2 root root 4096 Aug 27 10:48 third
-rw-r--r-- 1 root root  407 Aug 28 10:20 total.txt

 

(3): 详细显示例 1 中每个压缩的文件的信息,并不解压

[root@iZuf60ynur81p6k0ysvtneZ opt]# gzip -l
^Z
[4]+  Stopped                 gzip -l
[root@iZuf60ynur81p6k0ysvtneZ opt]# gzip -l *
         compressed        uncompressed  ratio uncompressed_name
                 31                   0   0.0% 123466.txt
                101                 102  26.5% 123.txt
                 24                   0   0.0% 2.h
                 24                   0   0.0% 3.c
gzip: a is a directory -- ignored
gzip: auth is a directory -- ignored
gzip: cp is a directory -- ignored
                 71                  47   4.3% echo.sh
gzip: first is a directory -- ignored
                286                 744  65.2% menu.txt
                 50                  25 -20.0% m
gzip: mv is a directory -- ignored
gzip: second is a directory -- ignored
                135                 148  27.0% test.txt
gzip: third is a directory -- ignored
                290                 407  35.6% total.txt
               1012                1473  33.2% (totals)

 

(4): 压缩一个 tar 备份文件,此时压缩文件的扩展名为 .tar.gz

gzip -r echo.tar
 
[root@iZuf60ynur81p6k0ysvtneZ first]# ll
total 8
-rwxrwxrwx 1 root test  47 Aug 28 11:33 echo.sh
-rw-r--r-- 1 root root 163 Sep 10 14:27 echo.tar
[root@iZuf60ynur81p6k0ysvtneZ first]# gzip -r echo.tar
[root@iZuf60ynur81p6k0ysvtneZ first]# ll
total 8
-rwxrwxrwx 1 root test  47 Aug 28 11:33 echo.sh
-rw-r--r-- 1 root root 189 Sep 10 14:27 echo.tar.gz

 

 

(5): 递归的压缩目录

gzip -rv first

 

(6): 递归地解压目录

gzip -dr first

 

以上基本上就是gzip的命令使用情况。

 

有好的建议,请在下方输入你的评论。

相关文章

服务器端口转发,带你了解服务器端口转发
服务器开放端口,服务器开放端口的步骤
产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
如何使用 WinGet 下载 Microsoft Store 应用
百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

发布评论