[英]Shell script to move files on remote SFTP Server
我試圖在本地下載文件后通過shell腳本將文件從一個目錄移動到遠程SFTP服務器上的另一個目錄。 我知道沒有通配符移動文件功能,所以似乎我唯一的選擇是單獨重命名文件。
如果有更好的方法來編寫此代碼,有人可以幫助我使用下面的代碼。
一旦將文件下載到我的本地目錄,我所要做的就是將文件移動到SFTP服務器上的存檔目錄。
我知道有其他方法可以使用python / perl腳本,但我被限制在linux盒子上使用Shell腳本。
#!/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
你可以使用帶有mmv
選項的lftp
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
用法示例
lftp -u $FTP_USER,$FTP_PWD sftp://$FTP_SERVER:22 <<EOF
mmv dir/to/path /dir/to/renamed/path
EOF
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.