This post is one of the R language learning notes, focusing on file manipulation.
Viewing the Working Directory
> getwd()
[1] "/Users/username"
wd
stands for working directory.
Setting the Working Directory
> setwd('/Users/username/')
Viewing All Files in the Working Directory
> dir()
[1] "example-1.csv"
[2] "example-2.csv"
...
Creating a New File
> file.create("example-3.csv")
[1] TRUE
Copying a File
> file.copy("example-3.csv", "foo.R")
[1] TRUE
Renaming a File
> file.rename("foo.R", "bar.R")
[1] TRUE
Deleting a File
> file.remove("bar.R")
[1] TRUE
Checking if a File Exists
> file.exists("test.R")
[1] FALSE
Path Joining
> file.path('/User/foo', 'bar')
[1] "/User/foo/bar"
Displaying File Information
> file.info('example.csv')
size isdir mode mtime ctime atime uid gid uname grname
example.csv 69112020 FALSE 644 2017-06-02 18:30:17 2017-06-02 18:30:17 2017-06-03 10:53:30 501 20 user staff
Creating a Directory
> dir.create("folder")
To create directories recursively, set the recursive
parameter to TRUE
, similar to the Linux command mkdir -p
.
# mkdir -p
> dir.create(file.path("folder", "subfolder"), recursive=TRUE)
Deleting a Directory
> unlink("folder", recursive = TRUE)
recursive = TRUE
means to recursively delete all files under the directory.