PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, May 14, 2022

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

 May 14, 2022     bash, linux, sftp, shell, ubuntu     No comments   

Issue

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.


Solution

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 - 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


Answered By - Martin Prikryl
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing