4.文件压缩与解压缩

1.ziphtml

功能:兼容unix和windows,能够一次性压缩多个文件windows

语法:zip 压缩后的路径文件 须要压缩的文件1 文件2bash

经常使用选项:ide

-r:递归压缩工具

解压缩:spa

unzip 须要解压的文件 (默认解压到当前目录)3d

unzip 须要解压的文件 -d 解压后的路径unix

示例:htm

[root@localhost tmp]# zip /tmp/123.zip /etc/*
[root@localhost tmp]# unzip 123.zip -d test/
[root@localhost tmp]# ls test/
etc


2.gzip递归

功能:只能压缩单个文件,压缩速度相对较快,压缩率较低,cpu消耗相对较低

语法:gzip 须要压缩的文件

           gzip 须要压缩的文件1 须要压缩的文件2  

经常使用选项:

-c:保留源文件压缩和解压缩

-v:打印详细信息

-d 解压缩的参数

-t 检查压缩文件,看是否有错误

-# 压缩等级1-9,越高越好,但也越慢,默认是6


解压缩:

gunzip 须要解压的文件

gzip -d 须要解压的文件

查看压缩文件:

zcat

问题1:gzip工具能不能压缩一个目录?

答案是不能的

示例:

[root@localhost tmp]# gzip *   #压缩当前目录下的全部文件
[root@localhost tmp]# ll      #只有目录没有压缩
total 207200
-rw-r--r--. 1 0 0        32 May 20 13:33 1addf98dffa.gz
-rw-r--r--. 1 0 0        31 May 20 13:35 1ddf67dfdf.gz
-rw-r--r--. 1 0 0     71460 Jun  4 11:49 20170609.tar.gz
-rw-r--r--. 1 0 0        31 May 20 13:33 3dfdf7ghhn.gz
drwxr-xr-x. 2 0 0      4096 Jun  4 11:30 etc
[root@localhost tmp]# gzip -d *  #解压当前目录的全部文件


3.bzip2

功能:只能压缩单个文件,压缩率相对较高,压缩速度相对慢 cpu消耗相对较高

语法:

        bzip2 须要压缩的文件

        bzip2 须要压缩的文件1 须要压缩的文件2

经常使用选项:

-c 将压缩过程当中产生的数据输出到屏幕上

-d 解压缩参数

-k 保留原文件

-z 压缩的参数

-v 显示压缩比

-# 压缩等级1-9,和gzip同样


解压缩:

bunzip2 须要解压的文件

bzip2 -d 须要解压的文件

查看压缩文件

bzcat

示例:

[root@localhost tmp]# bzip2 -v9 123.zip  #打印压缩比
  123.zip:  1.082:1,  7.392 bits/byte,  7.59% saved, 240313 in, 222064 out.
[root@localhost tmp]# ls 123.zip.bz2 
123.zip.bz2
[root@localhost tmp]# ls 123.zip   #可是原文件没有了
ls: cannot access 123.zip: No such file or directory
[root@localhost tmp]# bunzip2 123.zip.bz2 
[root@localhost tmp]# bzip2 -z -k 123.zip  #加-k就保留了原文件
[root@localhost tmp]# ls 123*
123.zip  123.zip.bz2


4.tar

功能:打包工具

语法:tar [OPTION...] [FILE]...

参数:

-c:建立一个tar包

--delete : 从归档文件 (而非磁带) 中删除

-f:指定包名

-z:调用gzip压缩工具压缩

-j:调用bzip2压缩工具压缩

-v:显示详细信息

-x:解压

-t:查看tar包内容

-r:追加文件到tar包

-J:调用xz压缩工具

-C:指定解压的路径

-p :使用原文件的原来属性(属性不会依据使用者而变)

-P :可使用绝对路径来压缩!

说明:

参数前面-无关紧要

示例:

