Linux file Packaging and compression

Source: Internet
Author: User
Tags bz2 rar unpack

Organize from

Https://www.shiyanlou.com/courses/running/61

One, file packaging and decompression

Before you talk about the Unzip tool on Linux, it is important to understand the following commonly used compressed package file formats. On Windows Our most common is nothing more than these three kinds, the *.zip *.rar *.7z suffix of the compressed file, while on the Linux common commonly used in addition to the above three kinds *.gz , *.xz *.bz2 *.tar *.tar.gz *.tar.xz There,,,,,, *tar.bz2, briefly described as follows:

file suffix name description
*.zip Zip program Packaging compressed Files
*.rar rar program Compressed Files
*.7z 7zip Program compressed Files
*.tar tar program package, uncompressed file
*.gz gzip The program (GNU Zip) compressed Files
*.xz XZ Program compressed Files
*.bz2 bzip2 Program compressed Files
*.tar.gz tar packaged, gzip compressed files
*.tar.xz tar package, XZ program compressed Files
*tar.bz2 tar package, bzip2 program compressed file
*.tar.7z tar package, 7z program compressed file

There are so many kinds of compressed files, so many commands, but we generally only need to master a few commands, including, zip rar tar . These commands and the corresponding decompression commands are described in turn.

1. zipCompression Packager
    • To use the Zip Package folder:
$ zip -r -q -o shiyanlou.zip /home/shiyanlou$ du -h shiyanlou.zip$ file shiyanlou.zip

The above command packs the Shiyanlou home directory into a file and looks at the size and type of the file after it is packaged. In the first line of the command, the -r parameter indicates that the recursive package contains the entire contents of the subdirectory, and the -q parameters are expressed in quiet mode, that is, not outputting information to the screen, -o indicating the output file, followed by packaging the output filename immediately thereafter. Later, use the du command to view the size of the file after packaging (this command is specified later).

    • Set the compression level to 9 and 1 (9 max, 1 min), RePack:
$ zip -r -9 -q -o shiyanlou_9.zip /home/shiyanlou -x ~/*.zip$ zip -r -1 -q -o shiyanlou_1.zip /home/shiyanlou -x ~/*.zip

A parameter is added here to set the compression level -[1-9] , 1 for the fastest compression but a large volume, and 9 for the smallest but most time consuming. The last one -x was to get rid of the zip file we created the previous time, or else it would be packaged in a compressed file this time, note that only absolute paths can be used here, otherwise it won't work .

We then use du the command to view the default compression level, the lowest, the highest compression level, and the size of the uncompressed file:

-d 0 *.zip ~ | sort

Through the Man Handbook:

    • H,--human-readble (as the name implies, you can try not to add the case)

    • D,--max-depth (depth of files viewed)

    • Create an encrypted ZIP package

Use -e parameters to create an encrypted compressed package:

-e -o shiyanlou_encryption.zip /home/shiyanlo

Note: for zip commands, because of some compatibility issues with Linux/unix in the text file format, such as line breaks (for invisible characters), Windows is CR+LF (carriage-return+ Line-feed: Carriage return plus line), and on Linux/unix for LF (line wrapping), so if you edit the text on Linux without processing, the open on the Windows system may look like there is no line break. If you want to have a zip archive created by Linux without any problems after extracting it on Windows, then you need to make some changes to the command:

-l -o shiyanlou.zip /home/shiyanlou

The need to add -l parameters will be LF converted to achieve the CR+LF above purpose

2. Use unzipcommand to unzip the zip file

Will shiyanlou.zip extract to the current directory:

$ unzip shiyanlou.zip

Use Quiet mode to extract the files to the specified directory:

-d ziptest

The above specified directory does not exist and will be created automatically. If you do not want to unzip just want to view the contents of the compressed package you can use the -l parameters:

-l shiyanlou.zip

Note: We should also pay attention to compatibility issues when using unzip to extract files, but here we are not concerned about the problem, but the problem of Chinese encoding, usually the Windows system created above the compressed file, If there is a document containing Chinese or Chinese as the file name of the default will be GBK or other encoding, and Linux above the default is UTF-8 encoding, if not add any processing, directly decompression can appear in Chinese garbled problem (sometimes it will automatically help you handle), in order to solve this problem, We can specify the encoding type when extracting.

Use -O the (English letter, capital O) parameter to specify the encoding type:

unzip -O GBK 中文压缩文件.zip
3. rarPackage Compression command

raris also a commonly used in Windows compressed file format, on Linux can be used rar and unrar tools to create and decompress RAR compression package.

    • Installation rar and unrar tools:
update$ sudo apt-get install rar unrar
    • To create a compressed package or add a file to a compressed package from a specified file or directory:
$ rm *.zip$ rar a shiyanlou.rar .

The above command uses a parameters to add a directory to an archive file, which is created automatically if the file does not exist.

Note: RAR command parameters are not - , if added will be an error.

    • To delete a file from the specified compressed package file:
$ rar d shiyanlou.rar .zshrc
    • To view the unresolved files:
$ rar l shiyanlou.rar
    • Use unrar unzip rar file

Full path Decompression:

$ unrar x shiyanlou.rar

Remove Path Decompression:

$ mkdir tmp$ unrar e shiyanlou.rar tmp/

RAR command parameters are very many, the above only involves some basic operations

4. tarPackaging tools

More commonly used on Linux is the tar tool, Tar was originally a packaging tool, but also the implementation of the tools such as 7Z,GZIP,XZ,BZIP2 support, the compression tools themselves can only achieve the file or directory (separate compressed directory files) compression, Does not implement the packaging of the file compression, so we do not have to learn a few other tools, tar decompression and compression are the same command, only the parameters are different, the use of more convenient.

The following is the tar basic use of commands, that is, do not compress just to package (create archive) and unpack the operation.

    • Create a tar package:
$ tar -cf shiyanlou.tar ~

The above command -c indicates the creation of a tar package file that -f specifies the name of the file to be created, noting that the file name must be immediately -f following the parameter, such as cannot be written tar -fc shiyanlou.tar and can be written tar -f shiyanlou.tar -c ~ . You can also add -v parameters to output the packaged files visually. It automatically removes the absolute path / , and you can also use the -P reserved absolute path character.

    • Unpack a file ( -x parameter) to an existing directory ( parameter) of the specified path -C :
$ mkdir tardir$ tar -xf shiyanlou.tar -C tardir
    • To view only the unresolved package file -t parameters:
$ tar -tf shiyanlou.tar
    • Keep file attributes and follow links (symbolic links or soft links), sometimes we use tar to back up files when you restore to other hosts you want to keep the attributes (parameters) of the file -p and the source file that the backup link points to instead of the link itself ( -h parameters):
$ tar -cphf etc.tar /etc

For a file that is created in a different compressed format, it is quite simple for tar and requires just another parameter, and here we use the gzip tool to create the *.tar.gz file as an example to illustrate.

    • We only need to add parameters based on the creation of the tar file -z and use it gzip to compress the file:
$ tar -czf shiyanlou.tar.gz ~
    • Unzip the *.tar.gz file:
$ tar -xzf shiyanlou.tar.gz

Now we're going to create or unzip the file using the other compression tools just to change one parameter:

compressed file format Parameters
*.tar.gz -z
*.tar.xz -J
*tar.bz2 -j

There are many parameters to the tar command, but these are common and need to know more you can see the man manual for more help

Linux file Packaging and compression

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.