[英]Folder and File creating with bash scripting
I have to create 50 folders and each fifth folder should have 2*2 files in it, eg 我必须创建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
here is my code : 这是我的代码:
#!/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
The problem with this is that you cd
into a directory but you never cd
back. 这里的问题是,你
cd
到一个目录,但你永远cd
回来。
The simplest way of fixing this is by adding parentheses around the cd
. 解决此问题的最简单方法是在
cd
周围添加括号。 These parentheses start a subshell, so the cd
stays within it: 这些括号开始一个子shell,因此
cd
保留在其中:
for (( f = 0; f < n; f++ )) do
( cd Folder$countFolder && touch File$f.txt )
done
This is equivalent to but shorter than a manual cd ..
: 这相当于但比手动
cd ..
短cd ..
:
for (( f = 0; f < n; f++ )) do
cd Folder$countFolder && { touch File$f.txt; cd ..; }
done
You need to go into the directory, make your files, then leave. 您需要进入目录,制作文件,然后离开。 The most efficient way to do this is the following:
最有效的方法如下:
#!/bin/bash
n=1
declare -i countFolder
for (( countFolder = 1; countFolder <= 50; countFolder++ ))
do
mkdir Folder$countFolder
ls
done
for (( countFolder = 5; countFolder <= 50; countFolder = countFolder+5 ))
do
let "n = n * 2"
cd Folder$countFolder
for (( f = 0; f < n; f++ )) do
touch File$f.txt
done
cd ..
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.