[英]Bash script: Looping through directories over SSH and downloading them to local machine
我正在尝试编写一个bash脚本:
目前,我有:
ssh username@server "for dir in ~/directoryname/*; (... something here!); done"
但是,我认为我无法在访问SSH服务器时使用scp。 有没有一种方法可以循环浏览并在此处下载所有内容?
仅当远程服务器可以访问您自己的系统时,才能远程运行scp
。 让我们假设没有。
您可以分两步进行操作:
ssh username@server "... some script that just echos the paths ..." > log
for line in $(<log); do scp username@server:$line ./dir/$line; done
或者您可以研究功能非常强大的rsync
。 它具有--include
和--exclude
选项,使您可以执行以下操作:
rsync -av username@server:~/somepath/ ./somepath/ [--exclude/--include flags]
是的,该脚本是传递给ssh的参数,并且在远程计算机上运行,这意味着您不能轻易引用自己的计算机来告诉它要将文件复制到何处。
您可以尝试在本地运行的bash脚本来获取要搜索的目录结构,并使用scp复制任何您想要检索的内容。
或者,根据涉及的大小,遍历所有directoryname/
,然后从中获取所需内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.