Reputation: 1397
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?
Upvotes: 24
Views: 67351
Reputation: 10026
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
Upvotes: 2
Reputation: 207445
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.
Upvotes: 3
Reputation: 522042
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.
Upvotes: 44