Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何使用“将文件从一个目录复制到另一个目录”;os/exec";打包_Linux_Go - Fatal编程技术网

Linux 如何使用“将文件从一个目录复制到另一个目录”;os/exec";打包

Linux 如何使用“将文件从一个目录复制到另一个目录”;os/exec";打包,linux,go,Linux,Go,如果我在目录A中运行GO代码,并且我需要将文件从目录B复制到目录C,那么怎么做?我尝试添加cmd.Dir=“B”,但它可以复制“B”目录中的文件,但当我尝试目录“C”的完整路径时,它抛出错误“退出状态1” 基本代码示例 当前在目录A中,位置为“/var/A” cmd:=exec.Command(“cp”、“/var/C/C.txt”、“/var/B/”) err:=cmd.Run()是用于运行外部程序的Go包,其中包括Linux实用程序 // The command name is the fi

如果我在目录A中运行GO代码,并且我需要将文件从目录B复制到目录C,那么怎么做?我尝试添加cmd.Dir=“B”,但它可以复制“B”目录中的文件,但当我尝试目录“C”的完整路径时,它抛出错误“退出状态1”

基本代码示例

当前在目录A中,位置为“/var/A” cmd:=exec.Command(“cp”、“/var/C/C.txt”、“/var/B/”) err:=cmd.Run()

是用于运行外部程序的Go包,其中包括Linux实用程序

// The command name is the first arg, subsequent args are the
// command arguments.
cmd := exec.Command("tr", "a-z", "A-Z")
// Provide an io.Reader to use as standard input (optional)
cmd.Stdin = strings.NewReader("some input")
// And a writer for standard output (also optional)
var out bytes.Buffer
cmd.Stdout = &out
// Run the command and wait for it to finish (the are other
// methods that allow you to launch without waiting.
err := cmd.Run()
if err != nil {
    log.Fatal(err)
}
fmt.Printf("in all caps: %q\n", out.String())

可能重复的谢谢!!我还有一个问题,如果我在目录A中运行GO代码,我需要将一个文件从目录B复制到目录C,它是如何做到的?我尝试添加cmd.Dir=“A”它可以将文件复制回“A”目录,但当我尝试使用目录“C”的完整路径时,它抛出错误“退出状态1”。请帮我做这个
。/B
。/C
?假设
x/A
x/B
x/C
<代码>。表示“父目录”。目录A=“/var/www/vhosts/api/src/copy.go”。目录B=“/mnt/home/sftp”和目录C=“/tmp/nitin”。上述所有目录都位于同一台服务器上。
cp/mnt/home/sftp/file.name/tmp/nitin/file.name
如果出现错误,您需要从标准错误中获取错误消息并从那里继续。也许首先要弄清楚如何从shell手动执行该操作?从shell手动执行该命令,但当我尝试从GO代码执行相同操作时,它抛出的标准错误是“退出状态1”