13

I need to upload files via FTP from the command line. I have this information: a URL, a username, a password, a port, and the fact that I am supposed to use passive mode.

How do I upload a file given this information?

Note that I am having to do this from a script, so I need to be able to enter this information automatically.

2
  • 1
    start reading ftp man pages?
    – NicM
    Dec 14, 2011 at 21:47
  • 3
    ftp happens do be one FTP client. Others might be better suited depending on what the user wants. Simply linking to a man page isn't exactly helpful.
    – Daniel Beck
    Dec 14, 2011 at 21:54

3 Answers 3

18

There's many CLI (command line) clients out there. Most common is simply ftp. You've got <host>, <port>, <username>, <pass> and "passive mode". Using ftp you should do:

ftp -p <host> <port>

-p switch means "use passive mode". Then you'll be asked about your username and password. After successful login (server let you know about that), you are able to give some commands. Type help and press "enter" to get list of commands available. You can use e.g. cd, ls, mkdir ftp commands to change working directory (on the server), list its contents and create a new directory. If before running ftp you were in the same directory as you files you want to send, you can use put or mput command to start actual transfer. First command sends one file, second multiple files using globbing (e.g. mput *pdf will send all pdf files in current directory). To get simple help about command you can use help <command> from within ftp app. I'd say that's enough for starters. For the rest use man ftp or info ftp. To end ftp session type bye. There are other ways to do that but I think this one is just elegant :).

As for the other clients, some interesting choices were pointed here, but I personally use lftp. It's just solid, good, flexible and easy to use ftp client. If you prefer more visual approach while still being under command line, you can go for mc or "Midnight Command". It's general application file manager utilizing Norton Commander paradigm, but can be also used to access ftp servers.

4

No problem on this one. There are lots of examples on Gist . Just go to that site and search for "FTP Script".

Here is one I found:

#!/bin/sh

HOST='some.ftp.server'
USER='myuser'
PASSWD='mypass'
FILE='myfile'

ftp -n $HOST <<END_SCRIPT
user ${USER} ${PASSWD}
cd /path/to/something
get $FILE
quit
END_SCRIPT

exit 0

Also, Command Line Foo is another good site, where I found this crazy awesome example of a ftp-ish file transfer:

Create a file server, listening in port 7000:

while true; do nc -l 7000 | tar -xvf -; done

Then, at client side:

tar c myfile | nc localhost 7000 ##Send file myfile to server
tar c mydir | nc localhost 7000 ## Send directory mydir to server
1
  • How is passive mode specified? It is crucial for many FTP sites. May 16, 2016 at 12:23
4

I'd highly recommend ncftp's ncftpput. It is very scriptable and is handy for this sort of thing.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .