[Turn]gzip,bzip2,tar,zip command use method detailed

Source: Internet
Author: User
Tags unpack

Original: http://blog.chinaunix.net/uid-20779720-id-2547669.html

1 gzip
Gzip (1) is the GNU compression program. It compresses only a single file. The basic usage is as follows:
$ gzip filename
After the program executes, the file name becomes filename.gz, and in general the size is smaller than the original file. Note that the program does not create a new file filename.gz, but instead changes the filename to filename.gz. Generally speaking, the compression rate of text files is higher. And those JPEG images, MP3 and other similar files compression rate is not so good, because they have already been compressed. This basic usage takes into account the compression time and the balance of file size after compression (note: If you want to make the compressed file smaller, you need more time). To get the minimum file size (maximum compression), you can use the following usage:
$ gzip-9 filename
This allows you to get the smallest file size that gzip can compress, but it takes a longer time to compress. The lower the number, the faster the compression, the less the compression rate.

Unzip the gzip compressed file, there are two commands to use, but in fact they are the same program. As long as the suffix of the compressed file is recognized by the GZIP program, it can be decompressed. The suffix name can be:. GZ,-gz,. Z,-Z,. Z and –Z. The first command you can use is:
$ gunzip filename.gz
With the above command, the filename.gz is decompressed and the suffix name is removed. GZ becomes filename. Gunzip is actually part of the GZIP program, which has the same effect as the Gzip–d command. But when we use it, we often use gunzip because it seems to be cooler.

2 bzip2
BZIP2 (1) is another option for Slackware Linux inside the compression program. Its compression algorithm differs from GZIP. There are advantages and disadvantages compared to gzip. The main advantage of BZIP2 is that it compresses the size of the file, and for the same file, the size of the bzip2 compression is almost always less than the compression result of gzip. In some cases, the gap will be quite large. This advantage is especially true for users who download files using kittens. As a reminder, when downloading files on some public FTP servers, select the. bz2 files in the. gz and. bz2 files as much as possible. This is a basic network etiquette because it reduces the burden on the server to serve more people.
The downside of bzip2 is that it consumes more CPU than gzip. That is, compressing the same file, bzip2 is more time consuming than gzip, and consumes more CPU resources. So, when you consider which compression program to choose between the two, you need to measure the speed and size of the compressed file which is more important to you.

BZIP2 is used the same way as gzip, so we don't spend time discussing it. Like Gunzip, the bunzip2 effect is equivalent to bzip2–d. The biggest difference from Gzip is that BZIP2 uses the suffix name. bz2.
$ bzip2 filename
$ bunzip2 filename.bz2
$ gunzip-9 filename


3 tar
Tar is a GNU Tape archive program. It can compress several files or directories into a single file. This feature allows us to compress the entire directory tree, while using gzip and bzip2 alone is not possible. Tar has a lot of command-line arguments, and you can find them on the manual pages. In this section, we're just talking about the use of tar, which is often used.

The most common use of tar is to unzip and unpack the files that we download from the Web site or FTP. A.tar.gz is the file name format we often see, which is what we usually call "Tarball", which is to pack some files in tar first and then compress them with gzip. You may also see a file named A.tar.z, which is the same thing, but you will generally only encounter older UNIX systems.
In addition, you will see files such as a.tar.bz2 in other places. Kernel source files are published in this format in order to make files smaller and easier to download. As you might have guessed, it's the first thing to pack some files in tar and then compress them with bzip2.

We can unpack the file in the format above with some command-line arguments in tar. The-Z parameter means that the file is decompressed first by running Gunzip. The most common uses for unpacking a tarball are:
$ TAR-XVZF filename.tar.gz
There are a lot of parameters here, so what does each of them mean?
-X means extracting the file. This is a very important parameter because it tells tar exactly what to do with the input file. Here, we want to separate the packaged files into the pre-packaged state.
-V indicates details. Adding this parameter allows the program to list each file that is unpacked. If you find the list too annoying, you can simply remove this parameter to turn off the function. Conversely, if you need more detailed information about each unpacking file, you can use the-VV parameter.
-Z is to tell Tar to run gunzip to unzip the file first.
-F is to tell Tar that the next string character entered at the command line is the file that needs to be processed.

The above command can also be written in some other form. In the older system because of the lack of a new version of the GNU Tar program, you might see it written like this:
$ gunzip filename.tar.gz | TAR-XVF-
This line of command first extracts the files and then outputs the extracted files to the TAR program. Gzip can output its processed data to standard outputs. Here is the gzip uncompressed file as standard output, and then the pipeline sends the file to the TAR program for unpacking. The last "-" represents the processing of standard input. It writes the unpacked file to disk.

Another way to do this is to remove the dash from the first command format, like this:
$ tar xvzf filename.tar.gz

You may also encounter bzip2 compressed packaging files. The TAR program version in Slackware Linux can be just like the gzip compressed package file, except that the parameter-Z is replaced with-j:
$ TAR-XVJF filename.tar.bz2

It is important to note that tar will put unpacked files into the current directory. So if you want to extract a file in the/tmp directory to your home directory, here are a few things to choose from:
Select one, copy the file to the home directory, then unpack
Select two to specify the path to the unpacked file
Select three, specify the path to the file after unpacking with the-c parameter

$ CD $HOME
$ cp/tmp/filename.tar.gz
$ TAR-XVZF filename.tar.gz

$ CD $HOME
$ tar-xvzf/tmp/finename.tar.gz

$ CD/
$ tar-xvzf/tmp/filename.tar.gz-c $HOME

All of the above commands are equivalent. Each approach is to unpack the files to your home directory, and the source files remain in place.

It says so many of the commands to unpack with tar, so let's look at how tar is packaged.
In most cases, it is only possible to use the parameter "-C" instead of the parameter "-X":
$ tar-cvzf filename.tar.gz.
In this command, the parameter-C tells Tar to create a packaged file, and the parameter-Z is to compress the packaged file through the GZIP program. Filename.tar.gz is the name of the file you are going to build. (Translator add: "." At the end of the command line) is to tell tar to package all files/folders in the current directory)

The parameter "-F" is not required, but it is usually a good idea to add it. If not added, Tar writes the data to standard output, which requires the pipeline to output tar to another program, like this:
$ TAR-CV Filename.tar. | GPG--encrypt

This line of command to package all the files in the current directory to create a non-compressed tar archive, through the pipeline output to the GPG program to encrypt, so that people do not know the key can not read the contents of the file.

4 zip
Finally, discuss the two programs that handle ZIP files. Zip files are extremely common in the Windows world, so Linux has programs to handle them. ZIP file compression program is called ZIP, the decompression program is called unzip.
$ zip foo *
This line of command will create a ZIP file foo.zip that includes all the files in the current directory. The zip will automatically
The. zip suffix name plus, so we don't need to add in the command. You can also add a parameter-R to make the Zip Add all folder directories in the current directory to the zip file as well:
$ zip-r Foo *

Extracting the files is simple, like this:
$ unzip Foo.zip
This will unzip all the files and folders inside the Foo.zip.

Zip program also has a number of advanced application parameters, including the establishment of self-extracting package, compression is to keep the source files, adjust the size of the compressed file, and so on, do not repeat here. If you want to know more, please use man to view Help.

[Turn]gzip,bzip2,tar,zip command use method detailed

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: info-contact@alibabacloud.com 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.