Create and decompress tar, tar.gzand tar.bz2 files in Linux and compare their compression ratios

Source: Internet
Author: User
Tags tmp file
The swap file and the compression ratio between them can be easily forgotten when the memory space of your head is limited and is not frequently used. The following is an example:


Common compressed file extensions in Linux:
 *. ZFiles compressed by the compress Program (used at the earliest stage) are compressed to replace the source file;
 *. ZipA file package program on the exaggerated platform, applicable to Linux, windows, and Mac OS systems;
 *. Bz2 Files compressed by Bzip2 program;
 *. GzFiles compressed by the gzip program;
 *. TarThe files packaged by the tar program are not compressed;
 * .Tar.gzFiles packaged by the tar program are compressed by the gzip program;
 * .Tar.bz2Files packaged by the tar program are compressed by the Bzip2 program.
TIPS: the so-called extension is only a common format agreed by everyone, in order to more easily identify the compression type at a glance, of course, you can pack or compress the extension name in any format, as long as you want.

A) * The. ZIP format is the most common one. It features a superb operating system platform and can be used on Windows, Linux, and Mac OS at the same time.

You can use zip to compress files that need to be shared to Windows users.

ZIP files:

[Root @ local tmp] $ zip-r tmp
Adding: test/(stored 0%)
Adding: test/data-file (deflated 24%)
Adding: test/r (deflated 51%)

Below is a zip file:

[Root @ local tmp] $ rm-rf test # to decompress the test, delete the tmp file in the current directory first.
[Root @ local tmp] $ unzip
Creating: test/
Inflating: test/data-file
Inflating: test/rB)

Compress and decompress files in the *. Tar * ,.tar.gz and * .tar.bz2 formats and compare them:
Tar is a widely used document packaging format in Linux. Its advantage is that it only consumes a very small amount of CPU and time to package files. It is only a packaging tool and is not responsible for compression. compression is implemented by gzip and Bzip2.

Therefore, put *. Tar * 2.16.tar.gz and * .tar.bz2 together for example:

For example, we want to package and compress the test directory under/var/tmp to the current directory.

[Root @ local tmp] $ tar-cvf test.tar test ### only package, not compress
[Root @ local tmp] $ tar-zcvf test.tar.gz test ### compress the package with gzip
[Root @ local tmp] $ tar-jcvf test.tar.bz2 test ### compress the package with bzip2
[Root @ local tmp] $ ll test.tar * ### compare the compressed file size
-Rw-r -- 1 Lee mock 10240 01-18 test.tar
-RW-r -- 1 Lee mock 357 01-18 test.tar.bz2
-RW-r -- 1 Lee mock 327 01-18 test.tar.gz


How to decompress the package:

[Root @ local TMP] $ tar-xvf test.tar
[Root @ local TMP] $ tar-xvf test.tar.gz
[Root @ local TMP] $ tar-jxvf test.tar.bz2

In the above test, each time you delete the decompressed test file, decompress it to another directory, if you do not define the directory path and name after decompression, the package name is extracted to the current directory by default and used as the folder name. If you want to decompress the package to another directory (for example,/var/tmp/find:

[Root @ local TMP] $ tar-jxvf test.tar.bz2-C/var/tmp/find


About the compression ratio of tar.gzand tar.bz2:

* .Tar.gz does not occupy too much CPU during compression, and can obtain an ideal compression ratio. It is also abbreviated as tgz.

* .Tar.bz2 good information on the Internet said this compression format is the best compression rate, it takes more CPU and time than the previous method.

However, it was found that the size of test.tar.gzwas smaller than that of test.tar.bz2 (does it mean that * .tar.gz has a smaller compression rate)

The file package for this test is attached as follows (click to download the file name ):
Two files in the Test Folder:Data-file and r
Test.tar test.tar.gz test.tar.bz2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.