简体   繁体   中英

Shell script to move files on remote SFTP Server

I am trying to move files from one directory to another directory on the remote SFTP Server through shell script after downloading files locally. I understand that there is no wildcard move files function, so it seems that my only option is to rename files individually.

Can someone help me with the code below if there is a better way of writing this code.

All i am trying to do is move files to archive directory on the SFTP Server, once files are downloaded to my local directory.

I know there are other ways to do it with python/perl scripts but i am restricted to use Shell scripting on linux box.

#!/usr/bin/ksh


#LOGGING
LOGFILE="/tmp/test.log"

#SFTP INFO
FTP_SERVER="test.rebex.net"
FTP_USER="demo"
FTP_PWD="password"
FTP_PORT=22
FTP_PICKUP_DIR="/"
LOCAL_DIR="/"


#-------DOWNLOAD FILES
expect <<END #> $LOGFILE
 send "$(date)\r";
 spawn sftp $FTP_USER@$FTP_SERVER 
 expect "*password: " 
 send "$FTP_PWD\r"; 
 expect "sftp> "
 send "mget *.ext\r"  
 expect "sftp>"
 send "exit\r"
END


#--------- MOVE FILES TO ARCHIVE ON SERVER
cd /home/ravi/Files

for fl in *.ext
do

expect <<END #> $LOGFILE
 send "$(date)\r";
 spawn sftp $FTP_USER@$FTP_SERVER 
 expect "*password: " 
 send "$FTP_PWD\r"; 
 expect "sftp> "
 send "rename $fl /ARCHIVE/$fl\r"  
 expect "sftp>"
 send "exit\r"
END

done 
#For Loop End

you can use lftp with mmv option

 mmv [-O directory] file(s) directory Move specified files to a target directory. The target directory can be specified after -O option or as the last argument. -O <dir> specifies the target directory where files should be placed 

Reference

Example usage

lftp -u $FTP_USER,$FTP_PWD sftp://$FTP_SERVER:22 <<EOF
   mmv dir/to/path /dir/to/renamed/path
   EOF

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM