Compression and packaging of Linux system files

table of Contents

Linux system file compression and packaging

1.gzip compression tool

2. bzip2 compression tool 

3. xz compression tool

4. zip compression tool

5. Tar packaging


Linux system file compression and packaging

In the windows system, the compressed file we have the most contact is in the .rar format, but in the linux system, the format cannot be recognized.

The compression format that can be recognized in Windows and Linux systems is .zip. Using compressed files not only saves disk space, but also reduces network bandwidth during transmission.

The most common compression format under Linux is .tar.gz, in addition to .tar, .gz, .bz2, .zip and other formats. The suffix name of the file can be added or not added in the Linux system, but our recommendation is to add it, so that it can better determine which compression tool is used to compress the compressed file, and then we can decompress the file.

Common types of compressed files in Linux system and corresponding compression tools

  • .gz: indicates the file compressed by the gzip compression tool;
  • .bz2: indicates the file compressed by the bzip2 compression tool.
  • .tar: files packaged by the tar packaging program (tar itself does not have a compression function, just merge a directory into a file);
  • .tar.gz: indicates that it is first packaged by the .tar packaging program and then compressed by gzip;
  • .tar.bz2: It can be understood that it is first packed by tar and then compressed by bzip2.
  • .tar.xz: It can be understood that it is first packed by tar, and then compressed by xz.

1.gzip compression tool

The format of the gzip command is gzip [-d #] filename, where # is a number from 1 to 9.

-d: This parameter is used when decompressing. 

-#: Indicates the compression level, 1 is the worst, 9 is the best, and 6 is the default

From the picture above, we can see that after compression, the original file will be overwritten and become the compressed file.

The same result is obtained after decompressing the file.The original compressed package will be replaced by the decompressed file.

gzip does not support compressed directories, and an error will be reported when using the gzip command to compress directories.

2. bzip2 compression tool 

The format of the bzip2 command is bzip2 [-dz] filename, which has only two common options -z (compress) and -d (decompress).

The compression level ranges from 1 to 9, and the default level is 9.

When compressing, files can be compressed with or without -z option

 

Add -d option to decompress

 The bzip2 command cannot also be used to compress directories. Errors will be reported when compressing directories

3. xz compression tool

The format of the xz command is xz [-dz] filename, which is similar to bzip2. The xz command also cannot compress directories, and errors will be reported when compressing directories

4. zip compression tool

The zip compression package is commonly used in Windows and Linux. It can compress directories and files. When compressing a directory, you need to specify the files in the directory.

After doing the experiment, you will find that when there are two or more secondary directories under the directory, the zip command just compresses the directory itself.

If you want to compress the files in the secondary directory together, you must add the -r option

5. Tar packaging

Packaging is different from compression. Packaging refers to packaging a directory into a file. This file can be compressed by a compression tool.

Tar itself is a packaging tool that can package directories into one file, it integrates all files into one large file, which is convenient for copying or moving. The format of this command is tar [-zjxcvfpP] filename target , which has multiple options

 
 
  • -z: indicates gzip compression at the same time.
  • -j: means to use bzip2 compression at the same time.
  • -J: means to use xz compression at the same time.
  • -x: indicates unpacking or decompressing.
  • -t: View the files in the tar package.
  • -c: Create a tar package or compressed file package.
  • -v: indicates visualization.
  • -f: followed by the file name (that is, -f filename, indicating that the compressed file name is filename, or the decompressed file filename. It should be noted that if there are multiple parameter combinations, please write the -f parameter to the end surface.
  • -p: Use the attributes of the original file, and what attributes will be returned after compression. (uncommonly used)
  • --exclude filename: When packaging or compressing, do not include the filename file. (uncommonly used)

How to use the tar command:

tar [name after packaging] [directory to be packaged]

It is recommended that the packaged name be followed by the .tar suffix name, so that we can distinguish the files.

Package the test directory.

Use the zip command to compress the already packed test1.tar.The following shows the decompression and unpacking process after packaging and compression (the -f option must be placed at the end);

Compressed file

This is to directly decompress the packaged and compressed file into a folder

The tar command can not only package directories, but also files

 

Whether it is packaging or unpacking, the original file will not be deleted, and it will overwrite the existing file or directory

Published 34 original articles · won praise 145 · Views 7189

Guess you like

Origin blog.csdn.net/lhrdlp/article/details/105343909