xin buxu1
xin buxu1

Reputation: 407

what the difference betwen mkdir vs mkdir -p

I tried to create folder in my local git repo using mkdir. It didn't work, but mkdir -p works.

Why?

I'm using Mac OS by the way. I checked the definition of mkdir -p. But I still don't quite understand.

Upvotes: 3

Views: 6159

Answers (3)

Alan W. Smith
Alan W. Smith

Reputation: 25425

Say you're in the directory:

/home/Users/john

And you want to make 3 new sub directories to end up with:

/home/Users/john/long/dir/path

While staying in "/home/Users/john", this will fail:

mkdir long/dir/path

You would have to make three separate calls:

mkdir long
mkdir long/dir
mkdir long/dir/path

The reason is that mkdir by default only creates directories one level down. By adding the "-p" flag, mkdir will make the entire set in one pass. That is, while this won't work:

mkdir long/dir/path

this will work:

mkdir -p long/dir/path

and create all three directories.

Upvotes: 12

georaldc
georaldc

Reputation: 1940

That flag will create parent directories when necessary. You were probably trying to create something with subdirectories and failing due to missing the -p flag

Upvotes: 0

ImLeo
ImLeo

Reputation: 1003

From the help of mkdir: -p, --parents no error if existing, make parent directories as needed

So you failed maybe just because you wanted to create both parent and child folder in one shoot without -p option.

Upvotes: 1

Related Questions