tar 批量打包一个文件夹下的多个目录(tar批量打包与解压)
对一个目录下的所有文件夹进行打包,当文件加比较少的时候,可以使用 tar 一个个进行操作,当目录下的文件夹比较多的时候,一个个打包就显得特别慢,这时候需要用一个脚本进行批量化打包,生成一个个独立的打包文件。
原始文件结构
pv@pv:~/Desktop/tmp$ tree ../tmp/
../tmp/
├── 1
│ └── Untitled Document
├── 11
├── 12
├── 1_back
│ └── Untitled Document
├── 2
│ ├── Untitled Document
│ └── Untitled Document (copy)
├── 3
│ └── Untitled Document
├── a
├── b
└── tar.sh
6 directories, 8 files
批量打包
- 指令
pv@pv:~/Desktop/tmp$ ls | awk '{ print "tar zcvf "$0".tar.gz " $0|"/bin/bash" }'
- 打包之后文件结构
pv@pv:~/Desktop/tmp$ tree ../tmp/
../tmp/
├── 1
│ └── Untitled Document
├── 11
├── 11.tar.gz
├── 12
├── 12.tar.gz
├── 1_back
│ └── Untitled Document
├── 1_back.tar.gz
├── 1.tar.gz
├── 2
│ ├── Untitled Document
│ └── Untitled Document (copy)
├── 2.tar.gz
├── 3
│ └── Untitled Document
├── 3.tar.gz
├── a
├── a.tar.gz
├── b
├── b.tar.gz
├── tar.sh
└── tar.sh.tar.gz
6 directories, 17 files
批量解压
在打包之后文件结构
上面的基础上进行演示
创建解压文件目录 tarTmp,将所有打包的文件移动到 tarTmp
pv@pv:~/Desktop/tmp$ mkdir tarTmp && mv *.gz tarTmp && cd tarTmp
pv@pv:~/Desktop/tmp/tarTmp$ ls
11.tar.gz 12.tar.gz 1_back.tar.gz 1.tar.gz 2.tar.gz 3.tar.gz a.tar.gz b.tar.gz tar.sh.tar.gz
pv@pv:~/Desktop/tmp/tarTmp$
- 批量解压
pv@pv:~/Desktop/tmp/tarTmp$ for i in $(ls *.tar.gz);do tar xzvf $i;done
解压结果
pv@pv:~/Desktop/tmp/tarTmp$ tree ../tarTmp/
../tarTmp/
├── 1
│ └── Untitled Document
├── 11
├── 11.tar.gz
├── 12
├── 12.tar.gz
├── 1_back
│ └── Untitled Document
├── 1_back.tar.gz
├── 1.tar.gz
├── 2
│ ├── Untitled Document
│ └── Untitled Document (copy)
├── 2.tar.gz
├── 3
│ └── Untitled Document
├── 3.tar.gz
├── a
├── a.tar.gz
├── b
├── b.tar.gz
├── tar.sh
└── tar.sh.tar.gz
6 directories, 17 files
tar命令批量解压方法_kwame211的博客-CSDN博客
https://blog.csdn.net/kwame211/article/details/88417993