第8章 压缩与归档
文件打包压缩介绍
1.什么是文件压缩与解压缩
当我们需要传输一份很大的文件,或者很多小文件时,通过网络传输是比较耗时的。
此时我们通常会将大文件或多个小文件压缩成一个压缩包文件,这样做既能压缩体积,也可以减少文件的数量,传输起来会更快更方便。
Linux和Windows常见压缩包格式
windows下常见压缩包格式:
.rar
.zip
Linux下常见压缩包格式
.gz
.zip
.bz2
.tar.gz #最常见
.tar.bz2
Linux下常见打包压缩命令
1.gzip/zcat 了解即可
关键参数:
-c 把压缩后的文件输出到标准输出设备,不去更动原始文件,默认不加参数会删除原文件
-d 解开压缩文件,默认使用-d解压缩后会删除压缩包
-v 显示压缩详细过程
安装命令:
yum install gzip -y
案例1: 直接压缩文件,不加参数默认gzip会删除原文件并生成*.gz的压缩包
[root@linux ~]# mkdir gzip
[root@linux ~]# cd gzip/
[root@linux gzip]# ls
[root@linux gzip]# touch 123.txt
[root@linux gzip]# gzip 123.txt
[root@linux gzip]# ll
总用量 4
-rw-r--r-- 1 root root 28 3月 24 20:15 123.txt.gz
案例2:压缩文件但是保留原文件
[root@linux gzip]# touch 123.txt
[root@linux gzip]# gzip 123.txt -c > 123.gz
[root@linux gzip]# ll
总用量 8
-rw-r--r-- 1 root root 28 3月 24 20:16 123.gz
-rw-r--r-- 1 root root 0 3月 24 20:16 123.txt
-rw-r--r-- 1 root root 28 3月 24 20:15 123.txt.gz
案例3:解压文件,但是解压完后会删除压缩包
[root@linux gzip]# ll
总用量 8
-rw-r--r-- 1 root root 28 3月 24 20:16 123.gz
-rw-r--r-- 1 root root 0 3月 24 20:16 123.txt
-rw-r--r-- 1 root root 28 3月 24 20:15 123.txt.gz
[root@linux gzip]# gzip -d 123.txt.gz
gzip: 123.txt already exists; do you wish to overwrite (y or n)? y
[root@linux gzip]# ll
总用量 4
-rw-r--r-- 1 root root 28 3月 24 20:16 123.gz
-rw-r--r-- 1 root root 0 3月 24 20:15 123.txt
案例4:解压文件,但是保留压缩包
[root@linux gzip]# ll
总用量 4
-rw-r--r-- 1 root root 28 3月 24 20:20 123.txt.gz
[root@linux gzip]# gzip -cd 123.txt.gz > 123.txt
[root@linux gzip]# ll
总用量 4
-rw-r--r-- 1 root root 0 3月 24 20:20 123.txt
-rw-r--r-- 1 root root 28 3月 24 20:20 123.txt.gz
案例5:使用zcat查看压缩文件但是不解压
[root@linux gzip]# echo shenzhenoldboy > linux6.txt
[root@linux gzip]# cat linux6.txt
shenzhenoldboy
[root@linux gzip]# gzip -c linux6.txt > linux6.txt.gz
[root@linux gzip]# ll
总用量 8
-rw-r--r-- 1 root root 15 3月 24 20:23 linux6.txt
-rw-r--r-- 1 root root 44 3月 24 20:24 linux6.txt.gz
[root@linux gzip]# zcat linux6.txt.gz
shenzhenoldboy
[root@linux gzip]# ll
总用量 8
-rw-r--r-- 1 root root 15 3月 24 20:23 linux6.txt
-rw-r--r-- 1 root root 44 3月 24 20:24 linux6.txt.gz
2.zip/unzip和windows通用
相关命令:
zip 压缩文件 了解即可 不常用
unzip 解压文件 需要掌握 常用
zip压缩命令关键参数:
-r 递归处理,将指定目录下的所有文件和子目录一并处理
-T 查看zip压缩包是否是完整
-v 显示执行过程
-q 不显示执行过程
unzip解压命令关键参数
-d 解压到指定目录
案例1: 将etc目录下的文件压缩成当前目录下的zip包并显示详细过程
zip -r etc.zip /etc/
案例2:将etc目录下的文件压缩成当前目录下的zip包,但是不显示详细过程
zip -rq etc.zip /etc/
案例3:解压etc.zip压缩包到指定目录
unzip etc.zip -d /opt/
3.tar打包压缩 !!!必须掌握!!!
tar命令是Linux下最常用的压缩解压缩工具,必须要掌握!
命令格式
tar [选项] filename
关键参数
c #创建新的归档文件
v #输出压缩或解压缩的详细过程
f #指定压缩包的名称,如果有多个参数,f参数写在最后
x #解压压缩文件
t #列出压缩包里的文件列表
h #打包软链接
z #使用gzip压缩归档后的文件,后缀名为*.tar.gz
j #使用bzip2压缩归档后的文件,后缀名为*.tar.bz2
J #使用sz压缩归档后的文件,后缀名为*.tar.xz
C #大写的C,指定将压缩包解压到哪个目录
X #排除多个文件
--exclude #如果需要排除的文件比较多,可以写进一个文件里,然后使用这个参数去读去排除文件的列表
常用打包压缩命令组合
zcf #打包成tar.gz格式
jcf #打包成tar.bz格式
Jcf #打包成tar.xz格式
zxf #解压tar.gz格式
jxf #解压tar.bz格式
xf #解压tar.xz格式
tf #查看压缩包的内容
打包压缩中的坑
如果打包压缩时的路径带有/根,则会提示我们需要移除/符号,这是为了防止我们解压缩的时候不小心误伤到了/
解决方法就是压缩的时候不使用绝对路径,可以先进入要压缩的目录或者上层目录,再进行压缩
[root@linux gzip]# tar zcvf passwd.tar.gz /etc/passwd
tar: 从成员名中删除开头的“/”
/etc/passwd
案例1: 将文件或目录进行打包压缩
#打包/etc目录并保存成etc.tar.gz
cd /
tar zcvf /opt/etc.tar.gz etc/
#打包/etc/passwd文件和/etc/hosts文件并保存成all.tar.gz
cd /
tar zcvf /opt/all.tar.gz etc/passwd etc/hosts
#打包链接文件的真实文件
cd /
tar zcfh /opt/local.tar.gz etc/rc.local
案例2:排除文件,并打包压缩
cd /
tar czf etc.tar.gz --exclude=etc/services etc/
案例3:将需要排除的文件写入文件中
cat > exclude.list << EOF
etc/services
etc/rc.local
etc/rc.d/rc.local
EOF
tar czfX etc.tar.gz -X exclude.list /etc/
案例4:查看压缩文件内容但是不解压
tar tf etc.tar.gz
案例4:解压缩文件到当前目录
tar zcvf etc.tar.gz
案例5:解压文件内容到指定的目录
tar zcvf etc.tar.gz -C /opt/
更新: 2024-09-21 19:41:14