Mylinux Note---9th Day

Source: Internet
Author: User
Tags compress archive gz file ziparchive

Compression and packaging of documents

One, gzip and gunzip compression

Command format

Compression: gzip [options] [filename ... ]

Decompression: gunzip [options] [filename ... ]

Common Command Options

-D: Unzip the compressed file

-L: For each compressed document, the following information is displayed: the size of the compressed file, the size of the uncompressed file, the compression ratio, and the name of the uncompressed file

-R: Compress or decompress files in each level subdirectory under the specified directory recursively

-T: Checking the integrity of compressed files

-V: To compress and decompress each file, display file name and compression ratio

-N: Adjusts the speed of compression with the specified number, 1 or--fast for the fastest compression (low compression ratio), 9 or--best for the slowest compression ratio (high compression), and a system default of 6

Instance:

1. Compress all files in the directory

[[email protected] bbb]# ls-r.:1.txt 2.txt 3.txt ccc./ccc:1.txt 2.txt 3.txt[[email protected] bbb]# gzip-r *[[email Protected] bbb]# ls-r.:1.txt.gz 2.txt.gz 3.txt.gz ccc./ccc:1.txt.gz 2.txt.gz 3.txt.gz

2. Compress some files

[[Email protected] bbb]# gzip 2.txt 3.txt[[email protected] bbb]# ls1.txt 2.txt.gz 3.txt.gz CCC

3, view the situation of compressed files

[[email protected] bbb]# gzip -rl *          compressed        uncompressed  ratio  uncompressed_name                 649                1534   59.3% 1.txt                3072                10688  71.5% 2.txt                3680                64128  94.3% 3.txt                3680   &nbSp;           64128  94.3% ccc/3.txt                3072                10688  71.5% ccc/ 2.txt                649                 1534  59.3 % ccc/1.txt              14802               152700  90.3%   (totals)

4. Unzip the file (same effect as gzip-d)

[[email protected] bbb]# gunzip-r *[[email protected] bbb]# ls-r.:1.txt 2.txt 3.txt ccc./ccc:1.txt 2.txt 3.txt



Second, bzip2 and BUNZIP2 compression

Command format

Compression: bzip2 [options] [filename ... ]

Decompression: BUNZIP2 [options] [filename ... ]


Common Command Options

-D: Unzip

-F: When compressed or decompressed, if the file already exists, the existing file is not overwritten by default. With-F, you can force an overlay

-K: The source file is deleted by default when compressed or decompressed. With-K, the source file is preserved.

-Q: Quiet mode

-S: Reduce the amount of memory used during program execution

-T: Testing the integrity of compressed files

-V: Displays detailed information when compressing or decompressing a file


Instance

1. Compress files and keep source files

[Email protected] bbb]# bzip2-kv 1.txt 2.txt 3.txt 1.txt:2.263:1, 3.536 bits/byte, 55.80% saved, 1534 in, 678 out.  2.txt:3.413:1, 2.344 Bits/byte, 70.70% saved, 10688 in, 3132 out. 3.txt:13.878:1, 0.576 Bits/byte, 92.79% saved, 64128 in, 4621 out. [[email protected] bbb]# ls1.txt 1.txt.bz2 2.txt 2.txt.bz2 3.txt 3.txt.bz2 CCC

2. Unzip the file

[[email protected] bbb]# ls1.txt.bz2 2.txt.bz2 3.txt.bz2 ccc[[email protected] bbb]# bunzip2-v * 1.txt.bz2:done 2.t Xt.bz2:done 3.txt.bz2:donebunzip2:input file CCC is a directory. [[email protected] bbb]# ls1.txt 2.txt 3.txt CCC



Three, zip and unzip compression

Command format

Zip [parameters] [zip file name [file 1 file 2 ... ]]

Common Command Options

-M: After compressing the file, delete the source file

-Q: Quiet mode, does not show the execution of the command during compression

-R: Recursively process all subdirectories and files in the specified directory

Unzip [parameters] Zip file

Common Command Options

-L: Lists what the compressed file contains

-V: Show detailed execution procedures


Instance

1. Compress all files and directories in the specified directory

[[email protected] bbb]# ls1.txt 2.txt 3.txt ccc[[email protected] bbb]# zip-r File.zip * Adding:1.txt (deflated 59%  ) Adding:2.txt (deflated 71%) Adding:3.txt (deflated 94%) adding:ccc/(stored 0) adding:ccc/3.txt (deflated 94%) Adding:ccc/1.txt (deflated 59%) Adding:ccc/2.txt (deflated 71%) [[email protected] bbb]# ls1.txt 2.txt 3.txt CCC fi Le.zip

