本文共 1304 字,大约阅读时间需要 4 分钟。
工作中搭建测试环境,有个需求如下:
指定路径下,新建多个子目录,每个子目录下面还有3个二级子目录,命令行敲命令新建的话,效率有点低,可以使用脚本实现;
脚本如下:
1 2 3 4 5 6 7 8 | [root@localhost ~] # cat 1.sh #!/bin/bash datadir= "/data/test/testdata" svc=(a100 b200 c300 d400 e500 f600 g700 h800 i900 j1000 k1100) for ((i=0;i<${ #svc[*]};i++)) do mkdir -p $datadir/${svc[$i]}/{bin,conf,log} done |
脚本解释:
datadir定义目录路径地址;
svc数组存放子目录的名称;
使用for循环轮询创建;
执行后的结果如下:
批量创建11个子目录及二级子目录;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | [root@localhost ~] # sh 1.sh [root@localhost ~] # ll /data/test/testdata/ total 44 drwxr-xr-x 5 root root 4096 Aug 22 14:55 a100 drwxr-xr-x 5 root root 4096 Aug 22 14:55 b200 drwxr-xr-x 5 root root 4096 Aug 22 14:55 c300 drwxr-xr-x 5 root root 4096 Aug 22 14:55 d400 drwxr-xr-x 5 root root 4096 Aug 22 14:55 e500 drwxr-xr-x 5 root root 4096 Aug 22 14:55 f600 drwxr-xr-x 5 root root 4096 Aug 22 14:55 g700 drwxr-xr-x 5 root root 4096 Aug 22 14:55 h800 drwxr-xr-x 5 root root 4096 Aug 22 14:55 i900 drwxr-xr-x 5 root root 4096 Aug 22 14:55 j1000 drwxr-xr-x 5 root root 4096 Aug 22 14:55 k1100 [root@localhost ~] # ll /data/test/testdata/a100/ total 12 drwxr-xr-x 2 root root 4096 Aug 22 14:55 bin drwxr-xr-x 2 root root 4096 Aug 22 14:55 conf drwxr-xr-x 2 root root 4096 Aug 22 14:55 log |