Linux命令之lz4命令使用示例
一、lz4命令簡(jiǎn)介
LZ4是一種壓縮格式,特點(diǎn)是壓縮/解壓縮速度超快(壓縮率不如gzip),如果你特別在意壓縮速度,或者當(dāng)前環(huán)境的CPU資源緊缺,可以考慮這種格式。lz4是一種非??焖俚臒o損壓縮算法,基于字節(jié)對(duì)齊LZ77系列壓縮方案。lz4提供每核400 MB/s的壓縮速度,可通過多核CPU線性擴(kuò)展。它的特點(diǎn)是極快的解碼器,每核速度可達(dá)多GB/s,通常在多核系統(tǒng)上達(dá)到RAM速度限制項(xiàng)目。lz4遵循上面說到的lz77思想理論,通過滑動(dòng)窗口、hash表、數(shù)據(jù)編碼等操作實(shí)現(xiàn)數(shù)據(jù)壓縮。壓縮過程以至少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命令語法格式,我們可以通過lz4 --help或者man lz4命令獲取lz4命令的幫助信息。
[root@s76 ~]# lz4 --help
[root@s76 ~]# man lz4
3、命令安裝
centos7默認(rèn)安裝了lz4命令,如果沒有安裝,可以使用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命令使用語法及參數(shù)說明
1、命令格式
#lz4 [arg] [input] [output]
2、參數(shù)說明
| 參數(shù) | 參數(shù)說明 |
|---|---|
| -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)制寫入標(biāo)準(zhǔn)輸出,即使它是控制臺(tái) |
| -t | 測(cè)試壓縮文件完整性 |
| -m | 多個(gè)輸入文件(表示自動(dòng)輸出文件名) |
| -r | 在目錄上遞歸操作(也設(shè)置為-m) |
| -l | 使用舊格式壓縮(Linux內(nèi)核壓縮) |
到此這篇關(guān)于Linux命令之lz4命令的文章就介紹到這了,更多相關(guān)Linux lz4命令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文學(xué)會(huì)使用Linux內(nèi)核模塊&proc實(shí)例統(tǒng)計(jì)所有進(jìn)程信息
這篇文章主要介紹了使用Linux內(nèi)核模塊&proc實(shí)例統(tǒng)計(jì)所有進(jìn)程信息詳解,2023-05-05
Shell 實(shí)現(xiàn)多任務(wù)并發(fā)的示例代碼
本文主要介紹了Shell 實(shí)現(xiàn)多任務(wù)并發(fā)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
shell腳本實(shí)現(xiàn)linux系統(tǒng)文件完整性檢測(cè)
這篇文章主要介紹了shell腳本實(shí)現(xiàn)linux系統(tǒng)文件完整性檢測(cè),本文給出的腳本使用對(duì)比MD5的方法,檢測(cè)linux系統(tǒng)文件是否被替換等問題,需要的朋友可以參考下2014-12-12
淺析使用?Auditbeat?模塊監(jiān)控?shell?命令的問題
Auditbeat Audited 模塊可以用來監(jiān)控所有用戶在系統(tǒng)上執(zhí)行的 shell 命令,在終端用戶偶爾才會(huì)登錄的服務(wù)器上,通常需要進(jìn)行監(jiān)控,本文給大家介紹使用?Auditbeat?模塊監(jiān)控?shell?命令的相關(guān)知識(shí),感興趣的朋友一起看看吧2022-02-02

