6.1 Introduction to Compression and Packaging 6.2 gzip Compression Tool 6.3 bzip2 Compression Tool 6.4 xz Compression Tool

6.1 Introduction to Compression and Packaging

Common compressed file types in Linux environment:

.zip,.gz,.bz2,.xz,
.tar.gz,.tar.bz2,.tar.xz

The purpose of compression

  • Easy file transfer
  • Save disk space
  • Reduce the time it takes to transfer
  • save bandwidth

6.2 gzip compression tool

gzip is the abbreviation of GNUzip, it is a GNU free software file compression program for UNIX system file compression. We often use files with the suffix .gz in Linux, and they are in gzip format.

Note:  gzip cannot compress directory files
Syntax:  gzip [options] [filename]
options:
-d: decompression (=gunzip)
-num: specify the compression level, where num represents a number from 1 to 9, 9 is the best compression, the default is 6 (The higher the compression level, the higher the CPU consumption)

 compression direct compression

[root@cham3 d6z]# find /etc/ -type f -name "*conf"  -exec cat {} >> 1.txt \;
[root@cham3 d6z]# du -sh 1.txt               文件压缩前大小
2.2M	1.txt
[root@cham3 d6z]# wc -l 1.txt                查看其内容总行数
34392 1.txt
[root@cham3 d6z]# gzip 1.txt                 压缩
[root@cham3 d6z]# ls
1.txt.gz                                     压缩后,源文件会消失
[root@cham3 d6z]# du -sh 1.txt.gz 
336K	1.txt.gz                             压缩后文件大小
[root@cham3 d6z]# gzip -d 1.txt.gz           解压文件
[root@cham3 d6z]# du -sh 1.txt
1.3M	1.txt                                解缩后文件大小
[root@cham3 d6z]# wc -l 1.txt
34392 1.txt
[root@cham3 d6z]# gzip -1 1.txt              指定压缩级别
[root@cham3 d6z]# du -sh 1.txt.gz 
392K	1.txt.gz
[root@cham3 d6z]# gunzip 1.txt.gz 
[root@cham3 d6z]# gzip -9 1.txt              指定压缩级别
[root@cham3 d6z]# du -sh 1.txt.gz 
336K	1.txt.gz
[root@cham3 d6z]# file 1.txt.gz             file 1.txt.gz   查看文件属性
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Thu Nov  9 10:41:48 2017, max compression

 Specify the compressed directory

[root@cham3 d6z]# gzip -c 1.txt > /tmp/1.txt.gz 
[root@cham3 d6z]# ls /tmp/1.txt.gz
/tmp/1.txt.gz
[root@cham3 d6z]# ls  压缩完成后源文件不会消失
1.txt  2.txt.gz
[root@cham3 d6z]# file /tmp/1.txt.gz
/tmp/1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Thu Nov  9 11:12:44 2017

Use the zcat command: 

[root@cham3 d6z]# zcat 2.txt.gz     可查看压缩文件内部内容

decompress

Decompress directly

gzip -d

[root@cham3 d6z]# gzip -d 1.txt.gz           解压文件    解压后文件消失
[root@cham3 d6z]# du -sh 1.txt
1.3M	1.txt                                解缩后文件大小
[root@cham3 d6z]# wc -l 1.txt
34392 1.txt

eg2:gunzip 

[root@cham3 d6z]# gunzip 1.txt
[root@cham3 d6z]# ls
1.txt  2.txt.gz

 

6.3 bzip2 compression tool

bzip2 is a lossless compression software based on Burrows-Wheeler transform, the compression effect is better than the traditional LZ77/LZ78 compression algorithm. It is a free software. It can be freely distributed and used for free. It is widely present in many distributions of UNIX & LINUX. bzip2 enables high-quality data compression. It utilizes advanced compression technology and can compress ordinary data files by 10% to 15%. The compression speed and decompression efficiency are very high! Supports most compression formats, including tar, gzip and so on.

Note:  bzip2 cannot compress directory files
Syntax:  bzip2 [options] [filename]
options:
-d: decompress
-z: compress (=bzip2, so it can be used directly without this parameter)

