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.


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.

  • @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 2 '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 2 '11 at 13:52

Maybe this is what you are looking for?

 mkdir -p work/{F1/{temp1,temp2},F2,F3}
  • Hey, I was wondering recently if things could be done as simple as that. Thanks! – rozcietrzewiacz Aug 2 '11 at 13:55

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).

  • 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. 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 6 '18 at 20:13
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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.