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

压缩工具

在Windows系统中,咱们能够将目录和文件压缩,从而下降磁盘的使用量和网络资源的使用量。一样的在Linux系统中咱们同样能够把文件和目录压缩达到咱们下降网络资源和磁盘资源的使用量。在Linux系统中经常使用的压缩工具备:网络

  • gzip(能压缩文件,不能压缩目录)
  • zip
  • bz2
  • tar
  • xz

gizp 压缩工具

gzip压缩工具能够压缩文件,格式为gzip filename ,gzip有1-9九个压缩级别,默认级别是6。为了试验效果咱们先在/tmp下生成一个比较大的文件,可使用工具

find /etc/ -type f -name "*conf" -exec cat {} >> /tmp/1.txt ;ui

这条命令的意思是,找到/etc/下名字以conf结尾的文件 并把内容输出到/tmp/1.txt中。this

[root@localhost tmp]# find /etc/ -type f -name "*conf"  -exec cat {} >> /tmp/1.txt \;
[root@localhost tmp]# du -h 1.txt
15M	1.txt

而后使用gzip 压缩文件,而后查看它压缩先后的对比插件

[root@localhost tmp]# gzip 1.txt 
[root@localhost tmp]# du -h 1.txt.gz 
2.0M	1.txt.gz

发现压缩前是15M,压缩以后变成了2M,文件名变为了1.txt.gz ,这个后缀名是gzip工具附加给文件的,虽然没有实际意义,可是为了方便查找和管理你们都在遵循这个规则。当文件被压缩成压缩文件以后咱们没办法直接使用cat、more等命令查看它的内容,可是咱们可使用zcat 、zmore 等命令查看它的内容,还可使用wc -l查看文件有多少行,以及file命令来查看压缩文件的信息等。code

[root@localhost tmp]# wc 1.txt.gz 
   7235   41587 2009225 1.txt.gz
[root@localhost tmp]# wc -l 1.txt.gz     wc -l命令能够查看文件有多少行
7235 1.txt.gz
[root@localhost tmp]# zmore 1.txt.gz     zmore命令能够查看文件内容
------> 1.txt.gz <------
# This is an example configuration file for the LVM2 system.
# It contains the default settings that would be used if there was no
# /etc/lvm/lvm.conf file.
#
# Refer to 'man lvm.conf' for further information including the file layout.
#
# Refer to 'man lvm.conf' for information about how settings configured in
# this file are combined with built-in values and command line options to


[root@localhost tmp]# file 1.txt.gz      file命令能够查看文件信息
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Sat Jun 23 00:28:55 2018

当咱们直接使用gzip filename 时,会把文件压缩而且不会保存原文件,若是想要保存原文件而后生成一个新的压缩文件时,可使用 gzip -c 命令orm

[root@localhost tmp]# ls
1.txt  222  2.txt  form154853.pdf
[root@localhost tmp]# gzip -c 1.txt > 1.txt.gz  使用这个参数后面必须跟压缩后文件的文件名,不然会报错
[root@localhost tmp]# ls
1.txt  1.txt.gz  222  2.txt  form154853.pdf
[root@localhost tmp]# gzip -c 1.txt > 222/1.txt.gz 不但能够保留原文件,还能够给新生成的压缩文件制定目录
[root@localhost tmp]# tree /tmp
/tmp
├── 1.txt
├── 1.txt.gz
├── 222
│   └── 1.txt.gz
├── 2.txt
└── form154853.pdf

1 directory, 5 files

gzip -d = gunzip解压缩

能压缩确定能解压缩,gzip压缩的文件须要使用gunzip或gzip -d来解压缩 gunzip的使用方法跟gzip同样,能够不保留压缩文件,加-c选项能够保留压缩文件ip

[root@localhost tmp]# gzip -d -c 1.txt.gz > 3.txt
[root@localhost tmp]# ls
1.txt  1.txt.gz  222  2.txt  3.txt  form154853.pdf
[root@localhost tmp]# wc -l 3.txt 
189156 3.txt
[root@localhost tmp]# wc -l 1.txt
189156 1.txt
[root@localhost tmp]# gunzip -c 1.txt.gz > 222/11.txt
[root@localhost tmp]# tree /tmp
/tmp
├── 1.txt.gz
├── 222
│   ├── 11.txt
│   ├── 1.txt
│   └── 1.txt.gz
├── 2.txt
├── 3.txt
└── form154853.pdf
[root@localhost tmp]# gzip 222
gzip: 222 is a directory -- ignored

