Linux File compression and packaging

Source: Internet
Author: User

The compressed file is usually named 『*. tar, * .tar.gz ,*. tgz ,*. GZ ,*. Z ,*. bz2 "and so on. When you catch a compression file, you need to know which command is used to compress it. at present, some of the commonly seen compressed files are attached names:

*. Z files compressed by the compress program;
*. Bz2 Bzip2 compressed files;
*. Files compressed by the GZ gzip program;
* The data packaged by the. Tar program has not been compressed;
* .Tar.gz tar package files, which are compressed by Gzip

The earliest compress is the compress guy, but this compress command is no longer a preset compression software. Later, the GNU program developed a new generation of compression command gzip (GNU zip) this is used to replace compress, the old compression command, and Bzip2, a better compression command. however, these commands can only compress and decompress a single file. Isn't it annoying if a large number of files are required for each compression and decompression? In this case, the so-called packaging software is very important.

Tar can pack many files into one file! You can even play with directories. However, the simple tar function is just "packaging", that is, to assemble many files into one file. In fact, he did not provide the compression function. Later, in the GNU program, combine the entire tar with the compression function to provide more convenient and powerful compression and packaging functions for users!

Let's talk about these basic compression commands in Linux!

1) compress

[Root @ Linux ~] # Compress [-DCR] files or directories
Parameters:
-D: extract parameters.
-R: The files under the directory can be compressed at the same time!
-C: Output compressed data to standard output (output to the screen)

Example:

Example 1: copy/etc/man. config to/tmp and compress it
[Root @ Linux ~] # Cd/tmp
[Root @ Linux TMP] # cp/etc/man. config.
[Root @ Linux TMP] # compress man. config
[Root @ Linux TMP] # ls-l
-RW-r -- 1 Root 2605 Jul 27 :43 man. config. Z

Example 2: Unlock the compressed file
[Root @ Linux TMP] # compress-d man. config. Z

Example 3: compress man. config into another file for backup
[Root @ Linux TMP] # compress-C man. config> man. config. Back. Z
[Root @ Linux TMP] # ll man. config *
-RW-r -- 1 Root 4506 Jul 27 :43 man. config
-RW-r -- 1 Root 2605 Jul 27 :46 man. config. Back. Z

# This-C parameter is interesting! It will output the data in the compression process to the screen, instead of writing it
# File. Z file. Therefore, we can use the data stream redirection method to output the data to another file name.
# We will talk about data stream redirection in detail in bash shell.

This is the command used to compress and decompress the file *. Z! So when you see the *. Z file, you should know that it is compressed through the compress program! After compress is used, if other parameters are not specified, the original file will be replaced by *. Z later! The preceding case shows that the compressed file is man. config. After compression, only the compressed file man. config. Z will be left! How about decompression? Haha, it is to decompress man. config. Z into man. config! It's easy to use!

In addition to using the compress-D parameter, you can also directly use uncompress! It means the same! In addition, if you do not want to rename the original file *. z. When you want to create another file name, you can use the data stream to redirect, that is, the symbol greater than (>, store the data that should have appeared on the screen to another file. Of course, this requires the-C parameter ~ We will refer to the third article about data stream redirection!

In addition, compress is rarely used, because this program cannot be unlocked *. GZ files, while gzip can solve *. Z files, so it doesn't matter if your distribution does not have compress!

2) gzip and zcat

[Root @ Linux ~] # Gzip [-CDT #] Alias Name
[Root @ Linux ~] # Zcat alias .gz
Parameters:
-C: outputs compressed data to the screen and can be processed through data stream redirection;
-D: extract parameters;
-T: it can be used to check the consistency of a compressed file ~ Check whether the file has any errors;
-#: 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.

Example:

Example 1: copy/etc/man. config to/tmp and compress it with Gzip
[Root @ Linux ~] # Cd/tmp
[Root @ Linux TMP] # cp/etc/man. config.
[Root @ Linux TMP] # gzip man. config
# At this time, man. config will become man.config.gz!

Example 2: Read the file content of Example 1!
[Root @ Linux TMP] # zcat man.config.gz
# The man.config.gz extracted file content is displayed on the screen !!

Example 3: extract the archive of Example 1
[Root @ Linux TMP] # gzip-D man.config.gz

Example 4: compress man. config unlocked in Example 3 with the optimal compression ratio and keep the original file
[Root @ Linux TMP] # gzip-9-C man. config> man.config.gz

