Available inall subroutines.

Breaks a pathname into the directory component and returns it. A pathname is a forward slash-separated list of directory names followed by either a directory name or a filename. Trailing / characters are not counted as part of the pathname. Both relative and absolute pathnames are allowed. Relative pathnames may use a single dot (.) or double dots (..) to represent the current directory or the parent directory respectively.

In the usual case, std.dirname returns the string up to, but not including, the final /: for example, std.dirname("/foo/bar/foobar") is "/foo/bar".

If the pathname does not contain a forward slash, then std.dirname returns the string ".": for example, std.dirname("foo") is ".".

If the pathname only contains one or more forward slashes, then std.dirname returns the string "/": for example, std.dirname("/") is "/".

If the pathname is an empty string "", then std.dirname returns the string ".". The same behavior applies if std.dirname is invoked with a not-set header for the pathname: for example, std.dirname(req.http.Not-Set) is ".".

See std.basename for breaking a pathname into the filename component. The documentation also includes an example of how to use std.dirname and std.basename together to concatenate a complete pathname.

This function conforms to POSIX.1-2001.


std.dirname("") # returns "."
std.dirname("/usr/lib") # returns "/usr"
std.dirname("/usr/") # returns "/"
std.dirname("usr") # returns "."
std.dirname("/") # returns "/"
std.dirname(".") # returns "."
std.dirname("..") # returns "."

User contributed notes


Do you see an error in this page? Do have an interesting use case, example or edge case people should know about? Share your knowledge and help people who are reading this page! (Comments are moderated; for support, please contact