Shell Script to auto download file from sftp server

This is a shell scrip to download a file from an sftp server with password. File names might be with datetime stamp like FileName_2022030125600.CSV. The script will get to the sftp site, download the files with current datetime stamp to your preferred location.

set now [clock seconds]
set date [clock format $now -format (%Y%m%d}]
spawn sftp tp user@server-name:/path/to/file
expect "Password:"
send "password-here\n"
expect "sftp>"
send "get filename_$date* /path/to/whereto/downloadFile\n"
expect "sftp>"
send "exit\n"

You can save the script as sftp-download.ksh, then call it from wherever you’re as ./sftp-download.ksh