Linux: File packaging and compression

Source: Internet
Author: User
Tags bz2 gz file rar

Learning content Source: Experimental Building
Links: https://www.shiyanlou.com/


Learning content: Linux commonly used compression/decompression tools, introduced the zip, RAR, tar use.

File packaging and compression
The compressed package file format on Linux, in addition to the most common Windows *.zip, *.rar,. 7z suffix compressed files, as well as. GZ,. XZ,. bz2,. Tar,. tar.gz,. Tar.xz, tar.bz2

File Suffix Name description
*.zipzip Program Packaging Compressed files
*.rarrar Program Compressed Files
*.7z7zip Program Compressed Files
*.tartar program packaging, uncompressed files
*.gzgzip Program (GNU Zip) compressed files
*.XZXZ Program Compressed Files
*.bz2tar package, gzip program compressed files
*.tar.gztar package, gzip program compressed files
*.tar.xztar Packaging, XZ program compressed files
*.tar.bz2tar Packaging, BZIP2 program compressed files
*.tar.7ztar Packaging, 7z program compressed files

Zip compression Wrapper
Use Zip to package files

# package The test directory into a file,-R means that the recursive package contains the entire contents of the subdirectory,-Q for Quiet mode,-O for the output file, followed by the package output filename zip-r-q-o test.zip/home/test
   
    # 
     using the du command to view the size of a packaged file du-
    H test.zip
    # 
     Use the file command to view the size and type of files test.zip
   

Set the compression level to 9 and 1 (9 max, 1 min), repack

# 1 means the fastest compression, but the volume is large, 9 means the smallest volume but the longest time,-X excludes the last zip file we created, the path must be absolute path zip-r -9-q-o test_9.zip/home/test-x ~/*-R -1-q-o test_1.zip/home/test-x ~/*. zip#  then use the du command to view the default compression level, the lowest, the maximum compression level, and the size of the uncompressed file,-h for readable,-D table The depth of the file viewed Du-h- d 0 *.zip ~ | Sort

Create an encrypted ZIP package

# Use the-e parameter to create an encrypted compressed package zip-r-q-o test.zip/home/test


Note: For the zip command, there are some compatibility issues with Linux/unix in the text file format, such as line breaks (for invisible characters), in Windows CR+LF (carriage-return+line-feed: Carriage return add line) , and on the Linux/unix for LF (newline), 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 make a zip archive of Linux created on Windows without any problems, then you need to make some changes to the command
There are different types of variables in the shell that can participate in the operation, scoped to the scope

# Use the-l parameter to convert LF to CR+LFzip-r-l-o test.zip/home/test


Extracting a ZIP file using the unzip command

Use Zip to package files

# extract the Test.zip to the current directory Unzip Test.zip # Use Quiet mode to extract files to the specified directory unzip-q test.zip-D ziptest#  do not want to extract, only want to see the contents of the compressed package can use the-l parameter unzip- L Test.zip # Linux above the default is to use UTF-8 encoding, to prevent the extraction of Chinese garbled, to use the parameter-ounzip-o GBK Chinese compressed file. zip

RAR Packaging Compression Command
On Linux, RAR and Unrar tools can be used to create and decompress RAR packages respectively.

Installing RAR and Unrar tools

sudo apt-get updatesudo apt-get install rar unrar


Create a compressed package or add a file to a compressed package from a specified file or directory

RM *. zip#  Use the A parameter to add a directory ~ to an archive file, and if the file does not exist, it will automatically create rar a test.rar.


Note: RAR command parameters do not-if added will error.

To delete a file from the specified compressed package file

RAR d Test.rar. BASHRC


View unresolved files

RAR l Test.rar


Extracting RAR files using Unrar

# Full path Decompression Unrar x Test.rar # Remove Path Decompression mkdir tmpunrar e test.rar tmp/


Tar Packaging tools
More commonly used in Linux is the Tar tool, Tar was originally a packaging tool, but also to implement a tool 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.

Create a tar Package

# - c means to create a tar package file,-F to specify the file name to create, note that the file name must immediately follow the-f parameter # the absolute path is automatically removed/, you can also use-p to retain the absolute path character TAR-CF Test.tar ~

Unpack a file (-x parameter) to the existing directory of the specified path (-c parameter)

-XF test.tar-c Tardir

View only unresolved package file-T parameter

TAR-TF Test.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 properties of the file (-p parameter) and the source file that the backup link points to instead of the link itself (-H parameter)

TAR-CPHF etc.tar/etc


To use the Gzip tool to create a *.tar.gz file as an example, simply add the-Z parameter to the build tar file and use gzip to compress the file

TAR-CZF etc.tar.gz ~


Unzip the *.tar.gz file

Tar-xzf etc.tar.gz


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

Compress file Format parameters

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

Summarize 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 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.