How to upload one file by FTP from command line?
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:[email protected] my-local-file.txt
ftp: Invalid URL `ftp://'
Solution 1:
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
Solution 2:
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}
Solution 3:
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 and also refer to LFTP Manual
.
Solution 4:
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:[email protected]/my-local-file.txt my-local-file.txt