NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ATTRIBUTES |VERSIONS |STANDARDS |HISTORY |BUGS |EXAMPLES |SEE ALSO |COLOPHON | |
basename(3) Library Functions Manualbasename(3)basename, dirname - parse pathname components
Standard C library (libc,-lc)
#include <libgen.h>char *dirname(char *path);char *basename(char *path);
Warning: there are two different functionsbasename(); see below. The functionsdirname() andbasename() 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 '/', andbasename() returns the component following the final '/'. Trailing '/' characters are not counted as part of the pathname. Ifpath does not contain a slash,dirname() returns the string "." whilebasename() returns a copy ofpath. Ifpath is the string "/", then bothdirname() andbasename() return the string "/". Ifpath is a null pointer or points to an empty string, then bothdirname() andbasename() return the string ".". Concatenating the string returned bydirname(), a "/", and the string returned bybasename() yields a complete pathname. Bothdirname() andbasename() may modify the contents ofpath, so it may be desirable to pass a copy when calling one of these functions. 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 ofpath, so that the string referred to bypath should not be modified or freed until the pointer returned by the function is no longer required. The following list of examples (taken from SUSv2) shows the strings returned bydirname() andbasename() for different paths:path dirname basename /usr/lib /usr lib /usr/ / usr usr . usr / / / . . . .. . ..
Bothdirname() andbasename() return pointers to null-terminated strings. (Do not pass these pointers tofree(3).)
For an explanation of the terms used in this section, seeattributes(7). ┌──────────────────────────────────────┬───────────────┬─────────┐ │Interface│Attribute│Value│ ├──────────────────────────────────────┼───────────────┼─────────┤ │basename(),dirname() │ Thread safety │ MT-Safe │ └──────────────────────────────────────┴───────────────┴─────────┘
There are two different versions ofbasename() - 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 whenpath has a trailing slash, and in particular also when it is "/". There is no GNU version ofdirname(). With glibc, one gets the POSIX version ofbasename() when<libgen.h> is included, and the GNU version otherwise.
POSIX.1-2008.
POSIX.1-2001.
In the glibc implementation, the POSIX versions of these functions modify thepath argument, and segfault when called with a static string such as "/usr/". Before glibc 2.2.1, the glibc version ofdirname() did not correctly handle pathnames with trailing '/' characters, and generated a segfault if given a NULL argument.
The following code snippet demonstrates the use ofbasename() anddirname(): 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);basename(1),dirname(1)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-05-17basename(3)Pages that refer to this page:dmstats(8)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |