Robert Holman
Robert Holman

Reputation: 136

Difference between mkdir and mkdir -p?

I've been following Learn Code the Hard Way's tutorial on learning how to utilize the command line interface in PowerShell. In this article, it tells me to use the command mkdir -p i\like\icecream. At the bottom, it explains "mkdir -p will make an entire path even if all the directories don't exist."

I'm confused, as mkdir i\like\icecream without the -p argument still does the same thing. I've experimented and done stuff such as creating a "one" directory, then creating "one\two\three" with mkdir and it will automatically create a two directory for three to be placed in. Does PowerShell automatically assume -p or something in cases like this? I'm at a loss as to what this argument does.

Upvotes: 12

Views: 18178

Answers (2)

Simran kaur
Simran kaur

Reputation: 378

If we create a directory using,

  • mkdir, mkdir will throw an exception if the directory already exists,so we need to check before creating a directory over same path

whereas

  • mkdirp, mkdirp always checks automatically if the directory exists or not over the specified path, hence no need to implement any check.

Upvotes: 0

EBGreen
EBGreen

Reputation: 37720

PowerShell does its best to determine what parameter you mean even if you don't give it fully. Thus if you use the -p parameter, you are actually using -path.

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.

For more information, in the shell type:

Get-Help mkdir

I will also clarify that when you call mkdir, what you are really doing is calling the New-Item cmdlet and specifying the -ItemType parameter as Directory. That is why you see the New-Item help when you run that command. If you want to see the actual code for the mkdir function to see how it does this, do this:

(get-command mkdir).ScriptBlock

Upvotes: 15

Related Questions