Linux command gzip command details and use instances

Source: Internet
Author: User
Tags uncompress
There are two obvious advantages to reduce the file size. one is to reduce the storage space, and the other is to reduce the transmission time when the file is transmitted over the network. Gzip is a frequently used command in Linux to compress and decompress files. it is convenient and easy to use.

Gzip can be used not only to compress large and rarely used files to save disk space, but also to form popular compressed file formats in the Linux operating system together with the tar command. According to statistics, the gzip command has 60% ~ 70% compression rate.

1. command format:

Gzip [parameter] [file or directory]

2. command functions:

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

3. command parameters:

-A or -- ascii uses the ASCII text mode.

-C or -- stdout or -- to-stdout outputs the compressed file to the standard output device, without changing the original file.

-D or -- decompress or ---- uncompress to uncompress the compressed file.

-F or -- force forcibly compresses the file. Ignore whether the file name or hard connection exists and whether the file is signed.

-H or -- help online help.

-L or -- list lists information about compressed files.

-L or -- license displays version and copyright information.

-N or -- no-name: the original file name and time stamp are not saved.

-N or -- name: the original file name and time stamp are saved when the file is compressed.

-Q or -- quiet does not display warning information.

-R or -- recursive processing: All files and subdirectories under the specified directory are processed together.

-S <压缩字尾字符串> Or ---- suffix <压缩字尾字符串> Change the compressed character string.

-T or -- test to test whether the compressed file is correct.

-V or -- verbose displays the command execution process.

-V or -- version displays version information.

-Num uses the specified numeric num to adjust the compression speed.-1 or -- fast indicates the fastest compression method (low compression ratio), and-9 or -- best indicates the slowest compression method (high compression ratio ). The default value is 6.

4. example:

Example 1: compress each file in the test6directory into A. gz file.

Command:


Copy codeThe code is as follows: gzip *

Output:


Copy codeThe code is as follows:
[Root @ localhost test6] # ll
Total 604
--- Xr -- r -- 1 root mail 302108 11-30 linklog. log
--- Xr -- r -- 1 mail users 302108 11-30 log2012.log
-Rw-r -- 1 mail users 61 11-30 08:39 log2013.log
-Rw-r -- 1 root mail 0 11-30 08:39 log2014.log
-Rw-r -- 1 root mail 0 11-30 08:39 log2015.log
-Rw-r -- 1 root mail 0 11-30 08:39 log2016.log
-Rw-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 linklog.log.gz
--- Xr -- r -- 1 mail users 1341 11-30 log2012.log.gz
-Rw-r -- 1 mail users 70 11-30 08:39 log2013.log.gz
-Rw-r -- 1 root mail 32 11-30 08:39 log2014.log.gz
-Rw-r -- 1 root mail 32 11-30 08:39 log2015.log.gz
-Rw-r -- 1 root mail 32 11-30 08:39 log2016.log.gz
-Rw-r -- 1 root mail 32 11-30 08:39 log2017.log.gz
[Root @ localhost test6] #

Example 2: extract each compressed file in example 1 and list the detailed information.

Command:


Copy codeThe code is as follows:
Gzip-dv *

Output:


Copy codeThe code is as follows:
[Root @ localhost test6] # ll
Total 28
--- Xr -- r -- 1 root mail 1341 11-30 linklog.log.gz
--- Xr -- r -- 1 mail users 1341 11-30 log2012.log.gz
-Rw-r -- 1 mail users 70 11-30 08:39 log2013.log.gz
-Rw-r -- 1 root mail 32 11-30 08:39 log2014.log.gz
-Rw-r -- 1 root mail 32 11-30 08:39 log2015.log.gz
-Rw-r -- 1 root mail 32 11-30 08:39 log2016.log.gz
-Rw-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 linklog. log
--- Xr -- r -- 1 mail users 302108 11-30 log2012.log
-Rw-r -- 1 mail users 61 11-30 08:39 log2013.log
-Rw-r -- 1 root mail 0 11-30 08:39 log2014.log
-Rw-r -- 1 root mail 0 11-30 08:39 log2015.log
-Rw-r -- 1 root mail 0 11-30 08:39 log2016.log
-Rw-r -- 1 root mail 0 11-30 08:39 log2017.log
[Root @ localhost test6] #

Example 3: Detailed information of each compressed file in Example 1 is displayed without decompression

Command:


Copy codeThe code is as follows: gzip-l *

Output:


Copy codeThe code is as follows:
[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 Tartar file with the extension name .tar.gz.

Command:


Copy codeThe code is as follows: gzip-r log.tar

Output:


Copy codeThe code is as follows:
[Root @ localhost test] # ls-al log.tar
-Rw-r -- 1 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 -- 1 root 1421 11-29 :54 log.tar.gz

Example 5: recursive compression Directory

Command:


Copy codeThe code is as follows:
Gzip-rv test6

Output:


Copy codeThe code is as follows:
[Root @ localhost test6] # ll
Total 604
--- Xr -- r -- 1 root mail 302108 11-30 linklog. log
--- Xr -- r -- 1 mail users 302108 11-30 log2012.log
-Rw-r -- 1 mail users 61 11-30 08:39 log2013.log
-Rw-r -- 1 root mail 0 11-30 08:39 log2014.log
-Rw-r -- 1 root mail 0 11-30 08:39 log2015.log
-Rw-r -- 1 root mail 0 11-30 08:39 log2016.log
-Rw-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 linklog.log.gz
--- Xr -- r -- 1 mail users 1341 11-30 log2012.log.gz
-Rw-r -- 1 mail users 70 11-30 08:39 log2013.log.gz
-Rw-r -- 1 root mail 32 11-30 08:39 log2014.log.gz
-Rw-r -- 1 root mail 32 11-30 08:39 log2015.log.gz
-Rw-r -- 1 root mail 32 11-30 08:39 log2016.log.gz
-Rw-r -- 1 root mail 32 11-30 08:39 log2017.log.gz

Note:

In this way, all files under the test are changed to *. gz, and the Directory still exists, but the files in the directory are changed to *. gz. this is compression, which is different from packaging. Because it is a directory operation, you need to add the-r option, so that you can also perform recursion on the subdirectory.

Example 6: recursively decompress the Directory

Command:


Copy codeThe code is as follows:
Gzip-dr test6

Output:


Copy codeThe code is as follows:
[Root @ localhost test6] # ll
Total 28
--- Xr -- r -- 1 root mail 1341 11-30 linklog.log.gz
--- Xr -- r -- 1 mail users 1341 11-30 log2012.log.gz
-Rw-r -- 1 mail users 70 11-30 08:39 log2013.log.gz
-Rw-r -- 1 root mail 32 11-30 08:39 log2014.log.gz
-Rw-r -- 1 root mail 32 11-30 08:39 log2015.log.gz
-Rw-r -- 1 root mail 32 11-30 08:39 log2016.log.gz
-Rw-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 linklog. log
--- Xr -- r -- 1 mail users 302108 11-30 log2012.log
-Rw-r -- 1 mail users 61 11-30 08:39 log2013.log
-Rw-r -- 1 root mail 0 11-30 08:39 log2014.log
-Rw-r -- 1 root mail 0 11-30 08:39 log2015.log
-Rw-r -- 1 root mail 0 11-30 08:39 log2016.log
-Rw-r -- 1 root mail 0 11-30 08:39 log2017.log
[Root @ localhost test6] #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.