How to code "sudo cp" To copy file

As part of an R program, I have to code Linux "cp" to copy a file. This is Linux ubuntu 17.04. In addition, I have to use sudo. How can I include sudo cp in the code? Here is the script.

I have tried copyCommand<- paste("sudo", "", "cp"), buut I get the message: sudo: no tty present and no askpass program specified

if (paste(dirname(inFileName), "/", sep="") != OUTpath ) { 
     OS<-  Sys.info()["sysname"] # Get OS type
     if (OS=="Windows") { # Use OS-specific copy command
     copyCommand<- "copy"
    } else {
      copyCommand<- "cp"
    }

 paramsCopyFn<- paste(OUTpath, basename(inFileName), sep="")
   paramsCopyFn<- sub(".tsv", "_copy.txt", paramsCopyFn, fixed=T)
   cmd1<- paste(copyCommand, shQuote(inFileName), shQuote(paramsCopyFn) , sep=" ")	
   execSystemCmd(cmd1)	
  }
execSystemCmd<- function(cmd, OS=NULL) {
  if (is.null(OS)) OS<-  Sys.info()["sysname"] # Get OS type
  if (OS=="Windows") { # use OS-secific system shell command wrapper
    shell(cmd, translate=TRUE)
  } else {
    system(cmd)
  }

Can you change copyCommand to "sudo cp"?

And I'm guessing that R's file.copy() won't do what you need it to do?

Thank you! i realized that I can copy and change name using R.

1 Like

Actually, Is better to use R to copy and rename a file. Sudo is going to request a password from the users and then things get very complicated because you have to include users in a sudoers file. Even if you want to bypass the password request by making changes to the sudoers file you have to include the users with the granted permissions in the sudoers file. It would not be a good idea for 2 reasons: this is going to be a public site, and bypassing the sudo password request would create security issues.