Linux file Packaging, decompression instructions--TAR,BZIP2

Source: Internet
Author: User
Tags bz2 file permissions

This article is to Uncle Bird's Linux private cuisine (basic study) of the third edition of the study notes, the original text can refer to the original book Chinese website Bird uncle's Linux private dishes.

In the Linux environment, the operation of the file more through command-line instructions than simply mouse click, and file management is an important operation is compressed and pressurized operation. By compressing and decompressing files, you can make your files take up less storage space, and it makes it easier to transfer files. This article mainly introduces the common packaging instructions and compression instructions under Linux, and introduces the differences between the different extension names of compressed files.

Meaning of the file suffix

  With the development of compression technology, the compression instructions and formats provided in Linux environment begin to become more and more. In order to make it easy for users to distinguish between different compression techniques used by different compressed files and then operate with appropriate instructions, the suffix is used to identify the compression technology used in the compression or packaging process. The following are the common suffixes:

  

*. Z           //    Compress program compression generated files (now rarely used)*.gz          //    gzip program compresses the resulting files *.bz2         //    bzip2 Program compresses the resulting file *.               files generated by the TAR/TAR program packaging *.  Tar. GZ      //     files packaged by the TAR program and compressed by the GZIP program *.  Tar. bz2     //     files packaged by the TAR program and compressed by the BZIP2 program

As can be seen from the suffix, gzip, bzip2, tar directives are more commonly used in the packaging and compression process.

compression Commands --gzip, bzip2

  Gzip

Gzip can compress files that produce a compressed file with the suffix. GZ, and can also be used to decompress files produced by gzip, compress, and other programs. Without any options and parameters using gzip or only with parameters-when gzip reads input from the standard input and compresses the results in the standard output output.

The common instruction options for gzip are as follows:

gzip  [Options] file1 file2 file3
Directive options: (default function is compression)
-c//write output to standard output and keep the original file unchanged
-D //decompression Operation
-V//Output compressed/decompressed file name and compression ratio information
-digit //digit Part of the number (1-9), the compression speed, the smaller the digit, the faster the compression, but the lower the compression effect, the greater the digit, the slower the compression, the better the compression effect. The default is 6.

Note that compressing/extracting files using the GZIP instructions will cause the source file to disappear, i.e. the source file will be decompressed/compressed directly without preserving the backup. If you want to keep the original file you can use the-c parameter in conjunction with the Data Flow redirection operation (see the following example).

Gzip Exp1.txt exp2.txt//compress exp1.txt and exp2.txt respectively and do not retain the original file. Note For multiple file parameters, multiple files are compressed separately, rather than compressed together . Refer to the TAR directive below.

GZIP-DV exp1.gz//Unzip the exp1.gz and display information such as compression ratio.

GZIP-CD exp1.gz > EXP.1//The result of exp1.gz decompression is placed in the file Exp.1, and the original compressed file exp1.gz does not disappear

Pay special attention to the third instance,-D indicates the decompression, the-c parameter is output to the standard output, through the > symbol, the original output to the standard output of the decompression results redirected to the EXP.1, both extracted files, the original compressed file did not disappear.

Note: For gzip, you can use Gunzip for decompression, the effect is similar to gzip-d xx, and you can use Zcat to read the contents of the compressed file and display it in standard output, which functions like Gzip-c xx.

  Bzip2

  BZIP2 is a compression program with a better compression algorithm, which generally provides better compression than gzip. It has an instruction option similar to gzip, which compresses the compressed file that produces the. bz2 suffix.

The common command options for BZIP2 are as follows:

Base format: bzip2 [options] file1 file2 file3 Directive option: (default function is compression)-C // write output to standard output- D //
    
      for decompression Operation
    
-V // output compression/Decompression file name and compression ratio etc.- k //retain the original file during compression/decompression
// The digit part is the number (1-9), represents the compression speed, the smaller the digit, the faster the compression speed, but the lower the compression effect, the larger the digit, the slower the compression, the better the compression effect. The default is 6.

  

BZIP2 exp1.txt exp2.txt//Exp1.txt and Exp2.txt are compressed respectively, and the original file is not retained.

