This function creates directories on a remote server using the SFTP protocol.
It supports recursive directory creation, effectively behaving like
mkdir -p on a Unix-like system.
Usage
sftp_mkdir(
sftp_conn,
remote_url = NULL,
.recursive = TRUE,
.verbose = TRUE,
.ignore_error = .recursive
)Arguments
- sftp_conn
An
SFTPConnobject containing connection details and authentication. Created bysftp_connect.- remote_url
Character. The full URL or path of the file or directory to be operated on.
- .recursive
Logical. Defaults to
FALSE. IfTRUE, will recursively perform the SFTP operation:sftp_delete(): deletes the directory and everything within.sftp_list(): lists all the directories and files.sftp_mkdir(): creates all the missing parent directories.sftp_rename(): seesftp_mkdir().
- .verbose
Logical. Defaults to
TRUE. Prints helpful messages.- .ignore_error
Logical. If
TRUE, the function uses the*prefix in the curl quote command to ignore errors (e.g., if the directory already exists). This is useful in.recursive = TRUEbecause attempting to create a directory that already exists will return error. While this can be avoided by checking for directory existence, doing so adds extra step that impacts performance.
Details
When .recursive = TRUE, the function splits the path into segments and
attempts to create each one sequentially. It uses the * prefix for
internal calls to ensure that existing directories do not trigger errors.
Examples
if (FALSE) { # \dontrun{
if (interactive() || Sys.getenv("R_SFTP_TEST_SERVER") == "true") {
# Create new SFTP connection
sftp_conn <- sftp_connect(
hostname = "127.0.0.1",
port = "2222",
user = "tester",
password = "password123"
)
# Create a nested directory structure
sftp_mkdir(sftp_conn, "project/data/results/2026", .recursive = TRUE)
# Create a single directory and fail if parents are missing
sftp_mkdir(sftp_conn, "simple_dir", .recursive = FALSE)
}
} # }