I have a cron on my Ubuntu server that runs this bash script daily:

#!/usr/bin/expect -f
spawn sftp user@ip
expect "password:"
send "password\n"
expect "sftp>"
send "cd remote_directory\n"
expect "sftp>"
send "lcd ~/dtc/data/outstanding\n"
expect "sftp>"
send "mget * \n"
expect "sftp>"
send "exit\n"

It connects to a remote server via SFTP and downloads a bunch of files. This is working no problem, however, I want these files to appear in a certain directory on the machine (my server) running this script.

This is what I'm attempting to do on line 8 to no avail. How can I run this script via cron, and make sure it cds into the right directory on my server so that it places the downloaded files into the right directory?

  • you can cd into the directory you want to download the files into, before executing the expect script and then totally forget the lcd command on line 8
    – MelBurslan
    May 20, 2016 at 15:49
  • You could also think about using scp instead of sftp ? May 20, 2016 at 17:08

1 Answer 1


Simplest solution is to cd into the directory where you want to save the files and then run the expect script, leaving out the lcd command from the script on line 8.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .