Creating multiple nested directories with one command
37,218
Solution 1
mkdir
accepts multiple path arguments:
mkdir -p -- a/foo b/bar a/baz
Solution 2
To add to the above answers you can also do (in csh
, tcsh
, ksh
, bash
, zsh
, fish
, yash -o brace-expand
):
mkdir -p /path/{to,a}/{lot,of}/directories
Solution 3
Reading the man page is always a good place to start.
The -p
flag will create the required intermediate directories on the path.
Related videos on Youtube
Author by
user3142695
People who say nothing is impossible should try gargling with their mouths closed.
Updated on September 18, 2022Comments
-
user3142695 almost 2 years
How can I create multiple nested directories in one command?
mkdir -p /just/one/dir
But I need to create multiple different nested directories...
-
Stéphane Chazelas over 7 years@phk, you mean in
csh
and similar shells supporting that particular feature (that comes fromcsh
(late 70s)). -
Livinglifeback over 7 yearsDash being the most common current shell that doesn't support it. Bash, ZSH, and csh do.
-
phk over 7 years@StéphaneChazelas Oops. BTW, does something like caniuse.com for shells exist? (BTW, I should have added to my previous comment "For more information on its implementation in
bash
,") -
Stéphane Chazelas over 7 years
rc
/es
don't support it either but have a similar feature withmkdir /path/^(to some)^/directories
-
user3142695 over 7 yearsWhat does
--
mean? -
phk over 7 years@user3142695 End of options (e.g.
-s
/--some-thing
) and only (positional) arguments from now on. See also unix.stackexchange.com/q/11376/117599 It's not strictly necessary here, I just added it to signify further that those are multiple positional arguments. -
Pryftan almost 6 yearsYou know even though it doesn't 'technically answer' the particular it's a very good point; you have to read the man pages as well as do - and type it out yourself helps you memorise it - if you really want to learn. Otherwise it's just a custom can of scripts for you (and in that case it may very well be a can of worms).