Linux学习笔记(7)

来源:互联网 发布:火箭nba 知乎 编辑:程序博客网 时间:2024/06/05 09:58

1、压缩命令1

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

1、1.zip格式压缩

#压缩文件

zip 压缩文件名  源文件

运行效果:

[root@localhost ~]# zip cangls.zip cangls
  adding: cangls (stored 0%)

[root@localhost ~]# ll
总用量 48
-rw-------. 1 root root  1206 1月   8 21:11 anaconda-ks.cfg
-rw-r--r--. 1 root root     0 1月   9 21:33 cangls
-rw-r--r--. 1 root root   162 3月   7 13:08 cangls.zip
-rw-r--r--. 1 root root 25906 12月 27 05:48 install.log
-rw-r--r--. 1 root root  7690 12月 27 05:44 install.log.syslog

#压缩目录

zip -r 压缩文件名 源目录

运行效果:

[root@localhost ~]# mkdir jp
[root@localhost ~]# ls
anaconda-ks.cfg  cangls  cangls.zip  install.log  install.log.syslog  jp
[root@localhost ~]# touch jp/cangls
[root@localhost ~]# touch jp/longls
[root@localhost ~]# touch jp/boduols
[root@localhost ~]# zip -r jp.zip jp
  adding: jp/ (stored 0%)
  adding: jp/longls (stored 0%)
  adding: jp/cangls (stored 0%)
  adding: jp/boduols (stored 0%)
[root@localhost ~]# ll
总用量 56
-rw-------. 1 root root  1206 1月   8 21:11 anaconda-ks.cfg
-rw-r--r--. 1 root root     0 1月   9 21:33 cangls
-rw-r--r--. 1 root root   162 3月   7 13:08 cangls.zip
-rw-r--r--. 1 root root 25906 12月 27 05:48 install.log
-rw-r--r--. 1 root root  7690 12月 27 05:44 install.log.syslog
drwxr-xr-x. 2 root root  4096 3月   7 13:10 jp
-rw-r--r--. 1 root root   596 3月   7 13:10 jp.zip

#解压缩.zip文件

unzip 压缩文件

运行效果:

[root@localhost ~]# unzip cangls.zip
Archive:  cangls.zip
 extracting: cangls                  
[root@localhost ~]# unzip jp.zip
Archive:  jp.zip
   creating: jp/
 extracting: jp/longls               
 extracting: jp/cangls               
 extracting: jp/boduols              
[root@localhost ~]# ll
总用量 56
-rw-------. 1 root root  1206 1月   8 21:11 anaconda-ks.cfg
-rw-r--r--. 1 root root     0 1月   9 21:33 cangls
-rw-r--r--. 1 root root   162 3月   7 13:08 cangls.zip
-rw-r--r--. 1 root root 25906 12月 27 05:48 install.log
-rw-r--r--. 1 root root  7690 12月 27 05:44 install.log.syslog
drwxr-xr-x. 2 root root  4096 3月   7 13:10 jp
-rw-r--r--. 1 root root   596 3月   7 13:10 jp.zip

此种格式和Windows通用

1、2.gz格式压缩

gzip 源文件

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

运行效果:

[root@localhost ~]# ls
anaconda-ks.cfg  cangls  install.log  install.log.syslog  jp
[root@localhost ~]# gzip cangls
[root@localhost ~]# ls
anaconda-ks.cfg  cangls.gz  install.log  install.log.syslog  jp

从运行效果我们可以看出源文件cangls消失了

gzip -c 源文件 > 压缩文件

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

运行效果:

[root@localhost ~]# touch abc
[root@localhost ~]# ls
abc  anaconda-ks.cfg  cangls.gz  install.log  install.log.syslog  jp
[root@localhost ~]# gzip -c abc                                                       

ጺTabc

[root@localhost ~]# gzip -c abc > abc.gz
[root@localhost ~]# ls
abc  abc.gz  anaconda-ks.cfg  cangls.gz  install.log  install.log.syslog  jp

例如:gzip - cangls > cangls.gz

gzip -r 目录

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

运行效果:

[root@localhost ~]# gzip -r jp
[root@localhost ~]# cd jp/
[root@localhost jp]# ls
boduols.gz  cangls.gz  longls.gz
[root@localhost jp]# cd
[root@localhost ~]# ls
abc  abc.gz  anaconda-ks.cfg  cangls.gz  install.log  install.log.syslog  jp

从运行效果上看目录本身没有被压缩,而目录里面的子文件被压缩了

.gz格式解压缩

gzip -d 压缩文件

#解压缩文件

运行效果:

[root@localhost ~]# ls
abc.gz  anaconda-ks.cfg  cangls.gz  install.log  install.log.syslog  jp
[root@localhost ~]# gzip -d abc.gz
[root@localhost ~]# ls
abc  anaconda-ks.cfg  cangls.gz  install.log  install.log.syslog  jp

gunzip 压缩文件

#解压缩文件

运行效果:

[root@localhost ~]# gunzip cangls.gz
[root@localhost ~]# ls

abc  anaconda-ks.cfg  cangls  install.log  install.log.syslog  jp

gunzip -r 压缩文件

#解压目录

运行效果:

[root@localhost ~]# ls
abc  anaconda-ks.cfg  cangls  install.log  install.log.syslog  jp
[root@localhost ~]# gunzip jp
gzip: jp is a directory -- ignored
[root@localhost ~]# gunzip -r jp
[root@localhost ~]# cd jp/
[root@localhost jp]# ls
boduols  cangls  longls

1、3.bz2格式压缩

bzip2 源文件

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

运行效果:

[root@localhost ~]# bzip2 abc
[root@localhost ~]# ls
abc.bz2  anaconda-ks.cfg  cangls  install.log  install.log.syslog  jp

