根据文件名条件复制文件

Copying files based on filename condition

仅当文件名包含我的 file.txt 文件中的任何名称时,才从远程复制所有文件。

file.txt 包含类似

的名称
rahul
jon
babra

目前我正在这样做:

while read p; do      
scp -i somefile.pem myuser@myip:myfolder/\*$p\* .
done<file.txt

但这会为我的 file.txt 中的每个名称打开到远程的连接。

我正在寻找在单个连接中为所有文件提供服务的优化?

打开一个主连接,每个后续 scp 都可以重复使用。有关使用更安全的控制路径的建议,请参阅 man ssh_config 中的各种 Control* 选项。

# Don't run a command (-N), but open a reusable connection (-M and -S)
# then go to the background (-f)
ssh -fNM myuser@myip -S "%C"

while read p; do
  # Reuse the open connection (-S)
  scp -i somefile.pem -S "%C" myuser@myip:myfolder/\*$p\* .
done < file.txt

# Close the background connection
ssh -S "%C" -O exit