我有一个R函数,它使用带有相对符号链接的目录(例如../../data
)作为模板。我知道相对链接是有效的,因为函数将模板放在已知文件结构的特定部分。 file.copy
函数可以使用recrusive = TRUE
复制目录,但它会将符号链接转换为它们指向的目录的副本。 linux系统命令cp -r path1 path2
将正确复制链接,但我希望尽可能使用R函数。
如何复制R中包含指向目录外部的相对符号链接的目录?
我知道我可以编写自己的函数来递归列出文件(list.dirs
),找到符号链接(Sys.readlink
),然后重新创建它们(file.link
),同时复制所有其他文件,但我想知道该功能是否已经存在。
答案 0 :(得分:3)
我已经提出了一个解决方案,所以我想我会发布它,以防其他人可能需要这样做,实际上没有更传统的方法来做到这一点。我仍然有兴趣听到其他答案!
我提出了以下功能:
#===================================================================================================
#' Copy folders with links
#'
#' Copies folders like \link{\code{file.copy}} except it replicates links correctly on unix-like
#' systems.
#'
#' @param from (\code{character}) The path to the folder to be copied
#' @param to (\code{character}) Where to copy the folder to.
#'
copy_folder_with_links <- function(from, to) {
target <- file.path(to, basename(from))
if (file.exists(target)) stop(paste0("Target folder ", target, " already exists."))
# Get list of all files/folders to copy ----------------------------------------------------------
path <- data.frame(target = list.files(from, recursive = TRUE, all.files = TRUE, include.dirs = TRUE))
path$from <- file.path(from, path$target)
path$to <- file.path(to, basename(from), path$target)
# Get type of file/folders -----------------------------------------------------------------------
path$type <- factor("file", levels = c("file", "folder", "link"))
path$type[file.info(path$from)$isdir] <- "folder"
path$type[Sys.readlink(path$from) != ""] <- "link"
# Remove all files that are descendants of links -------------------------------------------------
is_child <- function(query, refs) {
sapply(refs, function(x) grepl(paste0("^", x), query) & query != x)
}
path <- path[!sapply(path$from, function(x) any(is_child(x, path$from) & path$type == "link")), ]
# Make copy --------------------------------------------------------------------------------------
invisible(lapply(path$to[path$type == "folder"], dir.create, recursive = TRUE))
invisible(file.copy(from = path$from[path$type == "file"], to = path$to[path$type == "file"]))
invisible(file.symlink(Sys.readlink(path$from[path$type == "link"]), path$to[path$type == "link"]))
}
它的工作方式类似于file.copy
(选项较少),但可以正确复制链接。它的工作原理是
list.files(from, recursive = TRUE, all.files = TRUE, include.dirs = TRUE)
list.files
)