LFTP tutorial on Linux with examples

There is certainly no shortage of ftp client on Linux: some come with a graphical user interface such as Filezilla, others are command line applications which can be used even when display servers, such as Xorg or Wayland are not available. In this article we talk about one of the most used and feature-rich CLI ftp client: lftp.

In this tutorial you will learn:

  • How to install lftp on the most used Linux distributions
  • How to connect and authenticate to a remote host
  • How to create, remove, edit and list bookmarks
  • Some of the most used lftp commands
  • How to run commands non-interactively
  • How to download torrent files with lftp

LFTP tutorial on Linux with examples

LFTP tutorial on Linux with examples

Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Distribution independent
Software lftp
Other Root permissions to install the lftp package
Conventions # – requires given linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux-commands to be executed as a regular non-privileged user

Installation

As described in its manual page, lftp is a sophisticated file transfer program which allows connections to remote hosts using a variety of protocols such as FTP, FTPS, SFTP, HTTP, HTTPS, BitTorrent protocols, etc. Being open source, the program is available in the repository of all the most used Linux-based operating systems; Installing the application on Debian and its derivatives such as Ubuntu and Linux Mint, is just a matter of running:

$ sudo apt-get update && sudo apt-get install lftp

On recent versions of Fedora, the package can be installed using the dnf package manager, just by issuing the following command:

$ sudo dnf install lftp


If ArchLinux is your daily driver, you can install lftp using pacman. The package is available in the “Extra” repository:

$ sudo pacman -S lftp

With the software installed on our system we can start taking a look at some of its most interesting features.

Connecting to a remote server

When using lftp there are basically two ways we can connect to a remote host. The first is by invoking the application from our shell and provide the URL of the remote host, the second is to use the open command, when already in the lftp prompt.

Connecting to a remote host when invoking lftp

To connect to a remote host when invoking the lftp application from our shell, all we have to do is to provide the remote machine URL, just as showed in the following example:

$ lftp ftp://ftp.remotehost.com

After launching the command, if the connection to the remote host is successful, we will be presented with the lftp prompt, ready to accept our commands (we can also enter the lftp prompt just by invoking the application without any option or argument):

lftp ftp.remotehost.com:~>

At this point, since we are already connected to the remote host, to authenticate ourselves we can use the login alias, or the user command (the first is just a built-in alias for the second). To perform the authentication we would
therefore run:

lftp ftp.remotehost.com:~> login ourusername ourpassword

If the password is not provided will be asked interactively:

lftp ftp.remotehost.com:~> login ourusername
Password:

Finally, to achieve a completely non-interactive authentication, the login credentials can be provided directly in the URL we pass to lftp for the connection:

$ lftp ftp://ourusername:ourpassword@ftp.remotehost.com

Connecting to a remote host when in the lftp prompt

To connect to a remote host when already using the lftp prompt, we must use the open command, which uses the following syntax:

open [OPTIONS] site

To connect to the same dummy remote machine we used in the previous example, we would run:

lftp :~> open ftp://ftp.remotehost.com

The open command, when used as in the previous example, will just connect us to the remote host, without performing any authentication. It’s, however possible to provide login information via the --user and --password options. For example, to connect and login to the remote machine with our credentials, we would run:

lftp :~> open ftp://ftp.remotehost.com --user ourusername --password ourpassword

Alternatively, we can provide the login information as part of the URL, just as we did before:

lftp :~> open ftp://ourusername:ourpassword@ftp.remotehost.com

Creating and managing bookmarks

In order to easily access and authenticate to a remote host, without having to provide all the needed information each time we want to connect to it, we can create bookmarks. To accomplish this task we can use the bookmark command when in the lftp prompt. The command accepts a series of subcommands which let us create and manipulate bookmarks; let’s see what they are.

Creating a bookmark (add subcommand)

The first subcommand, add, let us create a new bookmark. All we have to provide is the name we want to use for the bookmark and the connection information. For example, to create a bookmark for ftp://ftp.remotehost.com which includes the login information we would run:

lftp :~> bookmark remotehost ftp://ouruser:ourpassword@ftp.remotehost.com

As you see, we provided the password for the authentication in the remote machine as part of the URL to be used with the bookmark. Here we should notice two important things: the first is that by default passwords are not stored in the bookmarks file (~/.local/share/lftp/bookmarks or ~/.lftp/bookmarks) unless we specify the option set bmk:save-passwords yes in the application configuration file (per-user settings are stored in the ~/.lftprc or ~/.lftp/rc files, the system-wide configuration file, instead, is /etc/lftp.conf).

The second thing to notice is that passwords are stored in plain-text and this could represent a security risk if the machine we are working on cannot be trusted.

Providing the URL to be used for a bookmark is not always necessary. If we are already connected to the remote host we are creating a bookmark for, we can just invoke the bookmark command and provide only the bookmark name we want to use:

lftp ouruser@ftp.remotehost.com:~> bookmark remotehost

To use the “remotehost” bookmark, and connect to the corresponding remote host, we would run:

$ lftp remotehost

Or, using the open command:

lftp :~> open remotehost


Removing a bookmark (del subcommand)

Removing a bookmark is really easy: all we have to do is to use the del subcommand and provide the name of the bookmark we want to remove, for example:

lftp :~> bookmark del remotehost

Editing bookmarks (edit subcommand)

