Linux文件压缩和打包

6.5、zip压缩工具

  • gzip、bzip2、xz都不支持压缩目录,zip支持压缩目录
  • 安装工具可以解压windows下的rar文件
        1. [root@centos7-01 d6z]# cp -r /root/111 .
	2. [root@centos7-01 d6z]# ls
	3. 111  1.txt  2.txt  3.txt  4.txt  6.txt
	4. [root@centos7-01 d6z]# du -sh 111/
	5. 2.0M    111/

  • 安装zip工具,yum install -y zip
  • 压缩文件:zip 2.txt.zip 2.txt
        1. [root@centos7-01 d6z]# zip 2.txt.zip 2.txt
	2.   adding: 2.txt (deflated 74%)
	3. [root@centos7-01 d6z]# ls
	4. 111  1.txt  2.txt  2.txt.zip  3.txt  4.txt  6.txt
	5. [root@centos7-01 d6z]# du -sh 2.txt.zip
	6. 528K    2.txt.zip


  • 压缩目录:zip -r 111.zip 3.txt 111 把目录111和文件3.txt同时压缩至111.txt
        1. [root@centos7-01 d6z]# zip -r 111.zip 3.txt 111
	2.   adding: 3.txt (deflated 74%)
	3.   adding: 111/ (stored 0%)
	4.   adding: 111/123/ (stored 0%)
	5.   adding: 111/123/234/ (stored 0%)
	6.   adding: 111/123/234/6.txt (deflated 74%)
	7. [root@centos7-01 d6z]#
	8. [root@centos7-01 d6z]# du -sh 111.zip
	9. 1.1M    111.zip
	10. [root@centos7-01 d6z]# du -sh 111
	11. 2.0M    111
	12. [root@centos7-01 d6z]#
  • zip压缩的特点是压缩之后不删除原文件
  • zip解压缩命令:unzip,需安装unzip工具:yum install -y unzip
        1. [root@centos7-01 d6z]# unzip 111.zip
	2. Archive:  111.zip
	3. replace 3.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
	4. replace 111/123/234/6.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
	5.   inflating: 111/123/234/6.txt       
	6. [root@centos7-01 d6z]# ls
	7. 111  111.zip  1.txt  2.txt  2.txt.zip  3.txt  4.txt  6.txt
	8. [root@centos7-01 d6z]#

  • unzip支持解压到指定目录下
        1. [root@centos7-01 d6z]# mkdir test
	2. [root@centos7-01 d6z]# unzip 2.txt.zip  -d test/
	3. Archive:  2.txt.zip
	4.   inflating: test/2.txt              
	5. [root@centos7-01 d6z]#
  • unzip时,压缩前的文件名不能被改动
        1. [root@centos7-01 d6z]# ls
	2. 111  111.zip  1.txt  2.txt  2.txt.zip  3.txt  3.txt.zip  4.txt  6.txt  test
	3. [root@centos7-01 d6z]# unzip 3.txt.zip -d test/33.txt
	4. Archive:  3.txt.zip
	5.   inflating: test/33.txt/3.txt       
	6. [root@centos7-01 d6z]# ls test/
	7. 2.txt  33.txt
	8. [root@centos7-01 d6z]# tree test/
	9. test/
	10. ├── 2.txt
	11. └── 33.txt
	12.     └── 3.txt
	13. 1 directory, 2 files
	14. [root@centos7-01 d6z]#

  • zip压缩后不能查看其内容,但是可以查看文件列表
  • 查看文件列表命令:unzip -l 111.zip
        1. [root@centos7-01 d6z]# unzip -l 111.zip
	2. Archive:  111.zip
	3.   Length      Date    Time    Name
	4. ---------  ---------- -----   ----
	5.   2048256  11-09-2017 23:53   3.txt
	6.         0  11-10-2017 00:30   111/
	7.         0  11-10-2017 00:30   111/123/
	8.         0  11-10-2017 00:38   111/123/234/
	9.   2048256  11-10-2017 00:31   111/123/234/6.txt
	10. ---------                     -------
	11.   4096512                     5 files
	12. [root@centos7-01 d6z]#

