Compression of Linux files

There are many commands under Linux that can compress files, and also make compressed files with various extensions. When I saw these commands and parameters before, I really didn't know which one to choose, so I studied it systematically today, and I also summarized it. I hope it can also be helpful to the students who read this blog.

The article mainly has three parts, what is file compression, compression commands and parameters, packaging and compression commands and parameters.

1. File compression

What is file compression? In layman's terms, a larger file is compressed and the file becomes smaller. It's like buying a bag of potato chips, and after deflating it, the whole bag becomes smaller. Then this process of deflation is "compression".

So how do we release these gases from our computer files? At present, the computer systems we use are all measured in byte units, 1byte = 8bit, each bit is 0 or 1, then if we want to record a number 1, we need a length of 1byte to represent, that is, 0000 0001. Here we can see that for a single representation of 1, 1 bit is enough. The first 7 bits of 0 are all used for padding. These bits could have been emptied. What compression does is to vacate these bits. Use 0 to fill the occupied bits to clear out the value of 1 in a more concise way. This is just one type of compression technique, there are others, such as 100 consecutive 1s, then we may record as "100 1s" after compression, instead of the real 100 "1s".

2. Compressed commands and parameters

Now that you know what file compression is, let's take a look at the commands and parameters for compression in Linux.

There are two commonly used compression commands, gzip and bzip2.

1、gzip

gzip can be said to be the most widely used compression command. The gzipped file is *.gz.

parameter:

-c : Output the compressed data in the form of standard output, that is, output to the screen;

-d : Decompression parameters;

-k : keep the original file, there are two files after compression, the original file and the compressed file;

-v : Display the file name and the compression ratio of each file;

-# : Compression level, -1 is the fastest and has the lowest compression ratio, -9 is the slowest and has the highest compression ratio.

For compressed text files, you can use the zcat command to view the file contents directly without decompression.

 

2、bzip2

bzip2 is a new generation of compression technology, the compression ratio is higher than gzip. The compressed file is *.bz2. Parameters and gzip The parameters are almost the same as gzip, but an explicit compression parameter -z is added.

3. Commands and parameters for packaging and compression

The compression commands mentioned in the previous section all compress a single file, so if you want to compress a folder, you need to use the package tar command.

The tar command packs multiple directories or files into a large file. It does not have the function of compression itself, but it can compress large files at the same time through the support of gzip/bzip2.

parameter:

1. Function parameters (only one of the functions can appear in the command)

-c : create a new package file;

-t : View the file name contained in the packaged file;

-x : unpack the file;

-u : Update package files.

2. Support compression parameters

-j : Compress/decompress with bzip2 support, the file name is preferably *.tar.bz2;

-z : Compress/decompress with gzip support, preferably *.tar.gz filename.

3. Other parameters

-v : show the file being processed;

-f : followed by the filename of the packaged file;

-C : followed by a specific target directory.

Example:

压缩:  tar -jcv -f yourfilename.tar.bz2 yourfilename

View: tar -jtv -f yourfilename.tar.bz2

Unzip: tar -jxv -f yourfilename.tar.bz2 -C yourdirectory

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325028494&siteId=291194637