bzip2 -k 源文件

#压缩之后保留源文件

运行效果:

[root@localhost ~]# bzip2 -k cangls
[root@localhost ~]# ls
abc.bz2  anaconda-ks.cfg  cangls  cangls.bz2  install.log  install.log.syslog  jp

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

.bz2格式解压缩

bzip2 -d 压缩文件

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

运行效果:

[root@localhost ~]# ls
abc.bz2  anaconda-ks.cfg  cangls  cangls.bz2  install.log  install.log.syslog  jp

[root@localhost ~]# bzip2 -d abc.bz2
[root@localhost ~]# ls
abc  anaconda-ks.cfg  cangls  cangls.bz2  install.log  install.log.syslog  jp

bunzip2 压缩文件

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

运行效果:

[root@localhost ~]# ls
abc  anaconda-ks.cfg  cangls.bz2  install.log  install.log.syslog  jp
[root@localhost ~]# bunzip2 cangls.bz2
[root@localhost ~]# ls
abc  anaconda-ks.cfg  cangls  install.log  install.log.syslog  jp

2、压缩命令2

2、1打包命令tar

tar -cvf 打包文件名 源文件
选项:
-c:打包
-v:显示过程
-f:制定打包后的文件名
例如:
tar -cvf longzls.tar longzls
解打包命令
tar -xvf 打包文件名
选项:
-x:解打包
例如:
tar -xvf longzls.tar
运行效果:
[root@localhost ~]# ls
anaconda-ks.cfg  cangls  install.log  install.log.syslog  jp  jp.tar
[root@localhost ~]# rm -rf jp
[root@localhost ~]# ls
anaconda-ks.cfg  cangls  install.log  install.log.syslog  jp.tar
[root@localhost ~]# tar -xvf jp.tar
jp/
jp/longls
jp/cangls
jp/boduols
[root@localhost ~]# ls
anaconda-ks.cfg  cangls  install.log  install.log.syslog  jp  jp.tar

2、2.tar.gz压缩格式

其实.tar.gz格式是先打包为.tar格式,再压缩为.gz格式
tar -zcvf 压缩包名.tar.gz 源文件
选项:
-z:压缩为.tar.gz格式
运行效果:
[root@localhost ~]# ls
anaconda-ks.cfg  cangls  install.log  install.log.syslog  jp  jp.tar
[root@localhost ~]# rm -rf jp.tar
[root@localhost ~]# ls
anaconda-ks.cfg  cangls  install.log  install.log.syslog  jp
[root@localhost ~]# tar -zcvf jp.tar.gz jp
jp/
jp/longls
jp/cangls
jp/boduols
[root@localhost ~]# ls
anaconda-ks.cfg  cangls  install.log  install.log.syslog  jp  jp.tar.gz
tar -zxvf 压缩包名.tar.gz
选项:
-x:解压缩.tar.gz格式
运行效果:
[root@localhost ~]# ls
anaconda-ks.cfg  cangls  install.log  install.log.syslog  jp.tar.gz
[root@localhost ~]# tar -zxvf jp.tar.gz
jp/
jp/longls
jp/cangls
jp/boduols
[root@localhost ~]# ls
anaconda-ks.cfg  cangls  install.log  install.log.syslog  jp  jp.tar.gz

2、3.tar.bz2压缩格式

tar -jcvf 压缩包名.tar.bz2 源文件
选项:
-j:压缩为.tar.bz2格式
运行效果:
[root@localhost ~]# ls
anaconda-ks.cfg  cangls  install.log  install.log.syslog  jp
[root@localhost ~]# tar -jcvf jp.tar.bz2 jp
jp/
jp/longls
jp/cangls
jp/boduols
[root@localhost ~]# ls
anaconda-ks.cfg  cangls  install.log  install.log.syslog  jp  jp.tar.bz2
tar -jxvf 压缩包名.tar.bz2
选项:
-x:解压缩.tar.bz2格式
运行效果:
[root@localhost ~]# ls
anaconda-ks.cfg  cangls  install.log  install.log.syslog  jp.tar.bz2
[root@localhost ~]# tar -jxvf jp.tar.bz2
jp/
jp/longls
jp/cangls
jp/boduols
[root@localhost ~]# ls
anaconda-ks.cfg  cangls  install.log  install.log.syslog  jp  jp.tar.bz2
tar -jxvf 压缩包名.tar.bz2 -C 指定压缩的路径
选项:
-C:指定的压缩路径
运行效果:
[root@localhost ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  jp  jp.tar.bz2
[root@localhost ~]# tar -jxvf jp.tar.bz2 -C /tmp/
jp/
jp/longls
jp/cangls
jp/boduols
[root@localhost ~]# ls /tmp/
jp  vmware-root

压缩多个源文件时,源文件用空格隔开
运行效果:
[root@localhost ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  jp  jp.tar.bz2
[root@localhost ~]# tar -zcvf test.tar.gz jp anaconda-ks.cfg 
jp/
jp/longls
jp/cangls
jp/boduols
anaconda-ks.cfg
[root@localhost ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  jp  jp.tar.bz2  test.tar.gz

tar -ztvf 压缩包名.tar.gz
选项:
-t:查看压缩文件
运行效果:
[root@localhost ~]# tar -ztvf test.tar.gz
drwxr-xr-x root/root         0 2015-03-07 13:35 jp/
-rw-r--r-- root/root         0 2015-03-07 13:10 jp/longls
-rw-r--r-- root/root         0 2015-03-07 13:10 jp/cangls
-rw-r--r-- root/root         0 2015-03-07 13:10 jp/boduols
-rw------- root/root      1206 2015-01-08 21:11 anaconda-ks.cfg

0 0
原创粉丝点击