I am trying to make a directory tree from A to Z where the next directory is within the current directory.
For example: B is within A and C is within B and so on..
-A
--B
---C
----...Z
Any clues on how to get it done the automated way?
I am trying to make a directory tree from A to Z where the next directory is within the current directory.
For example: B is within A and C is within B and so on..
-A
--B
---C
----...Z
Any clues on how to get it done the automated way?
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/mkdir -p creates the A/B/.../Z directory with any parent directories that needed creating.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
Even though muru's printf way can't be beat, I personally like jot for this sort of thing. jot isn't installed by default in Ubuntu. The athena-jot package provides it. Either of these commands works:
mkdir -p "$(jot -s/ -c 26 A)"
jot -s/ -c 26 A | xargs mkdir -p
Really any command that generates the sequence of letters and joins them with slashes will facilitate this, because its output can then be passed to mkdir -p either through command substitution (as in muru's answer) or using xargs. Here are some examples using a few tools and xargs that don't require that you install software, except perhaps on very minimal systems or Ubuntu Core:
perl -we 'print join "/", A..Z' | xargs mkdir -p
ruby -we 'print (?A..?Z).to_a * ?/' | xargs mkdir -p
python3 -c 'print("/".join(__import__("string").ascii_uppercase))' | xargs mkdir -p
Old Ubuntu releases come with Python 2 instead of Python 3. For that, just change python3 to python to make that last command work, if you really want to do this with Python.
Similarly, muru's short and simple way can alternatively be written:
printf '%s/' {A..Z} | xargs mkdir -p
The trailing /, in the directory path mkdir -p is asked to create, is no problem and arguably is stylistically preferable. But it's fine to omit it, as the other examples in this answer do.