Deprecation Notice:This article was written more than a year ago which means that its contents might no longer be up-to-date. If you notice any inaccuracies, feel free to leave a message in the comments below and we will look into updating the article soon as we can. Thank you for your support.

Table of Contents


Syncing files with lftp

lftp is handy command line tool when you want to mirror files from a local drive to a remote FTP folder. While a regular FTP application only allows you to upload or download files, lftp allows you to also keep the files in sync.

Requirements

  • Linux
  • macOS (with external package manager, such as Homebrew)
  • Windows (with PowerShell and external package manager, such as Chocalety)

The .netrc file

Our first step is to edit the hidden .netrc configuration file which is normally located in our home directory. This file is used for authentication information and is useful if we do not want to enter username and password every time we log-in to the FTP site.

The following information will be added to the .netrc file. Please note that the words inside the brackets are < placeholders >:

machine <mysite>
login <myusername>
password <mypassword>

The quickest way is to run the following command which does not care if the file does not exist yet.

$ echo “machine <mysite> login <myusername> password <mypassword>” >> ~/.netrc

Here is a real world example of the same command:

$ echo “machine ftp.mysite.com login geek password somethingclever” >> ~/.netrc

If you prefer, you can also edit the file manually with your favourite text editor.

$ cd ~
$ nano .netrc

The chmod command

Next we will change the file permissions of .netrc so that the file can only be read by ourselves.

$ chmod 600 ~/.netrc

Your file permissions should now look something like this if you type ls -la in your home directory:

$ ls -la ~/.netrc
-rw-------   1 geek  staff     90 18 Aug 18:48 /home/geek/.netrc


## The lftp command We are now ready to upload files. With the following command we can mirror a local folder to a directory on the remote FTP server. You will notice that username and password will not be requested because it has already been stored in the .netrc file.

$ lftp -c "open <mysite>; mirror --continue --reverse --delete <source> <destination>"
Let us explain in detail what these parameters do. For the sake of clarity, multiple options have been separated into their own line.

lftp the command itself
-c execute given commands
open mysite opens a connection to the FTP server
mirror mirrors specified source directory to the target directory
–continue continue a mirror job if possible
–reverse reverse mirror (put files)
–delete delete files not present at the source
source where to copy files from
destination where to copy files to

This is what the command would look when you type it in the shell:

$ lftp -c "open ftp.mysite.com; mirror --continue --reverse --delete /home/geek/myfolder public_html"


Further Information

For further information on the lftp command, type:

$ man lftp

See Also

How to mirror drives with rsync
How to mirror drives with rsync

How to install a Samsung ML-191x 252x Series printer with AirPrint support on a Raspberry Pi
How to install a Samsung ML-191x 252x Series printer with AirPrint support on a Raspberry Pi

Querying the Illuminates by Default attribute in Maya
Querying the Illuminates by Default attribute in Maya

Remove nodes from a set in Maya
Remove nodes from a set in Maya




comments powered by Disqus

See also