34

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.

3
  • ncftp is totally out the question ? Would be easiest imho. (and i know it'll do it as i use it that way)
    – Sirex
    Jan 11, 2011 at 9:30
  • Sirex: I would rather do it with tools that come by default on RedHat. If lftp proves unusable, I will add a requirement for users to install ncftp. Jan 11, 2011 at 9:40
  • The syntax for wput (a wget-like ftp-uploader): wput dir/ ftp://user:pass@host/dir
    – kenorb
    Jun 7, 2015 at 12:47

5 Answers 5

65

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.

8
  • 1
    +1 for tipping me that lftp's mirror mode works even if FXP is not available (I was convinced of the opposite after reading tutorials.papamike.ca/pub/lftp.html which says "both servers must support this protocol for this operation to succeed"). However, this particular command fails on my Ubuntu server: lftp: mirror: Name or service not known, Unknown command `-R'. Jan 11, 2011 at 8:13
  • I reworded my answer with a better invocation of lftp, hopefully that helps. Jan 11, 2011 at 8:47
  • I finally gt it to work! It seems that you must switch remote and local for it to work. Please update and I will accept this answer. Here is a command line that works: lftp -e "mirror -R /tmp/mylocalfiles /var/www" -u remoteuser,remotepassword remoteserver Jan 12, 2011 at 2:24
  • I think I saw in the lftp docs that you had to switch the local and remote dirs when using -R but I must have forgot. Updated my answer. Jan 12, 2011 at 2:29
  • How does it fail? Have you tried running 'lftp -vvv' for more verbose output to determine what it's trying to do? Jan 12, 2011 at 2:55
4
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
2
  • 2
    It would probably be better to use mirror -R here.
    – gauteh
    Mar 24, 2017 at 7:29
  • @gauteh you're right. best answer ^
    – austin
    Mar 27, 2017 at 23:42
1

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.

1

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
...
0

Finally got the answer!!! Create shell script ftpmirror.sh

#!/bin/bash 
path = /local-dir-path
lftp -e "mirror -R $path /$path" -u username,password ftp-server-ip
  • Path = local directory which we want to copy into ftp server
  • username = ftp server user name
  • password = ftp server password
  • ftp-server-ip = IP address of ftp server

If lftp package is not installed the installed it using yum.

You must log in to answer this question.

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