How can I get the powershell "mkdir" command to act exactly like Linux's mkdir -p
command?
Under Linux, mkdir -p
will create nested directories, but only if they don't exist already. For example, suppose you have a directory /foo
that you have write permissions for. mkdir -p /foo/bar/baz
creates bar and baz within bar under existing /foo
. You run the same command over again, you will not get an error, but nothing will be created.
The mkdir (make directory) command in the Unix, DOS, DR FlexOS, IBM OS/2, Microsoft Windows, and ReactOS operating systems is used to make a new directory. It is also available in the EFI shell and in the PHP scripting language. In DOS, OS/2, Windows and ReactOS, the command is often abbreviated to md . mkdir.
Linux Directories mkdir -p With the help of mkdir -p command you can create sub-directories of a directory. It will create parent directory first, if it doesn't exist. But if it already exists, then it will not print an error message and will move further to create sub-directories.
mkdir -p means: create the directory and, if required, all parent directories. The fact that this makes little sense when the path is specified as . , so the current working directory, does not change this. Most likely the line where the path is defined is meant to be adapted as required.
For the mkdir function, the -path parameter tells the function the path to create. -path is also by default the first argument to the function if no explicit parameters are provided. So calling the function with -p ( -path ) and without -p are exactly the same thing as far as the function is concerned.
You can ignore errors in PowerShell with the -ErrorAction SilentlyContinue
parameter (you can shorten this to -ea 0
). The full PowerShell command is
New-Item /foo/bar/baz -ItemType Directory -ea 0
You can shorten this to
md /foo/bar/baz -ea 0
(You can also type mkdir
instead of md
if you prefer.)
Note that PowerShell will output the DirectoryInfo
object it creates when using New-Item -ItemType Directory
(or the md
or mkdir
aliases). If you don't want any output, you can pipe to Out-Null
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With