If not installed, please install it first

[root@cham002 dabao]# yum -y install bzip2-x86_64 
已加载插件:fastestmirror


The usage of bzip2 is the same as gzip.

compression

[root@cham3 d6z]# bzip2 1.txt
[root@cham3 d6z]# ls
1.txt.bz2  2.txt.gz
[root@cham3 d6z]# du -sh 1.txt.bz2 
172K	1.txt.bz2                 较gzip压缩程度更高

decompress

[root@cham3 d6z]# bzip2 -d 1.txt.bz2 
[root@cham3 d6z]# ls
1.txt  2.txt.gz
[root@cham3 d6z]# du -sh *.txt
1.3M	1.txt
1.3M	2.txt
——————————————————————————————————————————————————————
[root@cham3 d6z]# bzip2 -z 1.txt
[root@cham3 d6z]# ls
1.txt.bz2  2.txt


[root@cham3 d6z]# bunzip2 1.txt.bz2   方法2
[root@cham3 d6z]# ls
1.txt  2.txt
[root@cham3 d6z]# !du
du -sh *.txt
1.3M	1.txt
1.3M	2.txt

Description:  Same as gzip, this command can also specify a directory for compression and decompression.

 

case

Scenario:  When viewing a directory file, there is a file 1.txt in it, and the following prompt appears when viewing its content with commands such as cat: "1.txt" may be a binary file. See it anyway? . Press 'y' at this time, a bunch of garbled characters will appear on the screen, quickly press q to exit, and then find the reason: use the file command!

[root@cham3 d6z]# ls
1.txt  2.txt.gz  3.txt
[root@cham3 d6z]# less 1.txt
"1.txt" may be a binary file.  See it anyway? 
[root@cham3 d6z]# file 1.txt
1.txt: bzip2 compressed data, block size = 900k

After viewing the file information by the file command, it is known that it is a '.bz2' compressed file, that is, you can use the bzcat command to view it, and change its file name to the correct format to prevent it from being misled again.

[root@cham3 d6z]# bzcat 1.txt
由于内容太多,在此不做演示
[root@cham3 d6z]# mv 1.txt 1.txt.bz2

6.4 xz compression tool

xz is a compressed file format, which is compressed by LZMA SDK, and the target file is 30% smaller than gzip compressed file (.gz or tgz) and 15% smaller than bz2.

 Note:  xz cannot be used to compress directory files
Syntax:  xz [options] [filename]
options:
-d: The decompression
method is the same as gzip and bzip2, and the compression degree is higher.

compression

[root@cham3 d6z]# xz 1.txt
[root@cham3 d6z]# du -sh 1.txt.xz 
40K	1.txt.xz
[root@cham3 d6z]# file 1.txt.xz    查看压缩文件类型
1.txt.xz: XZ compressed data
[root@cham3 d6z]#xzcat 2.txt.xz 查看压缩文件内容  “内容过多不复制”

decompress

[root@cham3 d6z]# xz -d 1.txt.xz 
[root@cham3 d6z]# ls
1.txt  2.txt.gz  3.txt
[root@cham3 d6z]# du -sh 1.txt 
2.0M	1.txt
————————————————————————————————————————————————————————
[root@cham3 d6z]# unxz 1.txt.xz 
[root@cham3 d6z]# ls
1.txt  2.txt.gz  3.txt

 Like gzip and bzip2, this command can also specify a directory for compression and decompression.

[root@cham3 d6z]# xz -c 1.txt > /tmp/1.txt.xz
[root@cham3 d6z]# ls /tmp/1.txt.
1.txt.bz2  1.txt.gz   1.txt.xz   
[root@cham3 d6z]# ls /tmp/1.txt.xz
/tmp/1.txt.xz
[root@cham3 d6z]# xz -d -c /tmp/1.txt.xz > ./4.txt
[root@cham3 d6z]# ls
1.txt  2.txt.gz  3.txt  4.txt

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325449659&siteId=291194637