Linux文件壓縮工具備:gzip、bzip二、rar、7zip、lbzip二、xz、lrzip、PeaZip、arj等。
====================================================
1.1 zip和unzip命令html
zip: 既歸檔又壓縮的工具,能夠壓縮目錄.
zip FILE
unzip FILE
unzip -d FILE +路徑
素材準備:
[root@localhost ~]# mkdir /test
[root@localhost ~]# cd /test
[root@localhost test]# for i in {1..5};do echo "test$i" > test$i.txt;done
[root@localhost test]# ls
test1.txt test2.txt test3.txt test4.txt test5.txt
[root@localhost test]# mkdir dir1
[root@localhost test]# cp /etc/fstab dir1/
實例1: 使用zip壓縮文件test1.txt
[root@localhost test]# zip test1.zip test1.txt
adding: test1.txt (stored 0%)
[root@localhost test]# ls test1*
test1.txt test1.zip
壓縮率爲最高壓縮test2.txt
[root@localhost test]# zip -9 test2.zip test2.txt
adding: test2.txt (stored 0%)
[root@localhost test]# ls test2*
test2.txt test2.zip
實例2: 講當前目錄dir1連同目錄下文件一塊兒壓縮
[root@localhost test]# zip -r dir1.zip dir1/
adding: dir1/ (stored 0%)
adding: dir1/fstab (deflated 44%)
[root@localhost test]# ls dir1*
dir1.zip
dir1:
fstab
實例3: 向壓縮文件中test1.zip中添加test2. txt文件
[root@localhost test]# zip -m test1.zip test2.txt
adding: test2.txt (stored 0%)
實例4: 刪除壓縮文件中的文件
[root@localhost test]# zip -d test1.zip test2.txt
deleting: test2.txt
實例5: 壓縮文件時排除某個文件
[root@localhost test]# zip test.zip *.txt -x test1.txt
adding: test3.txt (stored 0%)
adding: test4.txt (stored 0%)
adding: test5.txt (stored 0%)
實例6: 解壓文件test2.zip
[root@localhost test]# unzip test2.zip
Archive: test2.zip
extracting: test2.txt
實例7:將壓縮文件text.zip在指定目錄dir1下解壓縮
[root@localhost test]# unzip test.zip -d dir1
Archive: test.zip
extracting: dir1/test3.txt
extracting: dir1/test4.txt
extracting: dir1/test5.txt
實例8: 查看壓縮文件目錄,但不解壓
[root@localhost test]# unzip -v test.zip
Archive: test.zip
Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----
6 Stored 6 0% 06-17-2017 17:53 4e6f5599 test3.txt
6 Stored 6 0% 06-17-2017 17:53 012ec35e test4.txt
6 Stored 6 0% 06-17-2017 17:53 1835f21f test5.txt
-------- ------- --- -------
18 18 0% 3 files
linux
實例1: 使用gzip壓縮文件
[root@localhost test]# gzip test1.txt
[root@localhost test]# ls test1*
test1.txt.gz test1.zip
實例2: 使用gzip壓縮目錄下文件
[root@localhost test]# gzip -r dir1/
[root@localhost test]# ls dir1
fstab.gz test3.txt.gz test4.txt.gz test5.txt.gz
#注意: 以上壓縮以後原始文件就沒有了.
實例3: 壓縮但保留原始文件
[root@localhost test]# gzip -c test2.txt > test2.txt.gz
[root@localhost test]# ls test2*
test2.txt test2.txt.gz test2.zipless
實例1:解壓文件至原路徑
[root@localhost test]# gunzip test1.txt.gz
[root@localhost test]# ls test1*
test1.txt test1.zip
實例2:解壓至指定路徑
[root@localhost test]# gunzip -c test2.txt.gz > /tmp/test.txt
[root@localhost test]# cat /tmp/test.txt
test2
實例3:解壓目錄下的壓縮文件
[root@localhost test]# gunzip -r dir1
[root@localhost test]# ls dir1
fstab test3.txt test4.txt test5.txt
#注意: gunzip和 gzip -d等價。工具
bzip二、bunzip2是更新的Linux壓縮工具,比gzip有着更高的壓縮率。測試
bzip二、bunzip2示例以下:
# bzip2 -z man.config //將man.config以bzip2壓縮,此時man.config變成man.config.bz2
# bzip2 -9 -c man.config > man.config.bz2 //將man.config用最佳的壓縮比壓縮,並保留本來的檔案
# bzip2 -d man.config.bz2 //將man.config.bz2解壓縮,可用bunzip2取代bzip2 -d
# bunzip2 man.config.bz2 //將man.config.bz2解壓縮ui
實例1:壓縮文件
[root@localhost test]# xz test1.txt
[root@localhost test]# ls test1.txt.xz
test1.txt.xz
實例2:壓縮dir1目錄下文件
[root@localhost test]# xz dir1/*
[root@localhost test]# ls dir1
fstab.xz test3.txt.xz test4.txt.xz test5.txt.xz
實例3:查看壓縮文件內容
[root@localhost test]# xzcat test1.txt.xz
test1
實例4:解壓縮
[root@localhost test]# unxz test1.txt.xz
實例5:解壓縮目錄dir1下文件
[root@localhost test]# xz -d dir1/*
[root@localhost test]# ls dir1
fstab test3.txt test4.txt test5.txt
#注意:xz -d等價於unxzlua
不解壓,顯示壓縮文件的內容:zcat、zless、bzcat、bzless。
1.zcat、zless
[root@localhost test]# zcat test2.txt.gz
test2
2.bzcat、bzless
例如:
# bzcat man.config.bz2 //在屏幕上顯示man.config.bz2解壓縮以後的內容spa
例1:# tar -cf exam.tar exam1*.txt.net
(把全部exam1*.txt的文件打包成一個exam.tar文件。其中,-c是產生新備份文件;-f是輸出到默認的設備,能夠把它當作必定要加的選項)
例2:# tar -rf exam.tar exam2*.txthtm
(exam.tar是一個已經存在的打包文件了,再把exam2*.txt的全部文件也打包進去。-r是再增長文件的意思)
例3:# tar -uf exam.tar exam11.txt
(剛纔exam1*.txt已經打包進去了,可是其中的exam11.txt後來又作了更改,把新改過的文件再從新打包進去,-u是更新的意思)
例4:# tar -tf exam.tar
(列出exam.tar中有哪些文件被打包在裏面。-t是列出的意思)
例5:# tar -xf exam.tar
(把exam.tar打包文件中所有文件釋放出來,-x是釋放的意思)
例6:# tar -xf exam.tar exam2*.txt
(只把exam.tar打包文件中的全部exam2*.txt文件釋放出來,-x是釋放的意思)
打包的時候調用壓縮,z調用gzip壓縮,j調用bzip2壓縮,J調用xz壓縮
例7:# tar -zcf exam.tar.gz exam1*.txt
例8:解壓解包
1:# tar -xzvf exam.tar.gz
//加一個選項-v,就是顯示打包兼壓縮或者解壓的過程。由於Linux上最多見的軟件包文件是.tar.gz文件,所以,最常看到的解壓方式就是這樣了
2:# tar -xzvf exam.tgz
//.tgz文件名也是同樣的,因性質同樣,僅文件名簡單一點而已
3:# tar xzvf exam.tar.gz -C exam/
// 解壓到exam目錄中
4:# tar xjvf exam.tar.bz2 -C exam/
// j:使用bzip2
例9:打包壓縮
1:# tar cjvf test.tar.bz2 exam1*.txt
2:# tar czvf test.tar.gz exam1*.txt
# tar -czvf exam.tar.gz *.* 或 # tar -czvf exam.tgz *.*
# tar cjvf exam.tar.bz2 exam1*.txt
# tar xjvf exam.tar.bz -C exam/ // j:使用bzip2
實例10:打包壓縮後刪除源文件
tar -czf mess1.tar.gz test01 --remove-files
#注意: 在不知道使用什麼壓縮工具的時候能夠使用tar xf 進行解壓。
示例以下:
# find ./home -print |cpio -ov > home.cpio //將home目錄備份
# cpio -idv < /root/home.cpio //要恢復文件的時候
# cpio -tv < home.cpio //查看home.cpio文件
# find . -depth | cpio -ocvB > backup.cpio
將當前目錄下名爲inittab文件加入initrd.cpio包中
# find . -name inittab -depth | cpio -ovcB -A -F initrd.cpio
# find . -name inittab -depth | cpio -ovcB -A -O initrd.cpio
# find . -name inittab -depth | cpio -ovcB -A --quiet -O initrd.cpio(--quit:表示不顯示覆制塊)
從cpio壓縮包中的解壓出文件,示例以下:
# cpio --absolute-filenames -icvu < test.cpio //解壓到原始位置,解壓出來的每一個文件的時間屬性改成當前時間
# cpio --absolute-filenames -icvum < test.cpio //解壓到原始位置,同時不改變解壓出來的每一個文件的時間屬性
# cpio -icvu < test.cpio //解壓到當前目錄下
# cpio -icvum < test.cpio //解壓到當前目錄下
# cpio -icvdu -r < grub.cpio //在解包cpio時,對解包出來的文件件進行交互的改名
# cpio -icvu --to-stdout < grub.cpio //將cpio包中的文件解壓並輸入標準輸出,注意:既然解壓到標準輸出,因此就不能使用-d參數了
# cpio --absolute-filenames -vtc < boot.cpio //不忽略文件列表清單的文件名最前面的「/"
# cpio --no-absolute-filenames -vtc < boot.cpio //默認是忽略文件列表清單的文件中最前面的
.rar是在Windows中最多見的壓縮文件格式,在Linux中如需使用須要安裝rarlinux,官方:
http://www.rarsoft.com/download.htm
wget http://www.rarsoft.com/rar/rarlinux-x64-5.4.0.tar.gz
tar xf rarlinux-x64-5.4.0.tar.gz
cd rar
make && make install
RHEL7 下也能夠yum安裝,方法以下:
yum install ftp://rpmfind.net/linux/dag/redhat/el7/en/x86_64/dag/RPMS/rar-3.8.0-1.el7.rf.x86_64.rpm
或者使用RPM安裝UNRAR,下載地址: http://pkgs.repoforge.org/unrar/
安裝完成後便可使用rar和unrar命令。簡單用法:
實例1:壓縮文件
[root@localhost test]# rar a test test*.txt
RAR 3.80 Copyright (c) 1993-2008 Alexander Roshal 16 Sep 2008
Shareware version Type RAR -? for help
Evaluation copy. Please register.
Creating archive test.rar
Adding test1.txt OK
Adding test2.txt OK
Adding test3.txt OK
Adding test4.txt OK
Adding test5.txt OK
Done
rar a test test*.txt #該命令是將全部以test開頭,後綴爲txt的文件壓縮成一個rar包,名爲test,該程序會將.rar 擴展名將自動附加到包名後。
實例2:解壓文件
[root@localhost test]# rar e test.rar
RAR 3.80 Copyright (c) 1993-2008 Alexander Roshal 16 Sep 2008
Shareware version Type RAR -? for help
Extracting from test.rar
Extracting test1.txt OK
Extracting test2.txt OK
Extracting test3.txt OK
Extracting test4.txt OK
Extracting test5.txt OK
All OK
或者使用unrar
yum install ftp://rpmfind.net/linux/rpmfusion/nonfree/el/updates/7/x86_64/u/unrar-5.4.5-1.el7.x86_64.rpm
unrar e test.rar #該命令是將rar中的全部文件解壓出來。
$ rar --help
用法: rar <命令> -<選項 1> -<選項 N> <壓縮檔案> <文件...>
<@文件列表...> <解壓縮路徑\>
<命令>
a 添加文件到壓縮檔案
c 添加壓縮檔案註釋(註釋的大小最大不能超過62000字節)
cf 添加文件備註( 當使用'v'參數時顯示文件備註,最大不能超過32767字節)
注意:rar的<命令>部分不帶」-「號,只有<選項>部分帶」-「號,這一點注意一下。
例1:添加文件或目錄到壓縮檔案中,使用a命令。例如把文件files1添加到abc.rar中,使用a或m命令,a命令把file1文件添加到abc.rar檔案中保持原有的file1文件不變,m命令移動file1文件到file1.rar檔案中(壓縮完成後會刪除原有的file1文件,注意:m命令只針對文件進行操做)
rar a abc.rarfile1
說明:若是此時abc.rar檔案不存在,會自行建立abc.rar文檔案,若是abc.rar檔案已經存在,則將file1文件壓縮進abc.rar檔案中,若abc.rar檔案中已存在一個file1文件,則會更新file1文件。而且原有的file1文件依然存在於當前目錄下,若要將file1文件移動到file1.rar中請使用m命令,對於目錄一樣能夠進行此操做。
注意:若是隻輸入」rar a file1.rar」命令,而沒有接文件名或目錄名的話,會將當前目錄下全部的文件和文件夾壓縮進file1.rar文檔中。這點要注意一下。
例2:解壓縮abc.rar檔案中的內容,能夠使用e或x命令,假設abc.rar目錄中有一個名爲file1的文件和一個名爲test的目錄,test目錄中有一個名爲file2的文件,
rar e abc.rar
說明:使用e命令,會將abc.rar中的file1文件連同test目錄下的file2文件解壓到當前目錄。若是想保持abc.rar目錄中的目錄結構請使用x命令。
rar x abc.rar
說明:此時會將file1文件和test目錄解壓到當前文件夾。
例3:爲整個壓縮文件添加註釋
rar c abc.rar
說明:輸入該命令後,屏幕最下方會顯示
Processingarchivetest.
rarReadingcommentfromstdin
並有一個光標在閃爍,從光標閃爍的位置輸入註釋信息,輸入完了按下Ctrl+D結束輸入
例4:爲壓縮文件中的單個文件添加註釋,使用cf命令。假如如今要爲abc.rar檔案中的file1文件添加註釋
rar ch abc.rar file1
此時屏幕最下方會顯示
Readingcommentfor abc.rar : file1fromstdin
從光標閃爍出輸入想要爲file1添加的註釋,Ctrl+D結束輸入
例5:把整個檔案的註釋寫入到一個文件中,使用cw命令。加入要將abc.rar的註釋寫入到test.txt文件中
rar cw abc.rar test.txt
說明:若是當前目錄下不存在名爲test.txt的文件,則會自行建立一個名爲test.txt的文件,並將abc.rar的註釋寫入到text.txt文件中,若是當前目錄中已經存在一個名爲text.txt的文件,則會提示你是否覆蓋已經存在的文件,若是選擇Yes或者All,則會清空test.txt中原有的內容,而後將abc.rar的註釋內容寫入到text.txt文件中。
例6:刪除壓縮檔案中的文件或目錄,使用d命令。例如要刪除abc.rar檔案中的file1文件。
rar d abc.rar file1
說明:該命令刪除abc.rar檔案中的file1文件,對於目錄一樣有效。
解壓縮命令unrar的使用:
$unrar --help
用法: unrar <command> -<switch 1> -<switch N> <archive> <files...>
<@listfiles...> <path_to_extract\>
<命令>
e 解壓文件到當前目錄
l[t,b] 列出壓縮文檔信息[technical, bare]
p 打印文件到標準輸出
t 測試壓縮我俄當
v[t,b] 列出壓縮文檔的詳細信息[technical,bare]
x 解壓文件到完整路徑
如下爲<選項>部分,略去。
unrar的命令和rar具備一樣的效果,能夠看出unrar只包含了rar的一部分命令而已,所以使用rar就能夠完成全部操做。
Linux要解壓縮.7z文件,首先須要安裝7zip軟件。 yum install p7zip 使用7zip的命令是7za。 安裝完成後的使用方法: 7za {a|d|l|e|u|x} 壓縮包文件名 {文件列表或目錄,可選} a 向壓縮包裏添加文件或建立壓縮包,如向7z添加001.jpg,執行:7za a 001.7z 001.jpg;將001目錄打包執行:7za a 001.7z 001; d 從壓縮裏刪除文件,如將7z裏的001.jpg刪除,執行:7za d 001.7z 001.jpg l 列出壓縮包裏的文件,如列出7z裏的文件,執行:7za l 001.7z e 解壓到當前目錄,目錄結構會被破壞,如rar內有以下目錄及文件123/456/789.html,執行:7za e 001.rar,目錄123和456及文件html都會存放在當前目錄下。 x 以完整路徑解壓。 實例: → 7za a test.7z dir1/test*.txt #打包目錄dir下test開頭後綴爲txt的文件 → 7za x test.7z -o/tmp #解壓,-o表示輸出目錄,注意其與目錄路徑之間沒有空格 → # ls /tmp/dir1/ test3.txt test4.txt test5.txt