【问题标题】:Shell script to move files on remote SFTP Server用于在远程 SFTP 服务器上移动文件的 Shell 脚本
【发布时间】:2019-06-20 20:42:23
【问题描述】:

在本地下载文件后,我试图通过 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

【问题讨论】:

    标签: linux rename sftp expect


    【解决方案1】:

    您可以将lftp 与mmv 选项一起使用

    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

    示例用法

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

    【讨论】:

    • 是的,这可能是一个选项,但不幸的是我们没有安装新库的权限,所以只能使用 sftp。
    猜你喜欢
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 2012-10-20
    • 1970-01-01
    • 2014-06-09
    • 2015-06-01
    • 2013-12-18
    相关资源
    最近更新 更多