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 [options] source file". Ordinary files are called source files in the syntax during compression operations, while compressed files are referred to as source files during decompression operations. Even if a directory is specified, the gzip command can only compress all files in the directory, not the entire directory.
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".
Please note that the gzip command only works on compressed files, not directories. Even if a directory is specified, it will only compress all files in that 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.
Meaning | |
---|---|
Output the compressed data to standard output, preserving 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 is the lowest compression level and the worst compression ratio; -9 is the compression ratio Highest. 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 compression
<p></p>
When using the gzip command to compress a file, the source file will be deleted and a compressed file will be generated. 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】 Compressed directory
<p></p>
We 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.
<p></p>
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 the details information
<p></p>
gzip -dv * //解压文件,并列出详细信息
<p><img src="https://img.php.cn/upload/article/000/887/227/168567942114139.png" alt="linux gzip压缩命令如何使用"></p>Continue with Example 1, display compressed file information<p><pre class="brush:js;toolbar:false">gzip -l *</pre></p>
<p><img src="https://img.php.cn/upload/article/000/887/227/168567942270018.jpg" alt="linux gzip压缩命令如何使用"></p>
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!