realpath

realpath(string $path): string|false

Returns the canonicalized absolute pathname, resolving symlinks and . / .. components; the path must actually exist.

C C23

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

int main(void) {
    FILE *fp = fopen("snippet_realpath_demo.txt", "w");
    fputs("x", fp);
    fclose(fp);
    char *resolved = realpath("snippet_realpath_demo.txt", NULL);
    if (resolved == NULL) {
        return 1;
    }
    printf("%s\n", basename(resolved));
    free(resolved);
    return 0;
}
Output
snippet_realpath_demo.txt

C++ C++26

C++26
#include <filesystem>
#include <fstream>
#include <iostream>
int main() {
    { std::ofstream f("snippet_realpath_demo.txt"); f << "x"; }
    std::filesystem::path p = std::filesystem::canonical("snippet_realpath_demo.txt");
    std::cout << p.filename().string() << "\n";
    return 0;
}
Output
snippet_realpath_demo.txt

C# v11.0.100

v11.0.100
File.WriteAllText("snippet_realpath_demo.txt", "x");
string full = Path.GetFullPath("snippet_realpath_demo.txt");
Console.WriteLine(Path.GetFileName(full));
Output
snippet_realpath_demo.txt

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "os"
    "path/filepath"
)

func main() {
    os.WriteFile("snippet_realpath_demo.txt", []byte("x"), 0644)
    abs, _ := filepath.Abs("snippet_realpath_demo.txt")
    resolved, _ := filepath.EvalSymlinks(abs)
    fmt.Println(filepath.Base(resolved))
}
Output
snippet_realpath_demo.txt

Java v27.0.1

v27.0.1
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) throws IOException {
        Path p = Path.of("snippet_realpath_demo.txt");
        Files.writeString(p, "x");
        System.out.println(p.toRealPath().getFileName());
    }
}
Output
snippet_realpath_demo.txt

JavaScript v26.13.1

v26.13.1
const fs = require("node:fs");
const path = require("node:path");
fs.writeFileSync("snippet_realpath_demo.txt", "x");
const resolved = fs.realpathSync("snippet_realpath_demo.txt");
console.log(path.basename(resolved));
Output
snippet_realpath_demo.txt

PHP v8.6.0

v8.6.0
file_put_contents("snippet_realpath_demo.txt", "x");
echo basename(realpath("snippet_realpath_demo.txt"));
Output
snippet_realpath_demo.txt

Python v3.15.2

v3.15.2
from pathlib import Path

p = Path("snippet_realpath_demo.txt")
p.write_text("x")
print(p.resolve().name)
Output
snippet_realpath_demo.txt

Rust v1.99.1

v1.99.1
use std::fs;

fn main() {
    fs::write("snippet_realpath_demo.txt", "x").unwrap();
    let resolved = fs::canonicalize("snippet_realpath_demo.txt").unwrap();
    println!("{}", resolved.file_name().unwrap().to_str().unwrap());
}
Output
snippet_realpath_demo.txt

TypeScript v26.13.1

v26.13.1
import * as fs from "node:fs";
import * as path from "node:path";

fs.writeFileSync("snippet_realpath_demo.txt", "x");
const resolved: string = fs.realpathSync("snippet_realpath_demo.txt");
console.log(path.basename(resolved));
Output
snippet_realpath_demo.txt

Language differences

C: realpath(path, NULL) takes NULL to have the function malloc the buffer (POSIX 2008); the legacy form needs a PATH_MAX buffer.

C#: Path.GetFullPath only normalizes . and .. (.NET 6+ adds ResolveLinkTarget) — a half-way implementation that skips symlink resolution.

Go: EvalSymlinks alone won't fix relative paths, so the idiom is Abs + EvalSymlinks.

Java: toRealPath() throws NoSuchFileException (existence required by default).

JavaScript: realpathSync throws ENOENT.

PHP: realpath fundamentally differs from dirname/basename — it really touches the filesystem: every component must exist and symlinks are resolved along the way; on a missing path it returns false and emits a warning; the entry prints only the file-name portion of the result for two reasons: the working directory itself is random, and on macOS the temp path /var/... is actually a /private/var/... symlink, so a full resolved path carries a /private prefix and is not reproducible.

Python: Path.resolve() is non-strict by default and still builds an absolute path for a missing file (strict=True raises).

TypeScript: same as JS — realpathSync.