在这里咱们也能够看到,gzip是不支持压缩目录的。资源

bzip压缩工具

bzip2 命令的格式为bzip2 [-d / -z]_ filename_,它只有压缩-d和解压-z两个经常使用选项在压缩是能够不加- z 选项。压缩级别一样是1~9 ,默认的压缩级别是9,bzip2 命令也一样不能压缩目录,在压缩目录是会报错。若是系统没有安装这个工具可使用 yum install -y bzip2来安装it

[root@localhost tmp]# yum install -y bzip2
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
软件包 bzip2-1.0.6-13.el7.x86_64 已安装而且是最新版本
无须任何处理
[root@localhost tmp]# bzip2 1.txt 使用bzip2来压缩1.txt
[root@localhost tmp]# ls        能够看到源文件1.txt也消失了
1.txt.bz2  222  2.txt  3.txt  form154853.pdf
[root@localhost tmp]# du -h 1.txt.bz2  bzip2压缩的比较紧密,发现文件毕1.txt.gz更小
660K	1.txt.bz2
[root@localhost tmp]# bzip2 -d  1.txt.bz2  解压缩
[root@localhost tmp]# ls
1.txt  222  2.txt  3.txt  form154853.pdf
[root@localhost tmp]# du -h 1.txt 
7.2M	1.txt
[root@localhost tmp]# bzip2 -c 1.txt > 1.txt.bz2 bzip2也可使用-c选项来保留源文件
[root@localhost tmp]# ls
1.txt  1.txt.bz2  222  2.txt  3.txt  form154853.pdf
[root@localhost tmp]# bzmore 1.txt.bz2 
------> 1.txt.bz2 <------
# This is an example configuration file for the LVM2 system.
# It contains the default settings that would be used if there was no
# /etc/lvm/lvm.conf file.
#
# Refer to 'man lvm.conf' for further information including the file layout.
#
# Refer to 'man lvm.conf' for information about how settings configured in
# this file are combined with built-in values and command line options to
# arrive at the final values used by LVM.
#
# Refer to 'man lvmconfig' for information about displaying the built-in
# and configured values used by LVM.
#
# If a default value is set in this file (not commented out), then a
# new version of LVM using this file will continue using that value,
# even if the new version of LVM changes the built-in default value.

查看文件内容可使用bzmore ,bzcat等命令。

xz压缩工具

xz命令跟bzip2用法几乎同样,它压缩文件更加的紧密,占用CPU资源也更多

[root@localhost tmp]# ls
1.txt  1.txt.bz2  222  2.txt  3.txt  form154853.pdf
[root@localhost tmp]# xz 1.txt
[root@localhost tmp]# ls
1.txt.bz2  1.txt.xz  222  2.txt  3.txt  form154853.pdf
[root@localhost tmp]# du -h 1.txt.xz 
56K	1.txt.xz    能够看到压缩文件只有56K

使用xz -d 命令或unxz 来解压缩,一样不能压缩目录。 它也可使用-c的参数来保留原文件

[root@localhost tmp]# unxz 1.txt.xz > 44.txt
[root@localhost tmp]# ls
1.txt  1.txt.bz2  222  2.txt  3.txt  44.txt  form154853.pdf
[root@localhost tmp]# xz -c 44.txt > 222/333.txt  使用-c压缩44.txt会保留源文件
[root@localhost tmp]# tree /tmp
/tmp
├── 1.txt
├── 1.txt.bz2
├── 222
│   ├── 11.txt
│   ├── 1.txt
│   ├── 1.txt.gz
│   └── 333.txt
├── 2.txt
├── 3.txt
├── 44.txt
└── form154853.pdf

1 directory, 10 files
[root@localhost tmp]# xz 222
xz: 222: Is a directory, skipping     压缩目录会报错
相关文章
相关标签/搜索