一、lz4命令簡(jiǎn)介
LZ4是一種壓縮格式,特點(diǎn)是壓縮/解壓縮速度超快(壓縮率不如gzip),如果你特別在意壓縮速度,或者當(dāng)前環(huán)境的CPU資源緊缺,可以考慮這種格式。lz4是一種非常快速的無(wú)損壓縮算法,基于字節(jié)對(duì)齊LZ77系列壓縮方案。lz4提供每核400 MB/s的壓縮速度,可通過(guò)多核CPU線(xiàn)性擴(kuò)展。它的特點(diǎn)是極快的解碼器,每核速度可達(dá)多GB/s,通常在多核系統(tǒng)上達(dá)到RAM速度限制項(xiàng)目。lz4遵循上面說(shuō)到的lz77思想理論,通過(guò)滑動(dòng)窗口、hash表、數(shù)據(jù)編碼等操作實(shí)現(xiàn)數(shù)據(jù)壓縮。壓縮過(guò)程以至少4字節(jié)為掃描窗口查找匹配,每次移動(dòng)1字節(jié)進(jìn)行掃描,遇到重復(fù)的就進(jìn)行壓縮。centos7默認(rèn)安裝了lz4命令,可以實(shí)現(xiàn)lz4格式文件的壓縮和解壓縮。
二、命令使用示例
1、查看命令版本
lz4命令安裝版本是1.7.5
[root@s76 ~]# lz4 -V
*** LZ4 command line interface 64-bits v1.7.5, by Yann Collet ***
2、獲取命令幫助
日常使用中如果忘記lz4命令語(yǔ)法格式,我們可以通過(guò)lz4 –help或者man lz4命令獲取lz4命令的幫助信息。
[root@s76 ~]# lz4 –help
[root@s76 ~]# man lz4
3、命令安裝
centos7默認(rèn)安裝了lz4命令,如果沒(méi)有安裝,可以使用yum安裝方式安裝該命令。
[root@s76 ~]# yum install -y lz4 lz4-devel
4、壓縮單個(gè)文件
[root@s76 ~]# lz4 anaconda-ks.cfg test.lz4
Compressed 2927 bytes into 1825 bytes ==> 62.35%
5、壓縮多個(gè)文件
壓縮多個(gè)文件使用參數(shù)-m,壓縮后的文件名是源文件加上lz4后綴。lz4命令只可以將單個(gè)文件壓縮,如果我們需要將多個(gè)文件壓縮到一個(gè)文件,我們需要將lz4和tar命令結(jié)合使用。
[root@s76 ~]# lz4 -m anaconda-ks.cfg original-ks.cfg
[root@s76 ~]# ll
total 16
-rw——-. 1 root root 2927 Feb 8 15:19 anaconda-ks.cfg
-rw——-. 1 root root 1825 Feb 8 15:19 anaconda-ks.cfg.lz4
-rw——-. 1 root root 2045 Feb 8 15:19 original-ks.cfg
-rw——-. 1 root root 1216 Feb 8 15:19 original-ks.cfg.lz4
[root@s76 ~]# tar -cvf anaconda-ks.cfg original-ks.cfg |lz4 – 2.tar.lz4
Compressed 16 bytes into 35 bytes ==> 218.75%
6、壓縮目錄
lz4只能壓縮文件,如果需要壓縮目錄需要結(jié)合tar命令一起。
[root@s76 ~]# tar cvf – test | lz4 – 1.tar.lz4
test/
test/1.tar
Compressed 20480 bytes into 325 bytes ==> 1.59%

