您的位置:首页 > 运维架构 > Linux

【Linux学习笔记】12:压缩和解压缩命令(有关.zip .gz .bz2)

2017-08-25 12:39 666 查看
windows中的压缩包格式常见的有.zip,.rar,.7z等,在Linux中常见的压缩格式有.zip,.gz,.bz2,.tar.gz,.tar.bz2等。不同的压缩格式的压缩和解压命令是不同的。

.zip

windows和Linux下的zip文件是可以通用的。

*压缩文件:

zip 压缩文件名 源文件


*压缩目录:

zip -r 压缩文件名 源目录


压缩文件名不写扩展名也可以,因为Linux不区分扩展名,但是这样没有扩展名的文件易读性很差,很容易分不清这个文件是不是压缩文件,所以应当写清楚扩展名。

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


它是个空文件,所以压缩后体积变了0%。



压缩文件不一定比原文件小,这是正常的,因为要有压缩格式的开销。

尝试压缩一个装有空文件的目录:

[root@localhost ~]# zip -r mulu.zip mulu
adding: mulu/ (stored 0%)
adding: mulu/test1 (stored 0%)
adding: mulu/test2 (stored 0%)


这回体积变小了:



*解压缩:

unzip 压缩文件


尝试解压一个文件:

[root@localhost ~]# rm lzhgb
rm:是否删除普通空文件 "lzhgb"?y
[root@localhost ~]# unzip lzhgb.zip
Archive:  lzhgb.zip
extracting: lzhgb
[root@localhost ~]# ll
总用量 56
-rw-------. 1 root root  1392 8月  22 18:22 anaconda-ks.cfg
-rw-r--r--. 1 root root 27016 8月  22 18:22 install.log
-rw-r--r--. 1 root root  7572 8月  22 18:20 install.log.syslog
-rw-r--r--. 1 root root     0 8月  25 04:57 lzhgb
-rw-r--r--. 1 root root   160 8月  25 19:10 lzhgb.zip
drwxr-xr-x. 2 root root  4096 8月  25 19:17 mulu
-rw-r--r--. 1 root root   456 8月  25 19:18 mulu.zip


.gz

这个格式是linux专有的,但是也可以在windows中解压缩。

*压缩文件:

gzip 源文件


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

gzip -c 源文件 > 压缩文件


压缩为.gz格式的压缩文件,且源文件被保留。gzip命令本身是做不到的,这是用输出重定向的方式保留了源文件。”>”的意思是把命令的结果写入指定文件中去,如把ll命令结果写道shuchu中去:

[root@localhost ~]# touch shuchu
[root@localhost ~]# ll > shuchu
[root@localhost ~]# cat shuchu
总用量 52
-rw-------. 1 root root  1392 8月  22 18:22 anaconda-ks.cfg
-rw-r--r--. 1 root root 27016 8月  22 18:22 install.log
-rw-r--r--. 1 root root  7572 8月  22 18:20 install.log.syslog
-rw-r--r--. 1 root root    26 8月  25 04:57 lzhgb.gz
drwxr-xr-x. 2 root root  4096 8月  25 19:17 mulu
-rw-r--r--. 1 root root     0 8月  25 19:33 shuchu


“-c”是表示把压缩的结果输出到了屏幕上去(可以”gzip -c 文件”试一下),配合”>”输出重定向就实现了压缩并保留源文件的效果。

*压缩目录(下的子文件):

gzip -r 目录


这表示在目录里压缩替换目录下的所有子文件,但不能压缩目录,如:

[root@localhost ~]# gzip -r mulu
[root@localhost ~]# ll
总用量 56
-rw-------. 1 root root  1392 8月  22 18:22 anaconda-ks.cfg
-rw-r--r--. 1 root root 27016 8月  22 18:22 install.log
-rw-r--r--. 1 root root  7572 8月  22 18:20 install.log.syslog
-rw-r--r--. 1 root root    26 8月  25 04:57 lzhgb.gz
drwxr-xr-x. 2 root root  4096 8月  25 19:36 mulu
-rw-r--r--. 1 root root   351 8月  25 19:33 shuchu
[root@localhost ~]# cd mulu/
[root@localhost mulu]# ll
总用量 8
-rw-r--r--. 1 root root 26 8月  25 19:16 test1.gz
-rw-r--r--. 1 root root 26 8月  25 19:17 test2.gz


*解压缩

gzip -d 压缩文件


或者

gunzip 压缩文件


尝试解压文件:

[root@localhost ~]# ll
总用量 56
-rw-------. 1 root root  1392 8月  22 18:22 anaconda-ks.cfg
-rw-r--r--. 1 root root 27016 8月  22 18:22 install.log
-rw-r--r--. 1 root root  7572 8月  22 18:20 install.log.syslog
-rw-r--r--. 1 root root    26 8月  25 04:57 lzhgb.gz
drwxr-xr-x. 2 root root  4096 8月  25 19:36 mulu
-rw-r--r--. 1 root root   351 8月  25 19:33 shuchu
[root@localhost ~]# gunzip lzhgb.gz
[root@localhost ~]# ll
总用量 52
-rw-------. 1 root root  1392 8月  22 18:22 anaconda-ks.cfg
-rw-r--r--. 1 root root 27016 8月  22 18:22 install.log
-rw-r--r--. 1 root root  7572 8月  22 18:20 install.log.syslog
-rw-r--r--. 1 root root     0 8月  25 04:57 lzhgb
drwxr-xr-x. 2 root root  4096 8月  25 19:36 mulu
-rw-r--r--. 1 root root   351 8月  25 19:33 shuchu


可以看到原来的压缩包会消失。

尝试解压目录(要加-r):

[root@localhost ~]# ll mulu/
总用量 8
-rw-r--r--. 1 root root 26 8月  25 19:16 test1.gz
-rw-r--r--. 1 root root 26 8月  25 19:17 test2.gz
[root@localhost ~]# gunzip -r mulu
[root@localhost ~]# ll mulu/
总用量 0
-rw-r--r--. 1 root root 0 8月  25 19:16 test1
-rw-r--r--. 1 root root 0 8月  25 19:17 test2


可以看到目录中的内容会被解压。

.bz2

这个命令是不能压缩目录的。

*压缩文件:

bzip2 [-k] 源文件


选项:-k表示保留源文件。

如:

[root@localhost ~]# bzip2 lzhgb




又如:

[root@localhost ~]# bzip2 -k shuchu




*解压:

bzip2 [-k] -d 压缩文件




bunzip2 [-k] 压缩文件


选项:-k保留压缩文件。

如:

[root@localhost ~]# bunzip2 lzhgb.bz2


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: