linux--File Packaging and decompression

Source: Internet
Author: User
Tags builtin bz2 rar unpack

Before you talk about the compression tools on Linux, it's important to look at the commonly used compressed package file formats first. The most common on Windows is nothing more than these three kinds, the *.zip *.rar *.7z suffix of the compressed file. And in Linux above the common format in addition to the above three kinds, there,,,,,,, *.gz *.xz *.bz2 *.tar *.tar.gz *.tar.xz *.tar.bz2 briefly introduced 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.

3.1 Zip compression wrapper

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

The above command packages the directory/home/shiyanlou/desktop 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/Desktop -x ~/*.zip$ zip -r -1 -q -o shiyanlou_1.zip /home/shiyanlou/Desktop -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:

$ du -h -d 0 *.zip ~ | sort

Through the Man Handbook:

    • H,--human-readable (as the name implies, you can try not to add the case)
    • D,--max-depth (depth of the file being viewed)

So at a glance, you can see that the default compression level should be the highest, the effect is obvious, but you see in the environment after operation of the compressed file size may be somewhat different from the diagram, because the system in use, will generate some cache files in the current user's home directory, which for us to learn command use, Is irrelevant, these differences can be ignored.

    • Create an encrypted ZIP package

Use -e parameters to create an encrypted compressed package:

$ zip -r -e -o shiyanlou_encryption.zip /home/shiyanlou/Desktop

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:

$ zip -r -l -o shiyanlou.zip /home/shiyanlou/Desktop

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

3.2 Unzip the zip file using the unzip command

Will shiyanlou.zip extract to the current directory:

$ unzip shiyanlou.zip

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

$ unzip -q shiyanlou.zip -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:

$ unzip -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 above 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 used GBK or other encoding, and Linux above the default is UTF-8 encoding, if not add any processing, the direct 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.3 rar Package 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:
$ sudo apt-get 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:
$ cd Desktop$ rm *.rar$ rar a shiyanlou.rar .

The above command uses a parameters to add the current 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 gvim.desktop
    • 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.

More commonly used on Linux is the tar tool, Tar was originally a packaging tool, but also the implementation of 7z, Gzip, XZ, bzip2 and other tools, 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 /home/shyanlou/Desktop

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 /home/shiyanlou/Desktop
    • Unzip the *.tar.gz file:
$ tar -xzf shiyanlou.tar.gz

Now we need to create or unzip the file using the other compression tools only one parameter can be changed:

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

Said so much, in fact, the usual use of parameters is not so complex, just remember the common combination can be. Common commands:

    • Zip:
      • Package: Zip something.zip Something (directory please add-r parameter)
      • Unpacking: Unzip Something.zip
      • Specify path:-d parameter
    • Tar
      • Package: TAR-ZCVF Something.tar Something
      • Unpacking: TAR-ZXVF Something.tar
      • Specify path:-C parameter

linux--File Packaging and decompression

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.