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

How to modify the length limit of group_concat in Mysql

In MySQL, there is a function called "group_...

Native js implements a minesweeper game with custom difficulty

This article example shares the specific code of ...

Ubuntu 20.04 CUDA & cuDNN Installation Method (Graphical Tutorial)

CUDA installation download cuda Enter the nvidia-...

JavaScript Snake Implementation Code

This article example shares the specific code of ...

Detailed explanation of MySQL InnoDB index extension

Index extension: InnoDB automatically extends eac...

Share the pitfalls of MySQL's current_timestamp and their solutions

Table of contents MySQL's current_timestamp p...

Flex layout makes adaptive pages (syntax and examples)

Introduction to Flex Layout Flex in English means...

Summary of MySQL logical backup and recovery testing

Table of contents 1. What kind of backup is a dat...

jQuery implements all shopping cart functions

Table of contents 1. Select All 2. Increase or de...

The difference between the four file extensions .html, .htm, .shtml and .shtm

Many friends who have just started to make web pag...

CSS 3.0 text hover jump special effects code

Here is a text hovering and jumping effect implem...

Detailed explanation of json file writing format

Table of contents What is JSON Why this technolog...