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://'

migrated from serverfault.com Aug 15 '11 at 1:55

This question came from our site for system and network administrators.

  • 4
    How do I man page? – Ignacio Vazquez-Abrams Aug 15 '11 at 2:18
  • 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 Aug 15 '11 at 4:15
  • @IgnacioVazquez-Abrams man ftp at command line – c.gutierrez Mar 13 '15 at 3:46
  • Similar: Syncronizing files over FTP, but for multiple files. – kenorb Apr 15 '15 at 19:04
  • I'm getting '-u unknown option'. – Robert Reiz Aug 25 '15 at 11:58

10 Answers 10

up vote 159 down vote accepted

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
  • 9
    Wow, I didn't know curl supported ftp! It's super handy. – Sébastien Jun 25 '13 at 16:25
  • 48
    +1 for the curl solution. Why even bother with the other one? – Asaph Oct 9 '13 at 20:55
  • 2
    @Asaph because curl is not installed where I need to do this, so the other solution came in handy. Thanks. – bobef Sep 11 '14 at 8:12
  • 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 Mar 6 '16 at 18:57
  • 2
    the curl solution is the best and the easiest – user512278 Mar 28 '16 at 16:40

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}
  • Can you provide a link to the tool or its documentation? – bwDraco Jan 27 '15 at 16:11
  • 1
    Hi 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 Feb 3 '15 at 18:01

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

  • 8
    ftp: invalid option -- 'u' – Babken Vardanyan Aug 1 '14 at 17:06
  • 1
    Indeed :( ftp: u: unknown option – webDEVILopers Oct 28 '14 at 10:24

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.

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

.

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

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.

You could also use the sftp or ftp command

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

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

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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