A little lftp
Often there is a need for a simple command-line FTP client when a GUI is not available. In this article, we will get acquainted with the very basic operations of lftp, a simple yet powerful and reliable CLI-based FTP client, so you can easily transfer a file from the command-line the next time you need to.
What is lftp?
lftp is a free CLI-based FTP client made by Alexander Lukyanov. In fact, it can handle several other protocols including secure connections. Yet it has few dependencies and a minimal footprint.
The latest available version of lftp is 3.4.6 (or stable: 3.4.4) currently. Check you distribution’s package repositories for the `lftp’ package.
Source tarballs and some binaries can be downloaded from the download page on lftp’s site. The 3.4.6 tarballs can be found in the mirrors. Installation is simply ./configure; make; make install.
Connecting and logging in
Launch lftp with the host name or URL to the FTP server. For example:
$ lftp ftp.gnu.org
This will put you in a shell like this:
You can enter directives to lftp here and use it as a shell to navigate FTP sites and move files.
To log in with a userid you can use the user command, or skip this step to remain anonymous (default).
Navigating file trees
The interface is similar to BASH in many ways. Use the ls command to get a directory listing on the remote server:
lftp firstname.lastname@example.org:/> ls
lrwxrwxrwx 1 0 0 8 Aug 20 2004 CRYPTO.README -> .message
-rw-r–r– 1 0 0 17864 Oct 23 2003 MISSING-FILES
-rw-r–r– 2 0 0 4178 Aug 13 2003 MISSING-FILES.README
Other commands such as cd, cp, mv and rm are available. Note that the options are not the same as the BASH versions.
You can move around your local file tree too, with the lcd command. This changes the local current working directory of lftp, so this becomes the default location to transfer files from or to.
lftp ftp.gnu.org:/> lcd ~/tmp/
lcd ok, local cwd=/home/abhi/tmp
Transferring files is simple:
get /path/to/file will download file from the remote server into the local current working directory. For example, this is how we might download the XBoard package from the GNU FTP site:
put /path/to/file will upload a local file to the remote working directory.
Use the -O option to specify base directories other than the current working directory, for either get or put.
To find more commands and instructions with option listings, use the help directive:
lftp :~> help
Type help <command> to find usage and options for a particular command. help get shows:
lftp :~> help get
Usage: get [OPTS] <rfile> [-o <lfile>]
Retrieve remote file <rfile> and store it to local file <lfile>.
-o <lfile> specifies local file name (default – basename of rfile)
-c continue, reget
-E delete remote files after successful transfer
-a use ascii mode (binary is the default)
-O <base> specifies base directory or URL where files should be placed
If there is a site (or path) that you commonly visit, then it would be convenient to save it as a bookmark in lftp. Here is how we might bookmark tho XBoard location at the GNU site:
lftp :~> bookmark add ftp.gnu.org/gnu/xboard xb
lftp :~> bookmark list
Now that the bookmark is added, we can connect to the location (and cd into the xboard directory) with:
lftp :~> connect xb
Bookmarks can be removed:
lftp :~> bookmark del xb
Quitting lftp is simple:
lftp :~> quit
So far we have used lftp through its own shell-like interface. lftp also provides a simple program called `lftpget’ that can be used from an external shell like BASH.
This can be very handy to quickly grab a package, given the full URL, instead of interacting with lftp:
$ lftpget ftp://ftp.gnu.org/gnu/xboard/xboard-4.2.7.tar.gz
We have seen how to use lftp very simply to connect and log in to an FTP site, transfer files, save bookmarks, and get help.
lftp can be a very handy tool for system administrators and anyone that needs a robust and powerful yet simple FTP client in the command-line.
Where to Go Next
lftp is far more powerful than what we have seen here. Among other things, it supports multiple protocols, IPv6, throttling, scripting, aliases and extensive configuration. Refer to the following resources to learn to take advantage of these features.
Peter Matulis’ more detailed tutorial on using lftp as an FTP client.
NcFTP, an alternative command-line FTP client.
Feedback on this article is welcome. Please email abhishek (at) abhishek.geek.nz or post a comment at LinuxCult.com.
About the author
James Jay has been a GNU/Linux user for three years, involved in web design and programming for six years and is currently studying at university in New Zealand.