0

Using PERL, I'm trying to login to an FTP server, download all files from a specific directory and then move all these files to another directory on the server.

This is my code so far:

open( my $LFTP,'|-', "lftp -u $ftpuser,$ftppwd -e open $ftpserver" ) or die "Cannot open lftp: $!";
print $LFTP <<"END";
              mirror $remoteFiles $datadir
              renlist $remoteDir | "sed 's#\(.*\)#mv \"\1\" \"outgoing/archive\/\"#'" > list && source list && !rm list
END
close($LFTP) or die; # die unless lftp exit code is 0

The files download correctly but they won't move. I believe the problem is with the sed command.

The temp file created called "list" looks like that:

dir1/di2/file1.csv
dir1/di2/file2.csv 
dir1/di2/file3.csv
dir1/di2/file4.csv

While it should be looking like that:

mv "dir1/di2/file1.csv" "outgoing/archive/"
mv "dir1/di2/file2.csv" "outgoing/archive/"
mv "dir1/di2/file3.csv" "outgoing/archive/"
mv "dir1/di2/file4.csv" "outgoing/archive/"

It seems like the sed regex is not working correctly. However, if I type the exact same command directly on the LFTP terminal, it works fine.

Could you please tell me what is wrong with my regex and why it won't work when executed from within a perl script?

Thank you in advance for your help....

r1pster
  • 147
  • 1
  • 12
  • 1
    This would be much more simple to write and clearer to read if you were to use one of Perl's many FTP modules. [Net::FTP](http://metacpan.org/module/Net::FTP) has been distributed as a core module since Perl v5.7.3. I'm also wondering why you don't download the files to where you want them in the first place? – Borodin Nov 12 '15 at 14:23
  • Thank you. I did not think of this but it looks interesting. – r1pster Nov 12 '15 at 14:27

2 Answers2

1

Escaping of the \ character should do the trick:

print $LFTP <<"END";
          mirror $remoteFiles $datadir
          renlist $remoteDir | "sed 's#\\(.*\\)#mv \"\\1\" \"outgoing/archive\/\"#'"

Escaping the \ admits for perl's string literal syntax, resulting in the backslashes that escape sed's special chars.

collapsar
  • 17,010
  • 4
  • 35
  • 61
0

The problem might be all the escaping (backslashes before characters). You can try printing the resulting string to check what does the sed command looks like on the server.
I tried messing with it a little and another backslash before the '1' (\"\\1\") will probably solve your problem.

I would not use sed. Assuming you're using bash (the syntax is a bit different for other shells):

for file in `renlist $remoteDir`; do
    mv "$file" outgoing/archive/
done

This will just iterate through your files and move them.

YMI
  • 165
  • 1
  • 10