Linux命令之lz4命令如何使用

一、lz4命令簡介

lz4是一種壓縮格式,特點是壓縮/解壓縮速度超快(壓縮率不如gzip),如果你特別在意壓縮速度,或者當前環境的cpu資源緊缺,可以考慮這種格式。lz4是一種非??焖俚臒o損壓縮算法,基于字節對齊lz77系列壓縮方案。lz4提供每核400 mb/s的壓縮速度,可通過多核cpu線性擴展。它的特點是極快的解碼器,每核速度可達多gb/s,通常在多核系統上達到ram速度限制項目。lz4遵循上面說到的lz77思想理論,通過滑動窗口、hash表、數據編碼等操作實現數據壓縮。壓縮過程以至少4字節為掃描窗口查找匹配,每次移動1字節進行掃描,遇到重復的就進行壓縮。centos7默認安裝了lz4命令,可以實現lz4格式文件的壓縮和解壓縮。

二、命令使用示例

1、查看命令版本

lz4命令安裝版本是1.7.5

[root@s76 ~]# lz4 -V*** LZ4 command line interface 64-bits v1.7.5, by Yann Collet ***

2、獲取命令幫助

日常使用中如果忘記lz4命令語法格式,我們可以通過lz4 –help或者man lz4命令獲取lz4命令的幫助信息。

[root@s76 ~]# lz4 –help
[root@s76 ~]# man lz4

3、命令安裝

centos7默認安裝了lz4命令,如果沒有安裝,可以使用yum安裝方式安裝該命令。

[root@s76 ~]# yum install -y lz4 lz4-devel

4、壓縮單個文件

[root@s76 ~]# lz4 anaconda-ks.cfg test.lz4
Compressed 2927 bytes into 1825 bytes ==> 62.35%

5、壓縮多個文件

壓縮多個文件使用參數-m,壓縮后的文件名是源文件加上lz4后綴。lz4命令只可以將單個文件壓縮,如果我們需要將多個文件壓縮到一個文件,我們需要將lz4和tar命令結合使用。

[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只能壓縮文件,如果需要壓縮目錄需要結合tar命令一起。

[root@s76 ~]# tar cvf – test | lz4 – 1.tar.lz4
test/
test/1.tar
Compressed 20480 bytes into 325 bytes ==> 1.59%

Linux命令之lz4命令如何使用

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、解壓速度測試

1個22G的文件解壓花費時間5分18秒,解壓后的大小為45G。

Linux命令之lz4命令如何使用

三、lz4命令使用語法及參數說明

1、命令格式

#lz4 [arg] [input] [output]

2、參數說明

參數 參數說明
-1 快速壓縮(默認)
-9 高壓縮
-d 解壓縮(默認為.lz4擴展名)
-z 強制壓縮
-f 覆蓋輸出而不提示
-k 保留源文件(默認)
–rm 成功地解除/壓縮后刪除源文件
-h/-H 顯示幫助/長幫助和退出
-V 顯示版本號并退出
-v 詳細模式
-q 取消警告;指定兩次也可以取消錯誤
-c 強制寫入標準輸出,即使它是控制臺
-t 測試壓縮文件完整性
-m 多個輸入文件(表示自動輸出文件名)
-r 在目錄上遞歸操作(也設置為-m)
-l 使用舊格式壓縮(linux內核壓縮)

? 版權聲明
THE END
喜歡就支持一下吧
點贊11 分享