如何从 SFTP 服务器获取文件并将它们移动到 bash 脚本中的另一个文件夹?

问题描述 投票:0回答:3

如何从 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

我知道这是不对的,但我已经尽力了,如果有人可以帮助我,我将不胜感激。预先感谢。

linux bash shell ubuntu sftp
3个回答
2
投票

OpenSSH

sftp
对于此类任务来说并不是非常强大的客户端。你必须运行它两次。首先收集文件列表,使用该列表生成命令列表,并在第二次运行中执行这些命令。

类似这样的:

# Collect list of files
files=`sftp -b - [email protected] <<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 - [email protected]

在生产文件上运行脚本之前,我建议您首先将生成的命令列表输出到文件中以检查结果是否符合预期。

只需将最后一行替换为:

) > commands.txt

0
投票

使用 rsync https://rsync.samba.org/

rsync [选项]...SRC [SRC]...[USER@]HOST:DEST

rsync SFTP@ip_address:/home/FSTP/Folder1/* /home/SFTP/Done/

-1
投票

也许使用SFTP内部命令。

sftp get -r $remote_path $local_path 

或使用 -f 选项将文件刷新到磁盘

sftp get -rf $remote_path $local_path
© www.soinside.com 2019 - 2024. All rights reserved.