One linux command per day: gzip command

Keywords: Linux ascii network less

Reducing file size has two obvious advantages: one is to reduce storage space; the other is to reduce transmission time when transferring files over the network. Gzip is a command used frequently in Linux system to compress and decompress files, which is convenient and easy to use. Gzip can be used not only to compress large and less used files to save disk space, but also to compose a popular compressed file format in Linux operating system with tar command. According to statistics, the gzip command has a compression rate of 60%-70% for text files.

1. Command format:

gzip [parameter] [file or directory]

2. Command function:

gzip is a widely used compression program. When a file is compressed, its name will be followed by an extension of ".gz".

3. Command parameters:

  • - a or - ASCII uses ASCII text mode.
  • - c or - stdout or - to-stdout output the compressed file to the standard output device without changing the original file.
  • - d or --- decompress or --- uncompress uncompress to uncompress the compressed file.
  • - f or force to compress files. Ignore the existence of a file name or hard connection and whether the file is a symbolic connection.
  • - h or --- help online help.
  • - l or - list lists information about compressed files.
  • - L or --- license displays version and copyright information.
  • - The original file name and time stamp are not saved when n or no-name compresses the file.
  • - When N or --- name compresses a file, save the original file name and time stamp.
  • - q or - quiet does not display warning information.
  • - r or - recursive recursive processing, which processes all files and subdirectories in a specified directory.
  • - S < Compressed End String > or - suffix < Compressed End String > Change Compressed End String.
  • - t or --- test to test whether the compressed file is correct.
  • - v or verbose displays the instruction execution process.
  • - V or - version displays version information.
  • - num adjusts the compression speed with the specified number num, - 1 or - fast denotes the fastest compression method (low compression ratio), and - 9 or - best denotes the slowest compression method (high compression ratio). The default value of the system is 6.

4. Use examples:

Example 1: Compress each file in the test6 directory into. gz files
Order:

gzip *

Output:

[root@localhost test6]# ll
Total 604
---xr--r-- 1 root mail  302108 11-30 08:39 linklog.log
--xr--r-- 1 mail users 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 mail users     61 11-30 08:39 log2013.log
-rw-r--r-- 1 root mail       0 11-30 08:39 log2014.log
-rw-r--r-- 1 root mail       0 11-30 08:39 log2015.log
-rw-r--r-- 1 root mail       0 11-30 08:39 log2016.log
-rw-r--r-- 1 root mail       0 11-30 08:39 log2017.log
[root@localhost test6]# gzip *
[root@localhost test6]# ll
Total 28
---xr--r-- 1 root mail  1341 11-30 08:39 linklog.log.gz
---xr--r-- 1 mail users 1341 11-30 08:39 log2012.log.gz
-rw-r--r-- 1 mail users   70 11-30 08:39 log2013.log.gz
-rw-r--r-- 1 root mail    32 11-30 08:39 log2014.log.gz
-rw-r--r-- 1 root mail    32 11-30 08:39 log2015.log.gz
-rw-r--r-- 1 root mail    32 11-30 08:39 log2016.log.gz
-rw-r--r-- 1 root mail    32 11-30 08:39 log2017.log.gz
[root@localhost test6]#

Example 2: Unzip each compressed file in Example 1 and list detailed information
Order:

gzip -dv *

Output:

[root@localhost test6]# ll
Total 28
---xr--r-- 1 root mail  1341 11-30 08:39 linklog.log.gz
---xr--r-- 1 mail users 1341 11-30 08:39 log2012.log.gz
-rw-r--r-- 1 mail users   70 11-30 08:39 log2013.log.gz
-rw-r--r-- 1 root mail    32 11-30 08:39 log2014.log.gz
-rw-r--r-- 1 root mail    32 11-30 08:39 log2015.log.gz
-rw-r--r-- 1 root mail    32 11-30 08:39 log2016.log.gz
-rw-r--r-- 1 root mail    32 11-30 08:39 log2017.log.gz
[root@localhost test6]# gzip -dv *
linklog.log.gz:  99.6% -- replaced with linklog.log
log2012.log.gz:  99.6% -- replaced with log2012.log
log2013.log.gz:  47.5% -- replaced with log2013.log
log2014.log.gz:   0.0% -- replaced with log2014.log
log2015.log.gz:   0.0% -- replaced with log2015.log
log2016.log.gz:   0.0% -- replaced with log2016.log
log2017.log.gz:   0.0% -- replaced with log2017.log
[root@localhost test6]# ll
Total 604
---xr--r-- 1 root mail  302108 11-30 08:39 linklog.log
---xr--r-- 1 mail users 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 mail users     61 11-30 08:39 log2013.log
-rw-r--r-- 1 root mail       0 11-30 08:39 log2014.log
-rw-r--r-- 1 root mail       0 11-30 08:39 log2015.log
-rw-r--r-- 1 root mail       0 11-30 08:39 log2016.log
-rw-r--r-- 1 root mail       0 11-30 08:39 log2017.log
[root@localhost test6]#

