10月8日任务bash
6.5 zip压缩工具工具
6.6 tar打包spa
6.7 打包并压缩3d
支持压缩目录code
zip工具默认未安装,使用下面的命令进行安装ip
yum install -y zip unzipclass
zip压缩文件的同时保留原文件(更gzip等不一样),所以在解压缩时会提示是否覆盖原文件.test
基本使用扩展
# 压缩 [root@localhost tmp]# zip 1.txt.zip 1.txt adding: 1.txt (deflated 63%) [root@localhost tmp]# ls -l 1.txt* -rw-r--r--. 1 root root 25865478 ... 1.txt -rw-r--r--. 1 root root 9649872 ... 1.txt.zip # 解压缩 [root@localhost tmp]# unzip 1.txt.zip Archive: 1.txt.zip replace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: r new name: 2.txt inflating: 2.txt [root@localhost tmp]# ls -l 总用量 77036 -rw-r--r--. 1 root root 25865478 ... 1.txt -rw-r--r--. 1 root root 9649872 ... 1.txt.zip -rw-r--r--. 1 root root 25865478 ... 2.txt
目录的压缩打包
不一样于gzip、bzip二、xz,zip工具支持对目录的压缩
默认不加-r参数的zip dir1.zip dir1/ 只会对dir1目录内一级内容压缩,若是dir1目录内有子目录,子目录内的文件并不会被压缩!!
要想压缩整个dir1目录包括其子目录内的全部文件,须要加上 -r参数!具体以下面的几个例子。
实验目录结构
[root@localhost tmp]# tree dir1 dir1 ├── 1.txt └── dir2 └── 2.txt.xz 1 directory, 2 files
-l参数能够显示zip压缩包内的文件列表,具体使用看下面的例子
压缩目录时不加-r参数,默认只压缩目录自己即一个空目录
[root@localhost tmp]# zip dir1.zip dir1/ adding: dir1/ (stored 0%) [root@localhost tmp]# unzip -l dir1.zip Archive: dir1.zip Length Date Time Name --------- ---------- ----- ---- 0 ... 21:13 dir1/ --------- ------- 0 1 file
2. 加上-r参数压缩目录
[root@localhost tmp]# zip -r dir2.zip dir1/ adding: dir1/ (stored 0%) adding: dir1/1.txt (deflated 63%) adding: dir1/dir2/ (stored 0%) adding: dir1/dir2/2.txt.xz (deflated 0%) [root@localhost tmp]# unzip -l dir2.zip Archive: dir2.zip Length Date Time Name --------- ---------- ----- ---- 0 ... 21:13 dir1/ 25865478 ... 21:11 dir1/1.txt 0 ... 21:12 dir1/dir2/ 7845588 ... 21:12 dir1/dir2/2.txt.xz --------- ------- 33711066 4 files
[root@localhost tmp]# unzip 1.txt.zip -d ./dir1/dir2/ Archive: 1.txt.zip inflating: ./dir1/dir2/1.txt [root@localhost tmp]# tree dir1 dir1 ├── 1.txt └── dir2 ├── 1.txt └── 2.txt.xz 1 directory, 3 files
压缩包内的文件名在解压缩时不会改变,也没法在解压时重命名。
zip压缩较以前的几个压缩工具而言较为宽松,压缩率不高。
打包的目的是为了加快文件的传输速率,节约时间。一样的tar在打包的同时会保留原文件。
-c 建立
-v 详细信息
-f 后接包名
-x 解包
打包文件、目录、文件和目录
# 打包文件 [root@localhost tmp]# tar -cvf 1.txt.tar 1.txt 1.txt [root@localhost tmp]# ls -l 总用量 50532 -rw-r--r--. 1 root root 25865478 ... 1.txt -rw-r--r--. 1 root root 25876480 ... 1.txt.tar drwxr-xr-x. 3 root root ... dir1 # 打包文件和目录 [root@localhost tmp]# tar -cvf test.tar 1.txt dir1/ 1.txt dir1/ dir1/1.txt dir1/dir2/ dir1/dir2/2.txt.xz dir1/dir2/1.txt [root@localhost tmp]# ls -l 总用量 133984 -rw-r--r--. 1 root root 25865478 ... 1.txt -rw-r--r--. 1 root root 25876480 ... 1.txt.tar drwxr-xr-x. 3 root root 31 ... dir1 -rw-r--r--. 1 root root 85452800 ... test.tar
2. 解包,直接覆盖原目录、文件(不会提示)
[root@localhost tmp]# tar -xvf 1.txt.tar 1.txt [root@localhost tmp]# ls -l 总用量 133984 -rw-r--r--. 1 root root 25865478 ... 21:32 1.txt -rw-r--r--. 1 root root 25876480 ... 21:32 1.txt.tar drwxr-xr-x. 3 root root 31 ... 21:13 dir1 -rw-r--r--. 1 root root 85452800 ... 21:33 test.tar
3. 查看压缩包内文件列表
# tar -tf test.tar [root@localhost tmp]# tar -tf test.tar 1.txt dir1/ dir1/1.txt dir1/dir2/ dir1/dir2/2.txt.xz dir1/dir2/1.txt
4. 打包除file1等文件或目录外的目录(过滤文件)
# --exclude后接匹配项 [root@localhost tmp]# man tar ... --exclude=PATTERN exclude files, given as a PATTERN ...
# 2种写法 # --exclude参数在要打包的目录以后 [root@localhost tmp]# tar -cvf dir3.tar dir1/ --exclude="*.xz" dir1/ dir1/1.txt dir1/dir2/ dir1/dir2/1.txt # --exclude参数在要打包的目录以前 [root@localhost tmp]# tar -cvf dir2.tar --exclude="*.xz" dir1/ dir1/ dir1/1.txt dir1/dir2/ dir1/dir2/1.txt
.gz文件:-z参数
[root@localhost tar]# tar -zcvf dir1.tar.gz ../dir1/ tar: 从成员名中删除开头的“../” ../dir1/ ../dir1/dir2/ ../dir1/dir2/2.txt.xz ../dir1/dir2/1.txt ../dir1/1.txt [root@localhost tar]# ls -lh 总用量 26M -rw-r--r--. 1 root root 26M ... 15:08 dir1.tar.gz # 解压缩,直接覆盖同名文件,不询问 [root@localhost tar]# tar -zxvf dir1.tar.gz dir1/ dir1/dir2/ dir1/dir2/2.txt.xz dir1/dir2/1.txt dir1/1.txt [root@localhost tar]# ls -lh 总用量 26M drwxr-xr-x. 3 root root 31 ... 21:13 dir1 -rw-r--r--. 1 root root 26M ... 15:08 dir1.tar.gz
2. .bz2文件:-j参数
# 打包并压缩 [root@localhost tar]# tar -jcvf dir.tar.bz2 ../dir1/ tar: 从成员名中删除开头的“../” ../dir1/ ../dir1/dir2/ ../dir1/dir2/2.txt.xz ../dir1/dir2/1.txt ../dir1/1.txt [root@localhost tar]# ls -lh 总用量 24M -rw-r--r--. 1 root root 24M ... 15:10 dir.tar.bz2 # 解包解压缩 [root@localhost tar]# tar -jxvf dir.tar.bz2 dir1/ dir1/dir2/ dir1/dir2/2.txt.xz dir1/dir2/1.txt dir1/1.txt [root@localhost tar]# ls -lh 总用量 24M drwxr-xr-x. 3 root root 31 ... 21:13 dir1 -rw-r--r--. 1 root root 24M ... 15:10 dir.tar.bz2
3. .xz文件:-J参数
# 打包压缩为.xz文件 [root@localhost tar]# tar -Jcvf dir1.tar.xz ../dir1/ tar: 从成员名中删除开头的“../” ../dir1/ ../dir1/dir2/ ../dir1/dir2/2.txt.xz ../dir1/dir2/1.txt ../dir1/1.txt [root@localhost tar]# ls -lh 总用量 22M -rw-r--r--. 1 root root 22M ... 15:12 dir1.tar.xz # 解压 [root@localhost tar]# tar -Jxvf dir1.tar.xz dir1/ dir1/dir2/ dir1/dir2/2.txt.xz dir1/dir2/1.txt dir1/1.txt [root@localhost tar]# ls -lh 总用量 22M drwxr-xr-x. 3 root root 31 ... 21:13 dir1 -rw-r--r--. 1 root root 22M ... 15:12 dir1.tar.xz
4. 查看包内文件列表:-tf参数
[root@localhost tar]# tar -tf dir1.tar.xz dir1/ dir1/dir2/ dir1/dir2/2.txt.xz dir1/dir2/1.txt dir1/1.txt
tar命令自己没法选择压缩级别,在某些状况下能够经过先压缩再打包的方式来实现指定级别的压缩!
# 先按指定级别进行压缩 [root@localhost tar]# gzip -9 ../dir1/1.txt # 打包已压缩的文件 [root@localhost tar]# tar -cvf 1.tar.gz ../dir1/1.txt.gz tar: 从成员名中删除开头的“../” ../dir1/1.txt.gz [root@localhost tar]# ls -lh 总用量 9.2M -rw-r--r--. 1 root root 9.2M ... 15:19 1.tar.gz