例1:以gzip进行压缩
[root@liang ~]# tar zcvf hosts.tar.gz /etc/hosts
tar: Removing leading `/' from member names
/etc/hosts
对比不压缩的包大小
[root@liang ~]# du -h hosts.*
12K hosts.tar
4.0K hosts.tar.gz
解压
[root@liang ~]# tar zxvf hosts.tar.gz
etc/hosts
例2:以bz2方式压缩
[root@liang ~]# tar jcvf hosts.tar.bz2 /etc/hosts
tar: Removing leading `/' from member names
/etc/hosts
tar (child): bzip2: Cannot exec: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
报错:是因为没哟bzip2的解压工具,需要安装
解决 yum install bzip2 -y
[root@liang ~]# tar jcvf hosts.tar.bz2 /etc/hosts
tar: Removing leading `/' from member names
/etc/hosts
解压
[root@liang ~]# tar jxvf hosts.tar.bz2
etc/hosts
例3:以xz方式压缩
[root@liang ~]# tar Jcvf hosts.tar.xz /etc/hosts
tar: Removing leading `/' from member names
/etc/hosts
解压
[root@liang ~]# tar Jxvf hosts.tar.xz
etc/hosts
1.4对比三种打包方式的大小与速度
对比速度
[root@liang ~]# time tar zcvf etc.tar.gz /etc/
real 0m0.868s
user 0m0.740s
sys 0m0.099s
[root@liang ~]# time tar jcvf etc.tar.bz2 /etc/
real 0m2.037s
user 0m1.933s
sys 0m0.078s
[root@liang ~]# time tar Jcvf etc.tar.xz /etc/
real 0m9.828s
user 0m9.577s
sys 0m0.193s
time命令输入解释
real: 表示程序整个的运行耗时。可以理解为命令运行开始时刻你看了一下手表,命令运行结束时,你又看了一下手表,两次时间的差值就是本次real 代表的值
user:这个时间代表的是命令运行在用户态的cpu时间
sys: 这个时间代表的命令是运行在核心态的cpu时间
%cpu_usage = (user_time + sys_time)/real_time * 100%
我们这里只看速度的话,tar.gz最快,bz2次之。
对比大小
[root@liang ~]# du -sh /etc/
22M /etc/
[root@liang ~]# du -h etc*
6.0M etc.tar.bz2
6.9M etc.tar.gz
5.0M etc.tar.xz
压缩时间越久,效率就越高。
2.zip
1.1命令参数
需要安装 [root@liang ~]# yum install zip unzip -y zip 压缩命令 unzip 解压命令