ID EN
I/O & Files

files

R Base 3.6.2

These functions provide a low-level interface to the computer's file system.

Syntax

R
file.create(…, showWarnings = TRUE)
file.exists(…)
file.remove(…)
file.rename(from, to)
file.append(file1, file2)
file.copy(from, to, overwrite = recursive, recursive = FALSE,
          copy.mode = TRUE, copy.date = FALSE)
file.symlink(from, to)
file.link(from, to)
Sys.junction(from, to)

Arguments

Parameter Description
…, file1, file2 character vectors, containing file names or paths.
from, to character vectors, containing file names or paths. For file.copy and file.symlink and Sys.junction to can alternatively be the path to a single existing directory.
overwrite logical; should existing destination files be overwritten?
showWarnings logical; should the warnings on failure be shown?
recursive logical. If to is a directory, should directories in from be copied (and their contents)? (Like cp -R on POSIX OSes.)
copy.mode logical: should file permission bits be copied where possible?
copy.date logical: should file dates be preserved where possible? See Sys.setFileTime.

Return Value

These functions return a logical vector indicating which operation succeeded for each of the files attempted. Using a missing value for a file or path name will always be regarded as a failure. If showWarnings = TRUE, file.create will give a warning for an unexpected failure.

Details

The … arguments are concatenated to form one character string: you can specify the files separately or as one vector. All of these functions expand path names: see path.expand. file.create creates files with the given names if they do not already exist and truncates them if they do. They are created with the maximal read/write permissions allowed by the ‘umask’ setting (where relevant). By default a warning is given (with the reason) if the operation fails. file.exists returns a logical vector indicating whether the files named by its argument exist. (Here ‘exists’ is in the sense of the system's stat call: a file will be reported as existing only if you have the permissions needed by stat. Existence can also be checked by file.access, which might use different permissions and so obtain a

Examples

Example
R
# NOT RUN {
cat("file A\n", file = "A")
cat("file B\n", file = "B")
file.append("A", "B")
file.create("A") # (trashing previous)
file.append("A", rep("B", 10))
if(interactive()) file.show("A") # -> the 10 lines from 'B'
file.copy("A", "C")
dir.create("tmp")
file.copy(c("A", "B"), "tmp")
list.files("tmp") # -> "A" and "B"
setwd("tmp")
file.remove("A") # the tmp/A file
file.symlink(file.path("..", c("A", "B")), ".")
                     # |--> (TRUE,FALSE) : ok for A but not B as it exists already
setwd("..")
unlink("tmp", recursive = TRUE)
file.remove("A", "B", "C")
# }

See Also

file.info file.access file.path file.show list.files unlink basename path.expand. dir.create. Sys.glob to expand wildcards in file specifications. file_test Sys.readlink (for ‘symlink’s). https://en.wikipedia.org/wiki/Hard_link and https://en.wikipedia.org/wiki/Symbolic_link for the concepts of links and their limitations.