Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a file exists on a remote server using Perl?

Tags:

module

perl

ftp

How can I check if a file exists on a remote server using Perl?

Can I do this while using the Perl module Net::FTP?

CHECK TO SEE IF FILE EXISTS

if (-e $file_check) {  
    print "File Exists!\n";  
}   
else {  
    print "File Doesn't Exist!\n";  
}  
like image 480
Luke Avatar asked Jun 28 '10 16:06

Luke


People also ask

How do I check if a remote file exists in Python?

Here are a few options: test -e to see if any file exists (directory or regular file), test -f to see if it exists and is a regular file, or test -d to see if it exists and is a directory. This seems to break for paths including $HOME/test or ~/test .


2 Answers

You might be best served by using SSH to do this:

#!/usr/bin/perl

use strict;
use warnings;

my $ssh  = "/usr/bin/ssh";
my $host = "localhost";
my $test = "/usr/bin/test";
my $file = shift;

system $ssh, $host, $test, "-e", $file;
my $rc = $? >> 8;
if ($rc) {
    print "file $file doesn't exist on $host\n";
} else {
    print "file $file exists on $host\n";
}
like image 80
Chas. Owens Avatar answered Nov 14 '22 22:11

Chas. Owens


You could use a command such as:

use Net::FTP;
$ftp->new(url);
$ftp->login(usr,pass);

$directoryToCheck = "foo";

unless ($ftp->cwd($directoryToCheck))
{
   print "Directory doesn't exist
}
like image 40
claydiffrient Avatar answered Nov 14 '22 23:11

claydiffrient