Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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
scp一个shell脚本中的多个文件_Shell - Fatal编程技术网

scp一个shell脚本中的多个文件

scp一个shell脚本中的多个文件,shell,Shell,我有一个目录名列表 我想将scp导入远程计算机,进入我的每个目录名,并将一个文件复制回本地计算机 到目前为止,我已经: while read line do scp remote_machine:/home/$line/$line.dat ./local done < file_with_directory_names.txt 读取行时 做 scp远程机器:/home/$line/$line.dat./local 完成

我有一个目录名列表

我想将scp导入远程计算机,进入我的每个目录名,并将一个文件复制回本地计算机

到目前为止,我已经:

while read line
    do
        scp remote_machine:/home/$line/$line.dat ./local
    done < file_with_directory_names.txt
读取行时
做
scp远程机器:/home/$line/$line.dat./local
完成

我已经设置了授权密钥,这样我就不必每次都输入密码——但是这种方法确实可以为它传输的每个文件登录到远程机器。我想还有比这更好的方法。

您可以在一个
scp
参数中指定多个文件,方法是用空格分隔;您只需要确保它是
scp
本身的一个参数。这应该适用于您的情况:

scp "remote_machine:$(
    sed 's,.*,/home/&/&.dat,' file_with_directory_names.txt | xargs)" ./local

sed
命令在每一行上粘贴
/home/
前缀和名称
.dat
后缀;
xargs
在以空格分隔的单行上输出所有生成的路径名。在
remote\u machine:
部分之后插入源参数,所有部分都在双引号内,因此它仍然是
scp
的单个参数,可以继续了。

可以在单个
scp
参数中指定多个文件,方法是用空格分隔;您只需要确保它是
scp
本身的一个参数。这应该适用于您的情况:

scp "remote_machine:$(
    sed 's,.*,/home/&/&.dat,' file_with_directory_names.txt | xargs)" ./local
sed
命令在每一行上粘贴
/home/
前缀和名称
.dat
后缀;
xargs
在以空格分隔的单行上输出所有生成的路径名。在
remote\u machine:
part后面的源参数中插入所有内容,所有内容都包含在双引号中,因此它仍然是
scp
的单个参数,您可以开始了