On Linux package and compression and tar command

Time:2020-7-8

Packaging and compression

In Linux, packaging and compression are two different concepts. It is very helpful for us to understand complex file suffixes

pack

When several files and directories are packed together into a large file, it is just a simple package. Therefore, the file size after packaging is generally larger than the total number of files to be packed

compress

The compression ratio is used to measure the ratio of file size before and after compression

compress

There are three compression commands gzip, bzip2 and XZ in Linux. They adopt different compression algorithms. The descriptions and common parameters are as follows:

Compress command gzip bzip2 xz
File name follow up .gz .bz2 .xz
Parameter – D decompression decompression decompression
Parameter – K Keep the original file when compressing Keep the original file when compressing
Corresponding decompression command gunzip bunzip2 unxz
The corresponding command to view the contents of the file without decompression zcat bzcat xcat

Note: these three commands can only compress a single file, and the original file will be deleted by default if no parameters are specified

pack

The most commonly used packing command is tar command, and it can also call the three compression commands mentioned above, so the frequency is very high. Let’s take a look at some common parameters of tar

Common parameters

-f: The archive to operate on, such as – F file.tar
-c: Create Archive
-r: Add file to archive
-u: Update files in archive
-x: Expand Archive
-t: Do not unzip, directly view what files have been archived

Packaging example

Package all. PNG files into a single file named allpic.tar
tar -cf allpic.tar *.png

Appends all. JPG terminated files to the file allpic.tar in
tar -rf allpic.tar *.jpg

to update allpic.tar Medium front.jpg file
tar -uf allpic.tar front.jpg

list allpic.tar All files in the package (not decompressed)
tar -tf allpic.tar

Unzip it out allpic.tar All files in
tar -xf allpic.tar

Compression example

Tar has the option that it can call the three compression methods mentioned above after packaging to compress the packed files,

Gzip corresponding parameter – Z or — gzip

Bzip2 corresponds to – J or — bzip2

XZ corresponds to the parameter – J or — XZ

Therefore, we only need to add the corresponding parameters in the above parameter – C (create package) or – x (decompress file) command, and the file suffix can be changed to realize the function of packaging, compression and decompression. Changing the suffix to a different suffix is only convenient for our eyes to identify. It doesn’t matter if we don’t change it. To see what compression type the file is, just use the file command.

Call gzip to compress and decompress

Package and compress all. PNG files into a file named allpic.tar.gz file
tar -czf allpic.tar.gz *.png

list allpic.tar.gz All files in
tar -tzf allpic.tar.gz

take allpic.tar.gz Unzip the file to the current directory
tar -xzf allpic.tar.gz

Call bzip2 to compress and decompress

Package and compress all. PNG files into a file named allpic.tar.gz file
tar -cjf allpic.tar.bz2 *.png

list allpic.tar.gz All files in
tar -tjf allpic.tar.bz2

take allpic.tar.bz2 Unzip the file to the current directory
tar -xjf allpic.tar.bz2

Call XZ compression and decompression

Package and compress all. PNG files into a file named allpic.tar.gz file
tar -cJf allpic.tar.xz *.png

list allpic.tar.gz All files in
tar -tJf allpic.tar.xz

take allpic.tar.bz2 Unzip the file to the current directory
tar -xJf allpic.tar.xz

Summary of examples of decompressing various file suffixes

What we often need is to download a file and how to decompress it. Here is a summary:

For files ending in. Tar
tar -xf allpic.tar

about tar.gz Or. Tgz
tar -zxf allpic.tar.gz

about tar.bz2 Ending file
tar -jxf allpic.tar.bz2

about tar.xz Ending file
tar -Jxf allpic.tar.xz

For files ending in. GZ, instead of tar.gz File for
gzip -d allpic.gz
gunzip allpic.gz

For files with the end of. GZ. Bz2. XZ but without previous tar, you can parameter the above table, such as
gzip -d mypic.gz
bzip2 -d mypic.bz2
xz -d mypic.xz

gunzip mypic.gz
bunzip2 mypic.bz2
unxz mypic.xz

For the end of. Zip file (common under Windows)
unzip allpic.zip

For the end of the. Rar file
unrar e allpic.rar

The above detailed talk about Linux packaging and compression and tar command is the small editor to share all the content, I hope to give you a reference, also hope that you can support developeppaer more.

Recommended Today

Python design pattern 5 decorator

The second structural pattern worth learning is the decorator pattern, which allows programmers to dynamically add capabilities to objects in a transparent way (affecting other objects). Decorators can be written in a python way (which means using language features), thanks to the built-in decorator features(https://docs.python.org/3/reference/compound_stmts.html#function)。 A python decorator is a callable (function, method, or class) […]