Reputation: 65
How do I get the one by one files from SFTP server and move them to 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.
Upvotes: 3
Views: 19875
Reputation: 11
Use rsync https://rsync.samba.org/
rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
rsync SFTP@ip_address:/home/FSTP/Folder1/* /home/SFTP/Done/
To move files add option --remove-source-files
rsync --remove-source-files SFTP@ip_address:/home/FSTP/Folder1/* /home/SFTP/Done/
Upvotes: 0
Reputation: 202514
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
Upvotes: 3
Reputation: 11
Maybe use SFTP internal command.
sftp get -r $remote_path $local_path
OR with the -f option to flush files to disk
sftp get -rf $remote_path $local_path
Upvotes: 0