Linux常见压缩解压命令(重要的内容在后面)

linux的压缩文件有很多种,这里罗列几种。供以后方便查询:


.zip格式

zip  压缩文件名  源文件     ----压缩文件

zip  -r  压缩文件名  目录           ----压缩目录

unzip   ----解压缩

[root@localhost ~]# ls
abd              cangls       install.log.syslog
anaconda-ks.cfg  install.log  longls
[root@localhost ~]# clear
[root@localhost ~]# ls
abd              cangls       install.log.syslog
anaconda-ks.cfg  install.log  longls
[root@localhost ~]# rm -rf longls/
[root@localhost ~]# ls
abd              cangls       install.log.syslog
anaconda-ks.cfg  install.log
[root@localhost ~]# clear
[root@localhost ~]# touch longls
[root@localhost ~]# ls
abd              cangls       install.log.syslog
anaconda-ks.cfg  install.log  longls
[root@localhost ~]# zip longls.zip longls
ls  adding: longls (stored 0%)
[root@localhost ~]# ls
abd              install.log         longls.zip
anaconda-ks.cfg  install.log.syslog
cangls           longls
[root@localhost ~]# mkdir cangls
mkdir: 无法创建目录"cangls": 文件已存在
[root@localhost ~]# zip -r cangls.zip cangls
  adding: cangls (stored 0%)
[root@localhost ~]# ls
abd              cangls.zip          longls
anaconda-ks.cfg  install.log         longls.zip
cangls           install.log.syslog
[root@localhost ~]# ls
abd              cangls      install.log         longls
anaconda-ks.cfg  cangls.zip  install.log.syslog  longls.zip
[root@localhost ~]# rm -rf *.zip
[root@localhost ~]# ls
abd              cangls       install.log.syslog
anaconda-ks.cfg  install.log  longls
[root@localhost ~]# cd longls
-bash: cd: longls: 不是目录
[root@localhost ~]# rm -rf longls
[root@localhost ~]# ls
abd              cangls       install.log.syslog
anaconda-ks.cfg  install.log
[root@localhost ~]# cd cangls
-bash: cd: cangls: 不是目录
[root@localhost ~]# rm -rf cangls
[root@localhost ~]# ls
abd  anaconda-ks.cfg  install.log  install.log.syslog
[root@localhost ~]# mkdir cangls
[root@localhost ~]# ls
abd              cangls       install.log.syslog
anaconda-ks.cfg  install.log
[root@localhost ~]# cd cangls
[root@localhost cangls]# ls
[root@localhost cangls]# touch liuls
[root@localhost cangls]# touch zhangls
[root@localhost cangls]# touch wnangls
[root@localhost cangls]# ls
liuls  wnangls  zhangls
[root@localhost cangls]# cd..
-bash: cd..: command not found
[root@localhost cangls]# cd ..
[root@localhost ~]# ls
abd              cangls       install.log.syslog
anaconda-ks.cfg  install.log
[root@localhost ~]# zip -r cangls

zip error: Nothing to do! (cangls.zip)
[root@localhost ~]# zip -r cangls.zip cangls
  adding: cangls/ (stored 0%)
  adding: cangls/liuls (stored 0%)
  adding: cangls/zhangls (stored 0%)
  adding: cangls/wnangls (stored 0%)
[root@localhost ~]# ls
abd              cangls      install.log
anaconda-ks.cfg  cangls.zip  install.log.syslog
[root@localhost ~]# cd cangls
[root@localhost cangls]# ls
liuls  wnangls  zhangls
[root@localhost cangls]# cd ..
[root@localhost ~]# ls
abd              cangls      install.log
anaconda-ks.cfg  cangls.zip  install.log.syslog
[root@localhost ~]# rm -rf cangls
[root@localhost ~]# ls
abd              cangls.zip   install.log.syslog
anaconda-ks.cfg  install.log
[root@localhost ~]# unzip cangls.zip 
Archive:  cangls.zip
   creating: cangls/
 extracting: cangls/liuls            
 extracting: cangls/zhangls          
 extracting: cangls/wnangls          
