SFTP: Move (rename) many files from one folder to another (not one by one)

sftpshellsolarisunix

I need to move all the files from current_path to current_path/DestinationFolder.

The version of SFTP I'm using is: SFTP protocol version 2

The available commands are:

sftp> help
Available commands:
cd path                       Change remote directory to 'path'
lcd path                      Change local directory to 'path'
chgrp grp path                Change group of file 'path' to 'grp'
chmod mode path               Change permissions of file 'path' to 'mode' 
chown own path                Change owner of file 'path' to 'own'
help                          Display this help text
get remote-path [local-path]  Download file
lls [ls-options [path]]       Display local directory listing
ln oldpath newpath            Symlink remote file
lmkdir path                   Create local directory
lpwd                          Print local working directory
ls [path]                     Display remote directory listing
lumask umask                  Set local umask to 'umask'
mkdir path                    Create remote directory
put local-path [remote-path]  Upload file
pwd                           Display remote working directory
exit                          Quit sftp
quit                          Quit sftp
rename oldpath newpath        Rename remote file
rmdir path                    Remove remote directory
rm path                       Delete remote file
symlink oldpath newpath       Symlink remote file
version                       Show SFTP version
!command                      Execute 'command' in local shell
!                             Escape to local shell
?                             Synonym for help

I don't have mv command. I tried RNFT and didn't work.

Now, I can use rename:

 rename current_path/myFile.txt current_path/DestinationFolder/myFile.txt

and it's OK. But I need to move all (or many) of the files. The following didn't work:

 rename current_path/* current_path/DestinationFolder/

Couldn't rename file "current_path/*" to "current_path/DestinationFolder/": Bad message

Don't have access through SSH so I can't do things like echo "ssh login@server mv * current_path/DestinationFolder/"

I shouldn't mess with this server this is: no scripts, no activities, etc. I'm very limited.

Could you recommend me a way to solve this, please??

NOTE: this is in SOLARIS.

Best Answer

OpenSSH sftp is not very powerful client for such tasks. You would have to run it twice. First to collect list of files, use the list to generate list of commands, and execute those in a second run.

Something like this:

files=`sftp -b - user@example.com <<EOF
cd /source/folder
ls
EOF`
files=`echo $files|sed "s/.*sftp> ls//"` 

(
  echo cd /source/folder
  for file in $files; do
    echo get $file
    echo rename $file /destination/folder/$file
  done
) | sftp -b - user@example.com

Before you run the script on production files, I suggest, you first output the generated command list to a file to check, if the results are as expected.

Just replace the last line with:

) > commands.txt