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

Web Design Tips: Simple Rules for Page Layout

Repetition: Repeat certain page design styles thr...

XHTML language default CSS style

html,address, blockquote, body,dd,div, dl,dt,fiel...

Summary of ways to implement single sign-on in Vue

The project has been suspended recently, and the ...

Mysql 5.6.37 winx64 installation dual version mysql notes

If MySQL version 5.0 already exists on the machin...

HTML table tag tutorial (31): cell width and height attributes WIDTH, HEIGHT

By default, the width and height of the cell are ...

Examples of clearfix and clear

This article mainly explains how to use clearfix a...

Some tips on website design

In fact, we have been hearing a lot about web des...

Analysis and Solution of ERROR:2002 Reported When MySQL Starts

Preface This article mainly introduces the analys...

Detailed explanation of the usage of image tags in HTML

In HTML, the <img> tag is used to define an...

MySQL derived table (Derived Table) simple usage example analysis

This article uses an example to describe the simp...

How to use map to allow multiple domain names to cross domains in Nginx

Common Nginx configuration allows cross-domain se...

The difference between delete, truncate, and drop and how to choose

Preface Last week, a colleague asked me: "Br...