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

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

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的命令使用情况。

 

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


目录
相关文章
|
28天前
|
Ubuntu 搜索推荐 Linux
详解Ubuntu的strings与grep命令:Linux开发的实用工具。
这就是Ubuntu中的strings和grep命令,透明且强大。我希望你喜欢这个神奇的世界,并能在你的Linux开发旅程上,通过它们找到你的方向。记住,你的电脑是你的舞台,在上面你可以做任何你想做的事,只要你敢于尝试。
105 32
|
27天前
|
监控 Linux
Linux命令大全:echo与tail实现输出重定向。
这样,我们实现了使用echo和tail命令进行输出重定向的目的。在实际应用中,输出重定向技巧可节省时间,提高工作效率。希望本文内容对您了解和掌握Linux系统中echo与tail命令以及输出重定向的操作有所帮助。
96 27
|
24天前
|
消息中间件 Linux Kafka
linux命令使用消费kafka的生产者、消费者
linux命令使用消费kafka的生产者、消费者
103 16
|
1月前
|
缓存 Linux 数据安全/隐私保护
Linux环境下如何通过手动调用drop_caches命令释放内存
总的来说,记录住“drop_caches” 命令并理解其含义,可以让你在日常使用Linux的过程中更加娴熟和自如。
134 23
|
29天前
|
Ubuntu Linux
"unzip"命令解析:Linux下如何处理压缩文件。
总的来说,`unzip`命令是Linux系统下一款实用而方便的ZIP格式文件处理工具。本文通过简明扼要的方式,详细介绍了在各类Linux发行版上安装 `unzip`的方法,以及如何使用 `unzip`命令进行解压、查看和测试ZIP文件。希望本文章能为用户带来实际帮助,提高日常操作的效率。
198 12
|
1月前
|
消息中间件 NoSQL Linux
Redis的基本介绍和安装方式(包括Linux和Windows版本),以及常用命令的演示
Redis(Remote Dictionary Server)是一个高性能的开源键值存储数据库。它支持字符串、列表、散列、集合等多种数据类型,具有持久化、发布/订阅等高级功能。由于其出色的性能和广泛的使用场景,Redis在应用程序中常作为高速缓存、消息队列等用途。
378 16
|
13天前
|
Linux
linux文件重命名命令
本指南介绍Linux文件重命名方法,包括单文件操作的`mv`命令和批量处理的`rename`命令。`mv`可简单更改文件名并保留扩展名,如`mv old_file.txt new_name.txt`;`rename`支持正则表达式,适用于复杂批量操作,如`rename &#39;s/2023/2024/&#39; *.log`。提供实用技巧如大小写转换、数字序列处理等,并提醒覆盖风险与版本差异,建议使用`-n`参数预览效果。
|
13天前
|
安全 Linux 网络安全
Linux wget 常用命令详解
wget 是一款强大的命令行下载工具,支持 HTTP/HTTPS/FTP 协议。本文详细介绍其基础用法、高效下载参数、高级应用场景及参数速查。内容涵盖断点续传(-c)、后台下载(-b)、限速下载(--limit-rate)、递归下载(-r)、整站镜像(-mk)等实用功能,同时提供文件管理、网络优化与安全下载配置方法,助您高效完成各类下载任务。
|
2月前
|
Linux
Linux命令的基本格式解析
总的来说,Linux命令的基本格式就像一个食谱,它可以指导你如何使用你的计算机。通过学习和实践,你可以成为一个真正的“计算机厨师”,创造出各种“美味”的命令。
84 15
|
NoSQL Java Linux
Linux常用命令(文件目录操作、拷贝移动、打包压缩、文本编辑、查找)
Linux常用命令(文件目录操作、拷贝移动、打包压缩、文本编辑、查找)
OSZAR »