Find common prefix of two or more (normalised) file paths
common_path(paths, normalise = FALSE, fsep = .Platform$file.sep)Character vector of file paths
Whether to normalise paths (with
normalizePath, default FALSE)
Optional path separator (defaults to .Platform$file.sep)
Character vector of common prefix, "" when there is no common
prefix, or the original value of paths when fewer than 2 paths were
supplied.
Note that for absolute paths, the common prefix will be returned
e.g. common_path(c("/a","/b")) is "/"
Note that normalizePath 1) operates according to the
conventions of the current runtime platform 2) is called with
winslash=.Platform$file.sep which means that normalised paths will
eventually end up separated by "\" by default on Windows rather than by
"//", which is normalizePath's standard behaviour.
Other path_utils:
abs2rel(),
split_path()
common_path(c("/a","/b"))
#> [1] "/"
common_path(c("/a/b/","/a/b"))
#> [1] "/a/b"
common_path(c("/a/b/d","/a/b/c/d"))
#> [1] "/a/b/"
common_path(c("/a/b/d","/b/c/d"))
#> [1] "/"
common_path(c("a","b"))
#> [1] ""
common_path(c("","/a"))
#> [1] ""
common_path(c("~","~/"))
#> [1] "~"
common_path(c("~/a/b/d","~/a/b/c/d"), normalise = FALSE)
#> [1] "~/a/b/"
common_path(c("~","~/"), normalise = FALSE)
#> [1] "~"