文档的压缩与打包

Linux下的压缩文件:

在linux下最常见的压缩文件通常都是以.tar.gz 为结尾的,除此之外还有.tar, .gz, .bz2, .zip等等。linux系统中的后缀名其实要不要无所谓,但是对于压缩文件来讲必须要带上。这是为了判断压缩文件是由哪种压缩工具所压缩,而后才能去正确的解压缩这个文件。以下介绍常见的后缀名所对应的压缩工具。

  • .gz :gzip 压缩工具压缩的文件
  • .bz2: bzip2 压缩工具压缩的文件
  • .tar: tar 打包程序打包的文件(tar并没有压缩功能,只是把一个目录合并成一个文件)
  • .tar.gz :可以理解为先用tar打包,然后再gzip压缩
  • .tar.bz2 :同上,先用tar打包,然后再bzip2压缩

gzip压缩工具:

语法: gzip [-d#] filename 其中#为1-9的数字

“-d” : 解压缩时使用

“-#” : 压缩等级,1压缩最差,9压缩最好,6为默认

gzip 后面直接跟文件名,就在当前目录下把该文件压缩了,而原文件也会消失。

“gzip -d” 后面跟压缩文件,会解压压缩文件。gzip 是不支持压缩目录的。

gzip其他具体用法如下:

gzip用法

图:gzip具体用法

  • -d(decompress): 解压文件。
  • -l(list):列出压缩文件内容。
  • -q(quiet):抑制所有警告。
  • -r(recursive):递归的操作目录。
  • -t(test):测试压缩文件的完整性。
  • -1:快速压缩。
  • -9:压缩等级最高。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@localhost ~]# mkdir Dir
[root@localhost ~]# ls
anaconda-ks.cfg Dir Downloads Pictures Templates
Desktop Documents Music Public Videos
[root@localhost ~]# cd Dir
[root@localhost Dir]# ls
[root@localhost Dir]# touch file.txt
[root@localhost Dir]# ls
file.txt
[root@localhost Dir]# gzip file.txt
[root@localhost Dir]# ls
file.txt.gz
[root@localhost Dir]# cd ../
[root@localhost ~]# gzip Dir
gzip: Dir is a directory -- ignored
[root@localhost ~]# cd Dir
[root@localhost Dir]# ls
file.txt.gz
[root@localhost Dir]# gzip -d file.txt.gz
[root@localhost Dir]# ls
file.txt

bzip2压缩工具:

语法: bzip2 [-dz] filename

bzip2 只有两个选项需要掌握。

“-d” : 解压缩

“-z” : 压缩。压缩时,可以加 “-z” 也可以不加,都可以压缩文件,”-d” 则为解压的选项:

bzip2 同样也不可以压缩目录

bzip2其他具体用法如下:

bzip2

图:bzip2具体用法

tar压缩工具:

tar 本身为一个打包工具,可以把目录打包成一个文件,它的好处是它把所有文件整合成一个大文件整体,方便拷贝或者移动。

语法:tar [-zjxcvfpP] filename

“-z” : 同时用gzip压缩

“-j” : 同时用bzip2压缩

“-x”(extract) (提取) :解包或者解压缩

“-t”(list) : 查看tar包里面的文件,列出归档内容。

“-c” (create): 建立一个tar包或者压缩文件包

“-v” (verbose):详细地列出处理的文件。可视化。

“-f” : 后面跟文件名,压缩时跟 “-f 文件名”,意思是压缩后的文件名为filename, 解压时跟 “-f 文件名”,意思是解压filename. 请注意,如果是多个参数组合的情况下带有 “-f”,请把 “-f” 写到最后面。

“-p” : 使用原文件的属性,压缩前什么属性压缩后还什么属性。(不常用)

“-P” : 可以使用绝对路径。(不常用)

--exclude filename : 在打包或者压缩时,不要将filename文件包括在内。(不常用)

其他用法可使用tar –help来查看。

tar,打包还是解包,原来的文件是不会删除的,而且它会覆盖当前已经存在的文件或者目录。

打包的同时使用gzip压缩(常用):

tar命令非常好用的一个功能就是可以在打包的时候直接压缩,它支持gzip压缩和bzip2压缩。

“-cvzf”:可以在打包的同时使用gzip压缩。

“-tf” 可以查看包或者压缩包的文件列表.

“-zxvf” 用来解压.tar.gz的压缩包.

具体操作示例如下:

压缩文件

图:打包的同时使用gzip压缩和解压缩

打包的同时使用bzip2压缩:

和gzip压缩不同的是,这里使用 “-cjvf” 选项来压缩。同样可以使用 “-tf” 来查看压缩包文件列表。

压缩

图:使用bzip2来压缩解压文件

–exclude的使用方法:

语法:tar -cvf Dir.tar --exclude file.txt Dir

请注意,Dir.tar是放到了 –exclude 选项的前面。该选项除了可以排除文件,也可以排除目录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@localhost ~]# ls
anaconda-ks.cfg Dir Downloads Pictures Templates
Desktop Documents Music Public Videos
[root@localhost ~]# cd Dir
[root@localhost Dir]# ls
file.txt NewDir
[root@localhost Dir]# cd ../
[root@localhost ~]# tar -cvf Dir.tar --exclude file.txt Dir
Dir/
Dir/NewDir/
[root@localhost ~]# ls
anaconda-ks.cfg Dir Documents Music Public Videos
Desktop Dir.tar Downloads Pictures Templates
[root@localhost ~]# tar -tf Dir.tar
Dir/
Dir/NewDir/
[root@localhost ~]#
坚持原创技术分享,您的支持将鼓励我继续创作!