Bug 17197 - [WISHLIST] Allow 'file.copy()' to copy directories
Summary: [WISHLIST] Allow 'file.copy()' to copy directories
Alias: None
Product: R
Classification: Unclassified
Component: Misc (show other bugs)
Version: R-devel (trunk)
Hardware: Other Other
: P5 enhancement
Assignee: R-core
Depends on:
Reported: 2016-12-23 19:04 UTC by Kevin Ushey
Modified: 2016-12-23 21:06 UTC (History)
1 user (show)

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description Kevin Ushey 2016-12-23 19:04:13 UTC
Given the following code:

   file.copy("foo", "bar") # TRUE

A file "bar" is created, but it is not formed as a copy of the directory "foo"; rather, it seems to just be an empty file. This can be verified with e.g.

   > file.info("bar")[, 1:3]
       size isdir mode
   bar    0 FALSE  755

It would be useful if R's 'file.copy()' followed the shell semantics here, whereby a call to 'file.copy(<dir>, ...)' acted similarly to 'cp -R <dir> ...'.

Alternatively, a separate API 'dir.copy()' could be useful here.

An older mailing list post seems to suggest that file.copy() was intended to do this (at least in some development version of R) but perhaps that change was rolled back? http://r.789695.n4.nabble.com/copy-directory-command-td881914.html


> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12.1
Comment 1 Michael Lawrence 2016-12-23 21:06:00 UTC
The r-help post was probably referring to the recursive argument, which seems to work as long as the destination directory (to contain "foo") exists. This is different from the "cp -R" behavior. I guess it wouldn't be hard to perform the recursive copy into the parent directory, and then rename it. But that would break if the "to" directory was the same as the parent of "from". Right now, file.copy("foo", ".", recursive=TRUE) seems to just copy "foo" over itself without any complaint, even though it does attempt to catch that case. A general solution would recursively copy the _contents_ of "foo" into a new directory. That wouldn't be too hard to implement at the R level. Please consider submitting a patch.