ID EN
I/O & Files

basename

R Base 3.6.2

basename removes all of the path up to and including the last path separator (if any). dirname returns the part of the path up to but excluding the last path separator, or "." if there is no path separator.

Syntax

R
basename(path)
dirname(path)

Arguments

Parameter Description
path character vector, containing path names.

Return Value

A character vector of the same length as path. A zero-length input will give a zero-length output with no error. Paths not containing any separators are taken to be in the current directory, so dirname returns ".". If an element of path is NA, so is the result. "" is not a valid pathname, but is returned unchanged.

Details

tilde expansion of the path is done except on Windows. Trailing path separators are removed before dissecting the path, and for dirname any trailing file separators are removed from the result.

Examples

Example
R
# NOT RUN {
basename(file.path("","p1","p2","p3", c("file1", "file2")))
dirname(file.path("","p1","p2","p3","filename"))
# }

See Also

file.path path.expand.