I want to upload a file from my Linux server to some FTP server. I know we use put and get commands to upload and download files and the sftp command to connect to the FTP server.

But I wanted to do all this in one shell script and I have no idea how to connect to FTP using the sftp command within some script to upload some file. This is what I know but I don't know how it will work inside one sh script.

sftp -v -oIdentityFile=path user@server
put localPath ftpPath

Can anyone help me?

closed as unclear what you're asking by Anthon, Jenny D, goldilocks, Timo, slm Apr 7 '14 at 13:09

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • The sftp command is not for connecting to ftp, it's for connecting to sftp which serves a similar purpose but is not the same protocol. – jordanm Dec 18 '13 at 3:57
  • This answer might help: askubuntu.com/a/331078/69599 – mkc Dec 18 '13 at 4:20
  • do you want ftp connection or sftp ? does are two very different protocol – Kiwy Apr 7 '14 at 11:54

sftp is a shell command. It reads SFTP commands on its standard input.

You can use a here document to pass input to a command.

sftp -v -oIdentityFile=path user@server <<EOF
put localPath ftpPath

You can use variables inside the here document.

sftp -v -oIdentityFile=path user@server <<EOF
put $local_path $remote_path

This is not the simplest way to copy a file. SFTP lets you browse and transfer files, but there is also a shell command to directly copy one file (or even a directory, recursively).

scp -o IdentityFile=path localPath user@server:remotePath
  • scp is definitively the way to go! This makes it a lot easier to detect errors in your script as the sftp command may continue to process commands even if a previous command failed. – Alexis Wilke Apr 13 '15 at 4:26

You can specify a batch file using the -b option.

-b batchfile

Ripped from sftp man page:

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, 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*).

  • 1
    +1 but why did you rip the man page what if someone else wanted to read it – aanrv Sep 14 '17 at 19:51

Have you considered using rsync?

Uploading files via rsync is arguably the most preferred way and also, the easiest to script. It uses either SSH, RSH or its own protocol ("rsync://...") for target and source transfer.

It is more than likely, that rsync is installed on your system. If you can access it over SSH, you can use scp syntax:

rsync -alPvvz local_path user@host:remote_path

If you need oIdent, I suggest you read this unix.SE question, where this is explaines.

I understand it is not exactly what you asked for, but given that rsync is pretty much an industry standard, it is at least something to consider.


SFTP programs uses ssh protocol to access, manage, and transfer files to remote systems.It uses different protocols to transfer files. The user names and the data transfer is encrypted during communication. You can't use ftp client to connect to the sftp server or sftp client to connect to the ftp server that only supports ftp.


To upload file1 to use

ftp -u root@ file1

You will be prompted for a password. To work around this, you can fill out your .netrc:

cat >.netrc<<EOF
login user
password password
chmod 600 .netrc

If you have sftp access to the machine you maybe also have ssh access to the machine.

If you have ssh access you should check if the rsync command is available.

rsync has some advantages over ftp (secure connection, uploads only the parts of the file that have changed - very fast under some circumstances, easy recursive mirroring, compression).


It can be easily used in shell scripts. The only thing you have to do is to setup "key based authentication"


You can use ftp commands inside a shellscript in the following way:

ftp -dv ${SSHHOST} >$dirFtp/$fichFtpLog <<_FINFTP_
    put ${dirwork}/${fichSalida} ${rutaSubida}/${fichero}

dirwork/fichSalida ... are shellscript vars as example. You have to note that in order to use ftp in this way, you must configure .netrc so that your login be transparent to the ftp command.

As a resume, you can issue commands as if you were using the ftp command between FINFTP tags (you can use the tag you want).

  • we'r speaking of secure ftp not ftp or ftp over ssl – Kiwy Apr 7 '14 at 11:53
  • Ok I can't programm here your needs, what's important is the concept. You can do it the same way, connect as sftp and issue commands the same way as you do in ftp. – periket2000 Apr 7 '14 at 11:58

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