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.
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:
Retrieving the list of files.
From here it’s a simple matter to define a remote directory that we
want to look at:
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
‘):
And then we use the ls()
method to return a list of the files that match the
patttern:
Here’s the full code sample.
Which, (if there are files in the remote directory matching the pattern we’ve
specified), will produce output like:
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.