#把/etc目录下的文件所有打包成为/tmp/etc.tar,打包压缩名能够随便起,常规是以tar,便于区分
[root@localhost ~]# tar cvf /tmp/etc.tar /etc    #只打包,不压缩
[root@localhost ~]# ll /tmp/etc.tar
-rw-r--r--. 1 root root 27013120 Jun  4 16:50 /tmp/etc.tar
[root@localhost ~]# tar zcvf /tmp/etc.tar.gz /etc   #打包并以gzip压缩,.tgz也是同样
[root@localhost ~]# ll /tmp/etc.tar.gz
-rw-r--r--. 1 root root 9181726 Jun  4 16:52 /tmp/etc.tar.gz
[root@localhost ~]# tar jcvf /tmp/etc.tar.bz2 /etc   #打包并以bzip2压缩,.tbz2也是同样
[root@localhost ~]# ll /tmp/etc.tar.bz2
-rw-r--r--. 1 root root 8161113 Jun  4 16:55 /tmp/etc.tar.bz2
#如何查看打包或者压缩文件里都有哪些文件
[root@localhost tmp]# tar tf etc.tar.gz    #带t就是查看,带f是指定报名
[root@localhost tmp]# tar tvf etc.tar.gz  #v是详细信息,就想ls -l的长格式打印
#解压tar包到指定路径
[root@localhost tmp]# tar xf etc.tar.gz -C /home/test  #-C是指定路径
#往tar包追加内容
[root@localhost tmp]# tar -r /var  -f etc.tar.gz  #实践证实不能对压缩文件进行追加
tar: Cannot update compressed archives
tar: Error is not recoverable: exiting now
[root@localhost tmp]# tar -r /var/cache  -f etc.tar   #对打包的tar追加报错
tar: Removing leading `/' from member names
[root@localhost tmp]# tar -rP /var  -f etc.tar #追加进去了,这里用到了P选项
#释放tar包指定文件到指定目录
[root@localhost etc]# tar xvf etc.tar etc/passwd -C /tmp
[root@localhost tmp]# tar xvf etc.tar.gz etc/passwd -C /tmp  #压缩文件也是同样的方式
etc/passwd
#排除文件后再压缩文件
[root@localhost tmp]# tar zcvf /tmp/var.tar.tgz /var --exclude=*.html
[root@localhost tmp]# tar tf var.tar.tgz |grep -E "*.html"  #过滤出来的,但不是以.html结尾的
var/www/error/contact.html.var
var/www/error/HTTP_FORBIDDEN.html.var
var/www/error/HTTP_VARIANT_ALSO_VARIES.html.var

注意:

tar工具尽可能使用相对路径,参数f最好放到最后


5 练习:

一、找出/etc下面以.conf结尾的文件,并将其复制到/home下的backup目录中

[root@localhost tmp]# mkdir /home/backup
[root@localhost tmp]# find /etc/ -type f -name "*.conf"  -exec cp {} /home/backup \;
[root@localhost tmp]# ls /home/backup

二、将/home/backup下的全部文件所有打包并压缩到/tmp目录下名字为“5天之后的日期.tar.gz”

[root@localhost tmp]# tar czvf /tmp/$(date +%Y%m%d --date='5 days').tar.gz /home/backup/*
[root@localhost tmp]# date +%Y%m%d
20170604
[root@localhost tmp]# ll
total 653284
-rw-r--r--. 1 root root         0 May 20 21:33 1addf98dffa
-rw-r--r--. 1 root root         0 May 20 21:35 1ddf67dfdf
-rw-r--r--. 1 root root     71460 Jun  4 19:49 20170609.tar.gz

三、将/tmp下刚刚压缩的包解压到新建目录/tmp/test中,并打包成“当前的系统时间.tar”

[root@localhost backup]# mkdir test
[root@localhost backup]# tar xf 20170609.tar.gz -C test/
[root@localhost backup]# tar cvf ./$(date +%T).tar *
[root@localhost backup]# ls *.tar
19:56:09.tar

四、将/tmp/find.test文件追加到刚刚打好的tar包里

[root@localhost tmp]# cd /tmp
[root@localhost tmp]# touch find.test
[root@localhost tmp]# tar -r find.test -f /tmp/test/home/backup/19\:56\:09.tar
[root@localhost tmp]# tar tf /tmp/test/home/backup/19\:56\:09.tar
xcompose.conf
xim.conf
yum.conf
find.test
相关文章
相关标签/搜索