简体   繁体   中英

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

The goal is I want to monitor one directory from different server. For example the remote server is user@host .

I have list.txt that contents list of file that will be moved. And list.txt located in a remote server.

Currently I have this code.

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

When I run the code above, error exists. There's no such file or directory.

But when I log in to user@host and cat one file randomly from list.txt , the file exists.

The while loop runs on the local server. You need to put the script in quotes so it's an argument to the ssh command.

... Or a here document, like this:

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

... or more succinctly

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

Notice also the absence of a useless cat and the local file name resolution ( mv would have no idea about the SSH remote path syntax you were trying to use).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM