dirname
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 path
to before the last path separator /
. If it path
does not /
contain a separator , is an empty string or a NULL
pointer, 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) path
or a statically occupied memory area, it is recommended to call the function with a copy of the path.
Web links
-
dirname
: return the directory portion of a pathname - Open Group Base Specification -
dirname(1)
: return directory portion of pathname - OpenBSD General Commands Manual -
dirname(1)
: remove last component of filename - Debian GNU / Linux executables or shell commands man page - dirname (3) - extract the directory portion of a pathname. OpenBSD, accessed December 5, 2014 .
- dirname (3) - parse pathname components. Debian GNU / Linux, accessed December 5, 2014 .