I need to upload files to an SFTP server as part of an automated pipeline process (so can not be interactive). I do not have ssh access so I can not use scp or rsync.

I have had some success using the solution proposed in this answer:

sftp user@server <<EOF
put localPath remotePath

However I am looking for something a bit more solid, as I will have no indication if this fails. For example, if I wanted to download from an SFTP server I can use the following syntax:

sftp user@server:remotePath localPath

Is there an equivalent one-liner for uploading?


You can use "Batch Mode" of sftp. From manual:

> -b batchfile
>              Batch mode reads a series of commands from an input batchfile instead of stdin.  Since it lacks user interaction it
>              should be used in conjunction with non-interactive authentication.  A batchfile of ‘-’ may be used to indicate
>              standard input.  sftp will abort if any of the following commands fail: get, put, reget, reput, rename, ln, rm,
>              mkdir, chdir, ls, lchdir, chmod, chown, chgrp, lpwd, df, symlink, and lmkdir.  Termination on error can be sup‐
>              pressed on a command by command basis by prefixing the command with a ‘-’ character (for example, -rm /tmp/blah*).

Which means, you create a temporary file with the commands and execute the commands in the file with "sftp -b tempfile user@server"

There are other tools around for such things, e.g. lftp


The answer by Marco led me to create a simple script to wrap the process, but essentially what I am doing in the script is the following:

  1. creating a batchfile with put {local} {remote} commands for the file(s) to upload.
  2. running the following command:

sftp -b {batch_file} -i {identity_file} -o StrictHostKeyChecking=no {username}@{hostname}

This command will work without needing any input from the user, and will have an error code that can be checked for success or failure.

I just wanted to include this answer here in a more complete form for future visitors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.