26

How can I create multiple nested directories in one command?

mkdir -p /just/one/dir

But I need to create multiple different nested directories...

user3142695
  • 1,529
  • 7
  • 20
  • 34

3 Answers3

32

mkdir accepts multiple path arguments:

mkdir -p -- a/foo b/bar a/baz
phk
  • 5,893
  • 7
  • 41
  • 70
  • 1
    What does `--` mean? – user3142695 Jan 17 '17 at 22:28
  • 1
    @user3142695 End of options (e.g. `-s`/`--some-thing`) and only (positional) arguments from now on. See also http://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. – phk Jan 17 '17 at 22:29
17

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
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
Livinglifeback
  • 1,586
  • 10
  • 16
  • 3
    On `bash` and similar shells supporting that particular feature (_brace expansion_) that is. For more information, see http://wiki.bash-hackers.org/syntax/expansion/brace – phk Jan 17 '17 at 17:25
  • 1
    @phk, you mean in `csh` and similar shells supporting that particular feature (that comes from `csh` (late 70s)). – Stéphane Chazelas Jan 17 '17 at 17:29
  • Dash being the most common current shell that doesn't support it. Bash, ZSH, and csh do. – Livinglifeback Jan 17 '17 at 17:32
  • @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`_,") – phk Jan 17 '17 at 17:32
  • `rc`/`es` don't support it either but have a _similar_ feature with `mkdir /path/^(to some)^/directories` – Stéphane Chazelas Jan 17 '17 at 17:38
6

Reading the man page is always a good place to start.

The -p flag will create the required intermediate directories on the path.

symcbean
  • 5,008
  • 2
  • 25
  • 37
  • You 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). – Pryftan Aug 10 '18 at 22:37