linux中xz是用于對系統文件進行壓縮和解壓縮的命令,壓縮完成后,系統會自動在原文件后加上“.xz”的擴展名并刪除原文件;xz命令只能對文件進行壓縮,不能對目錄進行壓縮。
本教程操作環境:linux5.9.8系統、Dell G3電腦。
linux中xz是什么命令?
Linux系統中xz命令用法詳解(壓縮和解壓縮)
xz命令:POSIX 平臺開發具有高壓縮率的工具。它使用 LZMA2 壓縮算法,生成的壓縮文件比 POSIX 平臺傳統使用的 gzip、bzip2 生成的壓縮文件更小,而且解壓縮速度也很快,壓縮或解壓縮xz文件。
功能說明:
xz命令會對系統文件進行壓縮和解壓縮,壓縮完成后,系統會自動在原文件后加上.xz的擴展名并刪除原文件。xz命令只能對文件進行壓縮,不能對目錄進行壓縮。
語法結構:
xz(選項)(參數) xz [OPTION]... [FILE]...
xz --help
Usage: xz [OPTION]... [FILE]... Compress or decompress FILEs in the .xz format. -z, --compress force compression -d, --decompress, --uncompress force decompression -t, --test test compressed file integrity -l, --list list information about .xz files -k, --keep keep (don't delete) input files -f, --force force overwrite of output file and (de)compress links -c, --stdout, --to-stdout write to standard output and don't delete input files -0 ... -9 compression preset; default is 6; take compressor *and* decompressor memory usage into account before using 7-9! -e, --extreme try to improve compression ratio by using more CPU time; does not affect decompressor memory requirements -T, --threads=NUM use at most NUM threads; the default is 1; set to 0 to use as many threads as there are processor cores -q, --quiet suppress warnings; specify twice to suppress errors too -v, --verbose be verbose; specify twice for even more verbose -h, --help display this short help and exit -H, --long-help display the long help (lists also the advanced options) -V, --version display the version number and exit With no FILE, or when FILE is -, read standard input. Report bugs to <lasse.collin@tukaani.org> (in English or Finnish). XZ Utils home page: <http://tukaani.org/xz/>
-z, --compress # 強制壓縮 -d, --decompress, --uncompress # force decompression -t, --test # 測試壓縮文件的完整性 -l, --list # 列出有關.xz文件的信息 -k, --keep # 保留(不要刪除)輸入文件 -f, --force # 強制覆蓋輸出文件和(解)壓縮鏈接 -c, --stdout, --to-stdout # 寫入標準輸出,不要刪除輸入文件 -0 ... -9 # 壓縮預設; 默認為6; 取壓縮機*和* # 使用7-9之前解壓縮內存使用量考慮在內! -e, --extreme # 嘗試通過使用更多的CPU時間來提高壓縮比; # 要求不影響解壓縮存儲器 -T, --threads=NUM # 最多使用NUM個線程; 默認值為1; set to 0 # 設置為0,使用與處理器內核一樣多的線程 -q, --quiet # 抑制警告; 指定兩次以抑制錯誤 -v, --verbose # 冗長; 指定兩次更詳細 -h, --help # 顯示這個簡潔的幫助并退出 -H, --long-help # 顯示更多幫助(還列出了高級選項) -V, --version # 顯示版本號并退出
壓縮一個文件 20221119test2.txt,壓縮成功后生成 20221119test2.txt.xz, 原文件會被刪除。
xz 20221119test2.txt
解壓20221119test2.txt文件,并使用參數 -k 保持原文件不被刪除。
xz -dk 20221119test2.txt.xz
使用參數 -l 顯示 .xz 文件的基本信息。基本信息包括壓縮率、數據完整性驗證方式等。也可以和參數 -v 或 -vv 配合顯示更詳盡的信息。
xz -l 20221119test2.txt.xz xz -lv 20221119test2.txt.xz
使用參數 -0, -1, -2, … -6, … -9 或參數 –fast, –best 設定壓縮率。xz 命令的默認為 -6 ,對于大多數系統來說,甚至是一些較舊的系統,-4 … -6 壓縮率預設值都不錯的表現。
使用參數 -H 顯示 xz 命令所有 options. 參數 -H 比使用參數 –help 顯示的內容更詳細。
借助 xargs 命令并行壓縮多文件。下面的命令行可以將 /var/log 目錄下所有的擴展名為 .log 的文件壓縮。通過 xargs 命令同時運行多個 xz 進行壓縮。
# 運行此命令須有 root 權限。 find /var/log -type f -iname "*.log" -print0 | xargs -P4 -n16 xz -T1
相關推薦:《Linux視頻教程》