looping some statements

Hi,
assume there are some dir structure like -

I need to write a script to create 5 new directories under 'qwe' dir of all the above 3 dir structures. these 5 dir will have same name.
I don't want to use 15 mkdir statements. i just want to write 5 mkdir statemets and use them 3 times. I appreciate any help.

I am on UX-HP machine.

use -p option of mkdir.

mkdir -p /xyz/abc/123/qwe/dir1/dir2/dir3/dir4/dir5
mkdir -p /xyz/def/123/qwe/dir1/dir2/dir3/dir4/dir5
mkdir -p /xyz/ghi/123/qwe/dir1/dir2/dir3/dir4/dir5

Thanks Rohan. I am using mkdir -p option. my req is not to create dir1 to dir5 one below the other but to create all 5 under 'qwe'
/xyz/abc/123/qwe/dir1
/xyz/abc/123/qwe/dir2
.
.
/xyz/def/123/qwe/dir4
.
.
.
./xyz/ghi/123/qwe/dir5

Try:

for dir in /xyz/abc/123/qwe /xyz/dfe/123/qwe /xyz/ghi/123/qwe
do
 mkdir -p $dir/dirA $dir/dirB $dir/dirC $dir/dirD $dir/dirE
done
1 Like

try this

 
mkdir -pv /xyz/{ghi,dfe,abc}/qwe/dir{1,2,3,4,5}

thanks Anchal...it works !
Still 1 more doubt :slight_smile: is there any way we can assign the path
'/xyz/abc/123/qwe' to a variable and then use it in for loop's condition? like

path1 = /xyz/abc/123/qwe
path2 = /xyz/dfe/123/qwe
path3 = /xyz/ghi/123/qwe

for dir in path1 path2 path3
do
 mkdir -p $dir/dirA $dir/dirB $dir/dirC $dir/dirD $dir/dirE
done
path1="/xyz/abc/123/qwe"
path2="/xyz/dfe/123/qwe"
path3="/xyz/ghi/123/qwe"

for dir in $path1 $path2 $path3
do
mkdir -p $dir/dirA $dir/dirB $dir/dirC $dir/dirD $dir/dirE
done

thanx Anurag... 1 more quick doubt/ req :wink:
what if the name of the dir 'qwe' is read a command line argument. that is $1 contains the name of the dir. and I need to assign the path1 = "/xyz/abc/123/$1"

The above assignment doesn't seems to be working.

path1="/xyz/abc/123/$1"

No space on either side of equal sign.
If still doesn't work, Pls post the code and the error you get.

1 Like

"NO space" is the key to the success. :wink: thanx Anurag and all for the help. i got my script working perfectly now.

set -- /xyz/abc/123/qwe /xyz/dfe/123/qwe /xyz/ghi/123/qwe
for i in $*
do
    mkdir -p $i
done