我必须创建50个文件夹,每个文件夹中应该有2*2个文件,例如
folder1
.
.
folder5 - has 2 files in it
folder 6
.
.
folder 10 - has 4 files in it
.
.
folder 15 - has 8 files in it
这是我的代码:
#!/bin/bash
n=1
declare -i countFolder
for (( countFolder = 1; countFolder <= 50; countFolder++ ))
do
mkdir Folder$countFolder
done
for (( countFolder = 5; countFolder <= 50; countFolder = countFolder+5 ))
do
let "n = n * 2"
for (( f = 0; f < n; f++ )) do
cd Folder$countFolder && touch File$f.txt
done
done
最佳答案
这个问题是您cd
进入了一个目录,但您从未返回。
最简单的修复方法是在cd
周围添加圆括号。这些括号开始一个子shell,因此cd
保持在其中:
for (( f = 0; f < n; f++ )) do
( cd Folder$countFolder && touch File$f.txt )
done
这相当于但比手册短:
for (( f = 0; f < n; f++ )) do
cd Folder$countFolder && { touch File$f.txt; cd ..; }
done
关于linux - 使用bash脚本创建文件夹和文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37449516/