0

I'm sure this question has been asked elsewhere but I can't seem to phrase it in a way that returns a useful Google result.

I am creating a dozen directories that all have the same root path and I don't want to have to cd into it to be able to make these directories. The current command looks like something like, which is awful and repetitive:

$ mkdir frontend/app/components/Home frontend/app/components/Profile \ 
  frontend/app/components/Post frontend/app/components/Comment

An ideal syntax would be something along the lines of:

$ mkdir frontend/app/components/{Home, Profile, Post, Comment}

Is there something like this already that I just haven't found? I don't want to have to run a for loop just to make a few directories.

samcorcos
  • 2,330
  • 4
  • 27
  • 40
  • 1
    The "duplicate" is incorrect. The question linked to is about `mkdir -p`, while this question is not (see comments on @anubhava's answer). Anyway, there's an answer and the OP is happy, that's the most important :-). – Matthieu Moy Apr 20 '16 at 06:37

2 Answers2

2

Your wish is granted :-).

mkdir doesn't know and doesn't have to, but shells like bash or zsh understand the syntax {...,...,...}.

Just remove the spaces from your "along the lines of" and it works:

mkdir frontend/app/components/{Home,Profile,Post,Comment}

The shell will expand it to

mkdir frontend/app/components/Home frontend/app/components/Profile frontend/app/components/Post frontend/app/components/Comment

Since it is done by the shell, it works with any command.

Matthieu Moy
  • 15,151
  • 5
  • 38
  • 65
  • Ha! Looks like `bash` knows what it's doing. This even works with `touch` ``` touch app/components/{Home/index.js,ProfileShow/index.js,PostShow/index.js} ``` – samcorcos Apr 19 '16 at 20:31
  • 2
    @samcorcos The whole point of doing these types of replacements in the shell is that it works consistently with **any** command, since they just see the expanded arguments. – Barmar Apr 19 '16 at 20:35
  • @barmar That's good to know. – samcorcos Apr 19 '16 at 20:46
1

Remove spaces around comma and use -p option:

mkdir -p frontend/app/components/{Home,Profile,Post,Comment}
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • The `-p` option isn't needed unless `frontend/app/components` doesn't exist already. – Barmar Apr 19 '16 at 20:36
  • Yes that is right but `-p` is handy to suppress error for already existing directories as well. – anubhava Apr 19 '16 at 20:40
  • Why would you want to suppress the error if the directories aren't supposed to exist yet. It seems like this option is totally unrelated to the question. – Barmar Apr 19 '16 at 20:44
  • Yes a bit unrelated to the question. I just find it handy for the situation when say `Post` is already existing but remaining ones aren't – anubhava Apr 19 '16 at 20:46