What is equivalent to Linux mkdir -p in Windows?

119,911

Solution 1

The Windows mkdir does it automatically if command extensions are enabled. They are on just about every box I've ever used but, if they're not, you can create your own script to do it:

@echo off
setlocal enableextensions
md %1
endlocal

Expanding:

Command extensions are an added feature of cmd.exe which allows you to do so much more (at the cost of a little compatibility with earlier incarnations of the batch language).

Windows XP cmd.exe should have these extensions enabled by default but you can configure your box so that they're disabled by default (using "cmd /e:off" as the default processor). If you do that and want to use the extensions, your cmd files must have a setlocal to turn them back on.

The script above could be called md2.cmd and then you would be guaranteed to be able to create multiple directory levels with "md2 a\b\c" without having to worry whether the extensions were enabled.

Almost every one of the cmd scripts I write begins with:

setlocal enableextensions enabledelayedexpansion

to ensure I get as close as possible to the behavior of my beloved bash :-)

Solution 2

In Windows, mkdir creates directory trees by default.

mkdir a\b\c

Solution 3

For a strange reason when I attempted to create a directory with the following method;

mkdir src/main/java/main/resources 

it didn't work, I had to surround the path in double quotes, as shown below;

mkdir "src/main/java/main/resources"

Additionally, unix allows for this;

mkdir -p src/main/java src/main/resources

where two branches will be created as shown below, the equivalent to that on windows is;

mkdir "src/java/resources" "src/main/resources"

src
-----java
-------resources
-----main
-------resources

I hope this helps! xox

Solution 4

If you want to use forward slashes, just give the directory structure you want within double quotes. mkdir "org/frame/bu/fed/config"

Solution 5

mkdir by default makes all intermediate directories. Just ensure that you use '\' as the separator.

Share:
119,911
Wesley Reed
Author by

Wesley Reed

want to know the real facts.

Updated on May 06, 2020

Comments

  • Wesley Reed
    Wesley Reed about 4 years

    In Linux, mkdir -p creates a folder tree.

    What is the equivalent option in Windows to create a folder tree? Is there any?

  • Shyam Kumar Sundarakumar
    Shyam Kumar Sundarakumar about 15 years
    I think you wanted to put md and not mkdir.
  • Alan Haggai Alavi
    Alan Haggai Alavi about 15 years
    Both mkdir and md are the same in Windows.
  • Rich
    Rich about 15 years
    Except you happen to have some GNUWin32 stuff lying around in your path. Then, weirdly, the GNU mkdir is called upon using mkdir (and it took me a while to find out why I couldn't create folder trees anymore ...) :-)
  • Jay Sullivan
    Jay Sullivan over 9 years
    Can you explain how this answer is better than just running md?
  • paxdiablo
    paxdiablo over 9 years
    @notfed, if command extensions are disabled for some reason, md won't create the entire path. That's the situation I've given in the answer, and the one you'll need a script for, to temporarily enable them.
  • imlouisrussell
    imlouisrussell almost 6 years
    Double quotes worked for me in Windows 10 using ConEmu. +1!
  • jeb
    jeb about 5 years
    Where is the benefit of your answer. There are already answers (up to ten years old) with the same statements.
  • basit
    basit about 5 years
    I didn't see anyone mentioning that backslash has to be used for mkdir to behave like 'linux mkdir -p' (may be because it is assumed), they just say on windows mkdir makes intermediate directories by default. My answer is for someone who might have used '/' and saw that the directories are not created.
  • jeb
    jeb about 5 years
    I suppose that's more or less the answer of @AnupThakare. When you don't use backslashes, then you need quotes