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

In-depth understanding of the life cycle comparison between Vue2 and Vue3

Table of contents Cycle comparison usage Summariz...

Vue implements two-way data binding

This article example shares the specific code of ...

Vue project packaging and optimization implementation steps

Table of contents Packaging, launching and optimi...

vue2.x configuration from vue.config.js to project optimization

Table of contents Preface vue.config.js configura...

Solution - BASH: /HOME/JAVA/JDK1.8.0_221/BIN/JAVA: Insufficient permissions

1) Enter the folder path where the jdk file is st...

Solution to CSS flex-basis text overflow problem

The insignificant flex-basis has caused a lot of ...

jQuery plugin to achieve seamless carousel

Seamless carousel is a very common effect, and it...

How to quickly build a static website on Alibaba Cloud

Preface: As a junior programmer, I dream of build...

HTML cellpadding and cellspacing attributes explained in pictures

Cell -- the content of the table Cell margin (tabl...

Detailed explanation of mysql.user user table in Mysql

MySQL is a multi-user managed database that can a...

mysql 8.0.19 win10 quick installation tutorial

This tutorial shares the installation tutorial of...

Detailed explanation of the execution process of mysql update statement

There was an article about the execution process ...

How to install ELK in Docker and implement JSON format log analysis

What is ELK? ELK is a complete set of log collect...