One day, I learned how to package and decompress Linux Files.

Source: Internet
Author: User
Tags rar extension uncompress


For those who are new to Linux, they will surely be confused by a large number of packages and compressed files in Linux. We know that in Windows, the most common compressed files are in rar and zip formats and the currently emerging 7z format. It can be different from linux.there are many extensions such as .gz).tar.gz?tgz=bz2#.z#.tar. Is it dizzy. Of course. ziphe. rar in Windows can also be used in Linux, but the premise is that these software is installed, but there are too few users who use rar format in Linux. This article will summarize these common packaging and compression files. I hope you will not be confused when you encounter these files next time.



Before starting today's text, we must first understand two concepts: Packaging and compression. Packaging refers to converting a large number of files or directories into a total file. Compression refers to converting a large file into a small file through some compression algorithms. Why do we need to differentiate these two concepts? In fact, this is because many Linux compression programs can only compress one file, so when you want to compress a large number of files, you have to use another tool to compress these files into a package and then compress the original compression program.


Next let's take a look at the meanings of the various suffixes we mentioned above.

Files compressed by the. Z compress Program

Files compressed by the. gz gzip Program

. Bz2 bzip2 compressed file

The data packaged by the. tar program has not been compressed.

.Tar.gz tar package files, which are compressed by gzip

.Tar.bz2 files packaged by the tar program, which are compressed by bzip2

Common Linux compression commands are gzip and bzip2. As for compress, no one is using it. Gzip is a compression command developed by the GNU program, which has replaced compress. Later, GNU developed bzip2, a better compression command. However, these commands can only be compressed and decompressed for a single file, so we have the packaging command we mentioned earlier.



At the end of lifecycle. After the tar package is generated, other programs can be used for compression. First, let's talk about the basic usage of the tar command:


There are many parameters for the tar command, but there are only a few commonly used options.

-C: (create) create a package file. You can use-v to view the package file during the process.

-T: view the file names contained in the package.

-X: unpack or decompress the package. It can be used with-C (uppercase) To uncompress the package in a specific directory.

Note that-c,-t, and-x cannot appear at the same time.

-J: Use bzip2 to compress or decompress the file. In this case, the file name is * .tar.bz2.

-Z: supports gzip compression and decompression. The file name is * .tar.gz under normal conditions.

-V: displays the files being processed during compression/decompression.

-F:-f is followed by the file name to be processed immediately. Do not add any parameters. You can also write an option for-f.

-C: This option is used for decompression, followed by the Directory. The purpose is to extract the file to the specified directory.

-P: retains the original permissions and attributes of data. It is often used for backup (-c)

-P: retain the absolute path, that is, allow the backup data to contain the root directory.

-R: append an object to the package. For example, if the user has already packed the file and finds that there is another directory or some files have not been packaged, you can use this option to append the directory or files that you have forgotten to the package.

-Exclude = FILE: Do not package the FILE during compression.

-U: update the file. That is to say, replace the original backup file with the new file. If the file to be updated cannot be found in the backup file, append it to the end of the backup file.

-N: It is newer than the date followed by yyyy/mm/dd to be packaged into the new file.


The following is an example.

The tar-cf all.tar * .jpg command is to pack all. jpg files into a package named all.tar. -C indicates that a new package is generated.-f specifies the package file name.

The tar-rf all.tar *. gif command adds all .gif files to the package of all.tar. -R indicates adding files.

The tar-uf all.tar logo.gifcommand is a new logo.gif file in tarbao all.tar.tar.-u indicates the meaning of the update file.

The tar-tf all.tar command is to list all files in the all.tar package.-t indicates to list files.

The tar-xf all.tar command is used to extract all files in the all.tar package.-x is used to unlock the files.


The above is the most basic usage of tar. To help you compress or decompress files while packing and unpackage, tar provides a special feature. This means that tar can call other compression programs, such as gzip and bzip2, while packaging or unpacking.


1) tar calls gzip

Gzipis a program developed by gnuorganization. The file ending with .gz is the result of gzip compression. The decompress program relative to gzip is gunzip. Use the-z parameter in tar to call gzip.

The following is an example:

Tar-czf all.tar.gz *. jpg


Tar-xzf all.tar.gz

This command unlocks the generated package.


2) tar call bzip2

Bzip2is a more powerful compression program. The file ending with .bz2 is the result of bzip2 compression. The decompress program relative to bzip2 is bunzip2. Use the-j parameter in tar to call gzip.

The following is an example:

Tar-cjf all.tar.bz2 *. jpg


Tar-xjf all.tar.bz2

This command unlocks the generated package.


3) tar call compress

Compress is also a compression program, but it seems that there are not as many people using compress as gzip and bzip2 .. The file ending with Z is the compress compression result. The decompress program relative to compress is uncompress. Use the-Z parameter in tar to call gzip.

The following is an example:

Tar-cZf all.tar. Z *. jpg

Forbidden. Z

Tar-xZf all.tar. Z

This command is used to unbind the generated package.


4) tar unzipping a single file in the compressed package

