Linux 压缩,解压缩,打包指令

linux压缩文件扩展名有以下几种:
*.Z compress程序压缩的扩展名
*.gz gzip压缩后的扩展名
*.bz2 bzip2压缩后的扩展名
*.tar tar打包后的扩展名,没有被压缩过
*.tar.gz tar打包后经过gzip压缩后的扩展名
*.tar.bz2 tar打包后经过bzip2压缩后的扩展名

这些扩展名的意义是方便识别是那种压缩指令进行压缩的,从而就知道使用哪种指令进行解压缩。

其中compress指令由于压缩效率低下,并且gzip和bzip2都支持解压缩compress压缩过的文件,所以这个指令已经基本上没人使用了。
gzip 文件名
参数:
-v: 显示压缩比等信息
-c: 将压缩的数据输出到屏幕上,可以通过数据流重导向来处理
-d: 解压缩参数

使用-v参数显示压缩比,压缩后文件名为man_dbtest.conf.gz,压缩后
源文件不存在了

[root@localhost tmp]# gzip -v man_dbtest.conf 
man_dbtest.conf:     61.9% -- replaced with man_dbtest.conf.gz

加上-d参数,解压缩

[root@localhost tmp]# gzip -d man_dbtest.conf.gz 

使用-c参数,重导向数据流,自定义压缩后文件名,同时源文件依然存在。

[root@localhost tmp]# gzip -cv man_dbtest.conf > man_dbtest.conf2.gz
man_dbtest.conf:     61.9%

由于man_dbtest.conf是一个文本文件,可以使用zcat指令读取压缩后文件内容。

[root@localhost tmp]# zcat man_dbtest.conf.gz

bzip2 文件
-v: 显示压缩比信息
-c: 将压缩的数据输出到屏幕上,可以通过数据流重导向来处理
-d: 解压缩参数
bzip2的指令和gzip几乎是一致的,上面的操作均可以换成bzip2来执行,同样可以使用bzcat指令来读取用bzip2压缩过的文本文件。

上面讲的gzip和bzip2指令只能对文件进行压缩,没法对文件夹进行压缩。如果要想对文件夹进行压缩,可以使用tar指令先将文件夹打包,然后再压缩。
tar指令的参数非常多
-j: 通过bzip2支持进行压缩/解压缩
-z: 通过gzip支持进行压缩/解压缩
-v: 在压缩/解压缩过程中,将正在处理的文件名显示出来
-c: 建立压缩文件
-t: 查看压缩文件中有哪些文件名
-x: 解压缩或者解打开
-f: 后面接要被处理的文件名
-C: 大写的C,若要特定指定也锁目录,可以使用这个参数
注意,ctx三个参数不可同时出现。

对于tar指令,只需记住以下六个就够用了:
bzip2支持的压缩/解压缩方式
压缩:tar -jcv -f filename.tar.bz2 要被压缩的文件或者文件夹
查询:tar -jtv -f filename.tar.bz2
解压缩:tar -jxv -f filename.tar.bz2 -C 要将压缩文件解压缩到的目录

gzip支持的压缩/解压缩方式
压缩:tar -zcv -f filename.tar.gz 要被压缩的文件或者文件夹
查询:tar -ztv -f filename.tar.gz
解压缩:tar -zxv -f filename.tar.gz -C 要将压缩文件解压缩到的目录

创建文件夹tardirtest

[root@localhost tmp]# mkdir tardirtest
[root@localhost tmp]# cp man_dbtest.conf tardirtest/
[root@localhost tmp]# cp man_db.conf.gz tardirtest/
[root@localhost tmp]# cp man_db.conf.bz2 tardirtest/
[root@localhost tmp]# ll tardirtest/
total 16
-rw-r--r-- 1 root root 1986 Nov 17 15:13 man_db.conf.bz2
-rw-r--r-- 1 root root 1998 Nov 17 15:13 man_db.conf.gz
-rw-r--r-- 1 root root 5171 Nov 17 15:11 man_dbtest.conf

使用bzip2支持的压缩/解压缩方式

[root@localhost tmp]# tar -jcv -f tardirtest.tar.bz2 tardirtest/
tardirtest/
tardirtest/man_dbtest.conf
tardirtest/man_db.conf.gz
tardirtest/man_db.conf.bz2
[root@localhost tmp]# ll tardirtest*
-rw-r--r-- 1 root root 6899 Nov 17 15:19 tardirtest.tar.bz2

tardirtest:
total 16
-rw-r--r-- 1 root root 1986 Nov 17 15:13 man_db.conf.bz2
-rw-r--r-- 1 root root 1998 Nov 17 15:13 man_db.conf.gz
-rw-r--r-- 1 root root 5171 Nov 17 15:11 man_dbtest.conf

压缩后源文件依然存在,查看压缩文件内文件名

[root@localhost tmp]# tar -jtv -f tardirtest.tar.bz2 
drwxr-xr-x root/root         0 2017-11-17 15:13 tardirtest/
-rw-r--r-- root/root      5171 2017-11-17 15:11 tardirtest/man_dbtest.conf
-rw-r--r-- root/root      1998 2017-11-17 15:13 tardirtest/man_db.conf.gz
-rw-r--r-- root/root      1986 2017-11-17 15:13 tardirtest/man_db.conf.bz2

解压缩,解压缩后存放在/tmp/tardirtest2/ 文件夹下

[root@localhost tmp]# tar -jxv -f tardirtest.tar.bz2 -C tardirtest2/
tardirtest/
tardirtest/man_dbtest.conf
tardirtest/man_db.conf.gz
tardirtest/man_db.conf.bz2
[root@localhost tmp]# cd tardirtest2/
[root@localhost tardirtest2]# ll
total 0
drwxr-xr-x 2 root root 71 Nov 17 15:13 tardirtest
[root@localhost tardirtest2]# cd tardirtest/
[root@localhost tardirtest]# ll
total 16
-rw-r--r-- 1 root root 1986 Nov 17 15:13 man_db.conf.bz2
-rw-r--r-- 1 root root 1998 Nov 17 15:13 man_db.conf.gz
-rw-r--r-- 1 root root 5171 Nov 17 15:11 man_dbtest.conf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值