目录
1 压缩打包介绍
- Windows下打包后缀名常见有
.rar .zip .7z
- Linux打包后缀名常见有
.zip,.gz,.bz2,.xz,.tar.gz,.tar.bz2,.tar.xz
2 gzip压缩工具
- 新建一个文件,进行压缩
[root@worker1 ~]# find /etc/ -type f -exec cat {} > test.txt \;
[root@worker1 ~]# du -sh test.txt
20M test.txt
- 压缩
[root@worker1 ~]# du -sh test.txt
20M test.txt
[root@worker1 ~]# gzip test.txt
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt.gz
[root@worker1 ~]# du -sh test.txt.gz
6.9M test.txt.gz
- 解压
法1:
[root@worker1 ~]# gzip -d test.txt.gz
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt
法2:
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt.gz
[root@worker1 ~]# gunzip test.txt.gz
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt
- 指定压缩级别
gzip -9 test.txt 数值越大压缩效果越高范围1-9,默认是6
[root@worker1 ~]# du -sh test.txt
20M test.txt
[root@worker1 ~]# gzip -9 test.txt
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt.gz
[root@worker1 ~]# du -sh test.txt.gz
6.8M test.txt.gz
- 查看压缩文件内容
[root@worker1 ~]# zcat test.txt.gz | head
# Generated by LVM2 version 2.02.177(2)-RHEL7 (2018-01-22): Wed Sep 26 05:06:00 2018
contents = "Text Format Volume Group"
version = 1
description = "Created *before* executing 'vgcreate vg1 /dev/sdb1 /dev/sdb2'"
creation_host = "worker1" # Linux worker1 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64
creation_time = 1537909560 # Wed Sep 26 05:06:00 2018
- 指定压缩文件的路径,并且保留源文件
[root@worker1 ~]# gzip -c test.txt > ./test.txt.gz
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt test.txt.gz
- 指定解压文件的路径,并且保留压缩文件
[root@worker1 ~]# gunzip -c test.txt.gz > /tmp/test.txt.new
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt test.txt.gz
[root@worker1 ~]# ls /tmp/
dir1 dir2 lesson8 lesson9 passwd passwd.conf passwd.sh passwd.txt test.txt.new
- gzip不能压缩目录
3 bzip2压缩工具
- 安装bzip2工具包
[root@worker1 ~]# yum install bzip2
- 压缩
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt test.txt.gz
[root@worker1 ~]# du -sh test.txt
20M test.txt
[root@worker1 ~]# du -sh test.txt.gz
6.9M test.txt.gz
[root@worker1 ~]# bzip2 test.txt
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt.bz2 test.txt.gz
[root@worker1 ~]# du -sh test.txt.bz2
6.0M test.txt.bz2
- 解压
法1:
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt.bz2 test.txt.gz
[root@worker1 ~]# bzip2 -d test.txt.bz2
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt test.txt.gz
法2:
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt.bz2 test.txt.gz
[root@worker1 ~]# bunzip2 test.txt.bz2
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt test.txt.gz
- 指定压缩级别
bzip2 -6 test.txt 数值越大压缩效果越高范围1-9,默认是9
[root@worker1 ~]# bzip2 test.txt
[root@worker1 ~]# du -sh test.txt.bz2
6.0M test.txt.bz2
[root@worker1 ~]# bzip2 -6 test.txt
[root@worker1 ~]# du -sh test.txt.bz2
6.1M test.txt.bz2
- 如果把压缩的后缀更改了,使用file命令查看原文件的压缩文件格式
[root@worker1 ~]# mv test.txt.bz2 1.txt
[root@worker1 ~]# ls
1.txt anaconda-ks.cfg dir1 test.txt.gz
[root@worker1 ~]# file 1.txt
1.txt: bzip2 compressed data, block size = 600k
[root@worker1 ~]# mv 1.txt test.txt.bz
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt.bz test.txt.gz
[root@worker1 ~]# du -sh test.txt.bz
6.1M test.txt.bz
- 查看压缩文件内容
[root@worker1 ~]# bzcat test.txt.bz | head
# Generated by LVM2 version 2.02.177(2)-RHEL7 (2018-01-22): Wed Sep 26 05:06:00 2018
contents = "Text Format Volume Group"
version = 1
description = "Created *before* executing 'vgcreate vg1 /dev/sdb1 /dev/sdb2'"
creation_host = "worker1" # Linux worker1 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64
creation_time = 1537909560 # Wed Sep 26 05:06:00 2018
- 指定压缩文件的路径,并且保留源文件
[root@worker1 ~]# bzip2 -c test.txt > ./test.txt.bz2
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt test.txt.bz2 test.txt.gz
[root@worker1 ~]# du -sh test.txt.bz2
6.0M test.txt.bz2
- 指定解压文件的路径,并且保留源文件
[root@worker1 ~]# bzip2 -c -d test.txt.bz2 > /tmp/test.txt.new2
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt test.txt.bz2 test.txt.gz
[root@worker1 ~]# ls /tmp/
dir1 lesson8 passwd passwd.sh test.txt.new
dir2 lesson9 passwd.conf passwd.txt test.txt.new2
[root@worker1 ~]# du -sh /tmp/test.txt.new2
20M /tmp/test.txt.new2
- bzip2不能压缩目录
4 xz压缩工具
- 压缩
[root@worker1 ~]# du -sh test.txt
20M test.txt
[root@worker1 ~]# xz test.txt
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt.bz2 test.txt.gz test.txt.xz
[root@worker1 ~]# du -sh test.txt.xz
5.0M test.txt.xz
[root@worker1 ~]# du -sh test.txt.bz2
6.0M test.txt.bz2
[root@worker1 ~]# du -sh test.txt.gz
6.9M test.txt.gz
- 解压
法1:
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt.bz2 test.txt.gz test.txt.xz
[root@worker1 ~]# xz -d test.txt.xz
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt test.txt.bz2 test.txt.gz
法2:
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt.bz2 test.txt.gz test.txt.xz
[root@worker1 ~]# unxz test.txt.xz
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt test.txt.bz2 test.txt.gz
- 指定压缩级别
- xz -6 test.txt 数值越大压缩效果越高范围1-9,默认是9
[root@worker1 ~]# xz test.txt
[root@worker1 ~]# du -sh test.txt.xz
5.0M test.txt.xz
[root@worker1 ~]# xz -6 test.txt
[root@worker1 ~]# du -sh test.txt.xz
5.0M test.txt.xz
- 查看压缩文件内容
[root@worker1 ~]# xzcat test.txt.xz | head
# Generated by LVM2 version 2.02.177(2)-RHEL7 (2018-01-22): Wed Sep 26 05:06:00 2018
contents = "Text Format Volume Group"
version = 1
description = "Created *before* executing 'vgcreate vg1 /dev/sdb1 /dev/sdb2'"
creation_host = "worker1" # Linux worker1 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64
creation_time = 1537909560 # Wed Sep 26 05:06:00 2018
- 指定压缩文件的路径,并且保留源文件
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt test.txt.bz2 test.txt.gz
[root@worker1 ~]# xz -c test.txt > ./test.txt.xz
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt test.txt.bz2 test.txt.gz test.txt.xz
[root@worker1 ~]# du -sh test.txt.xz
5.0M test.txt.xz
- 指定解压文件的路径
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt.bz2 test.txt.gz test.txt.xz
[root@worker1 ~]# xz -d -c test.txt.xz > ./test.txt
[root@worker1 ~]# ls
anaconda-ks.cfg dir1 test.txt test.txt.bz2 test.txt.gz test.txt.xz
[root@worker1 ~]# du -sh test.txt
20M test.txt
[root@worker1 ~]# du -sh test.txt.xz
5.0M test.txt.xz
- xz不能压缩目录