Tweet

Retrieving a list of file names from an ftp server

You want to retrieve a list of file names that match some pattern from a directory on an ftp server.

Net::FTP

The Net::FTP module implements a simple ftp client in Perl. In this FAQ example, we'll use it to login to a remote ftp server and retrieve a list of files that match a pattern. Note that it is the file names that we're interested in here, not the files themselves.

Simple example

Let's establish a simple example first. Once you modify it to provide suitable values for the $ftp_site, $ftp_user and $ftp_password variables, this code will login to an ftp server, and return the output of the pwd command on that server.

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Net::FTP;

    my $ftp_site     = 'localhost';
    my $ftp_user     = 'a_login_name';
    my $ftp_password = 'a_password';

    my $ftp = Net::FTP->new($ftp_site) 
        or die "Could not connect to $ftp_site: $!";

    $ftp->login($ftp_user, $ftp_password) 
        or die "Could not login to $ftp_site with user $ftp_user: $!";

    print 'Remote directory is: ' . $ftp->pwd() . "\n"; 
    $ftp->quit();

As you can see, the Net::FTP methods are similar to the steps you'd expect to use in an interactive ftp session: the new() method connects your program to the server, the login() method logs your program in, and the pwd() method returns the current working directory.

The output from this program on my server is:

    Remote directory is: /home/simon

Retrieving the list of files.

From here it's a simple matter to define a remote directory that we want to look at:

    my $ftp_dir      = '/home/simon/software';

Then we define the glob-style pattern that determines the files that we want to list, (here we are interested in seeing all files that start with the string 'ex'):

    my $glob         = 'ex*';

And then we use the ls() method to return a list of the files that match the patttern:

    @remote_files = $ftp->ls($glob);

Here's the full code sample.

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Net::FTP;

    my $ftp_site     = 'localhost';
    my $ftp_dir      = '/home/simon/software';
    my $ftp_user     = 'a_login_name';
    my $ftp_password = 'a_password';
    my $glob         = 'ex*';
    my @remote_files;

    my $ftp = Net::FTP->new($ftp_site) 
        or die "Could not connect to $ftp_site: $!";

    $ftp->login($ftp_user, $ftp_password) 
        or die "Could not login to $ftp_site with user $ftp_user: $!";

    $ftp->cwd($ftp_dir) 
        or die "Could not change remote working " . 
                 "directory to $ftp_dir on $ftp_site";

    @remote_files = $ftp->ls($glob);

    foreach my $file (@remote_files) {
        print "Saw: $file\n";
    }

    $ftp->quit();

Which, (if there are files in the remote directory matching the pattern we've specified), will produce output like:

    Saw: expat-1.95.5.tar.gz
    Saw: expect.tar
    Saw: extralite.tar

Caveats

There are a number of issues that we don't touch on in this example. One is that storing a login and password in your Perl program itself is usually undesirable. Another is that the ftp protocol sends your login and password across the network as plain text. In many cases the secure ftp client sftp is a better choice.

See also

    perldoc Net::FTP
    perldoc Net::SFTP - for a pure Perl sftp client

Revision: 1.3 [Top]