简体   繁体   English

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

[英]How do I get the files from SFTP server and move them to another folder in bash script?

How do I get the one by one files from SFTP server and move them do another folder in Ubuntu bash script?如何从 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

I know it's not right, but i've tried my best and if anyone can help me, i will appreciate it.我知道这是不对的,但我已经尽力了,如果有人能帮助我,我将不胜感激。 Thanks in advance.提前致谢。

OpenSSH sftp is not very powerful client for such tasks.对于此类任务,OpenSSH sftp不是非常强大的客户端。 You would have to run it twice.你将不得不运行它两次。 First to collect list of files, use the list to generate list of commands, and execute those in a second run.首先收集文件列表,使用该列表生成命令列表,并在第二次运行时执行这些命令。

Something like this:像这样:

# 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

Before you run the script on production files, I suggest, you first output the generated command list to a file to check, if the results are as expected.在对生产文件运行脚本之前,我建议您首先将生成的命令列表输出到一个文件中,以检查结果是否符合预期。

Just replace the last line with:只需将最后一行替换为:

) > commands.txt

Maybe use SFTP internal command.也许使用 SFTP 内部命令。

sftp get -r $remote_path $local_path 

OR with the -f option to flush files to disk或者使用 -f 选项将文件刷新到磁盘

sftp get -rf $remote_path $local_path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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