18

As man mkdir states

   -p, --parents
          no error if existing, make parent directories as needed

When I ran this command

mkdir -p work/{F1,F2,F3}/{temp1,temp2}

It creates a folder structure like this work parent folder then F1,F2,F3 child folders and temp1 and temp2 child folders under three parent folder F1,F2,F3.

   work
     -F1
       -temp1
       -temp2
     -F2
       -temp1
       -temp2
     -F3
       -temp1
       -temp2

Now the problem is that I want to create temp1,temp2 folders only under F1 not under F2 and F3, but I'm confused on how I can write a command to do what I want.

sdanzig
  • 103
  • 3
Vishwanath Dalvi
  • 4,346
  • 5
  • 20
  • 17
  • @Caleb: sorry, I was not aware we were both editing at the same time, it seems that revision merging is not implemented by SE :-/ – Stéphane Gimenez Aug 02 '11 at 13:48
  • @Stephane: No worries. I've made a lot of edits and it's pretty rare that two major edits get clobbered like that. Thanks for contributing! – Caleb Aug 02 '11 at 13:52

3 Answers3

28

Maybe this is what you are looking for?

 mkdir -p work/{F1/{temp1,temp2},F2,F3}
Stéphane Gimenez
  • 28,527
  • 3
  • 76
  • 87
3

A very good description of brace expansion (with examples) can be found at subsection Brace Expansion of bash manual (man bash, press / to start search and search for Brace Expansion).

rozcietrzewiacz
  • 38,754
  • 9
  • 94
  • 102
  • Thank you, your answer led me to this great guide! Just needed to know that it's called "Brace Expansion" so I knew what to google. http://linuxcommand.org/lc3_lts0080.php Excerpt: "Perhaps the strangest expansion is called brace expansion. With it, you can create multiple text strings from a pattern containing braces. Here's an example: `[me@linuxbox me]$ echo Front-{A,B,C}-Back` `Front-A-Back Front-B-Back Front-C-Back`" – Rock Lee Jun 06 '18 at 20:13
0
mkdir -p work/F{1..3} work/F1/temp{1,2}

This first creates work and the three subdirectories before creating the lower level directories of work/F1. It's easy to read and to understand.

Or, if you absolutely need to combine everything into a single monster expression (there is absolutely no need to do so as it's difficult to read and maintain):

mkdir -p work/F{1/temp{1,2},2,3}
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • how about adding variables inside the curly braces? Like so : `mkdir -p work/{F1/{"$var"},F2,F3}`, where `var='temp1,temp2'` – Snow Jun 27 '22 at 15:53
  • @Snow Did you try that to see what happens? – Kusalananda Jun 27 '22 at 17:09
  • yes, it creates a single folder named "{temp1,temp2}" – Snow Jun 27 '22 at 17:12
  • 1
    @Snow Yes, the order of evaluation is such that the brace expansion happens first and the expansion of variables later. This means that the value of a variable can not affect a brace expansion. – Kusalananda Jun 27 '22 at 17:17