basename
basename is a Unix program and part of the POSIX standard. It outputs the file name from a path name without leading directory names. If a suffix is specified that matches the file extension , the suffix is also removed.
Application examples
$ basename /home/alice/bild.jpg
bild.jpg
$ basename /home/alice/bild.jpg .jpg
bild
The function of the stand-alone program may seem trivial at first glance, but it is still of great use for completing subtasks in the context of script programming .
Library function
POSIX also contains basename as a function in the C programming language . This is declared in the header file libgen.h as follows:
#include <libgen.h>
char *basename(char *path);
The return value is the part of pathafter the last path separator /. In the POSIX variant, this is either a pointer to the /character following the last separator or a period .if it is pathempty or NULL. The glibc variant returns an empty string if patha /completed or only thereof. In both implementations, a pointer to a part of pathor a statically allocated memory area can be returned, which is why it is recommended to call the function with a copy of the path.
See also
-
dirname creates the (complementary) path up to the last separator
/.
Web links
-
basename: return non-directory portion of a pathname - Open Group Base Specification -
basename(1): return filename portion of pathname - OpenBSD General Commands Manual -
basename(1): Remove directory and suffix from filename - Debian GNU / Linux Executable programs or shell commands man page - basename (3) - extract the base portion of a pathname. OpenBSD, accessed December 5, 2014 .
- basename (3): parse pathname components. Debian GNU / Linux, accessed December 5, 2014 .