当前位置: > CentOS > CentOS教程 >

Centos7-文件归档与压缩

时间:2018-11-15 14:37来源:linux.it.net.cn 作者:IT
文章目录
1.tar
1.1命令与参数
1.2归档例子
1.3打包加压缩
2.zip
1.1命令参数
1.2例子
3.gzip、bzip2、xz压缩工具
3.1gzip、bzip2、xz的使用
1.tar
1.1命令与参数
用法:tar [参数] [压缩文件名] [要压缩的文件]
使用参数时,可以不使用 -
参数:
-c create,创建文件
-x extract,提取解压还原文件
-v 显示执行显示过程
-f 指定备份文件
-t 列出备份文件内容,不解包查看包中的内容
-C 指定解包位置
-z --gzip,以gzip方式压缩 扩展名:tar.gz
-j 以bz2方式压缩 扩展名:tar.bz2
-J 以xz方式压缩 扩展名:tar.xz
 
1.2归档例子
打包/etc/hosts文件
[root@it.net.cn ~]# tar cvf hosts.tar /etc/hosts
tar: Removing leading `/' from member names
/etc/hosts
[root@it.net.cn ~]# ll hosts.tar 
-rw-r--r--. 1 root root 10240 Sep 14 20:16 hosts.tar
在使用绝对路径进行压缩时,将默认从文件名中删除该路径中前面的/符号,这样揭阳时,会直接解压到当前目录,不然会覆盖掉原系统中的路径文件。
指定路径解包
[root@it.net.cn ~]# tar xvf hosts.tar -C /opt/
etc/hosts
[root@it.net.cn ~]# ll /opt/etc/
total 4
-rw-r--r--. 1 root root 158 Jun  7  2013 hosts
打包多个文件
[root@it.net.cn ~]# tar cvf all.tar /etc/hosts /opt/etc/ /etc/passwd
tar: Removing leading `/' from member names
/etc/hosts
/opt/etc/
/opt/etc/hosts
/etc/passwd
[root@it.net.cn ~]# ll all.tar 
-rw-r--r--. 1 root root 10240 Sep 14 20:25 all.tar
不解包文件的情况下,查看包有什么文件
[root@it.net.cn ~]# tar -tvf all.tar 
-rw-r--r-- root/root       158 2013-06-07 10:31 etc/hosts
drwxr-xr-x root/root         0 2018-09-14 20:23 opt/etc/
-rw-r--r-- root/root       158 2013-06-07 10:31 opt/etc/hosts
-rw-r--r-- root/root      1040 2018-08-15 13:36 etc/passwd
打包多目录
[root@it.net.cn ~]# tar cvf dir.tar /etc/ /var/
[root@it.net.cn ~]# ll dir.tar    
-rw-r--r--. 1 root root 149657600 Sep 14 20:29 dir.tar
 
 
1.3打包加压缩
例1:以gzip进行压缩
[root@it.net.cn ~]# tar zcvf hosts.tar.gz /etc/hosts
tar: Removing leading `/' from member names
/etc/hosts
对比不压缩的包大小
[root@it.net.cn ~]# du -h hosts.*
12K     hosts.tar
4.0K    hosts.tar.gz
解压
[root@it.net.cn ~]# tar zxvf hosts.tar.gz 
etc/hosts
例2:以bz2方式压缩
[root@it.net.cn ~]# 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@it.net.cn ~]# tar jcvf hosts.tar.bz2 /etc/hosts
tar: Removing leading `/' from member names
/etc/hosts
解压
[root@it.net.cn ~]# tar jxvf hosts.tar.bz2 
etc/hosts
例3:以xz方式压缩
[root@it.net.cn ~]# tar Jcvf hosts.tar.xz /etc/hosts
tar: Removing leading `/' from member names
/etc/hosts
解压
[root@it.net.cn ~]# tar Jxvf hosts.tar.xz 
etc/hosts
1.4对比三种打包方式的大小与速度
对比速度
[root@it.net.cn ~]# time tar zcvf etc.tar.gz /etc/
real    0m0.868s
user    0m0.740s
sys     0m0.099s
[root@it.net.cn ~]# time tar jcvf etc.tar.bz2 /etc/
real    0m2.037s
user    0m1.933s
sys     0m0.078s
[root@it.net.cn ~]# 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@it.net.cn ~]# du -sh /etc/
22M     /etc/
[root@it.net.cn ~]# du -h etc*
6.0M    etc.tar.bz2
6.9M    etc.tar.gz
5.0M    etc.tar.xz
压缩时间越久,效率就越高。
 
2.zip
1.1命令参数
需要安装
[root@it.net.cn ~]# yum install zip unzip -y
zip 压缩命令
unzip 解压命令
参数:
-r 递归压缩,压缩目录
-d 指定加压位置
1.2例子
压缩hosts
[root@it.net.cn ~]# zip hosts.zip /etc/hosts
  adding: etc/hosts (deflated 65%)
[root@it.net.cn ~]# du -h hosts.zip 
4.0K    hosts.zip
解压
[root@it.net.cn ~]# unzip hosts.zip 
Archive:  hosts.zip
  inflating: etc/hosts 
 
3.gzip、bzip2、xz压缩工具
3.1gzip、bzip2、xz的使用
[root@it.net.cn test]# touch test01
[root@it.net.cn test]# gzip test01 
[root@it.net.cn test]# ls
test01.gz
解压
[root@it.net.cn test]# gzip -d test01.gz 
[root@it.net.cn test]# ls
test01 
只能对文件进行压缩,且压缩后源文件会消失,一般不适用
bzip2,xz这两个工具可以通过添加参数-k来保留源文件
bzip2
[root@it.net.cn test]# touch test02 
[root@it.net.cn test]# bzip2 -k test02
test01.gz  test02  test02.bz2
解压
[root@it.net.cn test]# rm -f test02
[root@it.net.cn test]# ls
test01  test02.bz2
[root@it.net.cn test]# bzip2 -d test02.bz2 -k
[root@it.net.cn test]# ls
test01  test02  test02.bz2
xz
[root@it.net.cn test]# xz -k test03  
[root@it.net.cn test]# ls
test01  test02  test02.bz2  test03  test03.xz
[root@it.net.cn test]# rm -f test03
[root@it.net.cn test]# xz -d test03.xz -k
[root@it.net.cn test]# ls
test01  test02  test02.bz2  test03  test03.xz
 
(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容