mkdir -p in Mac
Asked Answered
T

4

22

I have been reading the description of the OSX Man page. It has description like following regarding mkdir -p:

-p

Create intermediate directories as required. If this option is not specified, the full path prefix of each operand must already exist. On the other hand, with this option specified, no error will be reported if a directory given as an operand already exists. Intermediate directories are created with permission bits of rwxrwxrwx (0777) as modified by the current umask, plus write and search permission for the owner.

I am not quite following this description. especially "If this option is not specified, the full path prefix of each operand must already exist. On the other hand, with this option specified, no error will be reported if a directory given as an operand already exists."

Does someone has an example regarding this explanation?

Tile answered 9/1, 2014 at 8:4 Comment(3)
Please try on apple.stackexchange.comTeratogenic
@Teratogenic The question is generally applicable to virtually all systems that have mkdir, not just OS X.Holomorphic
@Holomorphic Change the tags then.Teratogenic
H
43

Given this directory structure:

/
  foo/
  bar/
    baz/

This will obviously work:

mkdir /foo/x

This will not work:

mkdir /foo/x/y

Because /foo/x does not exist, the directory /foo/x/y cannot be created underneath it. The prefix /foo/x/ needs to exist in order to create /foo/x/y.

This is where -p comes in. This works:

mkdir -p /foo/x/y

/foo/x will implicitly be created together with /foo/x/y.

If you try:

mkdir /bar/baz

You'll get an error that the directory already exists. However, if you do:

mkdir -p /bar/baz

you will not get an error, it'll just silently ignore all already existing directories and be content with the result without doing anything.

Holomorphic answered 9/1, 2014 at 8:28 Comment(0)
S
3

Imagine you have an empty folder, and you want to create a subdirectory called "d1" and a subirectory inside "d1" called "d2". Normally you must do this:

mkdir d1
mkdir d1/d2

With the "-p" option you can have mkdir create the in-between directory (d1) for you:

mkdir -p d1/d2

The bit you are asking about says that if "d1" already exists and you use "mkdir -p" it won't matter that it is already there and there won't be any error messages.

Stenotype answered 9/1, 2014 at 8:31 Comment(0)
I
1

This work on version 10.10.4

mkdir -pv d1/d/d3 
Ibbison answered 2/8, 2015 at 16:1 Comment(0)
B
1

Not sure if it is mac only, but on mac os x you can do

mkdir -p src/{main,test}/{java,resources,scala}

which will not only give you a nested, but also cartesian product of your directories:

src/test/java
src/test/resources
src/test/scala
src/main/java
src/main/resources
src/main/scala
Billybillycock answered 27/12, 2018 at 19:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.