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?
mkdir , in the form of mkdir directory_name without a pathname, creates a directory in the current working directory, which is by default your home directory (often represented as ~ ).
Creates a directory or subdirectory. Command extensions, which are enabled by default, allow you to use a single mkdir command to create intermediate directories in a specified path. This command is the same as the md command.
The mkdir stands for 'make directory'. With the help of mkdir command, you can create a new directory wherever you want in your system. Just type "mkdir <dir name> , in place of <dir name> type the name of new directory, you want to create and then press enter.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With