压缩

mac2024-03-06  27

>什么是文件压缩? `将多个文件或目录合并成为一个特殊的文件` >2为什么要对文件进行压缩? `减少文件的体积` `加快资源的传输` `节省网络的宽带` >3windows的压缩包与linux的压缩包能否互通? windows:`ar zip 其实支持很多类型的压缩` linux:`ip tar.gz ...` >4*linux 下压缩包有哪些常见的类型 格式 压缩工具 .`zip zip压缩工具 (必须要会使用的)` .gz `gzip压缩工具,只能压缩文件,会删除原文件(通常配合tar使 用)` .bz2 `bzip2压缩工具,只能压缩文件,会删除原文件(通常配合tar使 用)` .`tar.gz 先使用tar命令归档打包,然后使用gzip压缩 (必须会的)` .`tar.bz2先使用tar命令归档打包,然后使用bzip压缩 (顺带就会)` >5 linux gzip工具使用 1.gzip打包与压缩,仅对文件有效 2.gzip filename 打包 3.gzip -d filename.gz 4.zcat filename.gz 查看包内文件的内容 [root@xuliangwei ~]# `yum install gzip -y` [root@xuliangwei ~]# `gzip file #对文件进行压缩` [root@xuliangwei ~]# `zcat file.gz #查看gz压缩后的文件` [root@xuliangwei ~]# `gzip -d file.gz #解压gzip的压缩包` 使用场景:当需要让某个文件快速关闭和快速启用. [root@xuliangwei ~]# gzip CentOS-Vault.repo --> CentOS-Vault.repo.gz [root@xuliangwei ~]# zcat CentOS-Vault.repo.gz --> 查 看不想解压的压 >6 linux zip 工具使用 默认情况下没有zip和unzip工具,需要进行安装 [root@xuliangwei ~]# yum install zip unzip -y >1.压缩文件为zip包 [root@xuliangwei ~]# zip filename.zip filename [root@xuliangwei ~]# unzip -l filename.zip #查看包内>2.压缩目录为zip包 [root@xuliangwei ~]# zip -r dir.zip dir/ >3.查看zip压缩包是否是完整的 [root@xuliangwei ~]# zip -T filename.zip test of filename.zip OK >4.不解压压缩查看压缩包中的内容 [root@xuliangwei ~]# unzip -l filename.zip [root@xuliangwei ~]# unzip -t filename.zip #检测文件是 否都ok >5.解压zip文件包, 默认解压至当前目录 [root@xuliangwei ~]# unzip filename.zip >6.解压zip内容至/opt目录 [root@xuliangwei ~]# unzip filename.zip -d /opt/ 打包 zip -r /tmp/test.zip file dir/ 解包 unzip tt.zip unzip tt.zip -d /opt >7.linux tar 工具使用 tar是linux下最常用的压缩与解压缩,支持文件和目录的压缩归档 语法:tar [-zjxcvfpP] filename `c#创建新的归档文件 x#对归档文件解包 t #列出归档文件里的文件列表 f #指定包文件名,多参数f写最后 z #使用gzip压缩归档后的文件(.tar.gz) j #使用bzip2压缩归档后的文件(.tar.bz2) J #使用xz压缩归档后的文件(tar.xz) C #指定解压目录位置 X #排除多个文件(写入需要排除的文件名称) h #打包软链接 --exclude #在打包的时候写入需要排除文件或目录 常用打包与压缩组合 cjf #打包tar.bz格式 cJf #打包tar.xz格式 使用田少,不考虑 zxf #解压tar.gz格式 jxf #解压tar.bz格式 czr #打包tar.gz格式 () tf #查看压缩包内容 >xf #自动选择解压模式 ()` >打包 `tar czf test.tar.gz test/ test2/ #以gzip方式压缩 tar cjf test.tar.bz2 dir.txt dir/ #以bz2方式压缩` >查看包内容 `tar tf test.tar.gz tar tf test.tar.bz2 tar tf test.tar.xz` >解压 `tar xf test.tar.gz tar xf test.tar.bz2 tar xf test.tar.xz tar xf root.tar.gz -C /tmp/ #解压至指定目录` >打包/tmp下所有文件 `find tmp/ -type f | xargs tar czf tmp.tar.gz tar czf tmp.tar.gz $(find /tmp/ -type f)` >3.打包链接文件,打包链接文件的真实文件 [root@xuliangwei /]# tar czfh local.tar.gz etc/rc.local >4.排除操作 `tar czf etc.tar.gz /etc/ --exclude=etc/services tar czf etc.tar.gz /etc/ --exclude=etc/passwd -- exclude=etc/shadow` >5.将需要排除的文件写入文件中 [root@oldboyedu opt]# cat pc.txt etc/gshadow etc/gshadowetc/passwd >etc/passwdetc/shadowetc/shadow etc/security/opasswd etc/pam.d/passwd [root@oldboyedu opt]# tar czXf pc.txt etc.tar.gz /etc/
最新回复(0)