Linux gzip command compression file implementation principle and code examples

Linux gzip command compression file implementation principle and code examples

gzip is a command often used in Linux systems to compress and decompress files. The new file compressed by this command is usually marked with the extension ".gz".

Let me emphasize again that the gzip command can only be used to compress files, not directories. Even if a directory is specified, only all files in the directory can be compressed.

The basic format of the gzip command is as follows:

[root@localhost ~]# gzip [options] source file

The source file in the command refers to a common file when performing a compression operation, and refers to a compressed file when performing a decompression operation. The commonly used options and their meanings of this command are shown in Table 1.

Table 1 Common options and meanings of the gzip command

Options meaning
-c Print the compressed data to standard output and preserve the original file.
-d Decompress the compressed file.
-r Recursively compress all files in the specified directory and its subdirectories.
-v For each compressed and decompressed file, the corresponding file name and compression ratio are displayed.
-l For each compressed file, the following fields are displayed:
  • The size of the compressed file;
  • The size of the uncompressed file;
  • Compression ratio;
  • The name of the uncompressed file.
-number Used to specify the compression level, -1 is the lowest compression level and the worst compression ratio; -9 is the highest compression ratio. The default compression ratio is -6.

【Example 1】Basic compression

The gzip compression command is very simple. You don’t even need to specify the name of the compressed package. You only need to specify the source file name. Let's try it:

[root@localhost ~]# gzip install.log
#Compress instal.log file
[root@localhost ~]# ls
anaconda-ks.cfg install.log.gz install.log.syslog
#The compressed file is generated, but the source file also disappears

【Example 2】Keep source file compression

When you compress a file using the gzip command, the source file disappears, resulting in a compressed file. At this time, some people will have obsessive-compulsive disorder and ask the author: Is it possible to prevent the source file from disappearing when compressing the file? Well, it's possible, but it's awkward.

[root@localhost ~]# gzip -c anaconda-ks.cfg >anaconda-ks.cfg.gz
#Use the -c option, but do not output the compressed data to the screen, but redirect it to the compressed file, so that the file can be compressed without deleting the source file
[root@localhost ~]# ls
anaconda-ks.cfg anaconda-ks.cfg.gz install.log.gz install.log.syslog
#You can see that both the compressed file and the source file exist

【Example 3】 Compress directory

We might 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 #Create a test directory and create several test files in it
[root@localhost ~]# gzip -r test/
#Compress the directory and no error is reported
[root@localhost ~]# ls
anaconda-ks.cfg anaconda-ks.cfg.gz install.log.gz install.log.syslog test
#But I found that the test directory still exists and has not been converted into a compressed file
[root@localhost ~]# ls test/
test1.gz test2.gz test3.gz
#The original gzip command does not pack the directory, but compresses all the sub-files in the directory separately

In Linux, packaging and compression are handled separately. The gzip command can only compress but not package, so there will be no package directory, and only the files in the directory will be compressed.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to view and execute historical commands in Linux
  • Teach you to hide Linux command line history
  • Shell history command recording function in Linux
  • How to Communicate with Other Users on the Linux Command Line
  • There is no make command in Linux (make: *** No target specified and no makefile or make command installation method found)
  • Use of Linux telnet command
  • Detailed explanation of how to adjust Linux command history

<<:  How does MySQL implement ACID transactions?

>>:  Encapsulation method of Vue breadcrumbs component

Recommend

Vue's detailed code for implementing the shuttle box function

Vue - implement the shuttle box function, the eff...

The shortest JS to determine whether it is IE6 (IE writing method)

Commonly used JavaScript code to detect which ver...

Display flex arrangement in CSS (layout tool)

Regarding display: flex layout, some people have ...

Why is IE6 used by the most people?

First and foremost, I am a web designer. To be mor...

A brief analysis of the knowledge points of exporting and importing MySQL data

Often, we may need to export local database data ...

JavaScript to achieve fireworks effects (object-oriented)

This article shares the specific code for JavaScr...

ReactRouter implementation

ReactRouter implementation ReactRouter is the cor...

js to achieve the complete steps of Chinese to Pinyin conversion

I used js to create a package for converting Chin...

Vue.js implements simple folding panel

This article example shares the specific code of ...

Docker enables seamless calling of shell commands between container and host

As shown below: nsenter -t 1 -m -u -n -i sh -c &q...

How to load third-party component libraries on demand in Vue3

Preface Take Element Plus as an example to config...

A brief discussion on JS packaging objects

Table of contents Overview definition Instance Me...

How to use wangEditor in vue and how to get focus by echoing data

Rich text editors are often used when doing backg...

An article to understand the advanced features of K8S

Table of contents K8S Advanced Features Advanced ...