This is a very basic question I am just quite new to bash and couldn't figure out how to do this. Googling unfortunately didn't get me anywhere.

My goal is to connect with sftp to a server, upload a file, and then disconnect.

I have the following script:



sftp -oPort=23 [email protected]:/home/kalenpw/TestWorld/plugins
#Change directory on server
#cd /home/kalenpw/TestWorld/plugins

#Upload file
put /home/kalenpw/.m2/repository/com/Khalidor/TestPlugin/0.0.1-SNAPSHOT/TestPlugin-0.0.1-SNAPSHOT.jar


the issue is, this script will establish an sftp connection and then do nothing. Once I manually type exit in connection it tries to execute the put command but because the sftp session has been closed it just says put: command not found.

How can I get this to work properly?



4 Answers 4


You can change your script to pass commands in a here-document, e.g.,


sftp -oPort=23 [email protected]:/home/kalenpw/TestWorld/plugins <<EOF
put /home/kalenpw/.m2/repository/com/Khalidor/TestPlugin/0.0.1-SNAPSHOT/TestPlugin-0.0.1-SNAPSHOT.jar   

The << marker followed by the name (EOF) tells the script to pass the following lines until the name is found at the beginning of the line (by itself).

  • Awesome that did exactly what I needed. I will have to read up on here documents. Thanks for the quick answer I'll accept it in 8 min.
    – kalenpw
    Commented Oct 7, 2016 at 21:25
  • No problem (I took a look for a duplicate but only found one closed as "unclear"). Commented Oct 7, 2016 at 21:26
  • Are you sure the server won't choke on the shell-script's comments?
    – alk
    Commented Oct 8, 2016 at 11:35
  • Also this approach depends on the kind of shell.
    – alk
    Commented Oct 8, 2016 at 11:38
  • 1
    @alk the script works with and without comments
    – kalenpw
    Commented Oct 8, 2016 at 17:10

You might prefer to use scp instead of sftp. scp behaves much like the ordinary cp command does, but the files can be remote:

scp -P 23 /home/kalenpw/.m2/repository/com/Khalidor/TestPlugin/0.0.1-SNAPSHOT/TestPlugin-0.0.1-SNAPSHOT.jar [email protected]:/home/kalenpw/TestWorld/plugins

This copies the file on you local machine into a directory on the remote machine without having to use the old-school ftp-style command interface.

The ssh, scp, and sftp services are usually available if any of them are; the same daemon program provides all of them simultaneously. In principle the server's administrator could choose to disable any of them, but in practice that's quite rare.

  • Didn't know about scp beforehand looks very useful. And you were right scp is already available on my machine
    – kalenpw
    Commented Oct 8, 2016 at 5:41
  • 2
    An old discussion, but for the record, it looks like and important difference between sftp and scp is that scp creates multiple sessions for efficiency to copy files in parallel, whereas sftp creates on session and uploads files sequentially. Depending on the destination host, the scp command maybe considered aggressive and trip fail safes like blocking your IP. Ask me how I know :)
    – KeiferJ
    Commented Feb 18, 2021 at 19:28
  • Nice find! I think if you use the ControlMaster option it will multiplex the extra sessions over the same TCP connection.
    – db48x
    Commented Feb 19, 2021 at 1:38

You can also use the -b option of sftp to indicate a file containing commands for sftp.

For example, you can put all your commands in file sftp_commands.txt:

cd /home/kalenpw/TestWorld/plugins
put /home/kalenpw/.m2/repository/com/Khalidor/TestPlugin/0.0.1-SNAPSHOT/TestPlugin-0.0.1-SNAPSHOT.jar

and run sftp as:

sftp -oPort=23 -b sftp_commands.txt [email protected]:/home/kalenpw/TestWorld/plugins 

Or you can pass the commands via STDIN too if you don't want to use a file.

From man sftp:

-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, rename, ln, rm, mkdir, chdir, ls, lchdir, chmod, chown, chgrp, lpwd, df, symlink, and lmkdir. Termination on error can be suppressed on a command by command basis by pre‐ fixing the command with a ‘-’ character (for example, -rm /tmp/blah*).


Another option would be to use curl:

curl -u user -T file.tar sftp://example.com/home/user/

You must log in to answer this question.

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