[root@localhost ~]# ls
abd              cangls      install.log
anaconda-ks.cfg  cangls.zip  install.log.syslog
[root@localhost ~]# cd cangls
[root@localhost cangls]# ls
liuls  wnangls  zhangls

.gz格式

gzip   源文件    ----压缩源文件,压缩后源文件会消失

gzip  -c  源文件  >  压缩文件        ----压缩为.gz格式,源文件保留

gzip  -r  目录    ---- 压缩目录下所有子文件,但是不能压缩目录

[root@localhost cangls]# cd ..
[root@localhost ~]# ls
abd              cangls      install.log
anaconda-ks.cfg  cangls.zip  install.log.syslog
[root@localhost ~]# rm -rf cangls
[root@localhost ~]# rm -rf cangls.zip 
[root@localhost ~]# ls
abd  anaconda-ks.cfg  install.log  install.log.syslog
[root@localhost ~]# touch cangls
[root@localhost ~]# ls
abd  anaconda-ks.cfg  cangls  install.log  install.log.syslog
[root@localhost ~]# gzip cangls.gz cangls 
gzip: cangls.gz: No such file or directory
[root@localhost ~]# ls
abd              cangls.gz    install.log.syslog
anaconda-ks.cfg  install.log
[root@localhost ~]# clear
[root@localhost ~]# ls
abd              cangls.gz    install.log.syslog
anaconda-ks.cfg  install.log
[root@localhost ~]# rm -rf cangls.gz 
[root@localhost ~]# ls
abd  anaconda-ks.cfg  install.log  install.log.syslog
[root@localhost ~]# touch cangls
[root@localhost ~]# gzip cangls 
[root@localhost ~]# ls
abd              cangls.gz    install.log.syslog
anaconda-ks.cfg  install.log
[root@localhost ~]# touch liuls
[root@localhost ~]# gzip -c liuls > liuls.gz
[root@localhost ~]# ls
abd              cangls.gz    install.log.syslog  liuls.gz
anaconda-ks.cfg  install.log  liuls
[root@localhost ~]# mkdir zhngls
[root@localhost ~]# gzip -r zhngls/
[root@localhost ~]# ls
abd              cangls.gz    install.log.syslog  liuls.gz
anaconda-ks.cfg  install.log  liuls               zhngls
[root@localhost ~]# cd zhngls/
[root@localhost zhngls]# touch WANGLS
[root@localhost zhngls]# ls
WANGLS
[root@localhost zhngls]# cd ..
[root@localhost ~]# gzip -r zhngls/
[root@localhost ~]# ls
abd              cangls.gz    install.log.syslog  liuls.gz
anaconda-ks.cfg  install.log  liuls               zhngls
[root@localhost ~]# cd zhngls/
[root@localhost zhngls]# ls
WANGLS.gz

.bz2格式

bzip2   源文件     ----不保留源文件

bzip2   -k  源文件     ----保留源文件  

bzip2  -d  压缩文件    ---- 解压缩  -k保留压缩文件

bunzip2   压缩文件    ----解压缩  -k  保留压缩文件

[root@localhost ~]# bzip2 zhangls 
[root@localhost ~]# ls
abd              install.log         liuls.gz
anaconda-ks.cfg  install.log.syslog  zhangls.bz2
cangls.gz        liuls.bz2           zhngls
[root@localhost ~]# touch wangls
[root@localhost ~]# bzip2 -k wangls 
[root@localhost ~]# ls
abd              install.log         liuls.gz    zhangls.bz2
anaconda-ks.cfg  install.log.syslog  wangls      zhngls
cangls.gz        liuls.bz2           wangls.bz2
[root@localhost ~]# ls
abd              install.log         liuls.gz    zhangls.bz2
anaconda-ks.cfg  install.log.syslog  wangls      zhngls
cangls.gz        liuls.bz2           wangls.bz2
[root@localhost ~]# rm -rf wangls
[root@localhost ~]# ls
abd              install.log         liuls.gz     zhngls
anaconda-ks.cfg  install.log.syslog  wangls.bz2
cangls.gz        liuls.bz2           zhangls.bz2
[root@localhost ~]# bunzip2 wangls.bz2 
[root@localhost ~]# ls
abd              install.log         liuls.gz     zhngls
anaconda-ks.cfg  install.log.syslog  wangls
cangls.gz        liuls.bz2           zhangls.bz2
[root@localhost ~]# bzip2 -dk zhangls.bz2 
[root@localhost ~]# ls
abd              install.log         liuls.gz  zhangls.bz2
anaconda-ks.cfg  install.log.syslog  wangls    zhngls
cangls.gz        liuls.bz2           zhangls

