Is mkdir -p Safe? Creating Folder That Already Exists

The “mkdir” is the command considered for creating the directory in Linux. The “-p” flag in the “mkdir” command is utilized for creating the parent directory/directories if they don’t exist. However, the user might doubt the safety of the “-p” flag if it is applied to the existing directory.

This post will demonstrate the mkdir “-p” command with practical implementation.

  • Is mkdir -p Safe?
  • How to Create a Folder That Already Exists?
    • Create a Parent Directory (No Existence of Parent Directory)
    • Create the Parent Directory, and Sub-Directories (Parent Directory Exist)

Is mkdir -p Safe?

Yes, mkdir -p is safe even if the folder already exists. The command does not overwrite or delete existing files or directories; it will do nothing and exit successfully. Using mkdir -p ensures that all necessary directories are created without checking if each directory already exists.

How to Create a Folder That Already Exists? 

To create the parent directory using the “-p” flag, the following syntax of the mkdir command is examined:


$ mkdir -p [Parent Directory]/[Sub-Directory 1]/[Sub-Directory 2] on
  • The “mkdir” command is used for creating the directory.
  • The “-p” flag creates the parent directory/directories.
  • Then type the “Parent Directory” along with the sub-directories based on the requirement.

Example 1: Create the Parent Directory and Sub-Directories (No Existence of Parent Directory)

The following command will create the parent directory along with the parent directory as “Parent_dir1/Parent_dir2/Henry”:

$ mkdir -p Parent_dir1/Parent_dir2/Henry

The parent directory and sub-directories have been created.

Let’s verify the content of each parent directory using the “ls” command as follows:

$ ls Parent_dir1
$ ls Parent_dir1/Parent_dir2

The hierarchy of directories (parent and subdirectories) is available in the operating system.

Example 2: Create the Parent Directory and Sub-Directories (Parent Directory Exist)

If the parent directory exists, the mkdir command will not override the parent directory, but it will create the sub-directories in the existing parent directory. 

In our case, the “Henry” directory exists with the file content “” as shown:

$ cd Henry
$ ls

The “Henry” directory has the “” file.

Let’s create the “Henry” as the parent directory and sub-directories “dir1” and “dir2” to test if it is a safe way to create:

$ mkdir -p Henry/dir1/dir2

The successful execution shows that the parent directory and sub-directories are created.

Let’s check the content of the “Henry” directory if it is deleted or not through the “ls” command:

$ ls Henry
$ ls Henry/dir1

The content of the existing parent directory is not deleted, but the sub-directories have been created under the parent directory. 


Yes, the “mkdir -p” command is safe to use; it creates the parent directory and subdirectories and will not override existing directories/subdirectories. The -p flag informs the mkdir command to create the mentioned directory and any missing parent directories. If the parent directory exists, it will not create it but create the sub-directories.

This write-up has briefly illustrated the “mkdir -p ” command in Linux