basename(3)
โจ basename(3) โ Library Functions Manual โจ
okay bestie, gather round, we're parsing pathname components today and it's honestly giving main character energy ๐ this one lives in section 3 of your man pages, and it's a whole saga. buckle up ๐
๐ธ NAME ๐ธ
basename, dirname โ parse pathname components
๐ LIBRARY ๐
Standard C library (libc, -lc) โ yeah she's built in, no extra packages, we love a low-maintenance queen ๐ซถ
๐ SYNOPSIS ๐
#include <libgen.h>
char *dirname(char *path);
char *basename(char *path);
like, that's it. two functions. that's the whole vibe check.
๐ DESCRIPTION ๐
โ ๏ธ WARNING, bestie, pay attention: there are two different functions called basename(). yes, TWO. see below. this is not a drill.
The functions dirname() and basename() break a null-terminated pathname string into directory and filename components. in the usual case, dirname() returns the string up to, but not including, the final '/', and basename() returns the component following the final '/'. trailing '/' characters are not counted as part of the pathname โ they're just out here being decorative, iconic but irrelevant ๐
If path does not contain a slash, dirname() returns the string "." while basename() returns a copy of path. If path is the string "/", then both dirname() and basename() return the string "/". And if path is a null pointer or points to an empty string, then both dirname() and basename() return the string ".". no cap, that's just how she is โจ
Concatenating the string returned by dirname(), a "/", and the string returned by basename() yields a complete pathname. it's giving puzzle pieces that snap together ๐งฉ
Both dirname() and basename() may modify the contents of path, so it may be desirable to pass a copy when calling one of these functions. yes literally, she will eat your string. protect your data ๐ซถ
These functions may return pointers to statically allocated memory which may be overwritten by subsequent calls. Alternatively, they may return a pointer to some part of path, so that the string referred to by path should not be modified or freed until the pointer returned by the function is no longer required. read that again. it matters. ๐ฅ
The following list of examples (taken from SUSv2) shows the strings returned by dirname() and basename() for different paths:
path dirname basename
/usr/lib /usr lib
/usr/ / usr
usr . usr
/ / /
. . .
.. . ..
look at the /usr/ row. dirname is / and basename is usr. the trailing slash got absolutely ghosted ๐
๐ฆ RETURN VALUE ๐ฆ
Both dirname() and basename() return pointers to null-terminated strings.
๐จ Do NOT pass these pointers to free(3). i'm serious. do not. that memory was never yours to free, bestie, that's a whole situation nobody wants ๐ญ
๐ ATTRIBUTES ๐
For an explanation of the terms used in this section, see attributes(7).
- Interface:
basename(),dirname() - Attribute: Thread safety
- Value:
MT-Safe
MT-Safe!! she's thread-safe!! we stan a function that doesn't start drama across threads ๐โจ
๐ VERSIONS ๐
There are two different versions of basename() โ the POSIX version described above, and the GNU version, which one gets after:
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <string.h>
The GNU version never modifies its argument, and returns the empty string when path has a trailing slash, and in particular also when it is "/". There is no GNU version of dirname(). dirname said "one of me is enough" and honestly? respect ๐
With glibc, one gets the POSIX version of basename() when <libgen.h> is included, and the GNU version otherwise. so like... the header you pick decides which girl you get. no pressure tho ๐
โญ STANDARDS โญ
POSIX.1-2008.
๐ HISTORY ๐
POSIX.1-2001.
she's been around. vintage. classic. legacy icon ๐
๐ BUGS ๐
In the glibc implementation, the POSIX versions of these functions modify the path argument, and segfault when called with a static string such as "/usr/".
๐จ girl. SEGFAULT. with a static string. like a string literal. as in dirname("/usr/") will just straight up delete your whole program from existence. do not do this. i am begging ๐
Before glibc 2.2.1, the glibc version of dirname() did not correctly handle pathnames with trailing '/' characters, and generated a segfault if given a NULL argument. old glibc was going through it fr fr ๐ญ (fixed now, but knowledge is power, bestie)
๐ธ EXAMPLES ๐ธ
The following code snippet demonstrates the use of basename() and dirname():
char *dirc, *basec, *bname, *dname;
char *path = "/etc/passwd";
dirc = strdup(path);
basec = strdup(path);
dname = dirname(dirc);
bname = basename(basec);
printf("dirname=%s, basename=%s\n", dname, bname);
see how she strdup()s the path twice before handing it over? that's the move. that's the whole move. protect the original, feed the copy to the function that eats strings ๐ซถโจ that's it, that's the entire tutorial, you're welcome.
๐ SEE ALSO ๐
and that's the page, bestie!! this was basename(3) from Linux man-pages 6.20, dated 2026-02-08 โ go forth, parse your paths, strdup() before you wreck yourself, and remember: there are two basename()s and only one of them is safe to free-adjacent. slay responsibly ๐๐ฆโจ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 2000, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms