what the difference betwen mkdir vs mkdir -p

2019-06-10 22:51发布

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.

3条回答
Viruses.
2楼-- · 2019-06-10 23:38

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楼-- · 2019-06-10 23:44

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

查看更多
可以哭但决不认输i
4楼-- · 2019-06-10 23:47

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.

查看更多
登录 后发表回答