Compression and decompression of Linux

257 Views
No Comments

A total of 1953 characters, expected to take 5 minutes to complete reading.


Compression and decompression of a single file

In linux, there are two commonly used compression software, gzip and bzip, and the commands are gzip andbzip2(bzip has been updated to version 2). However, both commands can only compress a single file, that is, they cannot be packaged into a compressed package.


GZIP

Compression

Use gzip [文件] to compress the file:
Compression and decompression of Linux
After compression, the 1.txt file will be replaced with the 1.txt.gz file, that is, the original file is compressed and no longer exists.
If you have multiple files, you can use the option -r Recursively process files, I .e.gzip -r [多个文件].
There are also compression options --fast and--best, One is the fastest compression speed (lowest compression ratio), and the other is the best compression ratio, use:

gzip [文件] --fast
gzip [文件] --best

More other parameters can be viewed in Helpgzip --help, orman gzip.

Decompression

To unzip the file, use gzip -d [文件] command, or usegunzip [文件]:
Compression and decompression of Linux
The file is restored from 1.txt.gz to 1.txt, that is, the decompression is complete.


BZIP2

Compression

Use bzip2 [文件] to compress the file:
Compression and decompression of Linux
Ditto, bzip2 also has compression options --fast and--best, One is the fastest compression speed (lowest compression ratio) and the other is the best compression ratio.
Note: bzip2 has no -r option, you cannot recursively process multiple files.

Decompression

Use bzip2 -d [文件] or bunzip2 [文件] to extract the file:
Compression and decompression of Linux


Packing

In order to solve the problem that gzip and bzip2 can only compress a single file, we can first package multiple files with tar and then compress them to become a "compression package".


TAR

The basic options for tar are:

# -f : 指定压缩包名称
# -c : 打包文件
# -x : 解压,不需要指定压缩包的压缩类型,它会自动匹配压缩包的类型自行解压。# -v : 显示压缩包压缩的过程
# -P:当压缩包中存在根目录是,自动移除根目录
# -t : 查看压缩包中的内容

# 打包当前文件夹下的内容
tar -cvf test.tar ./xxx

Packing

If you want to create a package folder, you can usetar -cvf test.tar ./test. It means to package all the contents in the test folder under the current folder (note that it will not be compressed) and display the detailed process. The file name after packaging is test.tar:
Compression and decompression of Linux

Unpack

To unpack the test.tar file, we usually use the command tar -xvf test.tar To unpack, there is also no compression here, just pack multiple files and then unpack:
Compression and decompression of Linux

View

If you only want to view the files and contents of the package, just enter tar -tf test.tar You can:
Compression and decompression of Linux

Compressed package

linux supports the compression and decompression of many types of compressed files, such as tar and zip. For convenience and practicality, tar also supports gzip and bzip compression methods, and supports one-click compression into compressed packages.


TAR

The tar command has special options to set the compression mode, commonly used are gzip and bzip2:

# -z : 指定使用 gzip 压缩,一般使用 gzip 压缩的文件都以.tar.gz 作为扩展名
# -j : 指定使用 bzip2 压缩,一般使用 bzip2 压缩的文件都以.tar.bz2 作为扩展名
# -J : 指定使用 xz 压缩,一般使用 xz 压缩的文件都以.tar.xz 作为扩展名

gzip compression and decompression

To create a gzip compressed package, you can use the command tar -zcvf test.tar.gz ./test, that is, compress all the contents under the test folder. Among them-z is compressed by gzip,-cis to create a compressed package,-vis to show the compression process,-fis to specify the compressed file name:
Compression and decompression of Linux
If we want to unzip test.tar.gz, we use tar -zxvf test.tar.gz to unzip:
Compression and decompression of Linux

bzip2 compression and decompression

To create a bzip2 compressed package, you can use the command tar -jcvf test.tar.bz2 ./test, that is, compress all the contents under the test folder. Among them-j is compressed by bzip2,-cis to create a compressed package,-vis to show the compression process,-fis to specify the compressed file name:
Compression and decompression of Linux
Similar to gzip decompression, bzip2 decompression only needs -z Replace -j that is, the command istar -jxvf test.tar.bz2:
Compression and decompression of Linux

xz compression and decompression

Xz is an efficient compressed file format that uses the LZMA algorithm to achieve a high compression ratio that significantly reduces file size while maintaining file readability. The xz file packaged by tar is usually. tar.xz.
Similar to the previous two compression commands, just use the option -J You can. For example, to compress the folder test, use the commandtar -Jcvf test.tar.xz ./test:
Compression and decompression of Linux
Unzip using commandtar -Jxvf test.tar.xz:
Compression and decompression of Linux

ZIP

Zip is a very convenient and effective compression format, which is commonly used in Windows systems. Linux, we first need to install the software package yum install unzip orsudo apt install unzipto manipulate the zip file.

Compression

Use command zip [压缩包.zip] [文件] You can compress files, command zip [压缩包.zip] [目录] to compress the directory.
For example, to compress the directory test, usezip test.zip ./test:
Compression and decompression of Linux

Decompression

Use command unzip [压缩包.zip] You can decompress the compressed package, which is very convenient, for exampleunzip test.zip:
Compression and decompression of Linux


write by dudu233

END
 0
Comment(No Comments)
验证码
en_USEnglish