首頁  >  文章  >  運維  >  Linux指令之lz4指令如何使用

Linux指令之lz4指令如何使用

WBOY
WBOY轉載
2023-05-11 20:49:135606瀏覽

一、lz4指令簡介

LZ4是一種壓縮格式,特點是壓縮/解壓縮速度超快(壓縮率不如gzip),如果你特別在意壓縮速度,或是當前環境的CPU資源緊缺,可以考慮這種格式。 lz4是一種非常快速的無損壓縮演算法,基於位元組對齊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 ==>lz4
Compressed 16 bytes into 35 bytes ==>lz ; 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 bytxt.lz==> ; 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、參數說明

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

以上是Linux指令之lz4指令如何使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除