tar.gz     格式   ----先将目录压缩成一个tar包,然后再压缩成gz格式。这样就解决了.gz格式不能压缩目录

tar  -cvf  打包文件名   源文件          ----压缩文件

     -c  : 打包

     -v: 显示过程

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


tar  -xvf   打包文件名     ----解压缩

     -x:解打包

[root@localhost ~]# mkdir cangls
[root@localhost ~]# ls
anaconda-ks.cfg  cangls  install.log  install.log.syslog
[root@localhost ~]# cd cangls
[root@localhost cangls]# touch wangls
[root@localhost cangls]# touch lils
[root@localhost cangls]# touch liuls
[root@localhost cangls]# cd ..
[root@localhost ~]# tar -cvf cangls.tar cangls
cangls/
cangls/lils
cangls/liuls
cangls/wangls
[root@localhost ~]# ls
anaconda-ks.cfg  cangls.tar   install.log.syslog
cangls           install.log
[root@localhost ~]# gzip cangls.tar
[root@localhost ~]# ls
anaconda-ks.cfg  cangls.tar.gz  install.log.syslog
cangls           install.log
[root@localhost ~]# gzip -d  cangls.tar.gz 
[root@localhost ~]# ls
anaconda-ks.cfg  cangls.tar   install.log.syslog
cangls           install.log
[root@localhost ~]# bzip2 cangls.tar 
[root@localhost ~]# ls
anaconda-ks.cfg  cangls.tar.bz2  install.log.syslog
cangls           install.log
[root@localhost ~]# bzip2 -d cangls.tar.bz2 
[root@localhost ~]# ls
anaconda-ks.cfg  cangls.tar   install.log.syslog
cangls           install.log
[root@localhost ~]# rm -rf cangls
[root@localhost ~]# ls
anaconda-ks.cfg  cangls.tar  install.log  install.log.syslog
[root@localhost ~]# tar -xvf cangls.tar 
cangls/
cangls/lils
cangls/liuls
cangls/wangls
[root@localhost ~]# ls
anaconda-ks.cfg  cangls.tar   install.log.syslog
cangls           install.log


真正的精华:


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

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

tar  -zxvf  压缩包名.tar.gz


tar   -jcvf  压缩包名.tar.bz2  源文件         ----压缩文件为.tar.bz2格式

tar  -jxcf   压缩包名.tar.bz2                      ----解压缩

[root@localhost ~]# mkdir cangls
[root@localhost ~]# cd cangls/
[root@localhost cangls]# touch wanls
[root@localhost cangls]# touch wanls2
[root@localhost cangls]# touch wanls23
[root@localhost cangls]# cd ..
[root@localhost ~]# ls
anaconda-ks.cfg  cangls  install.log  install.log.syslog
[root@localhost ~]# tar -zcvf cangls.tar.gz cangls
cangls/
cangls/wanls
cangls/wanls23
cangls/wanls2
[root@localhost ~]# ls
anaconda-ks.cfg  cangls.tar.gz  install.log.syslog
cangls           install.log
[root@localhost ~]# rm -rf cangls
[root@localhost ~]# tar -zxvf cangls.tar.gz 
cangls/
cangls/wanls
cangls/wanls23
cangls/wanls2
[root@localhost ~]# ls
anaconda-ks.cfg  cangls.tar.gz  install.log.syslog
cangls           install.log
[root@localhost ~]# rm -rf cangls
[root@localhost ~]# tar -zxvf cangls.tar.gz 
cangls/
cangls/wanls
cangls/wanls23
cangls/wanls2
[root@localhost ~]# ls
anaconda-ks.cfg  cangls.tar.gz  install.log.syslog
cangls           install.log
[root@localhost ~]# ls
anaconda-ks.cfg  cangls  install.log  install.log.syslog
[root@localhost ~]# tar -jcvf cangls.tar.gz cangls
cangls/
cangls/wanls
cangls/li1
cangls/wanls23
cangls/wanls2
cangls/li123
cangls/li12
[root@localhost ~]# ls
anaconda-ks.cfg  cangls.tar.gz  install.log.syslog
cangls           install.log
[root@localhost ~]# rm -rf cangls
[root@localhost ~]# ls
anaconda-ks.cfg  cangls.tar.gz  install.log  install.log.syslog
[root@localhost ~]# tar -jxvf cangls.tar.gz 
cangls/
cangls/wanls
cangls/li1
cangls/wanls23
cangls/wanls2
cangls/li123
cangls/li12
[root@localhost ~]# ls
anaconda-ks.cfg  cangls.tar.gz  install.log.syslog
cangls           install.log
解压后面再跟  -C  具体目录   -----解压当相应目录

