Connecting to FTP via the Linux command line
Solution 1:
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.
Solution 2:
I'd highly recommend ncftp's ncftpput. It is very scriptable and is handy for this sort of thing.
Solution 3:
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