samcorcos samcorcos - 7 months ago 10
Bash Question

mkdir: omit leading pathname when creating multiple directories?

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.

Answer

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.