簡體   English   中英

如何從 SFTP 服務器獲取文件並將它們移動到 bash 腳本中的另一個文件夾?

[英]How do I get the files from SFTP server and move them to another folder in 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

我知道這是不對的,但我已經盡力了,如果有人能幫助我,我將不勝感激。 提前致謝。

對於此類任務,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

也許使用 SFTP 內部命令。

sftp get -r $remote_path $local_path 

或者使用 -f 選項將文件刷新到磁盤

sftp get -rf $remote_path $local_path

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM