压缩打包介绍/gzip压缩工具/bzip2压缩工具/xz压缩工具

  • 6.1 压缩打包介绍 
  • 6.2 gzip压缩工具
  • 6.3 bzip2压缩工具
  • 6.4 xz压缩工具

常见的压缩文件格式

windows     .rar  .zip  .7z

linux         .zip,.gz,.bz2,.xz,.tar.gz,.tar.bz2,.tar.xz

 

 

  • gzip压缩工具

gzip  1.txt

gzip -d 1.txt.gz  /unzip 1.txt.gz

gzip -#1.txt  //#范围1-9默认6

不能解压缩目录

zcat 1.txt.gz

gzip -c 1.txt > /root/1.txt.gz

gunzip -c /root/1.txt.gz  > /tmp/1.txt.new

 

 

 

先创建一个大的文件1.txt,将文件.conf 的 追加到1.txt文件

[root@localhost d6z]# find /etc/ -type f -name "*conf"  -exec cat {} >> 1.txt ;

 

 

查看字符个数

wc 1.txt

[root@localhost d6z]# wc 1.txt 

  38280  154812 1419540 1.txt

 

用gzip 压缩1.txt

gzip 1.txt

[root@localhost d6z]# gzip 1.txt 

 

[root@localhost d6z]# ls -lh

总用量 340K

-rw-r--r--. 1 root root 340K 12月 18 21:07 1.txt.gz

 

查看1.txt.gz大小

[root@localhost d6z]# du -sh 1.txt.gz 

340K 1.txt.gz

 

解压文件

gzip  -d 1.txt .gz

[root@localhost d6z]# gzip -d 1.txt.gz 

[root@localhost d6z]# du -sh 1.txt 

1.4M 1.txt

 

 

查看多少行,多少字符个数

[root@localhost d6z]# wc 1.txt 

  38280  154812 1419540 1.txt

 

 压缩级别:1到9;

1级别压缩率最低,9压缩率最高,压缩级别可以保持默认

gzip -1 1.txt

[root@localhost d6z]# gzip -1 1.txt 

[root@localhost d6z]# du -sh 1.txt.gz 

396K 1.txt.gz

也可以用gunzip解压.gz

gunzip 1.txt.gz

[root@localhost d6z]# gunzip 1.txt.gz 

[root@localhost d6z]# du -sh 1.txt 

1.4M 1.txt

 

压缩到9级别 gzip -9 1.txt

[root@localhost d6z]# gzip -9 1.txt 

[root@localhost d6z]# du -sh 1.txt.gz 

340K 1.txt.gz

 

查看压缩文件信息

file 1.txt.gz

[root@localhost d6z]# file 1.txt.gz 

1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Mon Dec 18 21:07:40 2017, max compression

 

查看压缩文件的内容

zcat 1.txt.gz 先解压后cat

gzip 不能压缩目录

 

 

gzip -c  1.txt > /tmp/1.txt.gz   解压文件,并保存压缩后的文件.gz 到一个地方;其中(“>”表示重定向,-c表示解压后,并保存压缩文件)

 

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

[root@localhost d6z]# ls

1.txt

[root@localhost d6z]# ls /tmp/1.txt.gz 

/tmp/1.txt.gz

[root@localhost d6z]# file !$

file /tmp/1.txt.gz

/tmp/1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Mon Dec 18 21:07:40 2017

 

同样解压缩也是可以的。解压文件并保存1.txt.gz 到/tmp/d6z/2.txt

gzip -d  -c /tmp/1.xtx.gz > /tmp/d6z/2.txt

 

[root@localhost d6z]# gunzip -d -c  /tmp/1.txt.gz > /tmp/2.txt

[root@localhost d6z]# ls /tmp/2.txt 

/tmp/2.txt

 bzip2压缩工具 

复习gzip:

gzip 1.txt

gzip -d 1.txt.gz /unzip 1.txt.gz

gzip -# 1.txt  //# 表示范围 1-9,默认6

不能压缩目录

zcat 1.txt.gz //查看压缩包里面的内容,意思是先解压后cat

gzip -c 1.txt >  /root/1.txt.gz

gunzip -c /root/1.txt.gz >/tmp/1.txt.new  //其中 >号表示重定向

 

 

bzip2压缩能力比gzip 强

[root@localhost tmp]# du -sh 1.txt 

2.8M 1.txt

压缩:bzip2     1.txt

du -sh 1.txt.bz2

[root@localhost tmp]# du -sh 1.txt.bz2 

136K 1.txt.bz2

 

解压:

[root@localhost tmp]# bunzip2 1.txt.bz2 

[root@localhost tmp]# bzip2 -d 1.txt.bz2 

不支持压缩目录

用-c 指定压缩到目录:将1.txt 压缩到/tmp目录下

[root@localhost ~]# bzip2 -c /tmp/1.txt > /tmp/1.txt.bz2 

将压缩包指定解压到目录:

[root@localhost ~]# bzip2 -d -c /tmp/1.txt.bz2  > 2.txt

[root@localhost ~]# ls

2.txt  anaconda-ks.cfg

 

 

bzip2 默认压缩级别为9,也不需要指定级别

[root@localhost ~]# du -sh

140K .

[root@localhost ~]# bzip2 -9 2.txt 

[root@localhost ~]# du -sh

36K .

file查看文件的格式

[root@localhost ~]# file 2.txt.bz2 

2.txt.bz2: bzip2 compressed data, block size = 900k

 

有时候把压缩文件重名了.txt

例如:

[root@localhost ~]# mv 2.txt.bz2 2.txt

这时候可以用file命令查看文件类型

[root@localhost ~]# file 2.txt 

2.txt: bzip2 compressed data, block size = 900k

 

bzcat也可以查看文件内容:(就是先解压后cat查看文件内容)

bzcat 1.txt.bz2

 

 

  • xz压缩

[root@localhost ~]# du -sh

144K .

[root@localhost ~]# xz 2.txt 

[root@localhost ~]# du -sh

40K .

xz 默认压缩级别为9,最高。

压缩率:

xz>bzip2>gzip

解压:

xz -d 2.txt.xz

或者 unxz 2.txt.xz

 

指定压缩文件到某个目录

xz  -c   2.txt  > /tmp/2.txt.xz

指定解压文件到某个目录

xz  -d  -c  /tmp/2.txt.xz  >  /root/3.txt

也可以查看压缩文件的内容

xzcat  /tmp/2.txt.xz

 

 

 

 

原文地址:https://www.cnblogs.com/zhaocundang/p/8207115.html