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

 

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


目录
相关文章
|
2月前
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
364 1
二、Linux文本处理与文件操作核心命令
|
2月前
|
Linux
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
274 137
|
2月前
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
612 57
|
1月前
|
存储 安全 Linux
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
Linux虚拟机遇紧急模式?别慌!多因磁盘挂载失败。本文教你通过日志定位问题,用`xfs_repair`等工具修复文件系统,三步快速恢复。掌握查日志、修磁盘、验重启,轻松应对紧急模式,保障系统稳定运行。
308 2
|
2月前
|
缓存 监控 Linux
Linux内存问题排查命令详解
Linux服务器卡顿?可能是内存问题。掌握free、vmstat、sar三大命令,快速排查内存使用情况。free查看实时内存,vmstat诊断系统整体性能瓶颈,sar实现长期监控,三者结合,高效定位并解决内存问题。
191 0
Linux内存问题排查命令详解
|
2月前
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
218 16
|
3月前
|
Linux 网络安全 开发工具
技术栈:这50条最常用的 Linux 命令你一定要会!
建议多在终端中实践,遇到不懂的命令就用 man 或 --help 了解详情!
482 0
|
3月前
|
安全 Linux Shell
Linux系统中sudo命令的高效运用技巧。
用户可以通过sudo -l来列出自己目前可执行的命令列表,这有助于用户了解自己的权限范围。
161 0
|
3月前
|
监控 Linux Shell
linux命令
常用 Linux 命令汇总
|
4月前
|
Linux C++
每天一个linux命令(8):cp 命令
cp 命令是 Linux 中用于复制文件或目录的命令。它的名字来源于英文单词 copy。这个命令非常常用,特别是在需要备份文件或创建文件副本时。
221 0