basename

from Wikipedia, the free encyclopedia

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