What Does the Mkdir -P Mean in a Script File

What is the `mkdir -p` equivalent in Powershell?

The mkdir -p command does two things:

  • It creates directories (and files) recursively
  • It doesn't print an error if any depth of the path already exists.

To achieve this behavior for in Powershell, use:

  • New-Item which already creates paths recursively
  • -Force to fail silently if paths already exist

Hence, to create C:/x/y where x and y must be directories, use:

New-Item -ItemType Directory -Path C:/x/y -Force

To create C:/x/y where x is a directory and y a file, use:

New-Item -ItemType File -Path C:/x/y -Force

Mkdir combined with -p flag

It looks like you're following a Unix-ish tutorial but running the commands on Windows in cmd.exe.

As the usage instructions say:

C:\>mkdir /?
Creates a directory.

MKDIR [drive:]path
MD [drive:]path

If Command Extensions are enabled MKDIR changes as follows:

MKDIR creates any intermediate directories in the path, if needed.
For example, assume \a does not exist then:

mkdir \a\b\c\d

is the same as:

mkdir \a
chdir \a
mkdir b
chdir b
mkdir c
chdir c
mkdir d

which is what you would have to type if extensions were disabled.

Windows commands don't use - for options (and in particular, the mkdir command built into cmd doesn't understand -p).


The part about "privileged" is for the shell option -p, as in bash -p. It has nothing to do with mkdir -p, which is explained in man mkdir:

-p, --parents

         no error if existing, make parent directories as needed

But again, that only applies to the Unix mkdir, not Windows / cmd.

What is equivalent to Linux mkdir -p in Windows?

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 :-)

How to mkdir only if a directory does not already exist?

Try mkdir -p:

mkdir -p foo

Note that this will also create any intermediate directories that don't exist; for instance,

mkdir -p foo/bar/baz

will create directories foo, foo/bar, and foo/bar/baz if they don't exist.

Some implementation like GNU mkdir include mkdir --parents as a more readable alias, but this is not specified in POSIX/Single Unix Specification and not available on many common platforms like macOS, various BSDs, and various commercial Unixes, so it should be avoided.

If you want an error when parent directories don't exist, and want to create the directory if it doesn't exist, then you can test for the existence of the directory first:

[ -d foo ] || mkdir foo

Only mkdir if it does not exist

Do a test

[[ -d dir ]] || mkdir dir

Or use -p option:

mkdir -p dir

Linux: Bash: what does mkdir return

The result of running

`mkdir -p "$FINALPATH"`

isn't the return code, but the output from the program. $? the return code. So you could do

if mkdir -p "$FINALPATH" ; then
# success
else
echo Failure
fi

or

mkdir -p "$FINALPATH"
if [ $? -ne 0 ] ; then
echo Failure
fi


Related Topics



Leave a reply



Submit