How to use the linux gzip compression command

WBOY
Release: 2023-06-02 12:17:00
forward
3562 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 [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 [选项] 源文件
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.

##OptionsMeaning-cOutput the compressed data to standard output, preserving 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 is the lowest compression level and the worst compression ratio; -9 is the compression ratio Highest. The default compression ratio is -6.
Table 1 Common options and meanings of gzip command
  • The size of the compressed file;

  • The size of the uncompressed file;

  • 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 <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
#可以看到压缩文件和源文件都存在
Copy after login
【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命令不会打包目录,而是把目录下所有的子文件分别压缩
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. <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 ~]#
Copy after login
Continue from Example 1 and list the details information <p></p>
gzip -dv * //解压文件,并列出详细信息
Copy after login

linux gzip压缩命令如何使用

Continue with Example 1, display compressed file information

gzip -l *
Copy after login

linux gzip压缩命令如何使用

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:yisu.com
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