Example 3: Detailed display of information for each compressed file in Example 1 without decompression

Order:

gzip -l *

Output:

[root@localhost test6]# gzip -l *
        compressed        uncompressed  ratio uncompressed_name
             1341              302108  99.6% linklog.log
             1341              302108  99.6% log2012.log
               70                  61  47.5% log2013.log
               32                   0   0.0% log2014.log
               32                   0   0.0% log2015.log
               32                   0   0.0% log2016.log
               32                   0   0.0% log2017.log
               2880              604277  99.5% (totals)

Example 4: Compress a tar backup file with the extension. tar.gz
Order:

gzip -r log.tar

Output:

[root@localhost test]# ls -al log.tar
-rw-r--r-- 1 root root 307200 11-29 17:54 log.tar
[root@localhost test]# gzip -r log.tar
[root@localhost test]# ls -al log.tar.gz 
-rw-r--r-- 1 root root 1421 11-29 17:54 log.tar.gz

Example 5: Recursive Compressed Directory
Order:

gzip -rv test6

Output:

[root@localhost test6]# ll
//Total 604
---xr--r-- 1 root mail  302108 11-30 08:39 linklog.log
---xr--r-- 1 mail users 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 mail users     61 11-30 08:39 log2013.log
-rw-r--r-- 1 root mail       0 11-30 08:39 log2014.log
-rw-r--r-- 1 root mail       0 11-30 08:39 log2015.log
-rw-r--r-- 1 root mail       0 11-30 08:39 log2016.log
-rw-r--r-- 1 root mail       0 11-30 08:39 log2017.log
[root@localhost test6]# cd ..
[root@localhost test]# gzip -rv test6
test6/log2014.log:        0.0% -- replaced with test6/log2014.log.gz
test6/linklog.log:       99.6% -- replaced with test6/linklog.log.gz
test6/log2015.log:        0.0% -- replaced with test6/log2015.log.gz
test6/log2013.log:       47.5% -- replaced with test6/log2013.log.gz
test6/log2012.log:       99.6% -- replaced with test6/log2012.log.gz
test6/log2017.log:        0.0% -- replaced with test6/log2017.log.gz
test6/log2016.log:        0.0% -- replaced with test6/log2016.log.gz
[root@localhost test]# cd test6
[root@localhost test6]# ll
//Total 28
---xr--r-- 1 root mail  1341 11-30 08:39 linklog.log.gz
---xr--r-- 1 mail users 1341 11-30 08:39 log2012.log.gz
-rw-r--r-- 1 mail users   70 11-30 08:39 log2013.log.gz
-rw-r--r-- 1 root mail    32 11-30 08:39 log2014.log.gz
-rw-r--r-- 1 root mail    32 11-30 08:39 log2015.log.gz
-rw-r--r-- 1 root mail    32 11-30 08:39 log2016.log.gz
-rw-r--r-- 1 root mail    32 11-30 08:39 log2017.log.gz

Explain:

In this way, all the files under test become. gz, and the directory still exists, but the files in the directory become. GZ accordingly. This is compression, unlike packaging. Because it's a directory operation, you need to add the - r option, so you can also recurse the subdirectory.

Example 6: Decompress directories recursively

Order:

gzip -dr test6

Output:

[root@localhost test6]# ll
Total 28
---xr--r-- 1 root mail  1341 11-30 08:39 linklog.log.gz
---xr--r-- 1 mail users 1341 11-30 08:39 log2012.log.gz
-rw-r--r-- 1 mail users   70 11-30 08:39 log2013.log.gz
-rw-r--r-- 1 root mail    32 11-30 08:39 log2014.log.gz
-rw-r--r-- 1 root mail    32 11-30 08:39 log2015.log.gz
-rw-r--r-- 1 root mail    32 11-30 08:39 log2016.log.gz
-rw-r--r-- 1 root mail    32 11-30 08:39 log2017.log.gz
[root@localhost test6]# cd ..
[root@localhost test]# gzip -dr test6
[root@localhost test]# cd test6
[root@localhost test6]# ll
Total 604
---xr--r-- 1 root mail  302108 11-30 08:39 linklog.log
---xr--r-- 1 mail users 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 mail users     61 11-30 08:39 log2013.log
-rw-r--r-- 1 root mail       0 11-30 08:39 log2014.log
-rw-r--r-- 1 root mail       0 11-30 08:39 log2015.log
-rw-r--r-- 1 root mail       0 11-30 08:39 log2016.log
-rw-r--r-- 1 root mail       0 11-30 08:39 log2017.log
[root@localhost test6]#

Posted by Zallus on Tue, 26 Mar 2019 04:09:31 -0700