6.6、tar打包

  • 便于大文件传输,节省带宽资源
  • tar -cvf liyang.tar liyang/ 参数: -c创建,-v可视化,-f文件名
        1. [root@centos7 ~]# tar -cvf liyang.tar liyang/
	2. liyang/
	3. liyang/1.txt
	4. liyang/2.txt
	5. [root@centos7 ~]# ls
	6. 13  1.sh  1.txt  23  2.txt  2.txt.bak  anaconda-ks.cfg  liyang  liyang.tar  name.sh
  • tar支持重复打包

  • 解包tar -xvf liyang.tar,tar解包后不会提示是否覆盖源文件,直接覆盖

        1. [root@centos7 ~]# tar -xvf liyang.tar 
	2. liyang/
	3. liyang/1.txt
	4. liyang/2.txt
	5. [root@centos7 ~]# ls
	6. 13  1.sh  1.txt  23  2.txt  2.txt.bak  anaconda-ks.cfg  liyang  liyang.tar  name.sh
	7. [root@centos7 ~]# 


  • tar支持文件和目录同时打包
  • tar -tf liyang.tar 参数:-tf查看包;
        1. [root@centos7 ~]# tar -tf liyang.tar 
	2. liyang/
	3. liyang/1.txt
	4. liyang/2.txt
	5. [root@centos7 ~]# 

  • tar可以过滤指定的文件或者目录
  • tar -cvf liyang.tar --exclude name.sh liyang/
  • 参数:--exclude后面跟指定过滤的文件或者目录
        1. [root@centos7 ~]# tar -cvf 123.tar --exclude name.sh liyang/
	2. liyang/
	3. liyang/1.txt
	4. liyang/2.txt
	5. liyang/2.txt.bak
	6. liyang/1.sh
	7. [root@centos7 ~]# tar -tf 123.tar 
	8. liyang/
	9. liyang/1.txt
	10. liyang/2.txt
	11. liyang/2.txt.bak
	12. liyang/1.sh
	13. [root@centos7 ~]# ls liyang/
	14. 1.sh  1.txt  2.txt  2.txt.bak  name.sh


6.7、打包并压缩

  • 打包的同压缩文件
  • tar -czvf test.tar.gz liyang/ 参数:z表示压缩类型为gz
  • tar -cjzf test.tar.bz2 liyang/ 参数:j表示压缩类型为bz2
  • tar -cJzf test.tar.bz2 liyang/ 参数:J表示压缩类型为xz
  • 解包把参数c改为x即可
        1. [root@centos7 ~]# tar -czvf test.tar.gz liyang/
	2. liyang/
	3. liyang/1.txt
	4. liyang/2.txt
	5. liyang/2.txt.bak
	6. liyang/1.sh
	7. liyang/name.sh
	8. [root@centos7 ~]# du -sh test.tar.gz 
	9. 2.9M     test.tar.gz
	10. [root@centos7 ~]# du -sh liyang/
	11. 16M     liyang/
	12. [root@centos7 ~]# 

  • 查看包和压缩同样使用-tf参数

扩展学习

  • 利用tar通过网络拷贝数据

  • cd /data // data目录下有我们要拷贝的目标文件目录 test

  • tar cvf - test| ssh 10.0.1.11 "cd /copy1/; tar xvf -"

  • 首先将要拷贝的目录test打包,"-" 代表标准输出,

  • 然后再ssh 到目标主机 10.0.1.11 ,运行相应的命令。

  • 其中tar xvf - 意思是,将前面的标准输出内容作为解包的对象。

  • 目的就是想拷贝的目录先打包,然后将打包好的文件拷贝到目标主机最后在目标主机上解包

  • 此处用一条命令实现了边打包边解包的过程

  • 把打包压缩的文件解压到指定目录下

  • 加参数-C后面跟指定目录即可

        1. [root@centos7 ~]# tar -xzvf test.tar.gz -C /tmp/
	2. liyang/
	3. liyang/1.txt
	4. liyang/2.txt
	5. liyang/2.txt.bak
	6. liyang/1.sh
	7. liyang/name.sh
	8. [root@centos7 ~]# ls /tmp/
	9. 123.txt  systemd-private-fb8f316ca79d40a9883bdbf646f85322-cups.service-zSGy4h
	10. 2.txt    systemd-private-fb8f316ca79d40a9883bdbf646f85322-httpd.service-tUPjmt
	11. liyang
	12. [root@centos7 ~]# ls /tmp/liyang/
	13. 1.sh  1.txt  2.txt  2.txt.bak  name.sh
	14. [root@centos7 ~]# 

Linux下不支持解压大于4G的文件解决方法

            * wget -c http://packages.sw.be/p7zip/p7zip-9.13-1.el5.rf.i386.rpm 
	    * wget -c http://packages.sw.be/p7zip/p7zip-plugins-9.13-1.el5.rf.i386.rpm 
  • 下载完后安装:
      * rpm -ivh p7zip-9.13-1.el5.rf.i386.rpm 
      * rpm -ivh p7zip-plugins-9.13-1.el5.rf.i386.rpm 

  • 解压命令:
  • 7z x 123.zip

转载于:https://my.oschina.net/u/3706694/blog/1572471

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值