How to use the linux gzip compression command

青灯夜游
Release: 2022-11-25 19:28:28
Original
10830 people have browsed it

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.

How to use the linux gzip compression command

#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 [选项] 源文件
Copy after login

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.

##- cOutput compressed data to standard output and preserve the source file. -dDecompress the compressed file. -rRecursively compress all files in the specified directory and subdirectories. -vFor each compressed and decompressed file, the corresponding file name and compression ratio are displayed. -lFor each compressed file, the following fields are displayed: -The number 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.
Table 1 gzip command common options and their meanings
OptionsMeaning
    The size of the compressed file;
  • Uncompressed file The size;
  • Compression ratio;
  • The name of the uncompressed file.
[Example 1] Basic compression

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

[root@localhost ~]# gzip install.log
#压缩instal.log 文件
[root@localhost ~]# ls
anaconda-ks.cfg install.log.gz install.log.syslog
#压缩文件生成,但是源文件也消失了
Copy after login

[Example 2] Keep source file compression

When 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
#可以看到压缩文件和源文件都存在
Copy after login

[Example 3] Compressing directories

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命令不会打包目录,而是把目录下所有的子文件分别压缩
Copy after login

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 ~]#
Copy after login

Continue from example 1 and list detailed information

gzip -dv * //解压文件,并列出详细信息
Copy after login

How to use the linux gzip compression command

Continue with Example 1, display compressed file information

gzip -l *
Copy after login

How to use the linux gzip compression command

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!