Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mkdir -p in Mac

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?

like image 762
user454083 Avatar asked Jan 09 '14 08:01

user454083


People also ask

Where does mkdir Create directory in Mac?

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 ~ ).

What is the command for mkdir?

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.

How do I create a mkdir folder?

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.


1 Answers

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.

like image 194
deceze Avatar answered Sep 30 '22 07:09

deceze