Automated way to create a directory tree
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
11
down vote
favorite
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?
command-line directory
add a comment |Â
up vote
11
down vote
favorite
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?
command-line directory
I wonder whatâÂÂs the practical purpose of such directories. IMHO this looks more like a question for Programming Puzzles & Code Golf.
â Melebius
Apr 10 at 9:33
3
@Melebius One possible purpose is to create a directory structure as a test case, in order to help verify that some other command, script, or application works property. Here's a recent instance where the question itself wasn't about creating specific directory structures, but knowing how to write commands to do so was useful for testing purposes, not in spite of how the specific structure was contrived and weird, but because of it. (We do also have many questions about creating specific directory structures, which can be found by searching for "mkdir -p
".)
â Eliah Kagan
Apr 10 at 13:11
add a comment |Â
up vote
11
down vote
favorite
up vote
11
down vote
favorite
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?
command-line directory
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?
command-line directory
command-line directory
edited Apr 10 at 12:51
![](https://i.stack.imgur.com/E0SEH.png?s=32&g=1)
![](https://i.stack.imgur.com/E0SEH.png?s=32&g=1)
David Foerster
26.2k1361106
26.2k1361106
asked Apr 10 at 5:32
![](https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=32)
![](https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=32)
Nish
483
483
I wonder whatâÂÂs the practical purpose of such directories. IMHO this looks more like a question for Programming Puzzles & Code Golf.
â Melebius
Apr 10 at 9:33
3
@Melebius One possible purpose is to create a directory structure as a test case, in order to help verify that some other command, script, or application works property. Here's a recent instance where the question itself wasn't about creating specific directory structures, but knowing how to write commands to do so was useful for testing purposes, not in spite of how the specific structure was contrived and weird, but because of it. (We do also have many questions about creating specific directory structures, which can be found by searching for "mkdir -p
".)
â Eliah Kagan
Apr 10 at 13:11
add a comment |Â
I wonder whatâÂÂs the practical purpose of such directories. IMHO this looks more like a question for Programming Puzzles & Code Golf.
â Melebius
Apr 10 at 9:33
3
@Melebius One possible purpose is to create a directory structure as a test case, in order to help verify that some other command, script, or application works property. Here's a recent instance where the question itself wasn't about creating specific directory structures, but knowing how to write commands to do so was useful for testing purposes, not in spite of how the specific structure was contrived and weird, but because of it. (We do also have many questions about creating specific directory structures, which can be found by searching for "mkdir -p
".)
â Eliah Kagan
Apr 10 at 13:11
I wonder whatâÂÂs the practical purpose of such directories. IMHO this looks more like a question for Programming Puzzles & Code Golf.
â Melebius
Apr 10 at 9:33
I wonder whatâÂÂs the practical purpose of such directories. IMHO this looks more like a question for Programming Puzzles & Code Golf.
â Melebius
Apr 10 at 9:33
3
3
@Melebius One possible purpose is to create a directory structure as a test case, in order to help verify that some other command, script, or application works property. Here's a recent instance where the question itself wasn't about creating specific directory structures, but knowing how to write commands to do so was useful for testing purposes, not in spite of how the specific structure was contrived and weird, but because of it. (We do also have many questions about creating specific directory structures, which can be found by searching for "
mkdir -p
".)â Eliah Kagan
Apr 10 at 13:11
@Melebius One possible purpose is to create a directory structure as a test case, in order to help verify that some other command, script, or application works property. Here's a recent instance where the question itself wasn't about creating specific directory structures, but knowing how to write commands to do so was useful for testing purposes, not in spite of how the specific structure was contrived and weird, but because of it. (We do also have many questions about creating specific directory structures, which can be found by searching for "
mkdir -p
".)â Eliah Kagan
Apr 10 at 13:11
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
25
down vote
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 toA B ... Z
,printf "%s/"
prints the arguments with a/
after them, so I getA/B/...Z/
- and
mkdir -p
creates theA/B/.../Z
directory with any parent directories that needed creating.
thanks for that. Thought I get this error while . I use the command mkdir: 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: No such file or directory
â Nish
Apr 10 at 6:08
3
@Nish There's a-p
in themkdir
command.
â muru
Apr 10 at 6:09
my bad, i tried that on my mac instead of ubuntu. And a follow up question is this--> let's say I wanted to have A1..A100 directory within A (in the same structure AtoZ). Then B1..B100 within B. So it looks something like this --> imgur.com/jfuKEdf
â Nish
Apr 10 at 6:17
11
Let's say you post a follow-up as an actual question instead of in the comments, and didn't use images but text. That would be much better, no?
â muru
Apr 10 at 6:25
I understand your point here. Its difficult to post it as its not a code and its a hugeass output at that. So simply looking at the screenshot makes it easier to understand.
â Nish
Apr 10 at 6:29
 |Â
show 3 more comments
up vote
7
down vote
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
thats pretty cool. Here's a followup question. let's say I wanted to have A1..A100 directory within A (in the same structure AtoZ). Then B1..B100 within B. So it looks something like this --> imgur.com/jfuKEdf
â Nish
Apr 10 at 6:10
@Nish ah, that'd require an inner loop sofor d in A..Z; do mkdir "$d"; cd "$d"; for i in 1..100; do mkdir "$d$i" ;done ; done
â Sergiy Kolodyazhnyy
Apr 10 at 6:15
Or even without inner loop,for d in A..Z; do mkdir "$d"; cd "$d"; mkdir "$d"1..100; done
â Sergiy Kolodyazhnyy
Apr 10 at 6:17
thanks for that. It seems to work but the order of the directory seem screwed up so 2 is not after 1. Its sorted randomly. Anyway to fix that? Here's a screenshot --> imgur.com/a/Zqj1c
â Nish
Apr 10 at 6:22
@Nish Sorting order is text-based, not numerical as I think you expect. As far astree
, I'm not sure how to fix that, but I think this might not even betree
's problem, but rather shell's sorting order. I'd suggest asking actual question about that either here or better yet on unix.stackexchange.com
â Sergiy Kolodyazhnyy
Apr 10 at 7:05
 |Â
show 2 more comments
up vote
2
down vote
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.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
25
down vote
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 toA B ... Z
,printf "%s/"
prints the arguments with a/
after them, so I getA/B/...Z/
- and
mkdir -p
creates theA/B/.../Z
directory with any parent directories that needed creating.
thanks for that. Thought I get this error while . I use the command mkdir: 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: No such file or directory
â Nish
Apr 10 at 6:08
3
@Nish There's a-p
in themkdir
command.
â muru
Apr 10 at 6:09
my bad, i tried that on my mac instead of ubuntu. And a follow up question is this--> let's say I wanted to have A1..A100 directory within A (in the same structure AtoZ). Then B1..B100 within B. So it looks something like this --> imgur.com/jfuKEdf
â Nish
Apr 10 at 6:17
11
Let's say you post a follow-up as an actual question instead of in the comments, and didn't use images but text. That would be much better, no?
â muru
Apr 10 at 6:25
I understand your point here. Its difficult to post it as its not a code and its a hugeass output at that. So simply looking at the screenshot makes it easier to understand.
â Nish
Apr 10 at 6:29
 |Â
show 3 more comments
up vote
25
down vote
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 toA B ... Z
,printf "%s/"
prints the arguments with a/
after them, so I getA/B/...Z/
- and
mkdir -p
creates theA/B/.../Z
directory with any parent directories that needed creating.
thanks for that. Thought I get this error while . I use the command mkdir: 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: No such file or directory
â Nish
Apr 10 at 6:08
3
@Nish There's a-p
in themkdir
command.
â muru
Apr 10 at 6:09
my bad, i tried that on my mac instead of ubuntu. And a follow up question is this--> let's say I wanted to have A1..A100 directory within A (in the same structure AtoZ). Then B1..B100 within B. So it looks something like this --> imgur.com/jfuKEdf
â Nish
Apr 10 at 6:17
11
Let's say you post a follow-up as an actual question instead of in the comments, and didn't use images but text. That would be much better, no?
â muru
Apr 10 at 6:25
I understand your point here. Its difficult to post it as its not a code and its a hugeass output at that. So simply looking at the screenshot makes it easier to understand.
â Nish
Apr 10 at 6:29
 |Â
show 3 more comments
up vote
25
down vote
up vote
25
down vote
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 toA B ... Z
,printf "%s/"
prints the arguments with a/
after them, so I getA/B/...Z/
- and
mkdir -p
creates theA/B/.../Z
directory with any parent directories that needed creating.
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 toA B ... Z
,printf "%s/"
prints the arguments with a/
after them, so I getA/B/...Z/
- and
mkdir -p
creates theA/B/.../Z
directory with any parent directories that needed creating.
edited Apr 10 at 9:10
Volker Siegel
8,65043349
8,65043349
answered Apr 10 at 5:45
muru
130k19273463
130k19273463
thanks for that. Thought I get this error while . I use the command mkdir: 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: No such file or directory
â Nish
Apr 10 at 6:08
3
@Nish There's a-p
in themkdir
command.
â muru
Apr 10 at 6:09
my bad, i tried that on my mac instead of ubuntu. And a follow up question is this--> let's say I wanted to have A1..A100 directory within A (in the same structure AtoZ). Then B1..B100 within B. So it looks something like this --> imgur.com/jfuKEdf
â Nish
Apr 10 at 6:17
11
Let's say you post a follow-up as an actual question instead of in the comments, and didn't use images but text. That would be much better, no?
â muru
Apr 10 at 6:25
I understand your point here. Its difficult to post it as its not a code and its a hugeass output at that. So simply looking at the screenshot makes it easier to understand.
â Nish
Apr 10 at 6:29
 |Â
show 3 more comments
thanks for that. Thought I get this error while . I use the command mkdir: 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: No such file or directory
â Nish
Apr 10 at 6:08
3
@Nish There's a-p
in themkdir
command.
â muru
Apr 10 at 6:09
my bad, i tried that on my mac instead of ubuntu. And a follow up question is this--> let's say I wanted to have A1..A100 directory within A (in the same structure AtoZ). Then B1..B100 within B. So it looks something like this --> imgur.com/jfuKEdf
â Nish
Apr 10 at 6:17
11
Let's say you post a follow-up as an actual question instead of in the comments, and didn't use images but text. That would be much better, no?
â muru
Apr 10 at 6:25
I understand your point here. Its difficult to post it as its not a code and its a hugeass output at that. So simply looking at the screenshot makes it easier to understand.
â Nish
Apr 10 at 6:29
thanks for that. Thought I get this error while . I use the command mkdir: 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: No such file or directory
â Nish
Apr 10 at 6:08
thanks for that. Thought I get this error while . I use the command mkdir: 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: No such file or directory
â Nish
Apr 10 at 6:08
3
3
@Nish There's a
-p
in the mkdir
command.â muru
Apr 10 at 6:09
@Nish There's a
-p
in the mkdir
command.â muru
Apr 10 at 6:09
my bad, i tried that on my mac instead of ubuntu. And a follow up question is this--> let's say I wanted to have A1..A100 directory within A (in the same structure AtoZ). Then B1..B100 within B. So it looks something like this --> imgur.com/jfuKEdf
â Nish
Apr 10 at 6:17
my bad, i tried that on my mac instead of ubuntu. And a follow up question is this--> let's say I wanted to have A1..A100 directory within A (in the same structure AtoZ). Then B1..B100 within B. So it looks something like this --> imgur.com/jfuKEdf
â Nish
Apr 10 at 6:17
11
11
Let's say you post a follow-up as an actual question instead of in the comments, and didn't use images but text. That would be much better, no?
â muru
Apr 10 at 6:25
Let's say you post a follow-up as an actual question instead of in the comments, and didn't use images but text. That would be much better, no?
â muru
Apr 10 at 6:25
I understand your point here. Its difficult to post it as its not a code and its a hugeass output at that. So simply looking at the screenshot makes it easier to understand.
â Nish
Apr 10 at 6:29
I understand your point here. Its difficult to post it as its not a code and its a hugeass output at that. So simply looking at the screenshot makes it easier to understand.
â Nish
Apr 10 at 6:29
 |Â
show 3 more comments
up vote
7
down vote
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
thats pretty cool. Here's a followup question. let's say I wanted to have A1..A100 directory within A (in the same structure AtoZ). Then B1..B100 within B. So it looks something like this --> imgur.com/jfuKEdf
â Nish
Apr 10 at 6:10
@Nish ah, that'd require an inner loop sofor d in A..Z; do mkdir "$d"; cd "$d"; for i in 1..100; do mkdir "$d$i" ;done ; done
â Sergiy Kolodyazhnyy
Apr 10 at 6:15
Or even without inner loop,for d in A..Z; do mkdir "$d"; cd "$d"; mkdir "$d"1..100; done
â Sergiy Kolodyazhnyy
Apr 10 at 6:17
thanks for that. It seems to work but the order of the directory seem screwed up so 2 is not after 1. Its sorted randomly. Anyway to fix that? Here's a screenshot --> imgur.com/a/Zqj1c
â Nish
Apr 10 at 6:22
@Nish Sorting order is text-based, not numerical as I think you expect. As far astree
, I'm not sure how to fix that, but I think this might not even betree
's problem, but rather shell's sorting order. I'd suggest asking actual question about that either here or better yet on unix.stackexchange.com
â Sergiy Kolodyazhnyy
Apr 10 at 7:05
 |Â
show 2 more comments
up vote
7
down vote
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
thats pretty cool. Here's a followup question. let's say I wanted to have A1..A100 directory within A (in the same structure AtoZ). Then B1..B100 within B. So it looks something like this --> imgur.com/jfuKEdf
â Nish
Apr 10 at 6:10
@Nish ah, that'd require an inner loop sofor d in A..Z; do mkdir "$d"; cd "$d"; for i in 1..100; do mkdir "$d$i" ;done ; done
â Sergiy Kolodyazhnyy
Apr 10 at 6:15
Or even without inner loop,for d in A..Z; do mkdir "$d"; cd "$d"; mkdir "$d"1..100; done
â Sergiy Kolodyazhnyy
Apr 10 at 6:17
thanks for that. It seems to work but the order of the directory seem screwed up so 2 is not after 1. Its sorted randomly. Anyway to fix that? Here's a screenshot --> imgur.com/a/Zqj1c
â Nish
Apr 10 at 6:22
@Nish Sorting order is text-based, not numerical as I think you expect. As far astree
, I'm not sure how to fix that, but I think this might not even betree
's problem, but rather shell's sorting order. I'd suggest asking actual question about that either here or better yet on unix.stackexchange.com
â Sergiy Kolodyazhnyy
Apr 10 at 7:05
 |Â
show 2 more comments
up vote
7
down vote
up vote
7
down vote
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
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
answered Apr 10 at 5:42
![](https://i.stack.imgur.com/U1Jy6.jpg?s=32&g=1)
![](https://i.stack.imgur.com/U1Jy6.jpg?s=32&g=1)
Sergiy Kolodyazhnyy
65.3k9130286
65.3k9130286
thats pretty cool. Here's a followup question. let's say I wanted to have A1..A100 directory within A (in the same structure AtoZ). Then B1..B100 within B. So it looks something like this --> imgur.com/jfuKEdf
â Nish
Apr 10 at 6:10
@Nish ah, that'd require an inner loop sofor d in A..Z; do mkdir "$d"; cd "$d"; for i in 1..100; do mkdir "$d$i" ;done ; done
â Sergiy Kolodyazhnyy
Apr 10 at 6:15
Or even without inner loop,for d in A..Z; do mkdir "$d"; cd "$d"; mkdir "$d"1..100; done
â Sergiy Kolodyazhnyy
Apr 10 at 6:17
thanks for that. It seems to work but the order of the directory seem screwed up so 2 is not after 1. Its sorted randomly. Anyway to fix that? Here's a screenshot --> imgur.com/a/Zqj1c
â Nish
Apr 10 at 6:22
@Nish Sorting order is text-based, not numerical as I think you expect. As far astree
, I'm not sure how to fix that, but I think this might not even betree
's problem, but rather shell's sorting order. I'd suggest asking actual question about that either here or better yet on unix.stackexchange.com
â Sergiy Kolodyazhnyy
Apr 10 at 7:05
 |Â
show 2 more comments
thats pretty cool. Here's a followup question. let's say I wanted to have A1..A100 directory within A (in the same structure AtoZ). Then B1..B100 within B. So it looks something like this --> imgur.com/jfuKEdf
â Nish
Apr 10 at 6:10
@Nish ah, that'd require an inner loop sofor d in A..Z; do mkdir "$d"; cd "$d"; for i in 1..100; do mkdir "$d$i" ;done ; done
â Sergiy Kolodyazhnyy
Apr 10 at 6:15
Or even without inner loop,for d in A..Z; do mkdir "$d"; cd "$d"; mkdir "$d"1..100; done
â Sergiy Kolodyazhnyy
Apr 10 at 6:17
thanks for that. It seems to work but the order of the directory seem screwed up so 2 is not after 1. Its sorted randomly. Anyway to fix that? Here's a screenshot --> imgur.com/a/Zqj1c
â Nish
Apr 10 at 6:22
@Nish Sorting order is text-based, not numerical as I think you expect. As far astree
, I'm not sure how to fix that, but I think this might not even betree
's problem, but rather shell's sorting order. I'd suggest asking actual question about that either here or better yet on unix.stackexchange.com
â Sergiy Kolodyazhnyy
Apr 10 at 7:05
thats pretty cool. Here's a followup question. let's say I wanted to have A1..A100 directory within A (in the same structure AtoZ). Then B1..B100 within B. So it looks something like this --> imgur.com/jfuKEdf
â Nish
Apr 10 at 6:10
thats pretty cool. Here's a followup question. let's say I wanted to have A1..A100 directory within A (in the same structure AtoZ). Then B1..B100 within B. So it looks something like this --> imgur.com/jfuKEdf
â Nish
Apr 10 at 6:10
@Nish ah, that'd require an inner loop so
for d in A..Z; do mkdir "$d"; cd "$d"; for i in 1..100; do mkdir "$d$i" ;done ; done
â Sergiy Kolodyazhnyy
Apr 10 at 6:15
@Nish ah, that'd require an inner loop so
for d in A..Z; do mkdir "$d"; cd "$d"; for i in 1..100; do mkdir "$d$i" ;done ; done
â Sergiy Kolodyazhnyy
Apr 10 at 6:15
Or even without inner loop,
for d in A..Z; do mkdir "$d"; cd "$d"; mkdir "$d"1..100; done
â Sergiy Kolodyazhnyy
Apr 10 at 6:17
Or even without inner loop,
for d in A..Z; do mkdir "$d"; cd "$d"; mkdir "$d"1..100; done
â Sergiy Kolodyazhnyy
Apr 10 at 6:17
thanks for that. It seems to work but the order of the directory seem screwed up so 2 is not after 1. Its sorted randomly. Anyway to fix that? Here's a screenshot --> imgur.com/a/Zqj1c
â Nish
Apr 10 at 6:22
thanks for that. It seems to work but the order of the directory seem screwed up so 2 is not after 1. Its sorted randomly. Anyway to fix that? Here's a screenshot --> imgur.com/a/Zqj1c
â Nish
Apr 10 at 6:22
@Nish Sorting order is text-based, not numerical as I think you expect. As far as
tree
, I'm not sure how to fix that, but I think this might not even be tree
's problem, but rather shell's sorting order. I'd suggest asking actual question about that either here or better yet on unix.stackexchange.comâ Sergiy Kolodyazhnyy
Apr 10 at 7:05
@Nish Sorting order is text-based, not numerical as I think you expect. As far as
tree
, I'm not sure how to fix that, but I think this might not even be tree
's problem, but rather shell's sorting order. I'd suggest asking actual question about that either here or better yet on unix.stackexchange.comâ Sergiy Kolodyazhnyy
Apr 10 at 7:05
 |Â
show 2 more comments
up vote
2
down vote
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.
add a comment |Â
up vote
2
down vote
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.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
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.
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.
edited Apr 11 at 14:49
answered Apr 11 at 12:07
Eliah Kagan
79.5k20221359
79.5k20221359
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1023515%2fautomated-way-to-create-a-directory-tree%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I wonder whatâÂÂs the practical purpose of such directories. IMHO this looks more like a question for Programming Puzzles & Code Golf.
â Melebius
Apr 10 at 9:33
3
@Melebius One possible purpose is to create a directory structure as a test case, in order to help verify that some other command, script, or application works property. Here's a recent instance where the question itself wasn't about creating specific directory structures, but knowing how to write commands to do so was useful for testing purposes, not in spite of how the specific structure was contrived and weird, but because of it. (We do also have many questions about creating specific directory structures, which can be found by searching for "
mkdir -p
".)â Eliah Kagan
Apr 10 at 13:11