Centos之压缩和解压缩命令

常用压缩格式:.zip .gz .bz2

常用压缩格式:.tar.gz  .tar.bz2

1、zip格式压缩



zip压缩文件名 源文件


压缩文件


zip -r 压缩文件名 源目录


压缩目录


[root@localhost ~]# zip 牛牛.zip 牛牛


-bash: zip: 未找到命令


我们压缩 牛牛 


报错 zip 未找到命令


我们用yum命令安装下


yum -y install zip


[root@localhost ~]# zip 牛牛.zip 牛牛


  adding: 牛牛 (stored 0%)


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  牛牛.zip


[root@localhost ~]# 


成功用zip命令压缩了牛牛文件


[root@localhost ~]# mkdir 书籍


[root@localhost ~]# touch 书籍/java.pdf


[root@localhost ~]# touch 书籍/php.pdf


[root@localhost ~]# touch 书籍/asp.pdf


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  牛牛.zip  书籍


[root@localhost ~]# ls /root/书籍/


asp.pdf  java.pdf  php.pdf


[root@localhost ~]# 


我们新建了一个书籍目录 然后目录下放了几个文件


[root@localhost ~]# zip -r 书籍.zip 书籍


  adding: 书籍/ (stored 0%)


  adding: 书籍/java.pdf (stored 0%)


  adding: 书籍/php.pdf (stored 0%)


  adding: 书籍/asp.pdf (stored 0%)


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  牛牛.zip  书籍  书籍.zip


[root@localhost ~]# 


压缩目录


2、.zip解压缩


unzip 压缩文件



解压缩.zip文件


我们先删除原文件


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  牛牛.zip  书籍  书籍.zip


[root@localhost ~]# rm -rf 牛牛


[root@localhost ~]# rm -rf 书籍


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛.zip  书籍.zip


[root@localhost ~]# 


[root@localhost ~]# unzip 牛牛.zip


-bash: unzip: 未找到命令


发现unzip没安装


安装下 


yum install -y unzip



[root@localhost ~]# unzip 牛牛.zip


Archive:  牛牛.zip


 extracting: 牛牛                  


[root@localhost ~]# unzip 书籍.zip


Archive:  书籍.zip


   creating: 书籍/


 extracting: 书籍/java.pdf         


 extracting: 书籍/php.pdf          


 extracting: 书籍/asp.pdf          


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  牛牛.zip  书籍  书籍.zip


[root@localhost ~]# 


解压成功


3、.gz格式压缩



gzip 源文件


压缩为.gz格式的压缩文件,源文件会消失


gzip -c 源文件 > 压缩文件


压缩为.gz格式,源文件保留


例如:gzip -c 书籍 > 书籍.gz


gzip -r 目录



压缩目录下所有的子文件,但是不能压缩目录



[root@localhost ~]# rm -rf *.zip


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  书籍


[root@localhost ~]# 


先删除zip


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  书籍


[root@localhost ~]# gzip 牛牛


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛.gz  书籍


[root@localhost ~]# 


压缩文件


[root@localhost ~]# gzip -r 书籍


[root@localhost ~]# ls


aaa  anaconda-ks.cfg  牛牛.gz  书籍


[root@localhost ~]# ls 书籍/


asp.pdf.gz  java.pdf.gz  php.pdf.gz


[root@localhost ~]# 


压缩目录


4、.gz格式解压缩



gzip -d 压缩文件


解压缩文件


gunzip 压缩文件


解压缩文件


[root@localhost ~]# gzip -d 牛牛.gz


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  书籍


[root@localhost ~]# 


用gunzip 牛牛.gz 效果一样


[root@localhost ~]# gunzip -r 书籍


[root@localhost ~]# ls 


anaconda-ks.cfg  牛牛  书籍


[root@localhost ~]# ls 书籍/


asp.pdf  java.pdf  php.pdf


[root@localhost ~]# 


解压目录


5、.bz2格式压缩



bzip2 源文件


压缩为.bz2格式,不保留源文件


bzip2 -k 源文件


压缩之后保留源文件


注意:bzip2命令不能压缩目录

[root@localhost ~]# bzip2 牛牛


-bash: bzip2: 未找到命令


[root@localhost ~]# yum -y install bzip2


命令不存在,我们就安装下;


[root@localhost ~]# bzip2 牛牛


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛.bz2  书籍


[root@localhost ~]# 


源文件没了,假如要保留源文件 bzip2 -k 牛牛

.bz2格式解压缩


bzip2 -d 压缩文件


#解压缩,-k保留压缩文件


bunzip2 压缩文件


#解压缩,-k 保留压缩文件


[root@localhost ~]# bzip2 -d 牛牛.bz2


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  书籍


[root@localhost ~]# 



6、打包命令tar



tar -cvf 打包文件名 源文件


选项:


-c :打包


-v :显示过程


-f :指定打包后的文件名


例如


tar -cvf 牛牛.tar 牛牛


[root@localhost ~]# 


[root@localhost ~]# tar -cvf 书籍.tar 书籍


书籍/


书籍/java.pdf


书籍/php.pdf


书籍/asp.pdf


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  书籍  书籍.tar


