0

I'm trying to copy files from remote server this way:

transferFiles() {
    for FILE in `ssh $USERNAME@$HOSTNAME find "${DIR}" -type f -mtime -1 -name "*log*" 2>/dev/null` ; do
        rsync -zR $USERNAME@$HOSTNAME:$FILE "${DIRII}"
    done
}

I want to copy all files which contains 'log'.

Unfortunately something is not working, I suspect that problem is quotes, I tried many solutions but still nothing.

Sorry I wasn't enough detailed. I was trying to echo $FILE inside loop to check if something is being sent to this variable. echo shows nothing, that's why I'm assuming that problem is in first line. Additionally I was trying to execute directly

ssh $USERNAME@$HOSTNAME find "${DIR}" -type f -mtime -1 -name "*log*"| less

but output was empty. In this case solution was insert find inside single quote

ssh $USERNAME@$HOSTNAME 'find "${DIR}" -type f -mtime -1 -name "*log*"'| less

This one is working. But now my problem is how to apply this into script. I can't do something like this:

transferFiles() {
    for FILE in `ssh $USERNAME@$HOSTNAME 'find "${DIR}" -type f -mtime -1 -name "*log*"' 2>/dev/null` ; do
        rsync -zR $USERNAME@$HOSTNAME:$FILE "${DIRII}"
    done
}

I tried also double quote except single quote for ssh and for find. alternately quote " ' ' " also not resolving this issue.

2
  • It could be helpful for answering the question if we could see excerpts of the plain output of the ssh command only, and maybe the result if you prefix the rsync command with an echo to just see what would have been executed.
    – Jaleks
    May 20, 2020 at 22:32
  • Note, you could also do a single rsync by piping the list of files into rsync --files-from=-. Or you could also run the rsync on the remote back to this machine, provided you have setup the usual ssh keys in that direction (you would need to quote the pipe | to ensure it was passed to the remote shell).
    – meuh
    May 21, 2020 at 9:05

1 Answer 1

0

so, finally I found solution. Problem was lack of backslash \ in -name "*log*"' In this case single quote for whole find is not necessary.

ssh $USERNAME@$HOSTNAME find "${DIR}" -type f -mtime -1 -name "\*log*"| less

This is working correctly.

1
  • You might benefit from switching from using “for FILE in ssh ...; do” to a pipeline like “ssh ... | while read FILE; do”
    – jsbillings
    May 22, 2020 at 18:28

You must log in to answer this question.