Connecting to an FTP Server with LFTP


#1

LFTP is a command line tool that will allow you to connect to an FTP server you might have access to. From there, you’ll be free to upload, download, and perform other file operations with your FTP server.

Installing LFTP

Your workspace may already have LFTP installed but make sure you have LFTP installed by running:

$ sudo apt-get install lftp

Connecting to your FTP server

After you have LFTP installed, make sure you know your FTP username and your server hostname. Once you have those you can simply connect to your FTP server by running:

$ lftp ftp://username@hostname

You may also connect to an SFTP server. Just be sure you know what port you would like to connect on and then run:

$ lftp sftp://username@hostname:port

Transferring and syncing files

Navigating your FTP server

Now that you’re connected to your server, you can navigate the files on your server just as you would files in your workspace, using ls to show files that are in your current directory and cd to change to a different directory.

Upload and Download Files

To download a file from your FTP server to your workspace, use the pget command.

$ pget file_from_server.txt

Now I’ll see file_from_server.txt in my workspace.

To upload a file to your FTP server use the put command.

$ put file_from_workspace_file.txt

And now my file_from_workspace_file.txt is accessible on my FTP server. :muscle:

Syncing files with mirror

Mirror is a set-it-and-forget-it kind of tool that you can use to sync your workspace with your FTP server and vice versa. The key here is to know which way things will be mirrored.

Normally I’ll want to run mirror with the -R argument because that will allow me to make changes in my workspace and then have them mirrored to my server.

Before I run the mirror command, I need to change to the directory I want from within lftp (so lftp knows where we are) by running:

$ lcd /home/ubuntu/workspace/

Then when I run mirror -R, it will copy that whole directory I just changed to in my workspace to whatever directory I’m in on my server.

I can also skip the lcd command and instead pass a directory in my workspace as the first argument (this is the directory that will be mirrored). In other words, this is the base where I’ll be making changes and anything I do there will be copied to the FTP server.

The second argument will be a directory on my FTP server that will be getting the files, based on files in my workspace directory that is being mirrored.

$ mirror -R my_workspace_dir/ my_server_dir/

Now all my files from my_workspace_dir/ will be be transferred to my server in my_server_dir/.

On the other hand, if I’d like to mirror a directory on my FTP server and have changes there be mirrored to my workspace, I can use the mirror command without -R as follows.

$ mirror my_server_dir/ my_workspace_dir

Now I could make changes on my FTP server at my_server_dir/ and they’d automatically be mirrored in my workspace in my_workspace_dir/.

Automating Updates

A member of the community has submitted the following Gulpfile in order to automate updates to the FTP server you’re working with (this code can also be viewed in this JSFiddle).

////////////// gulpfile.js
// this gulp file should be located on the same folder as the mounted one you want to keep updating with lftp i.e 'mountedFolder'
// the lftp command in this example uses no password "remoteUsername, ..." so this should be edited to your needs
var gulp = require('gulp'),
    watch = require('gulp-watch');

gulp.task('default', ['vigilar']);

gulp.task('vigilar', function() {
  console.log('Watching files located in mountedFolder');
  return watch('./mountedFolder/**', function(datos){
      console.log('Kind of event: ' + datos.event);
      for(var i=0; i<datos.history.length; i++){
          var archivoLocal = datos.history[i];
          var archivoRel = datos.history[i].replace(datos.base,'');
          var archivoRemoto = '/remote/folder/to/save/changes' + archivoRel;
          var valid = true;
          if(archivoLocal.indexOf('/.') >= 0)
            valid=false; //ignore .git, .ssh folders and the like
          console.log('File [' +(i+1) + '] ' + (valid?'valid':'INVALID') + ' localFile: ' + archivoLocal + ', relativeFile: ' + archivoRel + ', remoteFile: ' + archivoRemoto);
          var comando = "lftp";
          var comando_params = "-c \"open -u remoteUsername, sftp://remote.server.com; put " + archivoLocal + " -o " + archivoRemoto + "\" ";
          if(valid){
              console.log('Performing in shell: ' + comando + ' ' + comando_params);
              var exec = require('child_process').exec;
              exec(comando + ' ' + comando_params);
          }
      }//end for
  });//end watch
});//end task
///////// end of gulpfile.js

Other LFTP resources


How to upload files to my server
Easiest way to Migrate wordpress site from C9 to GoDaddy
Can't upload file from workspace to FTP
#2

Hi!

I’m trying to FTP to my website using the method above. However, I keep getting the following error message:

Fatal error: Certificate verification: Not trusted

I think it’s because my website’s server doesn’t have SSL. How can I make it so it connects without a verified certificate?

Thanks!


#3

I needed to fork the Fiddle, change command, etc so figured I’d share. You should be able to just replace user, pass and site vars at the top with this. This doesn’t make directories for you but just to see i made a testFolder and new file in C9 per usual then opened a 2nd terminal from the one running gulp > logged in to lftp normaly and ran:

mkdir testFolder

Back on the normally created file, I hit ctrl+s to save and everything worked perfect for me so i was happy since that’s not really needed here anyway.

Hope it helps someone who lands here too.

My fork of above gulpfile.js Fiddle


#4

$ cat ~/.lftp/rc
set ssl:verify-certificate no


#5

Is there a way to establish FTP connection without username and hostname? I was able to do that before deprecating mounting, if I remember correctly I did it in ssh workspace. Does it have to be SSH workspace, or can I do it on hosted one as well? Maybe I don’t have the right packages installed? All I need to do is run my node js app, connect to ftp, open localhost and put a file in there.

Would really appreciate any help, thanks in advance!


#6

This does not require an SSH workspace so you can use this in a hosted workspace as well. As far as I know, FTP access requires a username and hostname at all times. If there’s another way to get FTP access, I’m not aware of it.


#7

I believe Lina was asking about anonymous FTP.


#8

Hello all,

when trying to connect from Linux to server 2012 r2 through FTP , and use this command (lftp -e “mirror -v -e -R MYDIRECTORY /; exit” -p 21 -u
username password FTP_://sitedoamin_

we get the error

“chmod: Operation not supported: MFF and SITE CHMOD are
not supported by this site”!!

can any one help me ?


#9

I have used node ftp, node jsftp trying to connect to my IIS ftp server. Connection blocked timed out. Then I tried lftp the line command approach. Connection timed out on socket errors. Mind you SSL is turned off on my IIS ftp server. I can go directly to a browser and test my IIS ftp. So I know its a working ftp server. I had a nice workflow feature that extracted data from mongoDB and staged the data in JSON files. All I had to do was ftp those files over to an app on another work space that I push to heroku. Just to avoid the manual copying.

Now I’ve been doing FTP since my IBM mainframe days. Could FTP between the mainframe and servers. Then on UNIX when I FTP’d my COBOL source code to a UNIX node where I made them executable off to QA they went. Its sad to see the multitude of problems with a technology that has been around for 30 years. I thought the people writing the open source script languages where smarter. Of course the Cloud Wars have not helped leaving many a casualty of Application Developer in the wake.

Really don’t need the ftp I’ll just move them manually but come now can we get some quality and reliability back in this game.


#10

This an amazingly helpful article. My host has the crappiest text editor known to the universe and now I don’t need to use it. Thanks a million!