文件管理-压缩打包命令
Posted Greenxu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件管理-压缩打包命令相关的知识,希望对你有一定的参考价值。
文件管理-压缩打包命令
为什么使用压缩?
节省磁盘空间占用率
节省网络传输带宽消耗
网络传输更加快捷
服务安装包都需要解压
压缩格式及命令
格式 | 命令 | 压缩工具 |
zip | zip | |
gzip | gzip | |
tar | tar | |
tar.gz | tar、gzip | |
zip
压缩命令zip
解压命令unzip
安装命令 yum install -y zip unzip
压缩语法:zip 指定保存位置 压缩包名 filename1 filename2 filname3 filenameN..
选项:
-r:递归压缩,包括目录下的所有文件(不写-r仅压缩打包目录,不包括目录下的文件)
指定保存位置不写,默认保存至当前工作路径
压缩包名自动zip后缀
解压语法:unzip 压缩包名
选项:
-l 查看压缩包里面都有哪些文件
-d 指定解压保存的路径 (-d:directory)
特性:
1.压缩文件后,源文件存在
2.可以指定多个文件一起压缩
3.可以指定压缩后保存的路径
4.可以压缩目录,压缩目录需要加选项-r,如果不加,压缩后,只有一个空目录,没有里面的文件
5.解压后,压缩包不会消失,如果解压保存的目录下出现同名文件,则会询问是否要覆盖

zip多个目录压缩到同一个包 源文件存在


zip压缩目录不加-r选项,仅压缩了目录


zip压缩目录加-r选项,压缩了目录及目录下文件

unzip -d 将当前目录下ys_mulu_r.zip 解压到/tmp/jiaya
gzip
安装命令:yum install -y gzip
压缩语法:gzip filename1 filename2 filname3 filenameN..(会打包成N个.gz文件)
解压语法:gzip -d filename.gz (-d:decompression)
特性:
1.压缩文件后,源文件不存在,压缩包的位置在源文件的目录下
2.解压后,压缩包不存在,只剩源文件
3.只能压缩文件,不能压缩目录
4.压缩后可以直接查看文件内容zcat
5.一个压缩包中,只会有一个文件

压缩文件后,源文件不存在,压缩包的位置在源文件的目录下

zcat查看压缩文件内容

解压后,压缩包不存在,只剩源文件

只能压缩文件,不能压缩目录

一个压缩包中,只会有一个文件 ,打包成3个
tar
tar命令本身是归档功能
选项可以不加-,最好不加。
c:archive归档
f:指定归档包名
z:使用gizp命令将归档文件压缩
v:显示压缩或解压的过程
x:解压归档文件
C:指定解压的位置(路径
t:查看压缩包里的文件都有哪些
j:使用bzip2压缩文件
J:压缩成.xz包
X:排除指定的文件
--exclude:排除指定文件
举例
1、zcf打包压缩
# tar zcf etc.taz /etc
tar: Removing leading `/ from member names
该命令步骤
1)先归档 tar cf etc.taz /etc
2)再使用gzip压缩 gzip etc.taz
2、xf解压并-C指定路径
# tar xf etc.taz -C ./mulu/etc1/
tar: etc/group: time stamp 2022-04-30 06:24:53 is 965279.305859636 s in the future
tar: etc/gshadow: time stamp 2022-04-30 06:24:53 is 965279.305400436 s in the future
tar: etc/cpcefind: time stamp 2022-04-30 05:55:35 is 963520.844119734 s in the future
tar: etc/cpcefind2: time stamp 2022-04-30 05:59:49 is 963774.844016734 s in the future
tar: etc/file1: time stamp 2022-04-30 06:07:37 is 964242.843952934 s in the future
tar: etc/file2: time stamp 2022-04-30 06:07:37 is 964242.843889734 s in the future
tar: etc/file3: time stamp 2022-04-30 06:07:37 is 964242.843827634 s in the future
tar: etc/file4: time stamp 2022-04-30 06:07:37 is 964242.843761234 s in the future
tar: etc/file5: time stamp 2022-04-30 06:07:37 is 964242.843388134 s in the future
tar: etc/file6: time stamp 2022-04-30 06:07:37 is 964242.843002534 s in the future
tar: etc/file7: time stamp 2022-04-30 06:07:37 is 964242.842852634 s in the future
tar: etc/file8: time stamp 2022-04-30 06:07:37 is 964242.842766534 s in the future
tar: etc/file9: time stamp 2022-04-30 06:07:37 is 964242.842353034 s in the future
tar: etc/file10: time stamp 2022-04-30 06:07:37 is 964242.842204734 s in the future
3、X 排除指定文件
# echo etc > xtt
# tar zcf pc etc1 -X xtt
# tar tf pc
etc1/
etc1/1.txt
etc1/paichu.txt
etc1/etc2
# ls etc1
1.txt etc etc2 paichu.txt
# tar xf pc -C 1解压到1目录
# ls 1
etc1
特性
1.压缩文件后,源文件存在
2.目录和文件都可以压缩
3.压缩后,压缩包的位置可以指定任意目录# tar zcf /usr/local/zls.tar.gz /etc /opt /tmp
4.可以查看压缩包里有哪些文件,但是查看不了文件内容# tar tf /usr/local/zls.tar.gz
5.一个压缩包中,可以有多个文件或目录
6.解压后,压缩包还在,源文件也可以随意指定路径 -C
7.万能解压命令:xf
## 注意:
1.tar命令在解压开文件时,如果有文件名冲突,则不会询问,直接覆盖
2.tar命令,在打包时,会自动删除绝对路径的"/"
3.以后打包,尽量使用相对路径,cd到需要打包目录或文件的上级目录
#cd /
# tar zcf /usr/local/src/opt.tgz opt/
文件管理-压缩打包命令_Greentree的技术博客_51CTO博客
以上是关于文件管理-压缩打包命令的主要内容,如果未能解决你的问题,请参考以下文章