aryan has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am trying to write a script that compares two directories and these directories can be on the local or remote server. In the below I am able to connect and cd to the directory I want but the find function doesn't run. Any help?
#Test FTP connections 1 $ftp1=Net::FTP->new($host1) or $ErrFlg=1; push @ERRORS, "Can't ftp to $host1: $!\n" if $ErrFlg; myerr() if $ErrFlg; print "SUCCESS: Connected to $host1\n"; $ftp1->login($username1,$password1) or $ErrFlg=1; push @ERRORS, "Can't login to $host1: $!\n" if $ErrFlg; $ftp1->quit if $ErrFlg; myerr() if $ErrFlg; print "SUCCESS: Logged into $host1\n"; $ftp1->cwd("$dir1") or $ErrFlg=1; push @ERRORS, "Can't cd to $dir1 $!\n" if $ErrFlg; myerr() if $ErrFlg; $ftp1->quit if $ErrFlg; $maxDepth=$maxDepth1; # Find function find ({preprocess => \&preprocess, wanted => \&wanted,}, "."); $ftp1->quit

Replies are listed 'Best First'.
Re: Comparing Directories on different servers
by ig (Vicar) on May 27, 2009 at 21:29 UTC

    One can only guess why the find function doesn't run because you have posted an incomplete fragment of code and no error messages. If you read How do I post a question effectively? and Writeup Formatting Tips then revise your posting to provide more information, you may get more help.

    In the mean time, I guess that you want the find to find files in the directory you have accessed via Net::FTP. I don't think find will do this: it will search directories on your local system.

    You may be able to map/mount the remote drive to your local system, then use File::Find to search it. Or, if you are on Windows, you may be able to use UNC paths to access the directory on a remote server.

Re: Comparing Directories on different servers
by almut (Canon) on May 27, 2009 at 21:47 UTC
    ... $ftp1->cwd("$dir1") or $ErrFlg=1; push @ERRORS, "Can't cd to $dir1 $!\n" if $ErrFlg; myerr() if $ErrFlg; $ftp1->quit if $ErrFlg; $maxDepth=$maxDepth1; # Find function find ({preprocess => \&preprocess, wanted => \&wanted,}, ".");

    ...but the find function doesn't run

    I'm afraid there's a misconception of what FTP does. Logging into some remote machine (ftp server) does not automagically transfer execution of your script onto that remote machine (if that was the idea). Even after logging in and the cwd, the script itself will still run locally on the machine where you started it. In other words, when your script calls the find routine, it will scan a local directory, if anything...

    Also, FTP may generally not be used to run arbitray commands remotely. It provides a predefined set of commands primarily intended for transferring files. You could get a directory listing of remote directories, but if you don't like the format the ftp server produces, you're essentially out of luck.

    If you want to run Perl code (File::Find) remotely to produce the directory listing or do something else while traversing the directories, you need a remote shell/execution protocol, like SSH.  Or mount/map the remote directory/filesystem into your local filesystem, as ig suggested. (Depending on your network setup, OS on local/remote machine, etc., one or the other option may not be available.)

Re: Comparing Directories on different servers
by snoopy (Curate) on May 27, 2009 at 22:50 UTC
    Update: Both almut and ig have already mentioned remote mounting. Here's some more background on doing this over ssh or ftp.

    On a 'nix system, non-root users can mount remote directories as a pseudo filesystem in user space, say using sshfs, or CurlFtpFs

    Both of these are underpinned by the absolutely brilliant FUSE (File System in User Space).

    Once mounted, you can, for example, access the file systems from the shell. You can also use utilities such as File::Find.

    If you have ssh access to the remove server, and sshfs is installed:

    % mkdir -p sshfs/someuser@somehost % sshfs someuser@somehost: sshfs/someuser@somehost % cd sshfs/someuser@somehost % ls -l # list remote files % echo awesome\! >cool.txt # create a file on the remote server
    If you only have ftp access to the remote host, curlftpfs does the same sort of thing:
    % mkdir -p ftpfs/someuser@somehost % curlftpfs ftpfs/someuser@somehost someuser:mypass@somehost % cd ftpfs/someuser@somehost % ls