如何从 SFTP 服务器逐个获取文件并将它们移动到 Ubuntu bash 脚本中的另一个文件夹?
#!bin/sh
FOLDER=/home/SFTP/Folder1/
sftp SFTP@ip_address
cd /home/FSTP/Folder1/
for file in "$FOLDER"*
<<EOF
cd /home/local/Folder1
get $file
EOF
mv $file /home/SFTP/Done
done
我知道这是不对的,但我已经尽力了,如果有人能帮助我,我将不胜感激。提前致谢。
最佳答案
OpenSSH sftp
对于此类任务而言并不是非常强大的客户端。你将不得不运行它两次。首先收集文件列表,使用列表生成命令列表,并在第二次运行时执行这些命令。
像这样:
# Collect list of files
files=`sftp -b - user@example.com <<EOF
cd /source/folder
ls
EOF`
files=`echo $files|sed "s/.*sftp> ls//"`
# Use the list to generate list of commands for the second run
(
echo cd /source/folder
for file in $files; do
echo get $file
echo rename $file /backup/folder/$file
done
) | sftp -b - user@example.com
在对生产文件运行脚本之前,我建议您首先将生成的命令列表输出到文件中以检查结果是否符合预期。
只需将最后一行替换为:
) > commands.txt
关于linux - 如何从 SFTP 服务器获取文件并将它们移动到 bash 脚本中的另一个文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53812365/