tbaveja
tbaveja

Reputation: 76

SCP issue with multiple files - UNIX

Getting error in copying multiple files. Below command is copying only first file and giving error for rest of the files. Can someone please help me out.

Command:

scp $host:$(ssh -n $host "find /incoming -mmin -120 -name 2018*") /incoming/

Result:

user@host:~/scripts/OTA$ scp $host:$(ssh -n $host "find /incoming -mmin -120 -name 2018*") /incoming/
Password:
Password:
2018084session_event 100% |**********************************************************************************************************|  9765 KB    00:00
cp: cannot access /incoming/2018084session_event_log.195-10.45.40.9
cp: cannot access /incoming/2018084session_event_log.195-10.45.40.9_2_3

Upvotes: 0

Views: 920

Answers (1)

ghoti
ghoti

Reputation: 46816

Your command uses Command Substitution to generate a list of files. Your assumption is that there is some magic in the "source" notation for scp that would cause multiple members of the list generated by your find command to be assumed to live on $host, when in fact your command might expand into something like:

scp remotehost:/incoming/someoldfile anotheroldfile /incoming

Only the first file is being copied from $host, because none of the rest include $host: at the beginning of the path. They're not found in your local /incoming directory, hence the error.

Oh, and in addition, you haven't escape the asterisk in the find command, so 2018* may expand to multiple files that are in the login directory for the user in question. I can't tell from here, it depends on your OS and shell configuration.

I should point out that you are providing yet another example of the classic Parsing LS problem. Special characters WILL break your command. The "better" solution usually offered for this problem tends to be to use a for loop, but that's not really what you're looking for. Instead, I'd recommend making a tar of the files you're looking for. Something like this might do:

ssh "$host" "find /incoming -mmin -120 -name 2018\* -exec tar -cf - {} \+" |
tar -xvf - -C /incoming

What does this do?

  • ssh runs a remote find command with your criteria.
  • find feeds the list of filenames (regardless of special characters) to a tar command as options.
  • The tar command sends its result to stdout (-f -).
  • That output is then piped into another tar running on your local machine, which extracts the stream.

If your tar doesn't support -C, you can either remove it and run a cd /incoming before the ssh, or you might be able to replace that pipe segment with a curly-braced command: { cd /incoming && tar -xvf -; }

The curly brace notation assumes a POSIX-like shell (bash, zsh, etc). The rest of this should probably work equally well in csh if that's what you're stuck with.

Limited warranty: Best Effort Only. Untested on animals or computers. Your milage may vary. May contain nuts.

If this doesn't work for you, poke at it until it does.

Upvotes: 1

Related Questions