Skip to the examples.

Background

If you’re not familiar with LFTP, it bills itself as a “sophisticated file transfer program”. It supports common protocols like FTP, HTTP/s, FXP, SFTP, and BitTorrent. It can also download multiple files in parallel, download one file as multiple segments in parallel, and resume a paused or canceled download. These capabilities make it particularly attractive to users of seedboxes, as the servers are generally geographically far from the user, with limited and or spotty connectivity.

Installing LFTP

If you’re on Linux, you should be able to find it in your distribution’s package repository. It’s also easily available on OS X via Homebrew. If neither of those work for you, the official site contains source code and rpm binaries. If you need LFTP for Windows, it appears to be readily available in Cygwin.

Ubuntu / Debian

$ sudo apt install lftp

CentOS / RHEL / Fedora

$ sudo yum install lftp

Mac OS X (Homebrew)

$ brew install lftp

Windows (Cygwin)

# ( untested, please let me know if it works, [email protected] )
> setup-x86_64.exe -q -N -n -d -D -s https://mirrors.sonic.net/cygwin -P lftp

Overview

LFTP consists of a number of commands. It will give you an interactive shell to call these commands, like an FTP or shell session. You can also pass commands directly to it with no interactivity, more like rsync. Each of its commands does different things and takes different options. Some can be strung together to do great things, but unless you use it frequently you’ll never remember them. That’s why I wrote down these common example use cases so you can get going quickly:

Examples

Note: I’ve seen some other LFTP blogs that show how to setup an FTP server so you can download your files with it. If you have SSH access to the remote server, just connect via SSH and save yourself the hassle and security exposure of running an FTP server.

Connecting to a Server

SSH/SFTP

  • Connect to a server via SSH, get dropped into an shell. LFTP will use SFTP over SSH so you don’t have to setup another server:

FTP

  • Connect to an FTP server with authentication, get dropped into an shell:
      $ lftp -p21 username ftp://ftp.ucsb.edu
    

HTTP/S

  • Connect to an HTTP/S server, get dropped into an shell.:
      $ lftp https://example.com
    

Downloading Files

Single file, multiple concurrent segments

  • Here we download the ISO with 8 concurrent segments:
      pget -n 8 ./debian-6/debian-6.i386.iso
    

Single directory, multiple concurrent files and segments

  • Here we download 1 directory for files at a time, with 8 concurrent segments per file:
      mirror -P 4 --use-pget-n=8 ./debian-6
    

Multiple files via glob, multiple concurrent segments

  • Here we download all the files in the debian-6 directory, 1 file at a time, with 12 concurrent segments:
      glob -- pget -n 12 ./debian-6/*
    

Multiple directories via glob, multiple concurrent files and segments

  • Here we download all of the directories that start with “debian”, 4 files at a time, 12 concurrent segments per file:
      glob -d -- mirror -P 4 --use-pget-n=12 ./debian*
    

Download a torrent

  • This seems to only work in interactive mode, so in this example we fire up a shell, then use the torrent command and pass our torrent file path or URL:
    $ lftp
     :~> torrent ./ubuntu-21.04-desktop-amd64.iso.torrent
    Name: ubuntu-21.04-desktop-amd64.iso
    dn:2.9G up:0 complete, ratio:0.00/0.00/0.00
    peers:17 connected:0 active:0 complete:0
    Seeding in background..
    
  • You can simplify startup like this:
    lftp -e "torrent ./ubuntu-21.04-desktop-amd64.iso.torrent;exit"
    
  • You could throw it in a bash script to make it even easier, let’s make a script called lftptorrent.sh:
    #!/bin/bash
    lftp -e 'torrent "$i";exit'
    
  • Then you just call the script (make sure to chmod +x it) like this whenever you have a new torrent to download:
    lftptorrent.sh ./ubuntu-22.04.desktop-amd63.iso.torrent
    

Uploading Files

Just like with downloading, you can use any of the following commands to upload files after you’ve connected to your server with your preferred protocol.

Upload an entire directory

  • This just pushes all the files up.
      mirror --reverse ./remote_target ./local_source
    

Upload a directory and remove remote files that don’t exist locally

  • By adding --delete, you can remove files remotely that you’ve removed locally. This is good for synchronizing local changes to a remote directory:
      mirror --delete --reverse ./remote_target ./local_source
    

Upload multiple files concurrently via glob expression

  • Note, if you’re transferring multiple files in a directory hierarchy, mirror is much easier to work with:
      mput -P4 ./*.mp4
    

Scripting

In addition to interactive and command interaction, LFTP supports writing scripts so you can automate your LFTPing. The following are some very basic examples to show how they can be executed. The commands are all the same as above::

  • Execute some commands and keep the shell open with -e flag:
      $ lftp -e "cd A; pget A.txt; cd ../B; pget B.txt" ftp://ftp.ucsb.edu
      lftp>
    
  • Execute a script file with -f flag:
      lftp -f ./script.lftp
    

Let’s look at a few other common use cases we can solve with scripts:

Collecting Files from a Seedbox Directory

  • Our script downloads remote_dir to local_dir with mirror and pget (2 files, 4 segments, continue). This will not remove local files that are missing on the remote directory, so you can use this to accumulate files that live on your seedbox temporarily, and safely remove them from your seedbox to make room for more stuff:
      # script.lftp
      open sftp://10.0.0.1
      mirror -c -P 2 --use-pget-n=4 ./remote_dir /mnt/local_dir
      quit
    

Synchronizing a Local and Remote Folder

  • If you want to keep the local directory synced with the remote one, you can add the --delete flag to delete files locally when they’re deleted on the remote end. This is more useful if you’re using mirror to sync something like a website directory:
      # sync.lftp
      open sftp://10.0.0.1
      mirror --delete -c -P 2 --use-pget-n=4 ./remote_dir /mnt/local_dir
      quit
    

Restoring a Local Folder from a Remote Backup

  • If you lose your server or need to restore your website from an sync backup, you can use the --reverse for mirror to upload the files instead of downloading them. Note that using pget won’t make a difference here:
      # resore.lftp
      open sftp://10.0.0.1
      mirror --reverse --delete -c ./remote_dir /mnt/local_dir
      quit
    

Safely Use Login Password in a Script

  • If you want to make your login password available to an lftp script through an environmental variable (without showing it to other users with ps), you can set the $LFTP_PASSWORD environmental variable and pass --env-password to your open command:
      export LFTP_PASSWORD=secretpassword
    
      # script.lftp
      open --env-password sftp://10.0.0.1
      mirror --reverse --delete -c ./remote_dir /mnt/local_dir
      quit