Shell script to make directories, subdirectories and files following a pattern

scriptingshell-script

Is it possible to create a script that will create the following structure after running it:

/test/1/1 -> A
/test/1/2 -> B
/test/1/3 -> C
/test/1/4 -> D
/test/1/5 -> E
/test/1/6 -> F
/test/1/7 -> G
/test/1/8 -> H
/test/1/9 -> I
/test/1/10 -> J 
/test/1/11 -> K
/test/1/12 -> L
/test/1/13 -> M
/test/1/14 -> N
/test/1/15 -> O
/test/2/1 -> P
/test/2/2 -> Q
/test/2/3 -> R
/test/2/4 -> S
/test/2/5 -> T
/test/2/6 -> U
/test/2/7 -> V
/test/2/8 -> X
/test/2/9 -> Y
/test/2/10 -> Z
/test/2/11 -> A
/test/2/12 -> B
/test/2/13 -> C
/test/2/14 -> D
/test/2/15 -> E
/test/3/1 -> F
/test/3/2 -> G
/test/3/3 -> H
.........
/test/10/15 -> ???

As you can see, in this example, the directory "test" is the parent directory and the numbered ones are subdirectories. The letters are files, not folders. Each time the alphabet ends, it will start from the beginning in the next directory.

How can I create a script that will make this easier rather than just creating them manually?

Best Answer

With zsh:

dirs=(test/{1..10}/{1..15}) n=0
mkdir -p $dirs &&
  for d ($dirs) {:>$d/$(([##36]n%26+10)); ((n++))}

The trick above being to use base36 numbers where digits 10 to 35 are expressed with A.. Z. Would be a bit more legible and generalisable with:

dirs=(test/{1..10}/{1..15}) n=0 l=({A..Z})
mkdir -p $dirs && for d ($dirs) : > $d/$l[1 + n++ % $#l]
Related Question