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?
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.