lftp Commands to Manage Files with Examples
Today 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. In LFTP command line shell or right from the bash shell. The latter one is particularity useful if you are going to write a script that uses LFTP. So here we go.
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 writting 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-get install lftp
Before we connect to FTP server, lets make a directory from where we will connect
miki@ThinkPad-X220T:~$ mkdir ftp
miki@ThinkPad-X220T:~$ cd ftp/
and fill it with some files to work with
miki@ThinkPad-X220T:~/ftp$ touch test{1..9}
We can now use lftp to connect to the FTP server
lftp ftp://miki@192.168.122.65
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 miki@192.168.122.65:~> put test1
2. List contents of remote directory
We can check what have been done with ls command.
lftp miki@192.168.122.65:~> 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 entire directory to the server, we will use reverse mirror command.
lftp miki@192.168.122.65:~> 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
Local directory can be changed with lcd command
lftp miki@192.168.122.65:~> 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 miki@192.168.122.65:~> lpwd
/home/miki/Videos
6. Resuming mirroring after interruption
LFTP allows resumption of the file transfer if it is interrupted. After interruption you continue by repeating the command with adding -c flag, which means continuation
^C Interrupt
lftp miki@192.168.122.65:~> mirror -R -c
7. Downloading the file from ftp server
Downloading files from remote directory to local directory is done with pget command
lftp miki@192.168.122.65:~> pget ubuntu-16.04.1-server-amd64.iso
8. Mirroring from the server to the local directory
We earlier show how to do opposite, now we use mirror command to get data pulled from 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 website.
miki@ThinkPad-X220T:~$ lftp -e “mirror -c” http://plegma.host/
Conclusion
We have gone trough all 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. This is all for this article, thank you for reading and have a nice day.