dirname

from Wikipedia, the free encyclopedia

dirname is a Unix program and part of the POSIX standard. It outputs the directory from a file path. The opposite of dirname is the basename program .

Application examples

$ dirname /home/alice/bild.jpg
  /home/alice
$ dirname /home/alice/
  /home

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 dirname as a function in the C programming language . This is declared in the header file libgen.h as follows:

#include <libgen.h>

char *dirname(char *path);

The return value is the part from pathto before the last path separator /. If it pathdoes not /contain a separator , is an empty string or a NULLpointer, the function returns a period .as the path for the current directory. If the path only consists of /this is also the result of the function. Since dirname returns a pointer to a part of (the possibly changed) pathor a statically occupied memory area, it is recommended to call the function with a copy of the path.

Web links