SFTP using BASH script

Hello Experts,

I want to write a script to send folder from local server to another server using SFTP.

Using username "oracle".
oracle@**********'s password:
Last login: Wed Aug 19 23:15:50 2015 from ***********
[oracle@********** ~]$ sftp root@**********
Connecting to **********...
root@**********'s password:
sftp> cd /root/Desktop/exp_files
sftp> lcd /home/oracle/app/EXP_DIR
sftp> put test.txt
Uploading test.txt to /root/Desktop/exp_files/test.txt
test.txt                                      100%    5     0.0KB/s   00:00
sftp> put test
skipping non-regular file test
sftp> put test/
skipping non-regular file test/
sftp> put -r test
put: Invalid flag -r
sftp> put -r test/*
put: Invalid flag -r
sftp> bye
[oracle@********** ~]$

Open in new window

I know if I zip the folder then I can send it and unzip it but this is not allowed as per the customer.
So can I create the same directory on the SFTP server and move all the files under it .

The script what I was looking to take 4 parameters like : host name , username , password , local path , remote path , foldername

Looking for the help here ,

Thanks in advance.
LVL 17
Swadhin Ray Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

you can use scp -r rather sftp. To automate passing user name/password you can use expect. Or you can set trust between your account and remote account (in this case password is required).

Please see links below for more info / help.

Swadhin Ray Author Commented:
I have written the script as below:

/usr/bin/expect<<EOD > output.log
spawn /usr/bin/sftp -o $USER@$HOST
expect "password:"
send "$PASSWORD\r"
expect "sftp>"
send "lcd $SOURCE_DIR\r"
expect "sftp>"
send "cd $TARGET_DIR\r"
expect "sftp>"
send "put $SOURCE_FILE\r"
expect "sftp>"
send "bye\r"
if [[ ${RC} -ne 0 ]]; then
  cat output.log | "Errors Received" 
  echo "Success" | "Transfer Successful" 

Open in new window

Now I am running the script like below but I am getting the errors too:

oracle@vmidelpgeismdb sftp]$ ./sftp.sh myhost root metlan /home/oracle/app/EXP_DIR test.txt /root/Desktop/exp_files
invalid command name "sftp"
    while executing
"sftp -o root@myhost"
./sftp.sh: line 24: Errors Received: command not found
[oracle@vmidelpgeismdb sftp]$ ./sftp.sh myhost root metlan /home/oracle/app/EXP_DIR test.txt /root/Desktop/exp_files
send: spawn id exp4 not open
    while executing
"send "metlan\r""
./sftp.sh: line 24: Errors Received: command not found
[oracle@vmidelpgeismdb sftp]$

Open in new window

But why you want to use sftp if it is not copying sub folders?

Did you try scp -r ?
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Swadhin Ray Author Commented:
will create a folder on target machine and then will place all the files from source to target. but yes want to use only SFTP.
Swadhin Ray Author Commented:
I have changed the script which is working fine.


set timeout -1

if { $argc != 6 } {
    puts "Usage $argv0 host user pass cd lcd file "
    exit 1

set host [lindex $argv 0]
set user [lindex $argv 1]
set pass [lindex $argv 2]
set cd [lindex $argv 3]
set lcd [lindex $argv 4]
set file [lindex $argv 5]

spawn sftp -oStrictHostKeyChecking=no -oCheckHostIP=no $user@$host
expect *assword:

send "$pass\r"
expect sftp>

send "cd $cd\r"
expect sftp>

send "lcd $lcd\r"
expect sftp>
send "put $file\r"

send "exit\r"
expect eof

Open in new window

But want to add something like below :

if [[ $RC -ne 0 ]]; then
  cat output.log | "Errors Received" 
  echo "Success" | "Transfer Successful" 

Open in new window

So that when i execute the script this will return me if file transfer is success or failed .

Can you help me in this part.
I am not sure how to do that, but you can use expect to handle returned messages after every command you send to sftp.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Swadhin Ray Author Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.