Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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 => 'myserver.com', 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 Net:SFTP:Foreign.pm, 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 (Monsignor) 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-10-25 18:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (147 votes), past polls