First, check the extracted file name.

Tar-z [j] tvf filename.tar.gz (bz2) | grep 'file name to be parsed'

Decompress the file

Tar-z [j] xvf source compressed package .tar.gz (bz2) file name to be extracted


5) package a directory, but not some files in the directory

You need to use the-exclude parameter.

For example, I want to back up the/etc and/root directories, but I don't want to package the files with the install switch, and I don't want to package the files myself.

Tar-jcv-f/root/systembk.tar.bz2-exclude =/root/install *-exclude =/root/systembk.tar.bz2/etc/root


6) only back up new files at a certain time

The-newer-mtime parameter is required for this time.

For example, if I want to back up a new file in the/etc directory

Tar-jcv-f/root/etc.newer.2011.02.16.tar.bz2-newer-mtime = "2011/02/16"/etc /*


Tar-jtv-f etc.newer.2011.02.16.tar.bz2

To view the content.


Finally, we will give a brief introduction to gzip, zcat, bzip2, and bzcat.

Gzip, zcat

The compressed file created by gzip is *. gz.

Use the gzip [-cdtv #] format to compress the file

Main parameters:

-C: output the compressed data to the screen.

-D: Extract

-T: used to check whether the compressed file is correct.

-V: displays the compression ratio of the original file/compressed file.

-#: Compression level.-1 is the fastest, but the compression ratio is the worst. -9 is the slowest, but the compression ratio is the best. The default value is-6.

When you use gzipin to compress, the original file will be compressed into a .gz file name under the predefined state, and the source file will no longer exist.


Decompression method:

Gzip-d filename.gz

Gunzip filename.gz

In the same way, after decompression, the .gz file will also be deleted to generate the source file.


We have mentioned the cat command before to display the file content. Of course, I can also use zcat to read the content in the compressed file.

Usage: zcat filename.gz


Bzip2, bzcat


Bzip2 [-cdkzv #] file to be compressed

Main parameters:

-C: output the data generated during the compression process to the screen.

-D: Extract

-K: Keep the source file without deleting the original file.

-Z: Compression

-V: displays the compression ratio of the original or compressed file.

-#: The same as gzip, it is used to calculate the compression ratio.-9 is the best, and-1 is the fastest.

Similarly, bzcat can be used to view the file content compressed with bzip2. In the pre-defined state, the original file will be compressed into a file name of .bz2, and the source file will no longer exist. However, you can use the-k parameter to retain the source file.

Decompression method:

Bzip2-d filename.gz

Bunzip2 filename.gz

You can also use the-k parameter to retain the source file.


With the above knowledge, you should be able to uncompress a variety of compressed files. The following is a summary of the compressed files in the tar series:

. Tar

Unpack: tar xvf FileName.tar

Package: tar cvf FileName.tar DirName


. Gz

Decompress 1: gunzip FileName.gz

Decompress 2: gzip-d FileName.gz

Compression: gzip FileName


Decompress: tar zxvf FileName.tar.gz

Compression: tar zcvf FileName.tar.gz DirName


. Bz2

Decompress 1: bzip2-d FileName.bz2

Decompress 2: bunzip2 FileName.bz2

Compression: bzip2-z FileName


Decompress: tar jxvf FileName.tar.bz2

Compression: tar jcvf FileName.tar.bz2 DirName


. Bz

Extract 1: bzip2-d FileName. bz

Decompress 2: bunzip2 FileName. bz

Compression: Unknown

. Tar. bz

Decompress: tar jxvf FileName.tar. bz

Compression: Unknown


. Z

Decompress: uncompress FileName. Z

Compression: compress FileName

. Tar. Z

Decompress: tar Zxvf FileName.tar. Z

Compression: tar Zcvf FileName.tar. Z DirName


. Tgz

Decompress: tar zxvf FileName. tgz

Compression: Unknown

. Tar. tgz

Decompress: tar zxvf FileName.tar. tgz

Compression: tar zcvf FileName.tar. tgz FileName


. Zip

Decompress: unzip

Compression: zip DirName




In addition, Linux has corresponding methods to decompress the compressed files .zipand .rar in Windows:

1)for. Zip

Linux provides zip and unzip programs, zip is a compression program, and unzip is a decompression program. They have many Parameter options. Here we will only give a brief introduction and give examples to illustrate their usage:

Zip *. jpg

This command compresses all .jpg files into a zip package.


This command decompress all files in


2XX vs. .rar

To process the. RAR file in linux, you must install RAR for Linux. You can download and install the file from the Internet:

After installation, there will be the rar and unrar programs, rar is the compression program, and unrar is the decompression program. They have many Parameter options. Here we will only give a brief introduction and give examples to illustrate their usage:

Rar a all *. jpg

This command is to compress all. jpg files into a rarpackage named all.rar. the program will automatically append the. rar extension name to the package name.

Unrar e all.rar

This command is to extract all files from all.rar.


Unzip,., these 10 types of compressed files have been decompressed. In the future, we should not have to worry about downloading a software and not knowing how to unbind it in Linux.

From Yu Fei's blog

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.