If I understand correctly, you want to recursively list all files/directories within a location on a remote server, then save the results to a local file.
This code, using Net::SFTP::Foreign's find() method (even though I'm using 'localhost' as the remote), does just that (using my scratch working directory underneath the SFTP root, as my whole home dir would take far too much time to test):
use warnings;
use strict;
use Net::SFTP::Foreign;
my $host = 'localhost';
my $list_file = 'files.txt';
my $sftp = Net::SFTP::Foreign->new($host);
# look in my scratch working directory
my @list = $sftp->find('scratch');
open my $wfh, '>', $list_file or die $!;
print $wfh "$_->{filename}\n" for @list;
My local output file contains:
scratch
scratch/sftp.pl
scratch/dt.pl
scratch/time.pl
scratch/auth.pl
scratch/or.pl
scratch/test
scratch/test/a.txt
|