7、壓縮后刪除源文件
[root@s76 ~]# lz4 –rm hi.txt hi.txt.lz4
Compressed 5 bytes into 24 bytes ==> 480.00%
[root@s76 ~]# ll
total 24
-rw-r–r–. 1 root root 325 Feb 12 20:57 1.tar.lz4
-rw——-. 1 root root 10240 Feb 12 20:40 anaconda-ks.cfg
-rw-r–r–. 1 root root 24 Feb 12 21:01 hi.txt.lz4
-rw——-. 1 root root 2045 Feb 8 15:19 original-ks.cfg
drwxr-xr-x. 2 root root 19 Feb 12 20:38 test
8、解壓lz4文件
[root@s76 ~]# lz4 -d hi.txt.lz4
Decoding file hi.txt
hi.txt.lz4 : decoded 5 bytes
[root@s76 ~]# ll
total 28
-rw-r–r–. 1 root root 325 Feb 12 20:57 1.tar.lz4
-rw——-. 1 root root 10240 Feb 12 20:40 anaconda-ks.cfg
-rw-r–r–. 1 root root 5 Feb 12 21:01 hi.txt
-rw-r–r–. 1 root root 24 Feb 12 21:01 hi.txt.lz4
-rw——-. 1 root root 2045 Feb 8 15:19 original-ks.cfg
drwxr-xr-x. 2 root root 19 Feb 12 20:38 test
9、解壓并刪除壓縮文件
[root@s76 ~]# lz4 –rm -d hi.txt.lz4
Decoding file hi.txt
hi.txt.lz4 : decoded 5 bytes
[root@s76 ~]# ll
total 24
-rw-r–r–. 1 root root 325 Feb 12 20:57 1.tar.lz4
-rw——-. 1 root root 10240 Feb 12 20:40 anaconda-ks.cfg
-rw-r–r–. 1 root root 5 Feb 12 21:01 hi.txt
-rw——-. 1 root root 2045 Feb 8 15:19 original-ks.cfg
drwxr-xr-x. 2 root root 19 Feb 12 20:38 test
10、高壓縮比方式壓縮
[root@s76 ~]# lz4 -9 hi.txt hi.txt.lz4
Compressed 5 bytes into 24 bytes ==> 480.00%
11、壓縮并覆蓋文件
[root@s76 ~]# lz4 hi.txt.lz4 hi.txt
hi.txt already exists; do you wish to overwrite (y/N) ? y
Compressed 24 bytes into 43 bytes ==> 179.17%
[root@s76 ~]# lz4 -f hi.txt.lz4 hi.txt
Compressed 24 bytes into 43 bytes ==> 179.17%
12、解壓并輸出文件
[root@s76 ~]# cat hi.txt
hi,wuhs
[root@s76 ~]# lz4 -dc hi.txt.lz4
hi,wuhs
13、解壓速度測(cè)試
1個(gè)22G的文件解壓花費(fèi)時(shí)間5分18秒,解壓后的大小為45G。

三、lz4命令使用語(yǔ)法及參數(shù)說(shuō)明
1、命令格式
#lz4 [arg] [input] [output]
2、參數(shù)說(shuō)明
| 參數(shù) | 參數(shù)說(shuō)明 |
|---|---|
| -1 | 快速壓縮(默認(rèn)) |
| -9 | 高壓縮 |
| -d | 解壓縮(默認(rèn)為.lz4擴(kuò)展名) |
| -z | 強(qiáng)制壓縮 |
| -f | 覆蓋輸出而不提示 |
| -k | 保留源文件(默認(rèn)) |
| –rm | 成功地解除/壓縮后刪除源文件 |
| -h/-H | 顯示幫助/長(zhǎng)幫助和退出 |
| -V | 顯示版本號(hào)并退出 |
| -v | 詳細(xì)模式 |
| -q | 取消警告;指定兩次也可以取消錯(cuò)誤 |
| -c | 強(qiáng)制寫(xiě)入標(biāo)準(zhǔn)輸出,即使它是控制臺(tái) |
| -t | 測(cè)試壓縮文件完整性 |
| -m | 多個(gè)輸入文件(表示自動(dòng)輸出文件名) |
| -r | 在目錄上遞歸操作(也設(shè)置為-m) |
| -l | 使用舊格式壓縮(Linux內(nèi)核壓縮) |






