mkdir
mkdir(string $directory, int $permissions = 0777, bool $recursive = false): bool
Creates a directory, optionally creating missing parent directories recursively.
C C23
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
/* C stdlib has no recursive mkdir; the idiom is a hand-written mkdirs creating level by level */
static int mkdirs(const char *path) {
char buf[256];
snprintf(buf, sizeof(buf), "%s", path);
for (char *p = buf + 1; *p; p++) {
if (*p == '/') {
*p = '\0'; /* temporarily truncate to create the current level */
if (mkdir(buf, 0755) != 0 && errno != EEXIST) {
return -1;
}
*p = '/';
}
}
return (mkdir(buf, 0755) == 0 || errno == EEXIST) ? 0 : -1;
}
int main(void) {
if (mkdirs("demo/parent/child") != 0) {
return 1;
}
printf("demo/parent/child\n");
struct stat st;
printf("%d\n", stat("demo/parent/child", &st) == 0 && S_ISDIR(st.st_mode));
return 0;
}
demo/parent/child 1
C++ C++26
#include <filesystem>
#include <iostream>
int main() {
std::filesystem::create_directories("demo/parent/child"); // creates all missing parent levels
std::cout << "demo/parent/child" << "\n";
std::cout << std::filesystem::is_directory("demo/parent/child") << "\n";
return 0;
}
demo/parent/child 1
C# v11.0.100
var dir = "demo/parent/child";
Directory.CreateDirectory(dir); // creates all missing parents, no error if it already exists
Console.WriteLine(dir);
Console.WriteLine(Directory.Exists(dir));
demo/parent/child True
Go v1.27.4
package main
import (
"fmt"
"os"
)
func main() {
if err := os.MkdirAll("demo/parent/child", 0755); err != nil { // MkdirAll creates all missing levels
panic(err)
}
fmt.Println("demo/parent/child")
info, err := os.Stat("demo/parent/child")
if err != nil {
panic(err)
}
fmt.Println(info.IsDir())
}
demo/parent/child true
Java v27.0.1
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
public static void main(String[] args) throws Exception {
Path dir = Path.of("demo/parent/child");
Files.createDirectories(dir); // creates all missing parents, no error if it already exists
System.out.println(dir);
System.out.println(Files.isDirectory(dir));
}
}
demo/parent/child true
JavaScript v26.13.1
const fs = require("node:fs");
fs.mkdirSync("demo/parent/child", { recursive: true }); // recursive creates missing parents
console.log("demo/parent/child");
console.log(fs.statSync("demo/parent/child").isDirectory());
demo/parent/child true
PHP v8.6.0
$dir = "demo/parent/child";
mkdir($dir, 0777, true); // recursive=true creates missing parent directories
echo $dir, "\n";
var_dump(is_dir($dir));
demo/parent/child bool(true)
Python v3.15.2
import os
os.makedirs("demo/parent/child", exist_ok=True) # exist_ok=True: no error if it already exists
print("demo/parent/child")
print(os.path.isdir("demo/parent/child"))
demo/parent/child True
Rust v1.99.1
use std::fs;
fn main() {
fs::create_dir_all("demo/parent/child").unwrap(); // std-only, creates all missing levels
println!("demo/parent/child");
println!("{}", fs::metadata("demo/parent/child").unwrap().is_dir());
}
demo/parent/child true
TypeScript v26.13.1
import * as fs from "node:fs";
fs.mkdirSync("demo/parent/child", { recursive: true });
console.log("demo/parent/child");
console.log(fs.statSync("demo/parent/child").isDirectory());
demo/parent/child true
Language differences
C: no recursive standard version — the idiom is a hand-written mkdirs calling mkdir level by level, treating EEXIST as success; the permissions argument is exposed (mode adjusted by umask).
C++: C++17 std::filesystem::create_directories fills in missing levels in one call and is idempotent; failure surfaces as an exception.
C#: Directory.CreateDirectory fills in missing levels and is idempotent; the higher-level API does not expose permissions.
Go: os.MkdirAll fills in missing levels and is idempotent; the permissions argument is exposed (mode adjusted by umask); failure surfaces as an error.
Java: Files.createDirectories fills in missing levels and is idempotent; the higher-level API does not expose permissions; failure surfaces as an exception.
JavaScript: fs.mkdirSync(..., { recursive: true }) fills in missing levels and is idempotent; failure surfaces as an exception.
PHP: creates a single level unless you pass recursive: true; the permissions argument only exists in some languages — PHP/C/Go take a mode that the umask then adjusts; success/failure: returns bool.
Python: os.makedirs needs exist_ok=True to tolerate existing paths (otherwise raises FileExistsError); fills in missing levels and is idempotent; failure surfaces as an exception.
Rust: fs::create_dir_all fills in missing levels and is idempotent; failure surfaces as a Result.
TypeScript: same as JS — fs.mkdirSync with recursive.