2. Compress some files

[[email protected] bbb]# ls1.txt 2.txt 3.txt ccc[[email protected] bbb]# zip file.zip 2.txt 3.txt adding:2.txt (defla Ted 71%) Adding:3.txt (deflated 94%) [[email protected] bbb]# ls1.txt 2.txt 3.txt CCC File.zip

3, view the situation of compressed files

[Email protected] bbb]# unzip-l file.zipArchive:file.zip Length Date time Name------------------------                      ----10688 03-25-2015 23:34 2.txt 64128 03-25-2015 23:34 3.txt----------------74816 2 files

4. Unzip the file

[Email protected] aaa]# lsbbb File.zip[[email protected] aaa]# unzip File.zipArchive:file.zip inflating:2.txt Infla Ting:3.txt[[email protected] aaa]# ls2.txt 3.txt BBB file.zip



Four, XZ compression

Command format

XZ [options] [filename ... ]

-D: Extracting Compressed files

[Email protected] bbb]# XZ file.zip 2.txt 3.txt

Unzip

[Email protected] bbb]# xz-d file.zip




V. PACKING PROCESS Tar

Command format

tar [options] tar file [directory or file]


Common options

-C: Create a new archive file

-D: Check the difference between the archive file and the specified directory

-r: Append files to the archive file

-T: List the contents of the archive file

-V: Displays information about command execution

-U: Added only if the file that needs to be appended is newer than the version of the file that already exists in the tar file

-X: Restore files or directories in an archive file

-Z: Archive files using zip compression

-j: Using bzip2 to compress archive files


Instance

1. Packaging files

[[email protected] aaa]# ls2.txt 3.txt BBB file.zip[[email protected] aaa]# TAR-CVF Fiel_2.tar bbb/bbb/bbb/3.txtbbb/fi  Le.zipbbb/ccc/bbb/ccc/3.txtbbb/ccc/1.txtbbb/ccc/2.txtbbb/1.txtbbb/2.txt[[email protected] aaa]# ls2.txt 3.txt BBB File_2.tar File.zip

2. View the contents of the archive file

[[email protected] aaa]# ls2.txt  3.txt  bbb  file_2.tar   file.zip[[email protected] aaa]# tar -tvf file_2.tardrwxr-xr-x root/root          0 2015-03-26 20:51 bbb/-rw-r--r--  root/root     64128 2015-03-25 23:34 bbb/3.txt-rw-r--r-- root/ root      7002 2015-03-26 20:51 bbb/file.zipdrwxr-xr-x root/ root         0 2015-03-25 23:11 bbb/ccc/-rw-r--r--  root/root     64128 2015-03-25 22:50 bbb/ccc/3.txt-rw-r--r--  root/root      1534 2015-03-25 22:50 bbb/ccc/1. Txt-rwxr-xr-x root/root     10688 2015-03-25 22:50 bbb/ccc/2. txt-rw-r--r-- root/root      1534 2015-03-25 23:34 bbb/1.txt-rwxr-xr-x root/root      10688 2015-03-25 23:34 bbb/2.txt[[email protected] aaa]#

3. Restore archived Files

[[email protected] aaa]# ls2.txt 3.txt BBB file_2.tar file.zip[[email protected] aaa]# RM-RF Bbb[[email protected] A aa]# ls2.txt 3.txt file_2.tar file.zip[[email protected] aaa]# TAR-XVF file_2.tarbbb/bbb/3.txtbbb/file.zipbbb/ccc/bbb/ Ccc/3.txtbbb/ccc/1.txtbbb/ccc/2.txtbbb/1.txtbbb/2.txt[[email protected] aaa]# ls2.txt 3.txt BBB File_2.tar file.zip

4. Append new files to the archive file

