sftp,

SFTP transfer files through command line non-interactive

Jan 13, 2022 · 1 min read · Post a comment
SFTP transfer files through command line non-interactive

This tutorial is aimed to show you how to download files from a remote sftp server and of course how to upload files to a remote sftp server. It’s useful if you are writing a bash or shell script which requires non-interactive usage of the sftp command and it’s very helpful for setting up cron jobs and automating the sftp transferring files actions.

Prerequisites

  • SFTP

Upload files from your machine to a remote sftp server

Step 1. The default syntax for uploading files:

sftp (user)@(host):(remote_dir) <<< $'put (local_file_path)'

Step 2. Let’s say you want to upload multiple .csv files from your machine to a remote sftp server which requires user and password provided non-interactively:

sshpass -p "user_password" sftp user@sftp.server.com:remote_dir <<< $'put *.csv'

Download files from your machine to a remote sftp server

Step 1. The default syntax for downloading files:

sftp (user)@(host):(remoteFileName) (localFileName)

Step 2. So if you want to download multiple .csv files from a remote server to your local machine which requires user and password provided non-interactively:

sshpass -p "user_password" sftp user@sftp.server.com:remote_dir/*.csv local_dir/

Conclusion

As I mentioned these commands are useful for setting up a cron job for automating your sftp transferring files. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.