How could I change the ownership of files from R? I suppose I could use system
and call chown
directly against a string, but I am wondering if there is a better way.
如何从R中改变文件的所有权?我想我可以使用系统直接调用chown,但是我想知道是否有更好的方法。
1 个解决方案
#1
2
As was already mentioned in the comments, the function Sys.chown()
does not exist. I have written a function that does the job for me. Compared to other functions of the type Sys.*
, this one has the disadvantage that chown
requires sudo
. Using sudo
with R's system()
does not work, but this answer suggests the use of gksudo
, which works fine for me.
正如在注释中已经提到的,函数Sys.chown()不存在。我写了一个函数来完成我的工作。与其他类型系统的功能相比。*,这个有chown需要sudo的缺点。在R的system()中使用sudo是行不通的,但是这个答案建议使用gksudo,这对我来说很好。
So this is the function definition:
这就是函数的定义
Sys.chown <- function(paths, owner = NULL, group = NULL) {
# create string for user:owner
if (!is.null(group)) {
og <- paste0(owner, ":", group)
} else {
og <- owner
}
# create string with files
files <- paste(paths, collapse = " ")
# run command
system(paste("gksudo chown", og, files))
invisible()
}
The first part creates the string that sets the owner and the group, which should be of the form owner:group
. However, it is possible to omit one or both of these argument and the function takes care of all the possibilities.
第一部分创建设置所有者和组的字符串,该字符串应该是窗体所有者:group。但是,可以省略其中的一个或两个参数,函数将处理所有的可能性。
Next comes the part where all the file names that have been supplied to paths
are put into a single string.
接下来是将提供给路径的所有文件名放入单个字符串的部分。
And finally, system()
is used to do the actual call of chown
. gksudo
will open a dialog window and ask for the password. Unfortunately, one has to type the password every time.
最后,system()用于执行chown的实际调用。gksudo将打开一个对话框窗口并询问密码。不幸的是,每次都要输入密码。
There are several useful options to chown
and a better implementation of Sys.chown()
should probably be able to handle some of them.
chown有几个有用的选项,而且Sys.chown()的更好实现应该能够处理其中的一些选项。
Example
例子
system("ls -l")
## total 0
## -rw-rw-r-- 1 user1 user1 0 Jan 21 19:32 test2.file
## -rw-rw-r-- 1 user1 user1 0 Jan 21 19:32 test.file
Sys.chown("test.file", owner = "user2")
Sys.chown("test2.file", group = "user2")
system("ls -l")
## total 0
## -rw-rw-r-- 1 user1 user2 0 Jan 21 19:32 test2.file
## -rw-rw-r-- 1 user2 user1 0 Jan 21 19:32 test.file
Sys.chown("test.file", owner = "user1", group = "user2")
system("ls -l")
## total 0
## -rw-rw-r-- 1 user1 user2 0 Jan 21 19:32 test2.file
## -rw-rw-r-- 1 user1 user2 0 Jan 21 19:32 test.file
Sys.chown(dir(), owner = "user1", group = "user1")
system("ls -l")
## total 0
## -rw-rw-r-- 1 user1 user1 0 Jan 21 19:32 test2.file
## -rw-rw-r-- 1 user1 user1 0 Jan 21 19:32 test.file
#1
2
As was already mentioned in the comments, the function Sys.chown()
does not exist. I have written a function that does the job for me. Compared to other functions of the type Sys.*
, this one has the disadvantage that chown
requires sudo
. Using sudo
with R's system()
does not work, but this answer suggests the use of gksudo
, which works fine for me.
正如在注释中已经提到的,函数Sys.chown()不存在。我写了一个函数来完成我的工作。与其他类型系统的功能相比。*,这个有chown需要sudo的缺点。在R的system()中使用sudo是行不通的,但是这个答案建议使用gksudo,这对我来说很好。
So this is the function definition:
这就是函数的定义
Sys.chown <- function(paths, owner = NULL, group = NULL) {
# create string for user:owner
if (!is.null(group)) {
og <- paste0(owner, ":", group)
} else {
og <- owner
}
# create string with files
files <- paste(paths, collapse = " ")
# run command
system(paste("gksudo chown", og, files))
invisible()
}
The first part creates the string that sets the owner and the group, which should be of the form owner:group
. However, it is possible to omit one or both of these argument and the function takes care of all the possibilities.
第一部分创建设置所有者和组的字符串,该字符串应该是窗体所有者:group。但是,可以省略其中的一个或两个参数,函数将处理所有的可能性。
Next comes the part where all the file names that have been supplied to paths
are put into a single string.
接下来是将提供给路径的所有文件名放入单个字符串的部分。
And finally, system()
is used to do the actual call of chown
. gksudo
will open a dialog window and ask for the password. Unfortunately, one has to type the password every time.
最后,system()用于执行chown的实际调用。gksudo将打开一个对话框窗口并询问密码。不幸的是,每次都要输入密码。
There are several useful options to chown
and a better implementation of Sys.chown()
should probably be able to handle some of them.
chown有几个有用的选项,而且Sys.chown()的更好实现应该能够处理其中的一些选项。
Example
例子
system("ls -l")
## total 0
## -rw-rw-r-- 1 user1 user1 0 Jan 21 19:32 test2.file
## -rw-rw-r-- 1 user1 user1 0 Jan 21 19:32 test.file
Sys.chown("test.file", owner = "user2")
Sys.chown("test2.file", group = "user2")
system("ls -l")
## total 0
## -rw-rw-r-- 1 user1 user2 0 Jan 21 19:32 test2.file
## -rw-rw-r-- 1 user2 user1 0 Jan 21 19:32 test.file
Sys.chown("test.file", owner = "user1", group = "user2")
system("ls -l")
## total 0
## -rw-rw-r-- 1 user1 user2 0 Jan 21 19:32 test2.file
## -rw-rw-r-- 1 user1 user2 0 Jan 21 19:32 test.file
Sys.chown(dir(), owner = "user1", group = "user1")
system("ls -l")
## total 0
## -rw-rw-r-- 1 user1 user1 0 Jan 21 19:32 test2.file
## -rw-rw-r-- 1 user1 user1 0 Jan 21 19:32 test.file