what the difference betwen mkdir vs mkdir -p

2019-06-10 23:16发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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