Gzip is a command used to compress and decompress the file *. GZ! In addition, Gzip also provides the compression ratio service! -1 is the worst compression ratio, but the compression speed is the fastest, while-9 can achieve a better compression ratio (after compression, the file size is relatively small !) But it will lose some speed! The default value is-6!
Gzip is also a commonly used compression command! Zcat is the command used to read the compressed file data! If the file we just compressed is a text file, do you still remember how to read it ?! That's right! That is, using cat. What about reading compressed files? Haha! Zcat is used!

The gzip compression command is mainly used to replace compress. Therefore, you can use gzip To uncompress compress files! The zcat command can read both compress and gzip compression files.

3) Bzip2, bzcat

[Root @ Linux ~] # Bzip2 [-CDZ] Alias Name
[Root @ Linux ~] # Bzcat registrant .bz2
Parameters:
-C: output the data generated during the compression process to the screen!
-D: extracted Parameters
-Z: Compression Parameters
-#: Same as gzip, it is used to calculate the compression ratio.-9 is the best, and-1 is the fastest!

Example:

Example 1: compress/tmp/man. config with Bzip2
[Root @ Linux TMP] # Bzip2-Z Man. config
# At this time, man. config will become man.config.bz2!

Example 2: Read the file content of Example 1!
[Root @ Linux TMP] # bzcat man.config.bz2
# The man.config.bz2 extracted file content is displayed on the screen !!

Example 3: extract the archive of Example 1
[Root @ Linux TMP] # Bzip2-D man.config.bz2

Example 4: compress man. config unlocked in Example 3 with the optimal compression ratio and keep the original file
[Root @ Linux TMP] # Bzip2-9-C man. config> man.config.bz2

The compress file name is automatically set to. Z, and the GZIP file name is automatically set to. GZ. Here, Bzip2 automatically builds the attached file name as. bz2! So when we use Bzip2-Z with the compression function, the man. config file is changed to man.config.bz2! Well, what if I want to read the content of this file? Are you sure you want to unlock it? Of course not required! You can use the simple bzcat command to read the content! For example, in the above example, we can use bzcat man.config.bz2 to read data without unlocking it! In addition, when you want to uncompress a file, the file name is. BZ,. bz2,. TBZ,. tbz2, and so on, you can try to use Bzip2 to solve it!

4) tar

[Root @ Linux ~] # Tar [-cxtzjvfppn] files and directories ....

Parameters:

-C: Create a File compression parameter command (create );
-X: Command to uncompress a file!
-T: view the files in the tarfile!
Note that C/X/T can only exist under the parameter! Cannot exist at the same time! Because it is impossible to simultaneously compress and decompress.

-Z: does it have the gzip attribute at the same time? That is, do I need to use gzip for compression?
-J: Does it have Bzip2 attributes at the same time? That is, do I need to use Bzip2 for compression?
-V: The file is displayed during compression! This is common, but it is not recommended to use it in the background execution process!

-F: use the file name. Please note that the file name should be followed immediately after F! Do not add parameters!
For example, if you use "Tar-zcvfp tfile sfile", it is wrong to write it as "Tar-zcvpf tfile sfile!

-P: use the original attribute of the original file (the attribute will not be changed based on the user)
-P: absolute paths can be used for compression!
-N: It is newer than the subsequent date (yyyy/mm/DD) to be packaged into the new file!
-- Exclude file: do not pack the file during compression!

Example:

Example 1: package all files in the/etc directory into/tmp/etc.tar
[Root @ Linux ~] # Tar-CVF/tmp/etc.tar/etc <= package only, do not compress!
[Root @ Linux ~] # Tar-zcvf/tmp/etc.tar.gz/etc <= compressed with Gzip
[Root @ Linux ~] # Tar-jcvf/tmp/etc.tar.bz2/etc <= compressed with Bzip2
# Note that the file name after parameter F is obtained by ourselves. We use. tar for identification.
# If the Z parameter is added, .tar.gz or. tgz is used to represent the tar file ~ compressed by gzip ~
# If you add the J parameter, use .tar.bz2 as the file name ~
# A warning message is displayed when the preceding command is executed:
# "Tar: removing leading '/' from Member names" is a special setting about absolute paths.

Example 2: Check the files in the/tmp/etc.tar.gz file?
[Root @ Linux ~] # Tar-ztvf/tmp/etc.tar.gz
# Since we use gzip compression, when you want to view the files in the TAR file,
# Add the Z parameter! This is important!

Example 3: Decompress the/tmp/etc.tar.gz file under/usr/local/src.
[Root @ Linux ~] # Cd/usr/local/src
[Root @ Linux SRC] # tar-zxvf/tmp/etc.tar.gz
# By default, We Can uncompress files anywhere! In this example,
# First, I will transform the working directory to the/usr/local/src directory, and unlock/tmp/etc.tar.gz,
# The unlocked directory will be in/usr/local/src/etc! In addition, if you enter/usr/local/src/etc
# The file attributes in this directory may be different from those in/etc!

