How to upload one file by FTP from command line?

117

50

I need to upload a single file to FTP server from Ubuntu. This operation should be done in a script (in non-interactive mode). What is the right syntax for ftp?

I'm trying this, to no avail:

$ ftp -u ftp://user:secret@ftp.example.com my-local-file.txt
ftp: Invalid URL `ftp://'

yegor256

Posted 2011-08-15T01:40:11.530

Reputation: 1 439

@IgnacioVazquez-Abrams man ftp at command line – c.gutierrez – 2015-03-13T03:46:45.467

Similar: Syncronizing files over FTP, but for multiple files.

– kenorb – 2015-04-15T19:04:48.593

I'm getting '-u unknown option'. – Robert Reiz – 2015-08-25T11:58:08.117

5How do I man page? – Ignacio Vazquez-Abrams – 2011-08-15T02:18:03.477

I don't know much about the ftp tool in Ubuntu, but it looks like it's choking on the ftp://. try taking that out maybe? – Nate Koppenhaver – 2011-08-15T04:15:31.483

Answers

188

Here is one approach:

$ ftp -n <<EOF
open ftp.example.com
user user secret
put my-local-file.txt
EOF

Alternatively, create (or edit) the ~/.netrc file in the home dir of the user that will run the ftp command, give it appropriate perms (chmod 0600 ~/.netrc), and add the following:

# ~/.netrc
machine ftp.example.com
login user
password secret

Then omit the login information, as in:

$ echo put my-local-file.txt | ftp ftp.example.com

Also, here's how you might do the same thing using curl:

$ curl -T my-local-file.txt ftp://ftp.example.com --user user:secret

Marty

Posted 2011-08-15T01:40:11.530

Reputation: 2 081

3@Asaph because curl is not installed where I need to do this, so the other solution came in handy. Thanks. – bobef – 2014-09-11T08:12:03.260

1+1 for the curl. Neat, clean and straight to the point! In Debian/Ubuntu "apt-get install curl", if you don't have it. – GTodorov – 2016-03-06T18:57:00.470

2the curl solution is the best and the easiest – None – 2016-03-28T16:40:02.860

FWIW: I tried this with a large file (162MB). At first I thought curl looked slow (~25mins), but I found that the ftp command took just as long. curl looks good if available, and has nice feedback showing Time Left etc. – xtempore – 2016-05-05T03:58:32.980

ftp.example.com:port for connection that require that. – SkorpEN – 2017-06-13T11:36:13.530

A variant of this syntax is:

$ curl -T my-local-file.txt ftp://user:secret@ftp.example.com – ViperGeek – 2017-08-10T22:17:34.827

For those who are going to use curl it is important to put your password inside single quotes ' if it contain special characters. otherwise you will end up with curl: (67) Access denied: 530 – Eng7 – 2020-02-11T12:37:34.970

FYI: I've noticed that I can't pass a full path of the file using the put raw command but I can with curl. – Hengjie – 2013-01-14T21:55:59.223

14Wow, I didn't know curl supported ftp! It's super handy. – Sébastien – 2013-06-25T16:25:32.873

65+1 for the curl solution. Why even bother with the other one? – Asaph – 2013-10-09T20:55:23.563

18

I can recommend ftp-upload. It's a neat little tool that you can install under ubuntu through sudo apt-get install ftp-upload.

Usage example:

ftp-upload -h {HOST} -u {USERNAME} --password {PASSWORD} -d {SERVER_DIRECTORY} {FILE_TO_UPLOAD}

Floris

Posted 2011-08-15T01:40:11.530

Reputation: 191

Can you provide a link to the tool or its documentation? – bwDraco – 2015-01-27T16:11:50.217

1Hi DragonLord, if you are in Ubuntu and you have installed ftp-upload (using the command I gave before) you can just do man ftp-upload. Hope that helps. – Floris – 2015-02-03T18:01:52.380

5

You need to fix the URL given in your statement. You received the error because the URL was incomplete - it was missing the name of the object you are uploading. Once you add the filename after 'example.com' as I have done below, you will see the single command does indeed work as you intended.

Try this:

ftp -u ftp://user:secret@ftp.example.com/my-local-file.txt my-local-file.txt

Paul

Posted 2011-08-15T01:40:11.530

Reputation: 67

10ftp: invalid option -- 'u' – Babken Vardanyan – 2014-08-01T17:06:56.803

2Indeed :( ftp: u: unknown option – webDEVILopers – 2014-10-28T10:24:18.253

ftp: u: unknown option – Morten – 2018-11-08T11:45:25.447

4

Install ncftp and use the ncftpput tool that comes along with it, pretty much something like this syntax:

ncftpput -u ftpuser -p ftppass ftphostname /path/where/to/upload localfile.name
if [ $? -ne 0 ]; then echo "Upload failed"; fi

You can even check if the upload status is good or bad. The normal ftp client can also be used along with expect.

O G

Posted 2011-08-15T01:40:11.530

Reputation: 276

4

You can also try lftp.

Here is an example:

lftp -e 'cd folder1/folder2; put /home/path/yourfile.tar; bye' -u user,password ftp.theserver.com

Refer here for more details

.

divinedragon

Posted 2011-08-15T01:40:11.530

Reputation: 202

To use it with non unix name I found this: NN="'non unix.file'" ; lftp -e "cd folder; put $NN; bye" -u anonymous, ftp.theserver.com – schweik – 2020-01-17T08:22:17.740

2

Upload a file to a remote location via command line

#!/bin/bash
#$1 is the file name
#usage:this_script <filename>
HOST='yourhost'
USER="youruser"
PASSWD="pass"
FILE="abc.php"
REMOTEPATH='/html'

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
cd $REMOTEPATH
put $FILE 
quit
END_SCRIPT
exit 0

Shal

Posted 2011-08-15T01:40:11.530

Reputation: 121

1

I use BusyBox's ftpput to do this:

# /bin/busybox ftpput

BusyBox v1.20.2 (Debian 1:1.20.0-7) multi-call binary.

Usage: ftpput [OPTIONS] HOST [REMOTE_FILE] LOCAL_FILE

Upload a file to a FTP server

    -v,--verbose            Verbose
    -u,--username USER      Username
    -p,--password PASS      Password
    -P,--port NUM           Port

Note: busybox ftpget work well too.

Naskel

Posted 2011-08-15T01:40:11.530

Reputation: 11

0

i improved Marty answer like below (include binary):

[ftp_example_1.sh]

$ ftp_example_sh.sh dump_file

ftp -n <<EOF
open 192.168.0.10
user anonymous aaa
binary
put $1
EOF

[ftp_example_2.sh]

$ftp_example_2.sh 192.168.0.10 dump_file

ftp -n <<EOF
open $1
user anonymous aaa
binary
put $2
EOF

sailfish009

Posted 2011-08-15T01:40:11.530

Reputation: 111

0

You could also use the sftp or ftp command

sftp {user}@{IP}
Password:
put {path To File On Local Computer}

iProgram

Posted 2011-08-15T01:40:11.530

Reputation: 505

-1

FtpPut(){
    echo `echo -e "open host\nuser user pass\nbinary\nput $1\nquit"|ftp -nv`
}
FtpPut asd.txt
FtpPut asd.mp4
FtpPut asd.php
...

Yöncü Bilişim Çözümleri

Posted 2011-08-15T01:40:11.530

Reputation: 1