zip压缩工具,tar打包,打包并压缩

zip 压缩工具

  • 支持压缩目录

  • 安装:yum install -y zip

[root@localhost ddd]# tree 
.
├── 111
│   ├── 1.txt
│   └── 222
│       └── 333
│           └── 444
├── 1.txt
└── 2.txt.bz2.xz

4 directories, 3 files
[root@localhost ddd]# du -sh 111/
2.0M    111/
[root@localhost ddd]# yum install -y zip
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.hosteurope.de
 * extras: ftp.hosteurope.de
 * updates: ftp.hosteurope.de
正在解决依赖关系
--> 正在检查事务
---> 软件包 zip.x86_64.0.3.0-11.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

====================================================================================================================================================
 Package                         架构                               版本                                     源                                大小
====================================================================================================================================================
正在安装:
 zip                             x86_64                             3.0-11.el7                               base                             260 k

事务概要
====================================================================================================================================================
安装  1 软件包

总下载量:260 k
安装大小:796 k
Downloading packages:
zip-3.0-11.el7.x86_64.rpm                                                                                                    | 260 kB  00:00:04     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : zip-3.0-11.el7.x86_64                                                                                                           1/1 
  验证中      : zip-3.0-11.el7.x86_64                                                                                                           1/1 

已安装:
  zip.x86_64 0:3.0-11.el7                                                                                                                           

完毕!
  • 用法:zip 1.txt.zip 1.txt

[root@localhost ddd]# zip 1.txt.zip 1.txt 
  adding: 1.txt (deflated 74%)
[root@localhost ddd]# ls
111  1.txt  1.txt.zip  2.txt.bz2.xz
[root@localhost ddd]# du -sh 1.txt.zip 
524K    1.txt.zip
[root@localhost ddd]#
    • 压缩目录:zip -r

[root@localhost ddd]# zip -r 111.zip 111/
  adding: 111/ (stored 0%)
  adding: 111/222/ (stored 0%)
  adding: 111/222/333/ (stored 0%)
  adding: 111/222/333/444/ (stored 0%)
  adding: 111/1.txt (deflated 74%)
[root@localhost ddd]# ls
111  111.zip  1.txt  1.txt.zip  2.txt.bz2.xz
[root@localhost ddd]#

压缩完以后源文件不消失linux

  • 解压缩:unzip

没有先安装一下:yum install -y unzip数组

  • 解压到指定目录:unzip 1.txt.zip -d 目录/

[root@localhost ddd]# unzip 1.txt.zip -d /tmp/567/
Archive:  1.txt.zip
  inflating: /tmp/567/1.txt
[root@localhost ddd]# ls -sh /tmp/567/
总用量 2.0M
2.0M 1.txt
[root@localhost ddd]#
  • zip是不能直接查看内容

  • 只能查看文件列表:unzip -l 111.zip

[root@localhost ddd]# unzip -l 111.zip 
Archive:  111.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  08-25-2017 21:45   111/
        0  08-25-2017 21:43   111/222/
        0  08-25-2017 21:43   111/222/333/
        0  08-25-2017 21:43   111/222/333/444/
  2043960  08-25-2017 21:45   111/1.txt
---------                     -------
  2043960                     5 files
[root@localhost ddd]#

tar打包工具

  • 用法:

    • -x : 表示解压缩
    • -t : 查看tar包里的文件
    • -v : 表示可视化
    • -c : 创建一个tar包或者压缩文件
    • -f : 后面跟文件名(表示压缩或解压后的文件名是什么),要注意若是是多个参数组合的状况下,-f 参数写到最后面。
    • -z : 同时用gzip压缩
    • -j : 同时用bzip2压缩
    • -J: 同时用xz压缩
    • --exclude :打包时过滤文件

打包命令:tar -cvf aminglinux.tar 111/

[root@localhost ddd]# tar -cvf aminglinux.tar 111/
111/
111/222/
111/222/333/
111/222/333/444/
111/1.txt
[root@localhost ddd]# ls
111  111.zip  1.txt  1.txt.zip  2.txt.bz2.xz  aminglinux.tar

解包命令:tar -xvf aminglinux.tar 111/

[root@localhost ddd]# tar -xvf aminglinux.tar 111/
111/
111/222/
111/222/333/
111/222/333/444/
111/1.txt

解包直接覆盖原目录架构

  • 打包也能够打包目录,文件,目录和文件

  • 查看tar包:tar -tf aminglinux.tar

[root@localhost ddd]# tar -tf aminglinux.tar 
111/
111/222/
111/222/333/
111/222/333/444/
111/1.txt
[root@localhost ddd]#
  • 打包时过滤文件:--exclude

用法:tar -cvf tar包 --exclude 须要过滤的文件名 打包的文件ide

[root@localhost ddd]# tar -cvf  aminglinux.tar --exclude 1.txt  111/
111/
111/222/
111/222/333/
111/222/333/444/
[root@localhost ddd]#

还能够这样用工具

[root@localhost ddd]# tar -cvf  aminglinux.tar --exclude "*.txt"  111/
111/
111/222/
111/222/333/
111/222/333/444/
  • -z : 打包同时用gzip压缩

[root@localhost ddd]# tar -czvf aminglinux.tar.gz 1.txt 111
1.txt
111/
111/222/
111/222/333/
111/222/333/444/
111/1.txt
[root@localhost ddd]# ls
111  111.zip  1.txt  1.txt.zip  2.txt.bz2.xz  aminglinux.tar  aminglinux.tar.gz  --exclude
[root@localhost ddd]# du -sh aminglinux.tar.gz 
1.1M    aminglinux.tar.gz
[root@localhost ddd]# du -sh 111/ 1.txt
2.0M    111/
2.0M    1.txt
[root@localhost ddd]#
  • -j : 打包同时用bzip2压缩

[root@localhost ddd]# tar -cjvf aminglinux.tar.bz2 1.txt 111
1.txt
111/
111/222/
111/222/333/
111/222/333/444/
111/1.txt
[root@localhost ddd]# du -sh aminglinux.tar.bz2
424K    aminglinux.tar.bz2
[root@localhost ddd]#
  • -J: 同时用xz压缩

[root@localhost ddd]# tar -cJvf aminglinux.tar.xz 1.txt 111
1.txt
111/
111/222/
111/222/333/
111/222/333/444/
111/1.txt
[root@localhost ddd]# du -sh aminglinux.tar.xz
60K aminglinux.tar.xz
[root@localhost ddd]#