[root@localhost ~]# ls
anaconda-ks.cfg  cangls.tar.gz  install.log.syslog
cangls           install.log
[root@localhost ~]# rm -rf cangls.tar.gz 
[root@localhost ~]# tar -jcvf cangls.tar.bz2 cangls
cangls/
cangls/wanls
cangls/li1
cangls/wanls23
cangls/wanls2
cangls/li123
cangls/li12
[root@localhost ~]# ls
anaconda-ks.cfg  cangls.tar.bz2  install.log.syslog
cangls           install.log
[root@localhost ~]# tar -jxvf cangls.tar.bz2 -C /tmp/
cangls/
cangls/wanls
cangls/li1
cangls/wanls23
cangls/wanls2
cangls/li123
cangls/li12
[root@localhost ~]# ls /tmp/
aa.hard  cangls
[root@localhost ~]# 
假如需要压缩多个文件呢?当然可以
[root@localhost ~]# ls
anaconda-ks.cfg  cangls.tar.bz2  install.log.syslog
cangls           install.log
[root@localhost ~]# tar -zcvf /tmp/test.tar.gz cangls anaconda-ks.cfg 
cangls/
cangls/wanls
cangls/li1
cangls/wanls23
cangls/wanls2
cangls/li123
cangls/li12
anaconda-ks.cfg
[root@localhost ~]# ls /tmp
aa.hard  cangls  test.tar.gz
[root@localhost ~]# cd /tmp
[root@localhost tmp]# ls
aa.hard  cangls  test.tar.gz
[root@localhost tmp]# tar -zxvf test.tar.gz 
cangls/
cangls/wanls
cangls/li1
cangls/wanls23
cangls/wanls2
cangls/li123
cangls/li12
anaconda-ks.cfg
[root@localhost tmp]# ls
aa.hard  anaconda-ks.cfg  cangls  test.tar.gz

tar    -ztvf       ------只查看压缩文件的目录,不解压文件
[root@localhost tmp]# ls
aa.hard  anaconda-ks.cfg  cangls  test.tar.gz
[root@localhost tmp]# rm -rf anaconda-ks.cfg 
[root@localhost tmp]# rm -rf cangls/
[root@localhost tmp]# ls
aa.hard  test.tar.gz
[root@localhost tmp]# tar -ztvf test.tar.gz 
drwxr-xr-x root/root         0 2018-01-03 06:54 cangls/
-rw-r--r-- root/root         0 2018-01-03 06:50 cangls/wanls
-rw-r--r-- root/root         0 2018-01-03 06:54 cangls/li1
-rw-r--r-- root/root         0 2018-01-03 06:50 cangls/wanls23
-rw-r--r-- root/root         0 2018-01-03 06:50 cangls/wanls2
-rw-r--r-- root/root         0 2018-01-03 06:54 cangls/li123
-rw-r--r-- root/root         0 2018-01-03 06:54 cangls/li12
-rw------- root/root      1202 2017-11-27 00:15 anaconda-ks.cfg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值