## How to force mkdir to create a directory if there isn’t already one

By admin | September 3, 2008

If you’ve ever tried to make a directory if there isn’t one existing, you probably have bumped into the following error:
mkdir: cannot create directory the_directory': File exists

To fix this, use this instead to suppress error and create parent directories if needed:
mkdir -p your_directory`

For more information, see the mkdir page at Wikipedia.

If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!

Topics: Linux | 3 Comments »

### 3 Responses to “How to force mkdir to create a directory if there isn’t already one”

1. Gaurav Khurana Says:
April 20th, 2015 at 11:46 am

Thanks it worked in full go ,, can you share what p stands for

2. admin Says:
June 6th, 2016 at 2:50 am

-p serves to force mkdir to also create the parent directories (hence the ‘p’).

3. Rajesh Damam Says:
October 9th, 2019 at 6:31 am

what if my directory is not a parent directory

[ram@raj ~]$mkdir opt [ram@raj ~]$ ls
opt

[ram@raj opt]$mkdir java mkdir: cannot create directory ‘java’: Permission denied [ram@raj opt]$ mkdir -p java

mkdir: cannot create directory ‘java’: Permission denied
[ram@raj opt]\$