Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

files sync

by paurus (Novice)
on Aug 06, 2005 at 21:55 UTC ( #481558=perlquestion: print w/replies, xml ) Need Help??

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

Esteemed monks,

I am trying to create code which will recursively sync files between two directories which are given on the command line. I would like the code to look through each directory, tell me which files are common and which are not, then sync (copy to whichever directory is file deficient) any of the files seen only once. In my novice and modest efforts, I have already come up with:
#!/usr/bin/perl -w # use pragmas ##### # use diagnostics; use strict; # use modules ##### use File::Find; use File::Basename; # Declare variables; ##### my $file; my %count; my @total_files; # Make sure command line argument is supplied ##### die "No command line arguments: $!\n" unless @ARGV; # wanted subroutine for File::Find ##### sub wanted { $file = $File::Find::name; if (basename($file) =~ /^\.$|^\.\.$|^\.DS/) { next; } # .DS_ f +or Mac OS parse push(@total_files, $file = basename($file)); } # process @ARGV ##### find(\&wanted, @ARGV); # create hash for basename file seen count ##### foreach $file(@total_files) { $count{$file} += 1; } # print to STDOUT any file not seen 2 or more times ##### foreach $file (keys %count) { if($count{$file} < 2) { print "SYNC this file: $file \n";
$file is only the basename. When I use the full path returned by File::Find, it doesn't meet my hash requirements, because each path is different (as I am sure you aready know..)
} } # for STDOUT format cleanliness ##### print "\n";
I am not sure how to identify by path each file that needs to go from the directory in which it exists to the directory in which it does not exist. Could you please steer me in a direction which would cause the code to "smartly" identify and sync each file which does not already exist in each location simultaneously?

Thank you for any aid.

Replies are listed 'Best First'.
Re: files sync
by BUU (Prior) on Aug 06, 2005 at 22:46 UTC
    perl -le'system("rsync @ARGV")'

    Ok, it's not really perl, but why are you reinventing the wheel?
      If this is over a network you may want to consider using rsync over ssh.
      rsync -e ssh
      Of course, this will require setting up ssh keys that don't require passphrases.
Re: files sync
by itub (Priest) on Aug 07, 2005 at 02:07 UTC
Re: files sync
by kral (Monk) on Aug 08, 2005 at 07:16 UTC
    Why don't you use File::DirSync? I used it at work for a couple of little backup scripts and it worked like a charm to me :)

    HTH,
    ----------
    kral
    (I apologise for my english!)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://481558]
Approved by polettix
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (8)
As of 2023-12-05 08:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (26 votes). Check out past polls.

    Notices?