Linux File compression and Packaging Commands

Source: Internet
Author: User
Tags tar unzip

Linux File compression and Packaging Commands

First, we need to clarify two concepts: Packaging and compression.
Packaging refers to converting a large number of files or directories into a total file;
Compression is to convert a large file into a small file through some compression algorithms.
Why do we need to differentiate these two concepts? This is because many Linux compression programs can only compress one file. In this way, when you want to compress a large number of files, you must first compress these files into a package (tar command ), then use the compression program to compress (gzip bzip2 command ).

At the end of lifecycle. After the tar package is generated, other programs can be used for compression.

1. gzip Tool
Syntax: gzip [-d #] filename where # Is a number ranging from 1 to 9. The default compression level is 6.
Only files can be compressed
-F forcibly overwrite the compressed file
-1. Fast File compression
-9. Optimal File compression
-V visual compression
Gzip filename generation filename.gz source file disappears
Decompress gzip-d filename.gz and the compressed file disappears.
Zcat is used to view gzip compressed packages.

Optimal compression and decompression

[Root @ localhost tmp] # gzip-9 a. img
[Root @ localhost tmp] # ls-lk
-Rw -------. 1 root 16507 Mar 26 13:00 a.img.gz
[Root @ localhost tmp] # gzip-d a.img.gz
[Root @ localhost tmp] # ls-lk
-Rw -------. 1 root 16568 Mar 26 13:00 a. img

Multiple files can be compressed simultaneously. gzip file1 file2 file3
1234 [root @ localhost tmp] # gzip a. img dhcp-4.3.1.tar
[Root @ localhost tmp] # ls-l
-Rw -------. 1 root 16902566 Mar 26 13:00 a.img.gz
-Rwxr-xr-x. 1 root 8987298 Mar 26 dhcp-4.3.1.tar.gz

Visual Compression
[Root @ localhost tmp] # gzip-v dhcp-4.3.1.tar
Dhcp-4.3.1.tar: 33.5% -- replaced with dhcp-4.3.1.tar.gz

2. bzip2 compression Tool
Syntax: bzip2 [-dz] filename
You can add "-z" or "-z" to compress files.
Bzip2 filename generation filename.bz2 source file disappears
The directory cannot be compressed.

-D. Force extract the file.
-Z: forcibly compresses the file. By default, you do not need to add
-K compression to retain the original file
-F force overwrite the original file during decompression
-V visual compression: displays the space-saving percentage and the size before and after compression;
Bzip2-d filename.bz2 decompress the package and the compressed file disappears.
You can use bzcat to view the compressed file content of bz2.

Multiple files can be compressed simultaneously bzip2 file1 file2

[Root @ localhost tmp] # bzip2 a. img
[Root @ localhost tmp] # ls-l
-Rw -------. 1 root 17025434 Mar 26 13:00 a.img.bz2
[Root @ localhost tmp] # bzip2-v dhcp-4.3.1.tar
Dhcp-4.3.1.tar: 1.538: 1, 5.200 bits/byte, 35.00% saved, 13506560 in, 8779359 out.

The source file is retained during compression, and the source file is forcibly overwritten during decompression.

[Root @ localhost tmp] # bzip2-k a. img
[Root @ localhost tmp] # ls-lh
-Rw -------. 1 root 17 M Mar 26 a. img
-Rw -------. 1 root 17 M Mar 26 13:00 a.img.bz2
[Root @ localhost tmp] # bzip2-dfv a.img.bz2
A.img.bz2: done
[Root @ localhost tmp] # ls-lh
Drwxr-xr-x. 2 root 4.0 K Mar 27 abc
-Rw -------. 1 root 17 M Mar 26 a. img

3. xz
The usage is the same as that of gzip and bzip2.
Xz filename generates filename. xz
The directory cannot be compressed.

-V visual compression: displays the compression progress and time;
Xz-d filename. xz decompress
Xcat is used to view the content of the XSS compressed package.

Multiple directories can be compressed at the same time, multiple directories can be decompressed, and displayed visually;
[Root @ localhost tmp] # xz a. img dhcp-4.3.1.tar
[Root @ localhost tmp] # ls-lh
-Rw -------. 1 root 17 M Mar 26 a. img. xz
-Rwxr-xr-x. 1 root 8.0 M Mar 26 dhcp-4.3.1.tar.xz
[Root @ localhost tmp] # xz-dv a. img. xz dhcp-4.3.1.tar.xz
A. img. xz (1/2)
100.0% 16.1 MiB/16.2 MiB = 0.997
Dhcp-4.3.1.tar.xz (2/2)
100.0% 8,146.0 KiB/12.9 MiB = 0.618

4. zip and unzip
Zip is a compression tool and unzip is a decompression tool. It can be used only after installation.

Command for installing zip: yum install-y zip
Command for unzip installation: yum install-y unzip
-V visually displays the compression process and the file size and compression percentage before and after compression;
-D: used for decompression. It specifies the directory to which the package is decompressed;
You cannot decompress multiple files at the same time.-v visualization is not supported;

Compressed file: zip filename.zip filename
Compressed Directory: zip-r dir.zip dir/
Decompress the zip package: unzip filename.zip


Decompress the abc directory to AB .zip, decompress it to the AB directory, and decompress the entire abc directory to the AB directory;
[Root @ localhost tmp] # zip-r AB .zip abc/
Adding: abc/(stored 0%)
Adding: abc/passwd (deflated 58%)
[Root @ localhost tmp] # unzip AB .zip-d AB
Archive: AB .zip
Creating: AB/abc/
Inflating: AB/abc/passwd
Inflating: AB/abc/a. img
[Root @ localhost tmp] # ls-lh
Drwxr-xr-x. 3 root 4.0 K Mar 27 AB
Drwxr-xr-x. 2 root 4.0 K Mar 27 abc
-Rw-r --. 1 root 17 M Mar 27 :25 AB .zip
[Root @ localhost tmp] # ls-l AB/
Drwxr-xr-x. 2 root 4096 Mar 27 :24 abc
[Root @ localhost tmp] # ls-l AB/abc/
-Rw -------. 1 root 16965117 Mar 27 a. img
-Rw-r --. 1 root 1019 Mar 27 13:23 passwd

You can use file name.gz name.zip to query which compression format is used;

5. tar packaging Tool

You can package directories or files.
Syntax: tar [-zjxcvfpP] filename
Package: tar-cvf test.tar test where test is a file or directory
-C indicates creating a package
-V Visual Packaging Process
-F indicates that the compressed file name is filename, and-f indicates that the compressed file name is filename. note that if multiple parameters are combined with "-f", write "-f" to the end.
-Use gzip to compress the-z package
-J: Use bzip2 to compress the package.
-J. Compress With xz when Packaging
-C: Specifies the decompressed directory.
Tar-C/tmp/-xvf 1.tar unzip to the specified directory/tmp

View package content: tar-tf test.tar
-T view the files in the tar package
Use tar-tf to view compressed packages: tar-tf 1.tar.gz or tar-tf 1.tar.bz2

Unpack: tar-xvf test.tar
-X unpack or decompress the package
Whether it is packaging or unpacking, the original file will not be deleted, but it will overwrite the existing file or directory.

Package the content in the abcdirectory as abc.tar, and decompress the abc.tar package;

[Root @ localhost tmp] # ls-l
Drwxr-xr-x. 2 root 4096 Mar 27 :27 abc
-Rw -------. 1 root 16965117 Mar 26 13:00 a. img
-Rwxr-xr-x. 1 root 13506560 Mar 26 dhcp-4.3.1.tar
[Root @ localhost tmp] # tar-cvf abc.tar abc
Abc/
Abc/passwd
Abc/a. img
[Root @ localhost tmp] # tar-tf abc.tar
Abc/
Abc/passwd
Abc/a. img
[Root @ localhost tmp] # tar-xvf abc.tar
Abc/
Abc/passwd
Abc/a. img

Pack multiple files to 11.tar at the same time
[Root @ localhost tmp] # tar-cvf 11.tar abc a. img dhcp-4.3.1.tar abc.tar
Abc/
Abc/passwd
Abc/a. img
A. img
Dhcp-4.3.1.tar
Abc.tar
[Root @ localhost tmp] # ls-lh
-Rw-r --. 1 root 62 M Mar 27 16:33 11.tar


Use gzip for compression during packaging: tar-czvf 1.tar.gz 1 where 1 can be a file or a directory
-Z indicates that gzip is used for both packaging and compression.
Decompress the compressed package of .tar.gz: tar-xzvf 1.tar.gz
Use bzip2 for compression: tar-cjvf 1.tar.bz2 1
-J indicates that bzip2 is used for both packaging and compression.
Decompress .tar.bz2: tar-xjvf 1.tar.bz2


Use gzip to compress and package, use bzip2 to compress and package, and compare the two compression formats. After bzip2 is compressed, the file size is smaller. Use xz compression to achieve the best compression effect! Minimum File size after compression!
The source file is 13 M, gzip is compressed to 8.6 M, bzip2 is compressed to 8.4 M, and xz is compressed to 8.0 M;

[Root @ localhost tmp] # ls-lh
-Rwxr-xr-x. 1 root 13 M Mar 26 dhcp-4.3.1.tar
[Root @ localhost tmp] # tar-czvf gzip.tar.gz dhcp-4.3.1.tar dhcp-4.3.1.tar
[Root @ localhost tmp] # tar-cjvf bzip2.tar.bz2 dhcp-4.3.1.tar dhcp-4.3.1.tar
[Root @ localhost tmp] # ls-lh
-Rw-r --. 1 root 8.4 M Mar 27 16:54 bzip2.tar.bz2
-Rwxr-xr-x. 1 root 13 M Mar 26 dhcp-4.3.1.tar
-Rw-r --. 1 root 8.6 M Mar 27 16:54 gzip.tar.gz
[Root @ localhost tmp] # xz dhcp-4.3.1.tar
[Root @ localhost tmp] # ls-lh
-Rw-r --. 1 root 8.4 M Mar 27 16:54 bzip2.tar.bz2
-Rwxr-xr-x. 1 root 8.0 M Mar 26 dhcp-4.3.1.tar.xz
-Rw-r --. 1 root 8.6 M Mar 27 16:54 gzip.tar.gz

Sometimes we can see a file with the suffix ". tar. xz", which is compressed using the xz tool,

Package and compress the package to the xz format: tar-cJvf dir.tar. xz dir/
Decompression method: tar-Jxvf file.tar. xz

You can exclude some files or directories from adding the parameter -- exclude during packaging.
Tar -- exclude 1.txt-czvf 1.tar.gz dir/
Exclude multiple files or directories: tar -- exclude "directory name" -- exclude "* file name"-czvf 1.tar.gz dir/

Package rootdirectories to 1.tar.gz and exclude files starting with install in the directory;

[Root @ localhost ~] # Tar-czvf 1.tar.gz -- exclude "install *"/root/

This article permanently updates the link address:

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.