Linux下最常用的打包程序就是tar了,使用tar程序打出來的包我們常稱爲tar包,tar包文件的命令通常都是以.tar結尾的。生成tar包後,就可以用其它的程序來進行壓縮了(tar包未被壓縮)。
tar
- 命令選項:
-c(create) 創建新的檔案文件
-f(file) 使用檔案文件或設備,這個選項通常是必選的。
-j(bzip2) 用bzip2來壓縮/解壓縮文件
-v(verbose) 詳細報告tar處理的文件信息
-x(extract) 解壓縮文件或目錄
-z(gzip) 用gzip來壓縮/解壓縮文件
- 舉例說明
# 加參數-p保持原有權限 #壓縮 [root@localhost tmp]# tar -zvcf helloword.tar.gz helloword* [root@localhost tmp]# tar -pjvcf helloword.tar.bz2 helloword* #解壓 [root@localhost tmp]# tar -zvxf helloword.tar.gz [root@localhost tmp]# tar -pjvxf helloword.tar.bz2
解/壓縮縮命令:
gzip
-
壓縮後的格式爲:*.gz
-
這種壓縮方式不能保存原文件;且不能壓縮目錄
- 命令舉例:
#壓縮 [root@localhost tmp]# gzip helloword [root@localhost tmp]# ls helloword.gz #解壓 [root@localhost tmp]# gunzip helloword.gz [root@localhost tmp]# ls helloword
zip
-
與gzip相比:1)可以壓縮目錄; 2)可以保留原文件;
-
選項:
-r(recursive) 遞歸壓縮目錄內的所有文件和目錄
#壓縮和解壓文件
[root@localhost tmp]# zip helloword.zip helloword
[root@localhost tmp]# unzip helloword.zip
#壓縮和解壓目錄
[root@localhost tmp]# zip -r helloword.zip helloword
adding: helloword/ (stored 0%)
adding: helloword/Test2/ (stored 0%)
adding: helloword/Test1/ (stored 0%)
adding: helloword/Test1/test4 (stored 0%)
adding: helloword/test3 (stored 0%)
[root@localhost tmp]# unzip helloword.zip
Archive: helloword.zip
creating: helloword/
creating: helloword/Test2/
creating: helloword/Test1/
extracting: helloword/Test1/test4
extracting: helloword/test3
bzip2
- 壓縮後的格式:.bz2
- 參數
-k 產生壓縮文件後保留原文件
#壓縮
[root@localhost tmp]# bzip2 helloword
[root@localhost tmp]# bzip2 -k helloword
#解壓
[root@localhost tmp]# bunzip2 helloword.bz2
常用命令舉例:
壓縮:
#將目錄裏所有jpg文件打包成tar.jpg
tar –cvf jpg.tar *.jpg
#將目錄裏所有jpg文件打包成jpg.tar後,並用gzip壓縮,生成一個gzip壓縮包,命名爲jpg.tar.gz
tar –czf jpg.tar.gz *.jpg
#將目錄裏所有jpg文件打包成jpg.tar後,並用bzip2壓縮,生成一個bzip2壓縮包,命名爲jpg.tar.bz2
tar –cjf jpg.tar.bz2 *.jpg
#將目錄裏所有jpg文件打包成jpg.tar後,並用compress壓縮,生成一個umcompress壓縮包,命名爲jpg.tar.Z
tar –cZf jpg.tar.Z *.jpg
#rar格式的壓縮,需要先下載rar for linux
rar a jpg.rar *.jpg
#zip格式的壓縮,需要先下載zip for linux
zip jpg.zip *.jpg
解壓:
#解壓 tar包
tar –xvf file.tar
#解壓 tar.gz
tar -xzvf file.tar.gz
#解壓 tar.bz2
tar -xjvf file.tar.bz2
#解壓 tar.Z
tar –xZvf file.tar.Z
#解壓 rar
unrar e file.rar
#解壓 zip
unzip file.zip
總結
1、*.tar 用 tar –xvf 解壓
2、*.gz 用 gzip -d或者gunzip 解壓
3、*.tar.gz和*.tgz 用 tar –xzf 解壓
4、*.bz2 用 bzip2 -d或者用bunzip2 解壓
5、*.tar.bz2用tar –xjf 解壓
6、*.Z 用 uncompress 解壓
7、*.tar.Z 用tar –xZf 解壓
8、*.rar 用 unrar e解壓
9、*.zip 用 unzip 解壓