How do I get the files from SFTP server and move them to another folder in bash script?
10,068
OpenSSH sftp
is not very powerful client for such tasks. 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 - [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]
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
Author by
Naura
Updated on June 05, 2022Comments
-
Naura almost 2 years
How do I get the one by one files from SFTP server and move them do another folder in Ubuntu bash script?
#!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.