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 解压