Automated way to create a directory tree

Solution 1:

With mkdir, printf and bash's brace expansion:

$ mkdir -p "$(printf "%s/" {A..Z})"
$ tree A
A
└── B
    └── C
        └── D
            └── E
                └── F
                    └── G
                        └── H
                            └── I
                                └── J
                                    └── K
                                        └── L
                                            └── M
                                                └── N
                                                    └── O
                                                        └── P
                                                            └── Q
                                                                └── R
                                                                    └── S
                                                                        └── T
                                                                            └── U
                                                                                └── V
                                                                                    └── W
                                                                                        └── X
                                                                                            └── Y
                                                                                                └── Z

25 directories, 0 files
  • {A..Z} expands to A B ... Z,
  • printf "%s/" prints the arguments with a / after them, so I get A/B/...Z/
  • and mkdir -p creates the A/B/.../Z directory with any parent directories that needed creating.

Solution 2:

At the very simple level, you could make use of {A..Z} expansion to generate all the letter, then iteratively make and enter each one:

~/test_directory$ for d in {A..Z}; do mkdir "$d"; cd "$d"; done
~/test_directory/A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R/S/T/U/V/W/X/Y/Z$ 

As you can see in my prompt output, now you have completely chained directory.

However, if actual directory names are different than just alphabet, you'll have to somehow provide the list of directory names, perhaps via a file, over which you iterate and do same process again. Basically, this

while IFS= read -r dir_name;do mkdir "$dir_name"; cd "$dir_name" ;done < directory_list.txt