[root@localhost ~]# 


打包书籍目录


[root@localhost ~]# gzip 书籍.tar 


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  书籍  书籍.tar.gz


[root@localhost ~]# 


把书籍.tar压缩gz


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  书籍  书籍.tar.gz


[root@localhost ~]# gzip -d 书籍.tar.gz


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  书籍  书籍.tar


[root@localhost ~]# bzip2 书籍.tar


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  书籍  书籍.tar.bz2


[root@localhost ~]# 


把书籍.tar压缩成bzip2


7、解打包命令



tar -xvf 打包文件名


选项:


-x : 解打包


例如:


tar -xvf 书籍.tar


[root@localhost ~]# bzip2 -d 书籍.tar.bz2


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  书籍  书籍.tar


[root@localhost ~]# tar -xvf 书籍.tar


书籍/


书籍/java.pdf


书籍/php.pdf


书籍/asp.pdf


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  书籍  书籍.tar


[root@localhost ~]# ls 书籍/


asp.pdf  java.pdf  php.pdf


[root@localhost ~]# 


8、.tar.gz压缩格式



其实.tar.gz格式是先打包为.tar格式,再压缩为.gz格式


tar -zcvf 压缩名.tar.gz 源文件


选项:


-z :压缩为.tar.gz格式


tar -zxvf 压缩包名.tar.gz


选项:


-x:解压缩.tar.gz格式


.tar.bz2压缩格式


其实.tar.bz2格式是先打包为.tar格式,再压缩为.bz2格式


tar -jcvf 压缩名.tar.bz2 源文件


选项:


-j :压缩为.tar.bz2格式


tar -zxvf 压缩包名.tar.bz2


选项:


-x:解压缩.tar.bz2格式


[root@localhost ~]# rm -rf 书籍.tar


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  书籍


[root@localhost ~]# tar -zcvf 书籍.tar.gz 书籍


书籍/


书籍/java.pdf


书籍/php.pdf


书籍/asp.pdf


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  书籍  书籍.tar.gz


[root@localhost ~]# 


9、压缩tar.gz



[root@localhost ~]# rm -rf 书籍


[root@localhost ~]# tar -zxvf 书籍.tar.gz


书籍/


书籍/java.pdf


书籍/php.pdf


书籍/asp.pdf


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  书籍  书籍.tar.gz


[root@localhost ~]# 


10、解压缩tar.gz



[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  书籍  书籍.tar.gz


[root@localhost ~]# tar -jcvf 书籍.tar.bz2 书籍


书籍/


书籍/java.pdf


书籍/php.pdf


书籍/asp.pdf


[root@localhost ~]# ls


anaconda-ks.cfg  牛牛  书籍  书籍.tar.bz2  书籍.tar.gz


[root@localhost ~]# 


压缩tar.bz2


[root@localhost ~]# tar -jxvf 书籍.tar.bz2 -C /tmp/


书籍/


书籍/java.pdf


书籍/php.pdf


书籍/asp.pdf


[root@localhost ~]# ls /tmp/


ks-script-NSb_Xk


systemd-private-7113799c2057477ab6ee497261ae7a13-vmtoolsd.service-pskbnQ


systemd-private-b37fa00ba37a4d119a61ec71ea2c7116-vmtoolsd.service-t7gach


systemd-private-dec75be7d6a64814b5ba654e693bd541-vmtoolsd.service-xituVu


yum.log


书籍


[root@localhost ~]# ls /tmp/书籍/


asp.pdf  java.pdf  php.pdf


[root@localhost ~]# 


解压缩到其他目录


压缩多文件到指定目录


[root@localhost ~]# tar -zcvf /tmp/test.tar.gz 书籍 anaconda-ks.cfg 


书籍/


书籍/java.pdf


书籍/php.pdf


书籍/asp.pdf


anaconda-ks.cfg


[root@localhost ~]# ls /tmp/


ks-script-NSb_Xk


systemd-private-7113799c2057477ab6ee497261ae7a13-vmtoolsd.service-pskbnQ


systemd-private-b37fa00ba37a4d119a61ec71ea2c7116-vmtoolsd.service-t7gach


systemd-private-dec75be7d6a64814b5ba654e693bd541-vmtoolsd.service-xituVu


test.tar.gz


yum.log


书籍


[root@localhost ~]# 


查看压缩包(不解压)


[root@localhost ~]# cd /tmp/


[root@localhost tmp]# tar -ztvf test.tar.gz


drwxr-xr-x root/root         0 2017-06-22 14:53 书籍/


-rw-r--r-- root/root         0 2017-06-22 14:42 书籍/java.pdf


-rw-r--r-- root/root         0 2017-06-22 14:42 书籍/php.pdf


-rw-r--r-- root/root         0 2017-06-22 14:42 书籍/asp.pdf


-rw------- root/root      1235 2017-06-22 01:34 anaconda-ks.cfg


[root@localhost tmp]# 


Reference:

[1] Java1234_小锋, Centos之压缩和解压缩命令, http://blog.java1234.com/blog/articles/305.html

原文地址:https://www.cnblogs.com/ryelqy/p/10104136.html