Implementation principle and code example of Linux gzip command compressed file

  • 2021-08-28 21:50:36
  • OfStack

gzip is a command commonly used in Linux systems to compress and decompress files. The new file compressed by this command is usually marked with the extension ". gz".

Emphasize again 1, gzip command can only be used to compress files, not compress directories. Even if a directory is specified, it can only compress all files in the directory.

The basic format of the gzip command is as follows:

[root @ localhost ~] # gzip [Options] Source

The source file in the command refers to the ordinary file when compressing the operation; When decompressing, it refers to compressed files. The common options and meanings of this command are shown in Table 1.

Table 1 Common options and meanings of gzip command

选项 含义
-c 将压缩数据输出到标准输出中,并保留源文件。
-d 对压缩文件进行解压缩。
-r 递归压缩指定目录下以及子目录下的所有文件。
-v 对于每个压缩和解压缩的文件,显示相应的文件名和压缩比。
-l 对每1个压缩文件,显示以下字段:
  • 压缩文件的大小;
  • 未压缩文件的大小;
  • 压缩比;
  • 未压缩文件的名称。
-数字 用于指定压缩等级,-1 压缩等级最低,压缩比最差;-9 压缩比最高。默认压缩比是 -6。

[Example 1] Basic compression

The gzip compression command is very simple, and you don't even need to specify the compressed package name after compression, just specify the source file name. Let's try:

[root@localhost ~]# gzip install.log
# Compress the instal. log file
[root@localhost ~]# ls
anaconda-ks.cfg install.log.gz install.log.syslog
The compressed file is generated, but the source file also disappears

[Example 2] Retain source file compression

When you use the gzip command to compress a file, the source file disappears, resulting in a compressed file. At this time, some people will have obsessive-compulsive disorder, so they will force the author: Can you compress the file and prevent the source file from disappearing? Well, it's ok, but it's awkward.

[root@localhost ~]# gzip -c anaconda-ks.cfg > anaconda-ks.cfg.gz
# Uses the-c option, but does not let the compressed data output to the screen, but redirects to the compressed file, so that the file can be reduced without deleting the source file
[root@localhost ~]# ls
anaconda-ks.cfg anaconda-ks.cfg.gz install.log.gz install.log.syslog
# You can see that both the zip file and the source file exist

[Example 3] Compressed directory

We may take it for granted that the gzip command can compress the directory. Let's try:

[root@localhost ~]# mkdir test
[root@localhost ~]# touch test/test1
[root@localhost ~]# touch test/test2
[root @ localhost ~] # touch test/test3 # Create a test directory and several test files in it
[root@localhost ~]# gzip -r test/
# Compressed directory, no error reported
[root@localhost ~]# ls
anaconda-ks.cfg anaconda-ks.cfg.gz install.log.gz install.log.syslog test
# But the test directory still exists and has not become a zip file
[root@localhost ~]# ls test/
testl .gz test2.gz test3.gz
The original gzip command does not package the directory, but compresses all the sub-files in the directory separately

In Linux, packaging and compression are handled separately. The gzip command will only compress, not package, so there will be no package directory, but only compress the files under the directory.


Related articles: