gzip:压缩或解压文件

xiaohai 2021-05-05 20:51:29 1737人围观 标签: Linux 
简介gzip命令用于将一个大的文件通过压缩算法变成一个小的文件。gzip不能直接压缩目录,因此目录需要tar进行打包成一个文件,然后tar再调用gzip进行压缩。
说明

  gzip命令用于将一个大的文件通过压缩算法变成一个小的文件。gzip不能直接压缩目录,因此目录需要tar进行打包成一个文件,然后tar再调用gzip进行压缩。

注意:gzip压缩后,源文件将会被删除,解压后,压缩文件也会被删除。

格式

  gzip [option] [file]

常用参数
  • -d:解开压缩文件
  • -v:显示执行的过程
  • -l:不解压列出压缩文件的内容信息
  • -c:将内容输出到标准输出,不改变原始文件
  • -r:对目录下的所有文件递归进行压缩操作
  • -数字[1-9]:指定压缩率,默认为6,值越大压缩率越高
  • -t:测试,检查压缩文件是否完整
示例

1、压缩所有的.txt文件

[root@localhost test]# ll
total 4
-rw-r----x. 1 root root 11 May  7 22:45 test_01.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_02.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_03.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_04.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_05.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_06.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_07.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_08.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_09.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_10.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_11.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_12.txt
-rw-r--r--. 1 root root  0 May  6 07:24 test.txt
[root@localhost test]# gzip *.txt
[root@localhost test]# ll #压缩后,源文件被删除了
total 52
-rw-r----x. 1 root root 43 May  7 22:45 test_01.txt.gz
-rw-r--r--. 1 root root 32 May  6 07:22 test_02.txt.gz
-rw-r--r--. 1 root root 32 May  6 07:22 test_03.txt.gz
-rw-r--r--. 1 root root 32 May  6 07:22 test_04.txt.gz
-rw-r--r--. 1 root root 32 May  6 07:22 test_05.txt.gz
-rw-r--r--. 1 root root 32 May  6 07:22 test_06.txt.gz
-rw-r--r--. 1 root root 32 May  6 07:22 test_07.txt.gz
-rw-r--r--. 1 root root 32 May  6 07:22 test_08.txt.gz
-rw-r--r--. 1 root root 32 May  6 07:22 test_09.txt.gz
-rw-r--r--. 1 root root 32 May  6 07:22 test_10.txt.gz
-rw-r--r--. 1 root root 32 May  6 07:22 test_11.txt.gz
-rw-r--r--. 1 root root 32 May  6 07:22 test_12.txt.gz
-rw-r--r--. 1 root root 29 May  6 07:24 test.txt.gz

2、不解压显示压缩文件的信息

[root@localhost test]# gzip -l *.txt.gz
         compressed        uncompressed  ratio uncompressed_name
                 43                  11 -18.2% test_01.txt
                 32                   0   0.0% test_02.txt
                 32                   0   0.0% test_03.txt
                 32                   0   0.0% test_04.txt
                 32                   0   0.0% test_05.txt
                 32                   0   0.0% test_06.txt
                 32                   0   0.0% test_07.txt
                 32                   0   0.0% test_08.txt
                 32                   0   0.0% test_09.txt
                 32                   0   0.0% test_10.txt
                 32                   0   0.0% test_11.txt
                 32                   0   0.0% test_12.txt
                 29                   0   0.0% test.txt
                424                  11 -3509.1% (totals)

3、解压文件,并显示解压的过程

[root@localhost test]# gzip -dv *.txt.gz
test_01.txt.gz:    -18.2% -- replaced with test_01.txt
test_02.txt.gz:      0.0% -- replaced with test_02.txt
test_03.txt.gz:      0.0% -- replaced with test_03.txt
test_04.txt.gz:      0.0% -- replaced with test_04.txt
test_05.txt.gz:      0.0% -- replaced with test_05.txt
test_06.txt.gz:      0.0% -- replaced with test_06.txt
test_07.txt.gz:      0.0% -- replaced with test_07.txt
test_08.txt.gz:      0.0% -- replaced with test_08.txt
test_09.txt.gz:      0.0% -- replaced with test_09.txt
test_10.txt.gz:      0.0% -- replaced with test_10.txt
test_11.txt.gz:      0.0% -- replaced with test_11.txt
test_12.txt.gz:      0.0% -- replaced with test_12.txt
test.txt.gz:      0.0% -- replaced with test.txt
[root@localhost test]# ll #源压缩文件被删除了
total 4
-rw-r----x. 1 root root 11 May  7 22:45 test_01.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_02.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_03.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_04.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_05.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_06.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_07.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_08.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_09.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_10.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_11.txt
-rw-r--r--. 1 root root  0 May  6 07:22 test_12.txt
-rw-r--r--. 1 root root  0 May  6 07:24 test.txt

4、压缩解压保留源文件(单文件压缩,使用不方便)

[root@localhost test]# cp /etc/services .
[root@localhost test]# ll -h services 
-rw-r--r--. 1 root root 655K May 14 15:17 services
[root@localhost test]# gzip -c services > services.gz #使用-c参数
[root@localhost test]# ll -h services*
-rw-r--r--. 1 root root 655K May 14 15:17 services
-rw-r--r--. 1 root root 133K May 14 15:17 services.gz
[root@localhost test]# gzip -dc services.gz > services2
[root@localhost test]# ll -h services*
-rw-r--r--. 1 root root 655K May 14 15:17 services
-rw-r--r--. 1 root root 655K May 14 15:17 services2
-rw-r--r--. 1 root root 133K May 14 15:17 services.gz
[root@localhost test]# diff services services2

gzip压缩和解压文件不能保存源文件,可以使用-c参数能解决,但是很不方便。那么gzip套件还包含了许多可以”在原地”处理压缩文件的实用程序。zcat、zgrep、zless、zdiff等作用于cat、grep、less和diff相同,但是它们的操作是压缩文件。

[root@localhost test]# zcat services.gz|head #查看压缩文件的里的内容,也可以重定向到一个新文件
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, most entries here have two entries
# even if the protocol doesn't support UDP operations.
# Updated from RFC 1700, ``Assigned Numbers'' (October 1994).  Not all ports