Linux中的壓縮和解壓縮--tar
阿新 • • 發佈:2018-12-14
引數解釋
tar命令:用途比較多,可以用於打包,壓縮和解壓縮等
打包和壓縮的區別
打包:將一堆檔案或目錄變成一個檔案
壓縮:將一個大檔案,通過壓縮演算法變成一個小檔案
Linux中的很多壓縮演算法只能對一個檔案進行壓縮,所以我們要壓縮的話,必須先進行打包
常用的壓縮演算法
gzip、bzip2、xz等
以下五個選項不能連用
-c:建立一個壓縮檔案
-x:解開一個壓縮檔案
-t:檢視壓縮檔案中的內容
-r:在一個歸檔的檔案的末尾新增一個檔案
-u:更新源壓縮包中的檔案
以下三個選項為常用的壓縮方式
-z:使用gzip壓縮
-j:使用bzip2壓縮
-J:使用xz壓縮
以下一個選項為必須引數
-f:該選項必須寫在最後,後面接檔名
常用選項:
-v:顯示壓縮或解壓縮的過程
配置舉例
壓縮
[[email protected] tmp]# tar -cvf boot.tar /boot
把/boot目錄打包為boot.tar。打包後的檔案,我們習慣使用.tar
[[email protected] tmp]# tar -zcvf boot.tar.gz /boot
把/boot目錄打包並且壓縮為gzip格式,命名為boot.tar.gz,gzip壓縮的檔案,我們習慣加一個gz
[[email protected] tmp]# tar -jcvf boot.tar.bz2 /boot
把/boot目錄打包並且壓縮為bzip2格式,命名為boot.tar.bz2,bzip2壓縮的檔案,我們習慣加一個bz2
檢視壓縮檔案的內容
[[email protected] tmp]#tar -tf boot.tar.bz2
解壓檔案
直接解壓
[[email protected] tmp]# tar xvf boot.tar //直接解壓tar包
[[email protected] tmp]# tar xzvf boot.tar.gz //解壓gzip2壓縮
要解壓到特定的目錄,需要cd切換到該目錄下,不能後面直接根目錄
[[email protected] tmp]# cd /tmp/Maidao/
[[email protected] Maidao]# tar xjvf /tmp/boot.tar.bz2
只想解壓/boot/grub2/grub.cfg檔案
[[email protected] Maidao]# tar xzvf /tmp/boot.tar.gz boot/grub2/grub.cfg
注意:boot/grub2/grub.cfg內的根目錄 / 是被拿掉的
在打包壓縮的時候,排除某一個檔案
[[email protected] tmp]# tar --exclude /boot/grub/splash.xpm.gz -zcvf boot2.tar.gz /boot/
PS:其實在Linux中,字尾名沒有什麼意義,只是便於人們檢視
補充:Windows的zip和rar格式也適用於Linux
zip和rar命令可以對檔案或者目錄進行zip或rar壓縮
unzip和unrar命令可以對檔案或者目錄進行zip或rar解壓縮
配置舉例:
zip壓縮一個檔案和目錄,同時測試解壓縮
1、是做zip壓縮一個檔案,把file1壓縮為filetest
[[email protected] tmp]# zip filetest file1
adding: file1 (stored 0%)
[[email protected] tmp]# ll
total 4
-rw-r--r--. 1 root root 0 Dec 7 22:44 file1
-rw-r--r--. 1 root root 160 Dec 7 22:44 filetest.zip
2、對/boot目錄進行壓縮,對目錄進行壓縮 -r 選項
把/boot目錄壓縮為boottest
[[email protected] tmp]# zip -r boottest /boot
3、解壓boottest
[[email protected] tmp]# unzip boottest.zip