Example 4: Under/tmp, I only want to unbind the etc/passwd in/tmp/etc.tar.gz.
[Root @ Linux ~] # Cd/tmp
[Root @ Linux TMP] # tar-zxvf/tmp/etc.tar.gz etc/passwd
# I can use tar-ztvf to check the file name in the tarfile. If you only need one file,
# You can issue it in this way! Notice! The root directory in etc.tar.gz/is removed!

Example 5: Back up all files in/etc/and save their permissions!
[Root @ Linux ~] # Tar-zcvpf/tmp/etc.tar.gz/etc
# This-P attribute is very important, especially when you want to keep the attribute of the original file!

Example 6: only new files in/home are backed up.
[Root @ Linux ~] # Tar-N '2014/1/01'-zcvf home.tar.gz/home

Example 7: I want to back up/home,/etc, but not/home/dmtsai
[Root @ Linux ~] # Tar -- exclude/home/dmtsai-zcvf myfile.tar.gz/home/*/etc

Example 8: package/etc/and unpack it under/tmp without generating an archive!
[Root @ Linux ~] # Cd/tmp
[Root @ Linux TMP] # tar-CVF-/etc | tar-xvf-
# This action is a bit like CP-r/etc/tmp ~ It is still useful!
# Note that the output file is changed to-, the input file is also changed to-, and there is another | Yes ~
# This represents standard output, standard input, and pipeline commands respectively!
# This part will be explained again when we mention this command in bash shell!

This is a multi-purpose compression command:

Tar can be used to integrate the entire directory or specified files into one file! For example, in Example 1 above, all files under/etc can be integrated into one file!
At the same time, tar can be used with gzip (this gzip function has been attached to tar) and integrated and compressed at the same time! Haha! Very convenient!
"Tar is an important command for backup! "Because we usually name the files after tar integration as *. tar, and if it also contains the gzip compression attribute, it will be named * .tar.gz! It's just to help us remember what the archive is! There is no practical significance in!

Absolute path and permission problems

In addition, it should be noted that there are several useful parameters to be understood in terms of the parameters used, that is,-P and-P! In example 1, a warning message is mentioned, that is, "Tar: removing leading '/' from Member names", which means, tar removed the/from the/etc directory! This is because you are worried about some problems when Uncompressing files in the future, because if the files in TAR have an "absolute path, the file you undo will be "certainly" under this path, that is,/etc, rather than the relative path (please think about it carefully here !) . The biggest problem with this is that, in case someone has taken your file and unlocked it on his system! In case of a directory like/etc on the system !), His files will be overwritten!

In the preset condition, if the package file is created using "absolute path", tar will automatically remove! This is the default value for the prerequisite "security" just described. Create a package file in an absolute path! So add the-p parameter (please note! Is an uppercase character )! So what is-P (lowercase letter? Haha! That-P indicates permission, that is, "permission! After-P is used, the packaged file will not change the permission based on the user's identity!

File update date:

There is also a noteworthy parameter! That is the-n parameter that is often used in backup cases! You can refer to the above example to understand it! In this example, the date is very important! In the case of backup, we all want to back up newer files. Why? We have backed up the old files! Why do we need to back up the data again, wasting time and system resources! At this time, this parameter is quite important!

About standard input/standard output:
In the above example, the last example is interesting: "Tar CVF-/etc | tar-xvf -』! He directly uses the pipeline command "pipe" to compress and decompress it! In the above example, we want to "copy the data under/etc directly to the current path, that is, bottom-up/tmp", but I think it is a bit troublesome to use CP-R, you can directly package the package in this way. The-in the command indicates the package file! Because we do not want to make the intermediate file exist, we can copy it in this way!

What are tarfile and tarball?
Tar has many functions, and because it is a process after being packaged, we often hear the tarball file, that is the archive compressed by tar package! If it is only packaged but not compressed, it is called tarfile ~ In addition, tar can also be used on backup storage media. The most common is the tape drive! Assume that my tape drive code is/dev/st0. When I back up all the data under my/home, you can use tar/dev/st0/home! Great!
In Linux, Gzip has been integrated into tar! However, in Sun or other older UNIX versions, tar does not integrate gzip, so if you need to decompress it, you need:
Gzip-D testing.tar.gz tar-xvf testing.tar
The first step is to decompress the file, and the second step is to extract the data! Unlike other compression programs, Bzip2, Gzip, and compress replace the original files when no special parameters are added, however, if tar is used, the original and subsequent files will exist!

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.