To edit our bookmarks we can use the edit subcommand. Once we run it, the file containing the bookmarks will be opened in our default text editor:

lftp :-> bookmark edit

All we have to do is to make the appropriate changes and than save them as we would do normally. The bookmarks file can also be edited by invoking our editor directly (it’s just a text file, nothing obscure), when not in the lftp prompt.

List bookmarks (list subcommand)

To list all the our bookmarks all we have to do is to use the list subcommand. The list of bookmarks will be reported on screen, and if passwords are stored inside of it, will be masked with an X character:

lftp :-> bookmark list
remotehost  ftp://ouruser:XXXX@ftp.remotehost.com/

Lftp commands

Until now we just saw how to connect, authenticate and create bookmarks with lftp. Now let’s see some of the most useful commands we can use when connected to a remote server. To obtain a complete list of the available commands we can type help and press enter, when in the lftp prompt:

lftp :~> help

Listing the content of a remote directory

The first thing we may want to do when a connection is established and we are authenticated on a remote server, is to list the content of the remote working directory. To accomplish the task, just as we would do locally, we can use the ls command:

lftp ouruser@ftp.remotehost.com:/> ls
dr-xr-xr-x    2 19304109   19304109         4096 Jul  7  2016 .
dr-xr-xr-x    2 19304109   19304109         4096 Jul  7  2016 ..
[...]

In the same fashion, to change the remote working directory, we can use the cd command. What if we want to run the “local” version of those commands, to list the content of our local working directory, or change it? All we have to do is to prefix the commands with an ! (exclamation mark): all the commands with that prefix will be considered as local shell commands. In the example below we list the content of our local working directory:

lftp ouruser@ftp.remotehost.com:/> !ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

Edit a remote file

To edit a remote file we can use the edit command. When we do so, the remote file is retrieved to a temporary directory on our local machine, and it is opened in our default text editor. Once the content is changed and saved,
the file is automatically re-uploaded.

Retrieve and upload files and directories

To retrieve a remote file to our local working directory, we can use the get command. In its basic usage we provide the remote file location and optionally the local path where it should be saved (by default the file will be saved using its basename). To save the remote wp-config-sample.php remote file locally, as sample.php, we would run:

lftp ouruser@ftp.remotehost.com/> get wp-config-sample.php -o sample.php

Multiple files can be downloaded at once by repeating the same syntax.

To perform the opposite operation, so to upload a local file to the remote host, we can use the put command. Basically, we provide the path of the local file to be uploaded as argument to the command. If the file must be saved remotely with a different name, we provide it as the argument of the -o option, just as we did above. In the following example we upload the local file wp-config-sample.php, and save it in the remote host as sample.php:

lftp ouruser@ftp.remotehost.com/> put wp-config-sample.php -o sample.php

The get and put commands can only retrieve and upload regular files: this means that they won’t work with directories. If we want to download or upload directories and their content we must use the mirror command`. The command, as its name suggests, creates a mirror of a remote directory on our local machine, or vice-versa. All we have to provide is the source target, which by default is considered to be the remote one, and, optionally the target directory, which by default is the local one. Let’s see an example:

lftp ouruser@ftp.remotehost.com/> mirror -P 10 site local_backup


The above command will create a mirror of the remote site directory on our local machine where it will be saved as local_backup. What is the -P option we used? The -P option (short for --parallel) accepts an integer as argument, which is the maximum number of files to be downloaded in parallel (downloading one file at the time would be really tedious!).

To invert the remote and local directories we must use the -R option, short form for --reverse: this option will make so that a reverse mirror is created, so the first argument passed to the mirror command is considered as the local
directory, and the second as the remote one. Files can be explicitly included or excluded from the mirror by using the

-i (--include) and -x (--exclude) options respectively. Both options accept a regular expression as argument: all file names matching the expression will be included or excluded from the mirror.

It’s also possible to use the -e option (short for --delete) to create identical mirrors: files existing on the destination but not on the source will be removed.

Running commands non-interactively

Until now we saw how we can run some commands from the lftp prompt. What if we want to run them in a completely non-interactive way? All we have to do is to use the lftp -c option. When this option is used the commands passed as arguments are executed, and then lftp exits automatically. The commands must be separated
by a semicolon. Suppose we want to connect to a remote host and create a mirror of the site directory locally to a directory named local_backup. We would run:

$ lftp -c "open ftp://ouruser:ourpassword@ftp.remotehost.com; mirror --parallel=10 site local_backup"

Downloading torrent files

Lftp can also be used as a torrent client. All we have to do is to provide the location of a torrent file (it can be a local file, a URL or a magnet link). By default the retrieved files are stored in the current working directory, but a different location can be specified via a the -O option. To download the Debian stable ISO via torrent in the ~/Download directory, for example, we would run:

lftp :~> torrent https://cdimage.debian.org/debian-cd/current/amd64/bt-cd/debian-10.7.0-amd64-netinst.iso.torrent -O ~/Downloads

Conclusions

In this tutorial we learned the basic usage of the lftp application: we saw how to install the application on some of the most used Linux distributions, how to connect and authenticate to a remote server, how to create bookmarks for quick locations access, and the commands which should be used to list the content of remote and local directories, to edit remote files, and to upload and download files to and from a remote host. We also saw how to execute commands non-interactively. Finally we saw how we can use lftp as torrent client. For a complete list of the
features and the commands we can run when using lftp, please consult its manual!



Comments and Discussions
Linux Forum