Home > Article > Operation and Maintenance > How to use the linux gzip compression command
In Linux, the gzip command is used to compress and decompress files. The extension of the new file compressed by this command is usually marked as ".gz", and the syntax is "gzip [option] source document". The source file in the syntax refers to an ordinary file when performing a compression operation; when performing a decompression operation, it refers to a compressed file. The gzip command can only be used to compress files, not directories. Even if a directory is specified, it can only compress all files in the directory.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
gzip is a command often used to compress and decompress files in Linux systems. The extension of the new file compressed by this command is usually marked as ".gz".
I would like to emphasize again that the gzip command can only be used to compress files, not 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: The source file in the
[root@localhost ~]# gzip [选项] 源文件
command refers to an ordinary file when compressing; when decompressing When compressing, it refers to compressing files. The commonly used options and meanings of this command are shown in Table 1.
Options | Meaning |
---|---|
Output compressed data to standard output and preserve the source file. | |
Decompress the compressed file. | |
Recursively compress all files in the specified directory and subdirectories. | |
For each compressed and decompressed file, the corresponding file name and compression ratio are displayed. | |
For each compressed file, the following fields are displayed: |
|
is used to specify the compression level. -1 has the lowest compression level and the worst compression ratio; -9 has the highest compression ratio. The default compression ratio is -6. |
[root@localhost ~]# gzip install.log #压缩instal.log 文件 [root@localhost ~]# ls anaconda-ks.cfg install.log.gz install.log.syslog #压缩文件生成,但是源文件也消失了[Example 2] Keep source file compressionWhen using the gzip command to compress a file, the source file will disappear, thus generating a compressed file. At this time, some people will have obsessive-compulsive disorder and ask the author: Can you prevent the source file from disappearing when compressing the file? Okay, it's possible, but it's very awkward.
[root@localhost ~]# gzip -c anaconda-ks.cfg >anaconda-ks.cfg.gz #使用-c选项,但是不让压缩数据输出到屏幕上,而是重定向到压缩文件中,这样可以缩文件的同时不删除源文件 [root@localhost ~]# ls anaconda-ks.cfg anaconda-ks.cfg.gz install.log.gz install.log.syslog #可以看到压缩文件和源文件都存在[Example 3] Compressing directoriesWe may take it for granted that the gzip command can compress directories. Let’s try it:
[root@localhost ~]# mkdir test [root@localhost ~]# touch test/test1 [root@localhost ~]# touch test/test2 [root@localhost ~]# touch test/test3 #建立测试目录,并在里面建立几个测试文件 [root@localhost ~]# gzip -r test/ #压缩目录,并没有报错 [root@localhost ~]# ls anaconda-ks.cfg anaconda-ks.cfg.gz install.log.gz install.log.syslog test #但是查看发现test目录依然存在,并没有变为压缩文件 [root@localhost ~]# ls test/ testl .gz test2.gz test3.gz #原来gzip命令不会打包目录,而是把目录下所有的子文件分别压缩In Linux, packaging and compression are handled separately. The gzip command can only compress, not package, so there will be a situation where there is no packaging directory, but only the files in the directory are compressed.
Case demonstration:
Compressed file[root@localhost ~]# ls //显示当前目录文件 a.c b.h d.cpp [root@localhost ~]# gzip * //压缩目录下的所有文件 [root@localhost ~]# ls //显示当前目录文件 a.c.gz b.h.gz d.cpp.gz [root@localhost ~]#Continue from example 1 and list detailed information
gzip -dv * //解压文件,并列出详细信息Continue with Example 1, display compressed file information
gzip -l *Related recommendations: "
Linux Video Tutorial"
The above is the detailed content of How to use the linux gzip compression command. For more information, please follow other related articles on the PHP Chinese website!