[[email protected] aaa]# ls2.txt  3.txt  bbb  file_2.tar   file.zip[[email protected] aaa]# tar rvf file_2.tar 2.txt2.txt[[email  protected] aaa]# tar -tvf file_2.tardrwxr-xr-x root/root          0 2015-03-26 20:51 bbb/-rw-r--r-- root/root      64128 2015-03-25 23:34 bbb/3.txt-rw-r--r-- root/root       7002 2015-03-26 20:51 bbb/file.zipdrwxr-xr-x root/root          0 2015-03-25 23:11 bbb/ccc/-rw-r--r-- root/root      64128 2015-03-25 22:50 bbb/ccc/3.txt-rw-r--r-- root/root       1534 2015-03-25 22:50 bbb/ccc/1.txt-rwxr-xr-x root/ root     10688 2015-03-25 22:50 bbb/ccc/2.txt-rw-r--r-- root/root       1534 2015-03-25 23:34 bbb/1.txt-rwxr-xr-x root/root     10688  2015-03-25 23:34 bbb/2.txt-rwxr-xr-x root/root     10688  2015-03-25 23:34 2.txt[[email protected] aaa]#


Update file

[[email protected] aaa]# ls2.txt 3.txt BBB file_2.tar file.zip[[email protected] aaa]# echo "aaaaaa" >> bbb/3.tx T[[email protected] aaa]# TAR-UVF file_2.tar bbb/3.txtbbb/3.txt[[email protected] aaa]#


5. Compress archive Files

Compress a directory by using the gzip command

[[email protected] aaa]# ls2.txt 3.txt BBB file_2.tar file.zip[[email protected] aaa]# TAR-CZVF file_3.tar.gz bbb/bbb /bbb/3.txtbbb/file.zipbbb/ccc/bbb/ccc/3.txtbbb/ccc/1.txtbbb/ccc/2.txtbbb/1.txtbbb/2.txt[[email protected] aaa]# Ls2.txt 3.txt BBB File_2.tar file_3.tar.gz file.zip

Compress packaging with bzip2

[[email protected] aaa]# ls2.txt 3.txt BBB file_2.tar file_3.tar.gz file.zip[[email protected] aaa]# TAR-CJVF file_4 . tar.bz2 Bbb/bbb/bbb/3.txtbbb/file.zipbbb/ccc/bbb/ccc/3.txtbbb/ccc/1.txtbbb/ccc/2.txtbbb/1.txtbbb/2.txt[[email Protected] aaa]# ls2.txt 3.txt bbb file_2.tar file_3.tar.gz file_4.tar.bz2 file.zip

Restoring gzip-Compressed archive files

[Email protected] aaa]# ls2.txt 3.txt bbb file_2.tar file_3.tar.gz file_4.tar.bz2 file.zip[[email protected] aaa]# RM-RF Bbb[[email protected] aaa]# ls2.txt 3.txt file_2.tar file_3.tar.gz file_4.tar.bz2 file.zip[[email protected] A aa]# TAR-XZVF file_3.tar.gzbbb/bbb/3.txtbbb/file.zipbbb/ccc/bbb/ccc/3.txtbbb/ccc/1.txtbbb/ccc/2.txtbbb/1.txtbbb/ 2.txt[[email protected] aaa]# ls2.txt 3.txt bbb file_2.tar file_3.tar.gz file_4.tar.bz2 file.zip

Restore BZIP2 Compressed archive files

[Email protected] aaa]# ls2.txt 3.txt bbb file_2.tar file_3.tar.gz file_4.tar.bz2 file.zip[[email protected] aaa]# RM-RF Bbb[[email protected] aaa]# ls2.txt 3.txt file_2.tar file_3.tar.gz file_4.tar.bz2 file.zip[[email protected] A aa]# TAR-XJVF FILE_4.TAR.BZ2BBB/BBB/3.TXTBBB/FILE.ZIPBBB/CCC/BBB/CCC/3.TXTBBB/CCC/1.TXTBBB/CCC/2.TXTBBB/1.TXTBBB /2.txt[[email protected] aaa]# ls2.txt 3.txt bbb file_2.tar file_3.tar.gz file_4.tar.bz2 file.zip

Package compression Comparison

[Email protected] aaa]# LS-LH file_*-rw-r--r--. 1 root root 170K March 21:27 file_2.tar-rw-r--r--. 1 root root 13K March 21:16 file_3.tar.gz-rw-r--r--. 1 root root 15K March 21:21 file_4.tar.bz2

Special usage: Exclude a file or directory when packaging

[Email protected] aaa]# tar rvf file_2.tar--exclude 2.txt











This content by Mentor: Amin provides technical support: with Amin learn Linux dot here




This article is from the "Mylinux" blog, make sure to keep this source http://mylinuxlife.blog.51cto.com/4706737/1625829

Mylinux Note---9th Day

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.