文档的压缩与打包

使用压缩文件,不仅能节省磁盘空间,而且在传输时还可以节省网络带宽。
Linux下最常见的压缩文件通常都是tar.gz格式的,除此之外,还有 .tar、.gz、.bz2、.zip等格式,不同的压缩工具使用的压缩算法也不同。
下面介绍Linux下常见的后缀名所对应的压缩工具:

.gz : 表示由gzip压缩工具压缩的文件
.bz2 : 表示由bzip2压缩工具压缩的文件
.xz : 表示由xz压缩工具压缩的文件
.zip : 表示由zip压缩工具压缩的文件
.tar : 表示由tar打包程序打包的文件(tar并没有压缩功能,只是把很多目录或文件合并成一个文件)
.tar.gz : 可以理解为先由tar打包,然后再由gzip压缩
.tar.bz2 : 可以理解为先由tar打包,然后再由bzip2压缩
.tar.xz : 可以理解为先由tar打包,然后再由xz压缩


6.1 gzip压缩工具

gzip语法:

gzip [-cd#] +文件名(不加参数直接跟文件名时,会在当前目录下直接压缩该文件,而原文件会消失)

参数:

-c 后面跟文件名,在当前目录下压缩该文件,而保留原文件
-d 后面跟 .gz压缩文件,表示解压压缩文件,而原文件会消失
-# 表示压缩等级,1~9,默认为6,9表示压缩最厉害,但也最消耗cpu资源

另外:

gzip不支持压缩目录,压缩目录时会报错
unzip 等同于 gzip,可以压缩文件,gunzip也可以解压缩文件,参数与gzip相同
zcat后面跟 .gz压缩文件,可以在不解压前提下直接查看文件内容

例子:

[root@localhost tmp]# ls
1.txt 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# gzip 1.txt
[root@localhost tmp]# ls
1.txt.gz 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# gzip -d 1.txt.gz
[root@localhost tmp]# ls
1.txt 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]#ls
1.txt.gz 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# zcat 1.txt.gz
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@localhost tmp]#gzip -c 1.txt > /root/1.txt.gz
[root@localhost tmp]# ls
1.txt 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# ls /root
1.txt.gz anaconda-ks.cfg
[root@localhost tmp]# gunzip -c /root/1.txt.gz > /tmp/1.txt.new
[root@localhost tmp]# ls
1.txt 1.txt.new 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# gzip 2
gzip: 2 is a directory – ignored
[root@localhost tmp]# gzip d 1.txt
gzip: d: No such file or directory


6.2 bzip2压缩工具

安装bzip2:yum install -y bzip2
bzip2语法:

