~ 3 min read

Creating Nested Folders with bash

#
#  5 level deep nested directory creation - this is roughly 65.5 million folders
#

#!/bin/bash

for p in {0..9} {a..z}
do
  cd ${p}
  for m in {0..9} {a..z}
  do
    cd ${m}
    for n in {0..9} {a..z}
    do
      mkdir $n
      cd ${n}
        for y in {0..9} {a..z} 
        do
          mkdir $y          
          cd ${y}
          for z in {0..9} {a..z} 
          do
            mkdir $z
            # pwd         #you may want to avoid printint, or it will take foever even in a 3 level deep structure :)
          done
          cd ".."
        done
        cd ".."
    done
    cd ".."
  done
  cd ".."
done

I have not tested the following but it should work and is somewhat simpler


#!/bin/bash

for p in {0..9} {a..z}
do
    for m in {0..9} {a..z}
    do
        for n in {0..9} {a..z}
        do
            for y in {0..9} {a..z} 
            do
                for z in {0..9} {a..z} 
                do
                    mkdir -p "./${p}/${m}/${n}/${y}/${z}
                done
            done
        done
    done
done

another option for a flatter structure would be to use doubles i.e. 00 01 02 ….. 8b 8c …..zz

This gives you 36 * 36 = 1296 variations in the first level and then adding a second level would give you about a million combinations :) which if you think about it, even if each folder has 100 files - that 100 million files.