Automate creation of directory structure in Linux using shell script

bash

#1

Hey I’m playing around with getting a range of folders created via script:

This is what i’ve got:

for i in {1..9}; 
do 
  mkdir -- "$i"/{$i}; 
done

I’m getting directory 1-9 : no such file or directory, seems close but not quite,

Cheers


#2

Got this to work:

for i in {1..9}; 
do 
   mkdir -- "$dir"./{$i}; 
done

#3

Your syntax is not correct. Try:

#!/bin/bash
for i in {1..9}
do 
  mkdir -p "${i}/${i}" 
done


#4

could you elaborate the difference between the ./ i used (i’m assuming i needed to state 'current working directory where script is executed) versus the / you used between the i index variable?

thanks!


#5

The ./ (current directory) is not needed. That is all. Different ways to solve problem.


#6

Hi

When I change the {1…9} to a string it makes it as a single directory ex. assign01…assign09.

for i in {“assign01…assign09”};

do mkdir -p “$i”/{$i};

done

Thanks for all the quick replies!

I see i’m not giving it a way to determine the increment of the string (assing01, assign02 etc.)


#7

So I’m thinking something like this may get me closer to what i want:

for $i in [“assign0$i”]

while ($i= 1; $i <= 9;$i = $i + 1 )

do mkdir -p “${i}/{$i}”

Any tips on getting that to work in bash (macOS)?

Cheers


#8

May I know what kind of dirs are you trying to create here? Can you give an example of dir pattern? Say

dir1
dir2

OR

dir1/1
dir2/2

#9

assign01
assign02

Thanks!


#10

You don’t even need a bash for loop. Just do it:

echo "assign0"{1..9}
mkdir "assign0"{1..9} 

But if you need a bash for loop, try:

for i in {01..09}
do
  mkdir assing"${i}"
done

That is all.


#11

Thats awesome. I guess i was overthinking it…


#12

Dijkstra’s approach is must:

Simplicity and elegance are unpopular because they require hard work and discipline to achieve and education to be appreciated.