目录
压缩打包
压缩打包
? Windows 下我们接触最多的压缩文件就是. rar 格式, 但 Linux 有自己所特有的压缩工具. 如果希望 Windows 和 Linux 互相能使用的压缩工具, 建议. zip` 格式
压缩的好处主要有:
节省磁盘空间占用率
节省网络传输带宽消耗
网络传输更加快捷
类型
格式 | 压缩工具 |
---|---|
.zip | zip 压缩工具 |
.gz | gzip 压缩工具,只能压缩文件,会删除源文件(通常配合 tar 使用) |
.bz2 | bzip2 压缩工具,只能压缩文件,会删除源文件(通常配合 tar 使用) |
.tar.gz | 先使用 tar 命令归档打包,然后使用 gzip 压缩 |
tar.bz2 | 先使用 tar 命令归档打包,然后使用 bzip 压缩 |
注意:
1.Linux 下常用的压缩文件以. tar.gz
2.Linux 下压缩文件必须带后缀
gzip 压缩工具
- # 安装 gzip 压缩工具
- [[email protected] ~]# yum install -y gzip
- # 创建文件
- [[email protected] ~]# echo 123>> file1
- # 压缩 file1
- [[email protected] ~]# gzip file1
- # 查看文件
- [roo[email protected] ~]# ll
总用量 4
-rw-r--r-- 1 root root 30 6 月 23 17:31 file1.gz
- # 查看文件类型
- [[email protected] ~]# file file1.gz
- file1.gz: gzip compressed data, was "file1", from Unix, last modified: Sun Jun 23 17:31:54 2019
- # 查看 gzip 压缩后的文件内容
- [[email protected] ~]# zcat file1.gz
- 123
- # 解压文件
- [[email protected] ~]# gzip -d file1.gz
- # 查看文件
- [[email protected] ~]# ll
总用量 4
-rw-r--r-- 1 root root 4 6 月 23 17:31 file1
练习题:
把 / etc 目录 拷贝 / root 下, 找到 / root/etc / 下面所有一级二级目录中的 普通文件, 压缩
[[email protected] ~]# find /root/etc/ -maxdepth 2 -type f|xargs gzip
注意:
1.gzip 不可以压缩目录, 只能压缩文件
2.gzip 压缩的文件不在当前目录下
3. 压缩会删除源文件
4. 解压会删除压缩文件
zip 压缩工具
-r: 递归压缩
- # 压缩
- [[email protected] ~]# zip zls.zip file1
- #解压
- [[email protected] ~]# unzip zls.zip
- Archive: zls.zip
- replace file1? [y]es, [n]o, [A]ll, [N]one, [r]ename:
替换 不替换 替换所有 啥也不做 改名
- #压缩目录
- [[email protected] tmp]# zip -r tmp_dir2.zip /tmp/
注意:
1. 压缩不会删除源文件
2. 解压不会删除压缩文件
3. 解压后会覆盖源文件内容
tar 压缩文件
tar 是 Linux 下最常用的压缩与解压缩, 支持文件和目录的压缩归档
- # 语法: tar [-zjxcvfP] filename
- c // 创建新的归档文件
- x // 对归档文件解包
- t // 列出归档文件里的文件列表
- v // 输出命令的归档或解包的过程
- f // 指定包文件名, 多参数 f 写最后
- C // 指定解压目录位置
- z // 使用 gzip 压缩归档后的文件 (.tar.gz)
- j // 使用 bzip2 压缩归档后的文件 (.tar.bz2)
- J // 使用 xz 压缩归档后的文件 (tar.xz)
- X // 排除多个文件 (写入需要排除的文件名称)
- h // 打包软链接
- P // 连带绝对路径打包
- --hard-dereference // 打包硬链接
- --exclude // 在打包的时候写入需要排除文件或目录
- // 常用打包与压缩组合
- czf // 打包 tar.gz 格式
- cjf // 打包 tar.bz 格式
- cJf // 打包 tar.xz 格式
- zxf // 解压 tar.gz 格式
- jxf // 解压 tar.bz 格式
- xf // 自动选择解压模式
- tf // 查看压缩包内容
- // 以 gzip 归档方式打包并压缩
- tar czf test.tar.gz test/ test2/
- // 以 bz2 方式压缩
- tar cjf test.tar.bz2 dir.txt dir/
- // 打包链接文件, 打包链接文件的真实文件
- [[email protected] ~]# cd /
- [[email protected] /]# tar czfh local.tar.gz etc/rc.local
- # 打包 / tmp 下所有文件
- [[email protected] ~]# cd /
- [[email protected] /]# find tmp/ -type f | xargs tar czf tmp.tar.gz
- # 打包 / tmp 下所有文件
- [[email protected] /]# tar czf tmp.tar.gz | xargs find tmp/ -type f
- # 打包 / tmp 下所有文件
- [[email protected] /]# tar czf tmp.tar.gz $(find tmp/ -type f)
注意:
不要使用绝对路径
如果非要打包绝对路径:[[email protected] ~]# tar zcfP b.sh.tar.gz /usr/local/nginx/a.sh
解压的时候:[[email protected] ~]# tar xfP b.sh .tar.gz
tar 打包可以接多个文件 f
tar zcf abc1 .tar.gz 1.txt 2.txt 3.txt
排除文件, 并打包压缩
- # 排除单个文件
- [[email protected] /]# tar czf etc.tar.gz --exclude=etc/services etc/
- # 排除多个文件
- [[email protected] /]# tar czf etc.tar.gz --exclude=etc/services --exclude=etc/rc.local etc/
- [[email protected] /]# tar czf etc.tar.gz --exclude=etc/{
- services,passwd,shadow,gshadow,group
- }
- # 将需要排除的文件写入文件中
- [[email protected] /]# cat paichu.list
- etc/services
- etc/rc.local
- etc/rc.d/rc.local
- # 指定需要排除的文件列表, 最后进行打包压缩
- [[email protected] /]# tar czfX etc.tar.gz paichu.list etc/
解压压缩文件
- // 解包或者解压缩
- [[email protected] /]# tar xf test.tar.gz
- // 将 tar.gz 解压至其他目录
- [[email protected] ~]# tar xf /etc/local.tar.gz -C /tmp
注意: 不管是打包还是解包, 源文件是不会删除, 但会覆盖当前已经存在的文件或者目录
小结
格式 | 压缩工具 |
---|---|
.zip | zip 压缩工具 zip -r dir.zip dirname zip file.zip filename |
.bz2 | gzip 压缩工具,只能压缩文件,会删除源文件(通常配合 tar 使用) |
.tar.gz | 先使用 tar 命令归档打包,然后使用 gzip 压缩 < br ztid="169" ow="0" oh="0"> tar zcf dirname.tar.gz dirname |
.tar.bz2 | 先使用 tar 命令归档打包,然后使用 bzip 压缩 < br ztid="173" ow="0" oh="0"> tar cjf dirname.tar.bz2 dirname |
.tar.xz | tar cJf dirname.tar.xz dirname |
常用打包与压缩组合
- czf // 打包 tar.gz 格式
- cjf // 打包 tar.bz 格式
- cJf // 打包 tar.xz 格式
- xf // 自动选择解压模式
- tf // 查看压缩包内容
压缩打包
来源: http://www.bubuko.com/infodetail-3117973.html