basename
basename(string $path, string $suffix = ""): string
Returns the trailing name component of a path.
C C23
C23
#include <stdio.h>
#include <string.h>
int main(void) {
const char *path = "/var/www/index.php";
const char *base = strrchr(path, '/');
printf("%s\n", base ? base + 1 : path);
return 0;
}
Output
index.php
C++ C++26
C++26
#include <filesystem>
#include <iostream>
int main() {
std::cout << std::filesystem::path("/var/www/index.php").filename().string() << "\n";
return 0;
}
Output
index.php
C# v11.0.100
v11.0.100
Console.WriteLine(Path.GetFileName("/var/www/index.php"));
Output
index.php
Go v1.27.4
v1.27.4
package main
import (
"fmt"
"path"
)
func main() {
fmt.Println(path.Base("/var/www/index.php"))
}
Output
index.php
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").getFileName());
}
}
Output
index.php
JavaScript v26.13.1
v26.13.1
const path = require("node:path");
console.log(path.basename("/var/www/index.php"));
Output
index.php
PHP v8.6.0
v8.6.0
echo basename("/var/www/index.php");
Output
index.php
Python v3.15.2
v3.15.2
from pathlib import Path
print(Path("/var/www/index.php").name)
Output
index.php
Rust v1.99.1
v1.99.1
use std::path::Path;
fn main() {
let base = Path::new("/var/www/index.php").file_name().unwrap();
println!("{}", base.to_str().unwrap());
}
Output
index.php
TypeScript v26.13.1
v26.13.1
import * as path from "node:path";
console.log(path.basename("/var/www/index.php"));
Output
index.php
Language differences
C: no standard function — the idiom is a hand-written strrchr to locate the last / and return the text after it, falling back to the whole string when no slash exists.
C++: C++17 std::filesystem::path::filename.
C#: Path.GetFileName.
Go: path.Base.
Java: Path.getFileName.
JavaScript: path.basename.
PHP: built-in path tools agree across languages — everything after the last slash; the unique second parameter strips a given suffix, which other languages handle as post-processing.
Python: Path.name.
Rust: Path::file_name.
TypeScript: same as JavaScript — path.basename.