12 lftp Commands to Manage Files with Examples

Written by: Linuxopsys   |   Last updated: July 4, 2022

In this guide, we are going to install LFTP, which is a command-line FTP client. LFTP supports many protocols in addition to FTP, such as HTTP, HTTPS, HFTP, FISH, FTPS, and FXP. It supports mirroring directories and you can have commands entered in two modes.

LFTP works on the command-line shell or right from the bash shell. Bash shell is particularly useful if you are going to write a script that uses LFTP.

Setting up FTP server

Since LFTP is a FTP client, to work with it we would need to have some FTP server setup. So lets set up a basic FTP server that we will use for our demonstration of LFTP features. Simple ftp server in Ubuntu is vsftpd, and we are going to install it from repositories

sudo apt install vsftpd

After it is installed, we need to allow writing by users to the server. By default, FTP server is read-only.

sed -i.orig 's/#write_enable=YES/write_enable=YES/g' /etc/vsftpd.conf

Since we changed config file we need to restart the server

systemctl restart vsftpd

Check status, to see if the server daemon is running

sudo systemctl status vsftpd

Using LFTP to connect to FTP server

Moving now to other computer, VM or server. The one where we will install LFTP and connect to FTP server. LFTP is as well present in Ubuntu repositories, so lets get it.

sudo apt install lftp

Before we connect to FTP server, let's make a directory from where we will connect

$ mkdir ftp
$ cd ftp/

and fill it with some files to work with

$ touch test{1..9}

We can now use lftp to connect to the FTP server

lftp ftp://[email protected]
Password:

Enter the password of your user and you are in. Since I was in ftp dir when I made FTP connection, working local directory on client is ~/ftp. First command that we will try is uploading a file to ftp server

1. Put command

This command takes one file from local directory and uploads it to server. We will upload file test1.

lftp [email protected]:~> put test1

2. List contents of remote directory

We can check what have been done with ls command.

lftp [email protected]:~> ls
-rw------- 1 1000 1000 0 Dec 01 12:13 test1

We see that file have been copied to FTP server.

3. Reverse mirroring of directory

If we want to mirror the entire directory to the server, we will use reverse mirror command.

lftp [email protected]:~> mirror -R

This will copy our local directory contents to the server. The opposite process is without -R. When we are not adding any paths to files, current local and remote directories are used. That is in our case ~/ftp and ~/.

4. Change local directory

The local directory can be changed with lcd command

lftp [email protected]:~> lcd /home/miki/Videos/
lcd ok, local cwd=/home/miki/Videos

5. Print local working directory

You can check what is your local working directory with lpwd command

lftp [email protected]:~> lpwd
/home/miki/Videos

6. Resuming mirroring after interruption

LFTP allows resumption of the file transfer if it is interrupted. After the interruption, you continue by repeating the command with adding -c flag, which means continuation.

^C Interrupt
lftp [email protected]:~> mirror -R -c

7. Downloading the file from ftp server

Downloading files from the remote directory to local directory is done with pget command

lftp [email protected]:~> pget ubuntu-16.04.1-server-amd64.iso
Output
421705192 bytes transfered

8. Mirroring from the server to the local directory

We earlier show how to do the opposite, now we use mirror command to get data pulled from the server

mirror

It is simple, just mirror.

9. Mirroring with arbitrary directories

You can also mirror with the data that is not in working local directory, for example with such command:

mirror . /home/miki/Videos/

This will pull data from servers working directory (the . is first path so it is current dir) to the ~/Videos on our local machine. It doesn't matter if we did not use lcd to move to ~/Vidoes, the pull will work.

10. Exiting the shell

The command for exiting the lftp shell is bye. This is important if you are going to script the lftp operations.

11. Using LFTP commands as part of the script

After we exited the LFTP lets see how we can use above commands without logging onto ftp server. That way you can embed it into bash script.

miki@ThinkPad-X220T:~$ lftp -e 'mirror /home/miki/ /home/miki/Videos/' -u miki,password ftp://192.168.122.65

12. Mirroring the site

The lftp can use HTTP protocol as well, which makes it suitable for downloading normal websites.

miki@ThinkPad-X220T:~$ lftp -e "mirror -c" http://linuxopsys.com/

Conclusion

We have gone through all the needed commands to successfully use LFTP. You can use it for many tasks, such as sharing files on your two computers, backing up your server periodically or backing up some internet site you like.

Thanks for reading, please leave your feedback and suggestions.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

1 Comment

1 thought on “12 lftp Commands to Manage Files with Examples”

Leave a Comment