linux中打包的作用,Linux基础知识:压缩与打包

我们日常使用Window的时候,经常会用到压缩与解压缩,如果要压缩一个文件,右击选择【添加到压缩文件】,解压缩则右击选择【解压到当前文件夹】,“点点点”就能完成。但是在一个没有装图形化界面的Linux操作系统又不能使用“点点点”,那该怎么操作呢?本文就Linux中如何使用压缩和打包工具做出解释。

为什么要压缩文件

压缩的目的是为了就是将文件通过压缩算法转变成一个体积更小格式的文件,减小了文件在硬盘上的占用空间,压缩文件的时候,特别的消耗CPU的时钟周期,因为CPU要进行大量的计算,所有压缩也是一种拿时间换空间的操作,同时也能使文件能够通过较慢的网络连接来实现更快的传输。

常用的压缩工具

在linux操作系统中提供了很多的压缩和解压缩工具,每个压缩工具在执行压缩的时候所用的算法是不一样的,设计越优良的算法,压缩的程度就越高。比较老的压缩工具有compress(现在已经不常用了),常用的压缩工具有:gzip、bzip、xz和zip。我们可以通过后缀名来区分压缩文件是被什么工具压缩的,例如如果使用compress压缩文件,得到的文件的后缀名是.z,其他的压缩的后缀名如下:

gzip、gunzip和zcat

gzip是最常用的压缩工具了,gunzip是对应的解压缩工具。zcat可以在不解压.gz格式的压缩文件的情况下查看文件的内容。

语法:gzip[OPTION]... FILE...

常用选项:-d:解压缩,相当于gunzip;-#:指定压缩比,默认是6,数字越大压缩比越大(1-9),压缩比=压缩前文件大小/压缩后文件的大小;-c:将压缩结果输出至标准输出。 gzip -c file > /path/to/somefile.gz

举例:

将/etc/init.d/functions复制到tmp目录下执行gzip压缩:

[root@localhost tmp]# cp /etc/init.d/functions /tmp/[root@localhost tmp]# ll

total16

-rw-r--r--. 1 root root 15131 Sep 3 06:20functions

[root@localhost tmp]#gzipfunctions

[root@localhost tmp]# ll

total8

-rw-r--r--. 1 root root 4694 Sep 3 06:20 functions.gz

也可以使用gunzip,为了方便记忆,建议大家直接使用-d选项就好了:

[root@localhost tmp]# gzipfunctions

[root@localhost tmp]# ll

total8

-rw-r--r--. 1 root root 4694 Sep 3 06:20functions.gz

[root@localhost tmp]#gunzipfunctions.gz

[root@localhost tmp]# ll

total16

-rw-r--r--. 1 root root 15131 Sep 3 06:20 functions

指定压缩比:

[root@localhost tmp]# gzip -9functions

[root@localhost tmp]# ll

total8

-rw-r--r--. 1 root root 4686 Sep 3 06:20 functions.gz

将压缩比设置为9之后,相对于压缩比6,仅仅只减少了8个字节,一般情况下都不需要去动压缩比,因为6已经是一个最好的选择。

使用-c将输出结果至标准输出,我们将看到一堆乱码,那-c选项到底有什么用呢?

我们在压缩文件的时候原文件会被删除,如果想保留原文件就可以通过-c选项来实现啦!

[root@localhost tmp]# gzip -c functions >functions.gz

[root@localhost tmp]# ll

total24

-rw-r--r--. 1 root root 15131 Sep 3 06:20functions-rw-r--r--. 1 root root 4694 Sep 3 06:39 functions.gz

可以使用zcat在不解压缩的情况下查看文件的内容:

[root@localhost tmp]# zcatfunctions.gz

#-*-Shell-script-*-#

# functions Thisfilecontains functions to be used by most or all

# shell scriptsin the /etc/init.d directory.

#

TEXTDOMAIN=initscripts

# Make sure umask is sane

umask022# Set up a default search path.

PATH="/sbin:/usr/sbin:/bin:/usr/bin"......(略)

bzip2、bunzip2和bzcat

和gzip类似,bzip2为压缩工具,bunzip2为解压缩工具,同样bzcat的作用了在不解压文件的情况下,查看文件内容。

语法:bzip2[OPTION]... FILE...

常用选项:-d:解压缩,相当于bunzip2-#:指定压缩比,默认是6,数字越大压缩比越大(1-9)-k:keep,压缩并保留原文件,bzip2不需要像gzip那样使用输出重定向至指定的文件,这样就方便多啦

我们来举例看一下:

将/etc/init.d/functions复制到tmp目录下,使用bzip2压缩:

[root@localhost tmp]# bzip2functions

[root@localhost tmp]# ll

total8

-rw-r--r--. 1 root root 4763 Sep 3 06:20 functions.bz2

说明bzip2在默认压缩的情况下也会删除原文件,节约了磁盘的空间。

再来看一下解压缩的方法:

[root@localhost tmp]# ll

total8

-rw-r--r--. 1 root root 4763 Sep 3 06:20functions.bz2

[root@localhost tmp]#

[root@localhost tmp]#bunzip2functions.bz2

[root@localhost tmp]# ll

total16

-rw-r--r--. 1 root root 15131 Sep 3 06:20functions

[root@localhost tmp]#bzip2functions

[root@localhost tmp]# ll

total8

-rw-r--r--. 1 root root 4763 Sep 3 06:20functions.bz2

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

[root@localhost tmp]# ll

total16

-rw-r--r--. 1 root root 15131 Sep 3 06:20 functions

好吧,还是建议大家记住一个-d选项就好啦!

现在我们来使用以下-k选项:

[root@localhost tmp]# ll

total16

-rw-r--r--. 1 root root 15131 Sep 3 06:20functions

[root@localhost tmp]#bzip2 -k functions

[root@localhost tmp]# ll

total24

-rw-r--r--. 1 root root 15131 Sep 3 06:20functions-rw-r--r--. 1 root root 4763 Sep 3 06:20 functions.bz2

使用bzcat在不打开压缩文件的情况下查看文件的内容:

[root@localhost tmp]# bzcatfunctions.bz2

#-*-Shell-script-*-#

# functions Thisfilecontains functions to be used by most or all

# shell scriptsin the /etc/init.d directory.

#

TEXTDOMAIN=initscripts

# Make sure umask is sane

umask022# Set up a default search path.

PATH="/sbin:/usr/sbin:/bin:/usr/bin"export PATH

......(略)