144

What does the -p flag do in mkdir -p?

0

2 Answers 2

177

The -p flag will create nested directories, but only if they don't exist already.

For example, suppose you have a directory /foo that you have write permissions for.

mkdir -p /foo/bar/baz  # creates bar and baz within bar under existing /foo

It is also an idempotent operation, because if you run the same command over again, you will not get an error, but nothing will be created.

0
5

This -p flag allows a parent folder to be created along with the nested folder.

For example:

mkdir directory/nested_directory 

it would return the following error : mkdir: cannot create directory ‘directory/nested_directory’: No such file or directory

But if you use -p such as:

mkdir -p directory/nested_directory

It will create both directory and it's nested nested_directory folders successfully.

1
  • Welcome to Super User! Before answering an old question having an accepted answer (look for green ✓) ensure your answer adds something new or is otherwise helpful in relation to them. Here is a guide on How to Answer. There is also a site tour and a help center. Oct 5, 2022 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.