How to upload a directory recursively to an FTP server by just using ftp or lftp?

I am writing a Linux shell script to copy a local directory to a remote server (removing any existing files).

Local server: ftp and lftp commands are available, no ncftp or any graphical tools.

Remote server: only accessible via FTP. No rsync nor SSH nor FXP.

I am thinking about listing local and remote files to generate a lftp script and then run it. Is there a better way?

Note: Uploading only modified files would be a plus, but not required.


lftp should be able to do this in one step, in particular with lftp mirror:

The lftp command syntax is confusing, original invocation I posted doesn't work. Try it like this:

lftp -e "mirror -R {local dir} {remote dir}" -u {username},{password} {host}

note the quotes around the arguments to the -e switch.


cd {local_dir}
lftp {server}
cd {remote_dir}
mput {local_dir}/*

This worked for me, many other attempts were failing. Once in lftp, more info available via:

help mput

Based on Phil's idea of using lftp's mirror mode, this command does the trick:

lftp -c 'open -e "mirror /tmp/thedir ftp://nico:mypass@remotehost/~/destination/" ftp://nico:mypass@localhost'

A drawback is that it requires the local server to have an FTP server running.


Rather than the common answer of using mirroring, I often find it preferable to use mput, especially if there are several directories to transfer.

For example, given the following:

lftp> !ls
mydir1
mydir2
mydir3
mydir4
mydir5

... if the goal is to transfer directories mydir[2-4], you can either clunkily use the mirror command:

lftp mirror -R mydir2 mydir2

... and repeat serially for mydir3 and mydir4... or you can use mput as follows:

lftp> mput -d mydir[2-4]/*

... and be done. The -d option is key here:

lftp> help mput
Usage: mput [OPTS] <files>
Upload files with wildcard expansion
...
 -d  create directories the same as in file names and put the
     files into them instead of current directory
...