BZIP2-DV exp1.bz2//Unzip the EXP1.BZ2 and display information such as compression ratio.

BZIP2-KD exp1.bz2//exp1.bz2 decompression, and the original compressed file exp1.bz2 will not disappear

Note: corresponding to bzip2, you can use BUNZIP2 for decompression operation, the effect is similar to bunzip2-d xx, while you can use Bzcat to read the contents of the compressed file and display in the standard output, functions similar to Bzip2-c xx.

Packaging Instructions --tar

As mentioned earlier, when Gzip or bzip2 has multiple files as parameters, the action is to compress individual files independently, rather than putting them together. This will not result in packaging compression that resembles a folder in a Windows environment. (Gzip and bzip2 can also use folders as parameters, using the-f option, but also compressing each of these files independently). In order to achieve the effect of packaging compression, you can use the command tar for file packaging Operations (archive), and then compression.

The TAR directive can package files into file files (archive) stored on disk/tape, which is typically accompanied by compression operations, or you can use the TAR instructions to decompress the packaged compressed files.

The tar Common command parameters are as follows:

  

basic format:tar  [Options] file_archive  // Note that the first parameter of tar must be a command option, that is, you cannot directly receive processing file Common command parameters: 
//Specify the action of tar, the following three options cannot appear in the same command
- C //Create a new packaged file (archive)
-X//Unzip the package file (archive)
- T //View the contents of the packaged file (archive), primarily the file name that makes up the packaged file (archive)

//Specify the supported compression/decompression mode, the operation depends on the previous parameters, if it is created (-c), then compression, if decompression (-X), decompression, without the following parameters, it is a simple packaging operation
-Z//Use gzip for compression/decompression, general use. tar.gz suffix
-j//Use BZIP2 for compression/decompression, general use. tar.bz2 suffix

//Specify the file used by the TAR directive and, if there is no compression operation, use. tar as the suffix
-F filename//-f The file used by the subsequent operation, separated by a space, and cannot have other parameters in the middle, it is recommended to put the parameter set at the end or separate as the parameter
The file function depends on the previous parameter, if it is created (-c), then the name (path) of the file created after-F, if (-x/t), then-F is the package compressed file name to extract/view

//Other auxiliary options
-V//verbose display of the file name being processed
-C Dir//To place the extracted files in the directory specified by-C
-P (lowercase)//Preserve file permissions and properties, which is more useful when backing up files
-P (uppercase)//Keep the absolute path of the original file, that is, the root directory where the file path starts will not be removed
--exclude=file//Exclude files that are not packaged

The common tar directives operate as follows:



tar -cvjpf etc.tar. bz2/etc //-c The name of the file created for creating a packaged file, corresponding to-F followed by Using the. tar.bz2 suffix, the-J flag uses bzip2 compression, and the last face is the specific operand/etc directory
 
View:
TAR-TVJF etc.tar.bz2 //-t for the view operation, the-F corresponds to the name of the file being viewed, the file suffix display is compressed using bzip2, so by adding the-j option,-V displays detailed permission information

Extract
TAR-XVJF etc.tar.bz2 //-x for decompression operation, then-f specifies the extracted files, the file suffix display is compressed using bzip2, so add the-J option, that is, using bzip2 decompression
To extract only one of the files in the specified package file, add the extracted file name as parameters at the end of the above instruction.

Note: Files packaged with tar save the original file path, and the root directory of all member file paths is removed by default, as

  

The purpose of this is, when the user in a directory such as/home/haha directory decompression operation, TAR will extract the file path and the current directory splicing, that is,/home/haha/etc/emacs, thereby extracting the files in the current directory. (If you still have an impression, the directory name can also be specified using the-C option), but if you do not remove the root directory of the file path when packing the compression, the file will be extracted according to the absolute path of the store, such as/etc/emacs, may overwrite the file under/etc, and the operation will be valid for backup and recovery. TAR provides the-p option to preserve the root directory of the file path.

Linux file Packaging, decompression instructions--TAR,BZIP2

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.