如何使用 while read 行将文件从一个列表文件移动到另一台服务器?

How to move file in another server from one list file using while read line?

目标是我想从不同的服务器监控一个目录。例如远程服务器是 user@host.

我有 list.txt 将要移动的文件的内容列表。并且 list.txt 位于远程服务器中。

目前我有这个代码。

ssh user@host cat /full-path/list.txt |
{
while read line;
do mv user@host:/full-path/$line user@host:/full-path/done/;
done;
}

当我运行上面的代码时,错误存在。没有这样的文件或目录。

但是当我从list.txt随机登录user@hostcat一个文件时,该文件存在。

while 循环在本地服务器上运行。您需要将脚本放在引号中,以便它成为 ssh 命令的参数。

... 或者这里的文档,像这样:

ssh user@host <<':'
    while read line; do
        mv /full-path/"$line" /full-path/done/
    done </full-path/list.txt
:

...或更简洁

ssh user@host 'cd /full-path && xargs -a list.txt mv -t done'

还请注意缺少 useless cat 和本地文件名解析(mv 不知道您尝试使用的 SSH 远程路径语法)。