Search code examples
linuxbashremote-serverscp

"Is a directory" error when copying specified directories from remote


I'm trying to download from remote last N directories using following script:

  echo "Downloading last $lastN failed tests..."
  RECENT=$(ssh $USER@$IP ls -1td $DIR/* | head -$lastN)
  scp -r $USER@$IP:"$RECENT" $TARGET

but it downloads only first file and throws errors for others:

bash: line 1: /some/path/test_2018-12-08-20-21-19: Is a directory
bash: line 2: /some/path/test_2018-12-07-15-08-53: Is a directory
bash: line 3: /some/path/test_2018-12-07-14-56-28: Is a directory
bash: line 4: /some/path/test_2018-12-07-14-54-12: Is a directory

What's wrong with my script?


Solution

  • First you need to iterate on all values of $RECENT variable, instead of specifying it at once to the scp command.

    for recentElement in $RECENT; do
      scp -r $USER@$IP:"$recentElement " "$TARGET/"
    done
    

    But, you may need to adapt the way you create the $TARGET variable, are you sure it is a directory?

    Did you consider using rsync, instead of scp?