dirname

dirname(string $path, int $levels = 1): string

Returns the directory portion of a path with the trailing name component removed.

C C23

C23
#include <libgen.h>
#include <stdio.h>

int main(void) {
    char buf[] = "/var/www/index.php";
    printf("%s\n", dirname(buf));
    return 0;
}
Output
/var/www

C++ C++26

C++26
#include <filesystem>
#include <iostream>
int main() {
    std::cout << std::filesystem::path("/var/www/index.php").parent_path().string() << "\n";
    return 0;
}
Output
/var/www

C# v11.0.100

v11.0.100
Console.WriteLine(Path.GetDirectoryName("/var/www/index.php"));
Output
/var/www

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "path"
)

func main() {
    fmt.Println(path.Dir("/var/www/index.php"))
}
Output
/var/www

Java v27.0.1

v27.0.1
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) {
        System.out.println(Path.of("/var/www/index.php").getParent());
    }
}
Output
/var/www

JavaScript v26.13.1

v26.13.1
const path = require("node:path");
console.log(path.dirname("/var/www/index.php"));
Output
/var/www

PHP v8.6.0

v8.6.0
echo dirname("/var/www/index.php");
Output
/var/www

Python v3.15.2

v3.15.2
from pathlib import Path

print(Path("/var/www/index.php").parent)
Output
/var/www

Rust v1.99.1

v1.99.1
use std::path::Path;

fn main() {
    let dir = Path::new("/var/www/index.php").parent().unwrap();
    println!("{}", dir.to_str().unwrap());
}
Output
/var/www

TypeScript v26.13.1

v26.13.1
import * as path from "node:path";
console.log(path.dirname("/var/www/index.php"));
Output
/var/www

Language differences

C: semantics agree across languages — everything before the last separator; pure string processing with no filesystem access, and the path need not exist; edge cases: a bare filename "index.php" returns "." everywhere, "/var" yields "/", and "/" itself yields "/"; details: <libgen.h> dirname() may modify its argument, so it must receive a writable char[], not a string literal (use POSIX dirname_r in threaded code).

C++: parent_path() gives /var/www for /var/www/index.php but /var for the trailing-slash form /var/www/ (www is treated as the filename).

Go: path.Dir also Cleans the path ("a//b/../c") — use path for URLs and path/filepath.Dir for OS paths; "/" returns "/".

Java: getParent() returns null for "/".

JavaScript: "/" returns "/".

PHP: the second $levels parameter climbs several directories in one call; "/" returns "/".

Rust: parent() returns Some("") for "/".