常用压缩格式: .zip .gz .bz2 .tar.gz .tar.bz2
.zip格式压缩
#压缩文件
命令: zip 压缩文件名 源文件
#压缩当前目录下所有文件为test.zip
root@virtual-machine:$ zip test.zip ./*
adding: const.properties (deflated 44%)
adding: emq.conf (deflated 71%)
adding: getheadimg.jpeg (deflated 1%)
#压缩目录
命令: zip -r 压缩文件名 源目录
#压缩指定目录为test02.zip
root@virtual-machine:$ zip -r test02.zip /home/chroot/tartest/
adding: home/chroot/tartest/ (stored 0%)
adding: home/chroot/tartest/test.zip (stored 0%)
adding: home/chroot/tartest/getheadimg.jpeg (deflated 1%)
adding: home/chroot/tartest/const.properties (deflated 44%)
adding: home/chroot/tartest/emq.conf (deflated 71%)
.zip格式解压缩
命令:unzip 压缩文件
#解压缩.zip文件
root@virtual-machine:$ unzip test02.zip
Archive: test02.zip
creating: home/chroot/tartest/
inflating: home/chroot/tartest/getheadimg.jpeg
inflating: home/chroot/tartest/const.properties
inflating: home/chroot/tartest/emq.conf
.gz格式压缩
gzip 源文件
#压缩为.gz格式的压缩文件,源文件会消失
命令: gzip 源文件 > 压缩文件
root@virtual-machine:$ gzip emq.conf > emq.gz
#压缩为.gz格式,源文件保留
命令: gzip -c 源文件 > 压缩文件
root@virtual-machine:$ gzip -c const.properties > const.gz
.gz格式解压缩
命令: gzip -d 压缩文件
#解压缩文件
root@virtual-machine:$ gzip -d emq.conf.gz
命令: gunzip 压缩文件
#解压缩文件
root@virtual-machine:$ gunzip const.gz
.bz2格式压缩
命令: bzip2 源文件
#压缩为.bz2格式,不保留源文件
root@virtual-machine:$ bzip2 const.properties
#自动生成const.properties.bz2
命令: bzip2 -k 源文件
#压缩之后保留源文件
注意: bzip2命令不能压缩目录
root@virtual-machine:$ bzip2 -k const
#自动生成const.bz2
.bz2格式解压缩
命令: bzip2 -d 压缩文件
#解压缩,-k保留压缩文件
root@virtual-machine:$ bzip2 -d const.bz2
命令: bunzip2 压缩文件
#解压缩,-k 保留压缩文件
root@virtual-machine:$ bunzip2 -k emq.conf.bz2
tar格式压缩命令
命令: tar -cvf 打包文件名 源文件
选项:
-c: 打包
-v: 显示过程
-f: 指定打包后的文件名
例如:
root@virtual-machine:$ tar -cvf file.tar ./
tar格式解压缩命令
命令: tar -xvf 打包文件名
选项:
-x: 解打包
例如:
root@virtual-machine:$ tar -xvf file.tar
.tar.gz压缩格式
其实.tar.gz格式是先打包为.tar格式,再压缩为.gz格式
命令: tar -zcvf 压缩包名.tar.gz 源文件
选项:
-z: 压缩为.tar.gz格式
root@virtual-machine:$ tar -zcvf file.tar.gz ./
.tar.gz解压缩格式
命令: tar -zxvf 压缩包名.tar.gz
选项:
-x: 解压缩.tar.gz格式
root@virtual-machine:$ tar -zxvf file.tar.gz
.tar.bz2压缩格式
命令: tar -jcvf 压缩包名.tar.bz2 源文件
选项:
-z: 压缩为.tar.bz2格式
root@virtual-machine:$ tar -jcvf file.tar.bz2 ./home
.tar.bz2解压缩格式
命令: tar -jxvf 压缩包名.tar.bz2
选面:
-x: 解压缩.tar.bz2格式
root@virtual-machine:$ tar -jxvf file.tar.bz2
解压缩指定位置命令
命令: tar -jzxf 压缩文件.tar.gz -C 目录
压缩多个文件及指定压缩文件保存位置:
root@virtual-machine:$ tar -jxvf file.tar.bz2 -C /tmp/
查看压缩包内容不解压命令
命令: tar -ztvf 压缩文件.tar.gz
root@virtual-machine:$ tar -ztvf file.tar.gz