bzip2 -[zcd#] +文件名(bzip2不支持压缩目录,加或不加 -z选项都可以压缩文件,不加 -c选项原文件原文件会消失)

参数:

-z 后面跟文件名,表示在当前目录下压缩该文件,而原文件会消失
-c 后面跟文件名,在当前目录下压缩该文件,同时保留原文件
-d 后面跟 .bz2压缩文件,表示解压压缩文件,而原文件会消失
-# 表示压缩等级,1~9,默认为9

另外:

bunzip2后面跟 .bz压缩文件,也是解压文件,等同于 bzip2 -d
bzcat后面跟 .bz2压缩文件,可以在不解压前提下直接查看文件内容

例子:

[root@localhost tmp]# bzip2 1.txt
[root@localhost tmp]# ls
1.txt.bz2 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# bzip2 -d 1.txt.bz2
[root@localhost tmp]# ls
1.txt 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# bzip2 -z 1.txt
[root@localhost tmp]# ls
1.txt.bz2 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# bzip2 2
bzip2: Input file 2 is a directory.
[root@localhost tmp]# bzip2 d 1.txt.bz2
bzip2: Can’t open input file d: No such file or directory.
bzip2: Input file 1.txt.bz2 already has .bz2 suffix.
[root@localhost tmp]# bzip2 -d 1.txt.bz2
[root@localhost tmp]# ls
1.txt 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# bzip2 -c 1.txt > /root/1.txt.bz2
[root@localhost tmp]# ls
1.txt 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# ls /root
1.txt.bz2 anaconda-ks.cfg
[root@localhost tmp]# bzip2 -d -c /root/1.txt.bz2 > /tmp/1.txt.new
[root@localhost tmp]# ls
1.txt 1.txt.new 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# ls /root
1.txt.bz2 anaconda-ks.cfg


6.3 xz压缩工具

xz语法:

xz -[zcd#] +文件名(xz不支持压缩目录,加或不加 -z选项都可以压缩文件,不加 -c选项原文件原文件会消失)

参数:

-z 后面跟文件名,表示在当前目录下压缩该文件,而原文件会消失
-c 后面跟文件名,在当前目录下压缩该文件,同时保留原文件
-d 后面跟 .bz2压缩文件,表示解压压缩文件,而原文件会消失
-# 表示压缩等级,1~9,默认为6

另外:

unxz后面跟 .xz压缩文件,也是解压文件,等同于 xz -d
xzcat后面跟 .xz压缩文件,可以在不解压前提下直接查看文件内容

例子:

[root@localhost tmp]# ls
1.txt 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# xz 1.txt
[root@localhost tmp]# ls
1.txt.xz 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# xz 2
xz: 2: Is a directory, skipping
[root@localhost tmp]# xz -d 1.txt.xz
[root@localhost tmp]# ls
1.txt 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# xz -z 1.txt
[root@localhost tmp]# ls
1.txt.xz 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# xz -d -c /tmp/1.txt.xz >/root/1.txt.new
[root@localhost tmp]# ls
1.txt.xz 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# ls /root
1.txt.new anaconda-ks.cfg
[root@localhost tmp]# unxz 1.txt.xz
[root@localhost tmp]# ls
1.txt 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC

一般情况下,压缩程度: gzip<bzip2<xz


6.4 tar打包工具

tar是一个打包工具,可以把目录和文件全部文件打包成一个大文件,方便复制或移动
tar语法:

tar [-zjJxtcvfpP] +文件名 tar

参数:

-z 表示同时用gzip压缩
-j 表示同时用bzip2压缩
-J 表示同时用xz压缩
-x 表示解包或者解压缩
-t 表示查看tar包里的文件
-c 表示建立一个tar包或者压缩文件包
-C 表示解包或解压到指定的目录
-v 表示可视化
-f 后面跟文件名(即 -f filename,表示压缩后的文件名为 filename,或者解压文件 filename,注意使用时 -f后紧跟文件名)
-p 表示使用原文件的属性,压缩前什么属性压缩后还是什么属性
-P 表示可以使用绝对路径
–exclude filename 表示在打包或压缩时,不要将 filename文件包括在内

使用 tar命令可以打包目录和文件,不管是打包还是解包,原文件是不会删除的,而且它会覆盖当前已经存在的文件或者目录
例子:

[root@localhost tmp]# ls
1.txt 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# touch 2/2.txt
[root@localhost tmp]# echo “hello” > !$
echo “hello” > 2/2.txt
[root@localhost tmp]# cp 1.txt 2/
[root@localhost tmp]# tree .
.
├── 1.txt
├── 2
│ ├── 1.txt
│ └── 2.txt
└── systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
└── tmp
3 directories, 3 files
[root@localhost tmp]# tar -cvf 2.tar 2
2/
2/2.txt
2/1.txt
[root@localhost tmp]# ls
1.txt 2 2.tar systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# rm -f 2.tar
[root@localhost tmp]# tar -cf 2.tar 2 1.txt
[root@localhost tmp]# ls
1.txt 2 2.tar systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# tar -cvf 2.tar –exclude 1.txt 2
2/
2/2.txt
[root@localhost tmp]# mkdir 2/3
[root@localhost tmp]# tar -cvf 2.tar –exclude 3 2
2/
2/2.txt
2/1.txt

tar命令可以在打包时直接压缩,它支持 gzip压缩、bzip2压缩、xz压缩。
gziptar -cvzf压缩成 .tar.gz 格式(tar -zxvf解压 .tar.gz格式)
bzip2tar -cvjf压缩成 .tar.bz2 格式(tar -jxvf解压 .tar.bz2格式)
xztar -cvJf压缩成 .tar.xz 格式(tar -Jxvf解压 .tar.xz格式)

[root@localhost tmp]# ls
1.txt 2 2.tar systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# tar -cvzf 2.tar.gz 2
2/
2/2.txt
2/1.txt
2/3/
[root@localhost tmp]# ls
1.txt 2 2.tar 2.tar.gz systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# tar -tf 2.tar.gz
2/
2/2.txt
2/1.txt
2/3/
[root@localhost tmp]# tar -xvzf 2.tar.gz
2/
2/2.txt
2/1.txt
2/3/
[root@localhost tmp]# ls
1.txt 2 2.tar 2.tar.gz systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC


6.5 zip压缩工具

zip支持压缩文件和目录,压缩目录时,需要指定目录下的文件,压缩完不删除原文件或原目录
zip后面先跟目标文件名,即压缩后的自定义压缩文件名,然后跟需要压缩的文件或者目录
zip安装: yum install -y zip
注意:当目录下还有二级目录甚至更多级目录时,zip命令仅仅是把二级目录本身压缩,想要一并压缩更多级目录下的文件,需要加上 -r选项
zip的压缩包无法直接查看文件内容

unzip 后面跟 .zip压缩文件,表示解压压缩文件
-l 查看文件中文件列表
-d +路径 指定路径解压文件

例子:

[root@localhost tmp]# ls
1.txt 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# ls 2
1.txt 2.txt 3
[root@localhost tmp]# zip 1.txt.zip 1.txt
adding: 1.txt (deflated 88%)
[root@localhost tmp]# ls
1.txt 1.txt.zip 2 systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# zip 2.zip 2/*
adding: 2/1.txt (deflated 88%)
adding: 2/2.txt (stored 0%)
adding: 2/3/ (stored 0%)
[root@localhost tmp]# ls
1.txt 1.txt.zip 2 2.zip systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# zip -r 2.zip 2/
updating: 2/1.txt (deflated 88%)
updating: 2/2.txt (stored 0%)
updating: 2/3/ (stored 0%)
adding: 2/ (stored 0%)
[root@localhost tmp]# ls
1.txt 1.txt.zip 2 2.zip systemd-private-674cebd990e248a38f6cead8029444c7-chronyd.service-4tsiYC
[root@localhost tmp]# unzip 2.zip
Archive: 2.zip
replace 2/1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
inflating: 2/1.txt
extracting: 2/2.txt

猜你喜欢

转载自blog.csdn.net/miss1181248983/article/details/80469226