0

I am trying to move files from one location to another on the remote server using sftp below:

for i in a b c d
do
sftp $REMUSR <<EOM>>$OUT 2>&1
rename $SOURDIR/sample_${i}_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].gz $REMDIR
quit
EOM
:
:
done

but i get the message

Couldn't rename file "/source/sample_a_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].gz" to       "/destin/": No such file or directory

though this file exists under the /source directory which i verified:

ls -l sample_a_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].gz
-rw-r--r--  1 prd admin 112 May 23 09:16 sample_a_20140330.gz

Pls help

user68112
  • 1
  • 1
  • If you want to write a bash script, then I would suggest to use `scp` instead as it will allow you to pass it bash variables – ek9 May 23 '14 at 07:35
  • @edvinas.me How does that work ? Doesn't the op want to move a remote file into a remote directory (at least that's the way I read it)? – user9517 May 23 '14 at 07:45

3 Answers3

0

sftp can't see the remote filenames to process the globbing, use ssh with mv instead:

 ssh $REMUSR mv $SOURDIR/sample_${i}_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].gz $REMDIR"

here the implied remote shell process will do the globing and pass the results to mv

Jasen
  • 826
  • 6
  • 12
0

I think you need to supply a destination file name

sftp> rename f destin/
Couldn't rename file "/home/iain/s" to "/home/iain/destin/": No such file or directory
sftp> rename f destin/s

I did a quick search and it doesn't look like there is a clean solution to this.

user9517
  • 115,471
  • 20
  • 215
  • 297
  • Alternative would be to use `scp` which would allow to pass it bash variables – ek9 May 23 '14 at 07:35
  • Am supposed to use sftp only for this, sadly. If I'm sure of having the required files under /source, can something like rename $SOURDIR/* $REMDIR be used atleast to move everything to /destin? – user68112 May 23 '14 at 08:21
  • rename is the same as mv - the deistination can be a dirctory name – Jasen Mar 03 '20 at 08:26
0

first, it looks like you are missing a destination on the remote host next, it looks as if you are trying to rename a directory to a file. if you are trying to rename a directory, try using the full path, and not use a trailing "/" (ex. rename /home/mydir /home/otherdir )

Also, it looks like the command is seeing the regex as an actual filename (based on the quotes around it, in your error message) try enclosing the command in the script in backticks "`".

Remember, just because you are executing on a remote server, the commands are being interpeted locally.

Another possibility is to use batch mode to run the commands, rather than the <> method you are using.

weismanm
  • 71
  • 4