压缩打包介绍,gzip压缩工具,bzip2压缩工具,xz压缩工具zip压缩工具,tar打包,打包并压缩...

常见压缩文件

  • windows :.rar     .zip    .7z

  • Linux : .zip   .gz    .bz2    .xz    .tar.gz    .tar.bz2    .tar.xz

压缩的优点:节省硬盘空间,方便传输,节省带宽,因为服务器上的硬盘与带宽成本要比家用高很多。


gzip压缩工具

  • gzip 不能压缩目录

先创建一个比较大的文件

[root@localhost tmp]# mkdir ddd
[root@localhost tmp]# cd ddd/
[root@localhost ddd]# find /etc/ -type f -name "*conf"
/etc/resolv.conf
/etc/pki/ca-trust/ca-legacy.conf
/etc/yum/pluginconf.d/fastestmirror.conf
/etc/yum/pluginconf.d/langpacks.conf
/etc/yum/protected.d/systemd.conf
/etc/yum/version-groups.conf
/etc/rdma/mlx4.conf
/etc/rdma/rdma.conf
/etc/asound.conf
/etc/kdump.conf
/etc/tuned/tuned-main.conf
/etc/audisp/audispd.conf
/etc/audisp/plugins.d/af_unix.conf
/etc/audisp/plugins.d/syslog.conf
/etc/audit/auditd.conf
/etc/chrony.conf
/etc/man_db.conf
/etc/sudo-ldap.conf
/etc/sudo.conf
/etc/e2fsck.conf
/etc/mke2fs.conf
/etc/vconsole.conf
/etc/locale.conf
[root@localhost ddd]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost ddd]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost ddd]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost ddd]# du -sh 1.txt
704K	1.txt
[root@localhost ddd]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost ddd]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost ddd]# du -sh 1.txt
1.2M	1.txt
[root@localhost ddd]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost ddd]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost ddd]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost ddd]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost ddd]# du -sh 1.txt
2.2M	1.txt
[root@localhost ddd]# 

这个增加的空间不是很正常,有很多虚的空间实际上看多少行。不然压缩在解压缩后大小和原来会不一样。

[root@localhost ddd]# du -sh 1.txt 
2.0M	1.txt
[root@localhost ddd]# wc -l 1.txt 
51576 1.txt
[root@aminglinux-01 ddd]# ls -lh 1.txt 
-rw-r--r--. 1 root root 1.3M 5月  28 10:13 1.txt

  • 然后打包一下:gzip

压缩之后,发现1.txt不见了,变成了1.txt.gz

[root@localhost ddd]# gzip 1.txt 
[root@localhost ddd]# ls
1.txt.gz
[root@localhost ddd]# du -sh 1.txt.gz 
524K	1.txt.gz

压缩后看看大小只有524kb,之前是2M多。

  • 解压gzip包 :gzip -d 1.txt.gz

gunzip 命令也是可以解压缩的。

[root@localhost ddd]# gzip -d 1.txt.gz 
[root@localhost ddd]# ls
1.txt
[root@localhost ddd]# du -sh 1.txt 
2.0M	1.txt
  • gzip -1~9 压缩有9个级别,9级别压缩包最小,1级别压缩包大。默认是6级别。压缩包压缩到最小后,级别在高,压缩包也不会变得更小了。

[root@localhost ddd]# gzip -d 1.txt.gz 
[root@localhost ddd]# ls
1.txt
[root@localhost ddd]# du -sh 1.txt 
2.0M	1.txt
[root@localhost ddd]# gzip -1 1.txt 
[root@localhost ddd]# du -sh 1.txt.gz 
620K	1.txt.gz
[root@localhost ddd]# gzip -d 1.txt.gz 
[root@localhost ddd]# gzip -9 1.txt
[root@localhost ddd]# du -sh 1.txt.gz 
524K	1.txt.gz

可以看到压缩级别越高压缩后的文件越小。但同时级别越高对cpu的压力越大。时间也会长。

  • 查看压缩文件属性 file

[root@localhost ddd]# file 1.txt.gz 
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: (最后更改时间)Thu Aug 24 20:07:13 2017, max compression(最大的压缩比)

  • 查看压缩文件内容 zcat

zcat 1.txt.gz
先解压后查看

  • 压缩成自定义文件名的压缩包并且源文件不消失 -c

[root@localhost ddd]# gzip -c 1.txt > /tmp/1.txt.gz
[root@localhost ddd]# ls
1.txt
[root@localhost ddd]# ls /tmp/1.txt.gz 
/tmp/1.txt.gz

  • 解压成自定义文件名的文件并且源压缩包不消失 -d -c

[root@localhost ddd]# gzip -d -c /tmp/1.txt.gz > /tmp/ddd/2.txt
[root@localhost ddd]# ls
1.txt  2.txt


bzip2 压缩工具

压缩率要比gzip更狠一些,使用的不同算法。

  • 也不支持压缩目录

  • 用法:bzip2 1.txt

没有的话需要安装一下:yum install -y bzip2

[root@localhost ddd]# ls
1.txt  2.txt
[root@localhost ddd]# ls -sh
总用量 4.0M
2.0M 1.txt  2.0M 2.txt
[root@localhost ddd]# gzip 1.txt 
[root@localhost ddd]# bzip2 2.txt 
[root@localhost ddd]# ls -sh
总用量 744K
524K 1.txt.gz  220K 2.txt.bz2
[root@localhost ddd]# 

明显bzip2压缩的要更小一些

  • 解压: bzip2 -d 或者 bunzip2

  • 也支持 -c : bzip2 -c 1.txt > /tmp/1.txt.bz2

  • 解压用法同gzip

  • 级别也是9个级别。默认压缩级别是9

  • file也可以查看

  • bzcat 查看压缩包具体内容


xz压缩工具

  • 同样不支持压缩目录

  • 用法基本相同。用的不多。

支持 -c -d选项。

  • 压缩更小,比bzip2还小。

[root@aminglinux-01 ddd]# xz 1.txt 
[root@aminglinux-01 ddd]# ls
1.txt.xz
[root@aminglinux-01 ddd]# du 1.txt.xz 
64	1.txt.xz

同样是1~9的级别,默认6级别。  


  • 支持压缩目录

之前的几个压缩工具都不支持目录压缩,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]# 


压缩完之后源文件不消失

  • 解压缩: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 需要过滤的文件名 打包的文件

[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]# 

转载于:https://my.oschina.net/u/3852961/blog/1819611

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值