Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this 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 –  Ketan Dec 18 '13 at 4:20
add comment

5 Answers

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.

share|improve this answer
add comment

To upload file1 to 127.0.0.1 use

ftp -u [email protected]: file1

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

cd
cat >.netrc<<EOF
machine 127.0.0.1
login user
password password
EOF
chmod 600 .netrc
share|improve this answer
add comment

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

https://en.wikipedia.org/wiki/Rsync

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

share|improve this answer
add comment

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
EOF

You can use variables inside the here document.

local_path=/path/to/local/file
remote_path=/somewhere/or/other
sftp -v -oIdentityFile=path user@server <<EOF
put $local_path $remote_path
EOF

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
share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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