在使用Linux的过程中,我们往往需要下载一些文件压缩包,怎么样对不同的压缩包减压以及压缩呢?可以使用下面的命令实现这样的操作:
.gz文件
- 压缩(gzip): 例如压缩文件services:
[root@localhost tmp]# gzip services #压缩文件services
[root@localhost tmp]# ls
akonadi-alex.8wwv3R services.gz #得到压缩包services.gz
- 减压(gunzip或gzip -d):例如减压文件services.gz:
[root@localhost tmp]# gunzip services.gz #减压压缩包services.gz
[root@localhost tmp]# ls
akonadi-alex.8wwv3R services #减压后得到的文件
注意:gzip压缩后保留原文件
gunzip减压后保留原压缩包
gzip能压缩目录
tar 命令
- -c 打包
- -x 解包
- -v 显示详细信息
- -f 指定文件名
- -z 先打包后压缩
例如:将目录testzip打包并重命名为testzip.tar:
[root@localhost tmp]# tar -cvf testzip.tar testzip
testzip/
testzip/services #显示的详细信息
这时只是将目录testzip打包为testzip.tar并没有压缩,此时我们就可以使用gzip来对testzip.tar进行压缩:
[root@localhost tmp]# tar -cvf testzip.tar testzip
testzip.tar.gz #压缩结果
如何将文件或目录打包并同时压缩呢?可以使用-z来实现:
[root@localhost tmp]# tar -zcvf testzip.tar.gz testzip #打包并压缩
将testtar.tar 减包:
[root@localhost tmp]# tar -xvf testtar.tar
将testzip.tar.gz 减压:
[root@localhost tmp]# tar -zxvf testzip.tar.gz
注意:tar操作会保留原文件
.zip文件
- 压缩(zip [-r(压缩目录所需参数)]):将services压缩为servicestestzip:
[root@localhost tmp]# zip -r servicestestzip services #将文件services压缩为servicestestzip
adding: services (deflated 80%) #压缩率为80%
[root@localhost tmp]# ls
akonadi-alex.8wwv3R servicestestzip.zip #压缩结果
- 减压:将文件servicestestzip减压:
[root@localhost tmp]# unzip servicestestzip.zip
- 注意:zip无论减压还是压缩都会保留原文件
.bzip2文件
- 压缩:将文件services压缩:
[root@localhost tmp]# bzip2 -k services #bzip2只能压缩文件 -k表示压缩后保留源文件
- 减压:将文件services减压:
[root@localhost tmp]# bunzip2 services.bz2
因为bzip2不能压缩目录,所以对于目录,它的用法与gzip相似,唯一不同的是它使用参数-j,减压压缩如下:
[root@localhost tmp]# tar -cvf testbzip2.tar.bz2 testbzip2
#将目录testbzip2打包并压缩为testbzip2.tar.bz2压缩包
[root@localhost testbzipw]# tar -xjvf testbzip2123.tar.bz2
#将压缩包testbzip2123.tar.bz2减压
本人原博客地址:http://120.27.248.71/index.php/2019/08/08/linux_xia_ya_suo_yu_jian_ya_ming_ling/#more-184