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 #压缩文件生成,但是源文件也消失了
[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 #可以看到压缩文件和源文件都存在
[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命令不会打包目录,而是把目录下所有的子文件分别压缩
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 ~]#
gzip -dv * //解压文件,并列出详细信息
gzip -l *
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!