Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Net::SFTP::Foreign hwo to list (ls) files

by ssubrat77 (Novice)
on Jun 03, 2011 at 05:51 UTC ( #907911=perlquestion: print w/ replies, xml ) Need Help??
ssubrat77 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, Need your expert advise to fix one urgent issue. I want to list all the files in SFTP remote server which satisfy the particular pattren like we do ls abcd*.txt. I treid to use the bleow code but not working. Please help
use Net::SFTP::Foreign; my $sftp = Net::SFTP::Foreign->new( host => '', port => '22', user => 'ftpuser', more => [qw( -i myserver.private.key.ppk)], ssh_cmd => 'plink'); $sftp->die_on_error("Unable to establish SFTP connection"); my $entry = "abcd*.txt"; my $files = $sftp->ls ('/etc', wanted => qr/$entry/) or die "unable to + retrive directory: ".$sftp->error;
-- Thanks for all the replies. My problem got resolved. Thanks a lot for your help

Comment on Net::SFTP::Foreign hwo to list (ls) files
Download Code
Re: Net::SFTP::Foreign hwo to list (ls) files
by philipbailey (Chaplain) on Jun 03, 2011 at 07:07 UTC

    You don't say exactly what is not working. What is the output when you run your program?

    Given the title of your post, presumably the failure is in the directory listing. Looking at the documentation for, the wanted value should be a full regex, not a glob-style expression. So perhaps you want a regex more like /^abcd.*\.txt$/ ? Your existing regex will find files matching "abc", followed by zero or more "d", followed by one character, followed by "txt".

    A couple of other customary comments: please enclose code in <code> tags. You can go ahead and edit your post now to do this. It's not advisable to say that your question is "urgent" as some of the monks do not like this, and might not encourage replies.

Re: Net::SFTP::Foreign hwo to list (ls) files
by salva (Abbot) on Jun 03, 2011 at 07:27 UTC
    The mget method does what you want:
    $sftp->mget("/etc/abcd*.txt", $local_dir);

    update: Oops, that would download the files. Use glob if you just want to list them:

    my $files = $sftp->glob("/etc/abcd*.txt");
    or maybe...
    my $files = $sftp->glob("/etc/abcd*.txt", names_only => 1);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://907911]
Approved by salva
Front-paged by toolic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2015-03-02 05:07 GMT
Find Nodes?
    Voting Booth?

    When putting a smiley right before a closing parenthesis, do you:

    Results (25 votes), past polls