Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Copy files by date modified

by svetho (Beadle)
on Jun 04, 2009 at 07:19 UTC ( #768319=snippet: print w/replies, xml ) Need Help??
Description: Yesterday I came across a problem to sync files between two computers in a network, but only those that had been created or modified within the last 60 days. Unfortunately rsync doesn't come with such a functionality (or it doesn't seem to - maybe I've overlooked something). It could well be that I've gone to work in a more complicated manner than necessary but I couldn't come up with anything better. So I took the extra step to copy the files that need to be synchronised to a temporary directory, preserving the original directory structure. Then the files can be effortlessly synced with rsync. Maybe this will come in handy for somebody else.

use strict;
use warnings;
use File::Path;
use File::Basename;
use File::Spec;
use File::Copy;
use Cwd;
use Getopt::Long;

# make output unbuffered
$| = 1;

my $path     = getcwd;
my $mtime    = 1;
my $dest;
my $verbose  = '';
my $showhelp = '';
my $result   = GetOptions("source=s"  => \$path,
                           "mtime=i"   => \$mtime,
                           "dest=s"    => \$dest,
                           "help"      => \$showhelp,
                           "verbose"   => \$verbose);

# print "\$path = $path\n\$mtime = $mtime\n\$dest = $dest\n\$showhelp 
+= $showhelp\n\$verbose = $verbose\n";
# exit(0);

my $usage = "Usage: $0 --dest <destination-path>\n\t" .
             "[--source <source-path> | --mtime <mtime> | --verbose] [
+--help]\n\t" . 
             "Type $0 --help for more information.";
my $help = <<EOHELP;
$0 - Copy file hierarchies selectively
     based on their modification times

    -s | --source=SourceDir     Source path. Default: pwd
    -m | --mtime                mtime (see man find). Default: 1
    -d | --dest=DestDir         Destination path. Mandatory option.
    -h | --help                 Print this help screen.
    -v | --verbose              Be more verbose. Default: false

if ($showhelp) {
    print $help;
unless ($dest) {
    die "$usage\n";

# remove trailing slash from $dest
$dest =~ s/\/$//;
my $find = "find $path -type f -mtime -$mtime";

my @files = `$find`;
my %dirs_created;
foreach (@files) {
    # debug

    my ($filename, $directories, $suffix) = fileparse($_);
    my $new_dir = File::Spec->canonpath(File::Spec->catdir($dest, $dir
    unless (exists $dirs_created{$new_dir}) {
        $dirs_created{$new_dir} = mkpath($new_dir);
        print "Created $new_dir\n" if $verbose;
    copy($_, File::Spec->catfile($new_dir, "$filename$suffix"));
    print File::Spec->catfile($new_dir, "$filename$suffix") . "\n" if 

Replies are listed 'Best First'.
Re: Copy files by date modified
by dwhite20899 (Friar) on Jun 08, 2009 at 00:59 UTC
    Hi, svetho!

    I also needed a date-modified mode for rsync, so I don't think you missed something there.

    I was downloading ISO files into various subdirectories of /home/me/isos and wanted to rsync occasionally, but didn't want to waste time on dirs I already had done.

    I wrote a script that "touch"ed a file when it exited, to set the date it was last run. I don't have the code handy, but it went like:

    $f = `find /home/me/isos -type f -iname '*iso' `; @imlist = split(/\n/,$f); $touchtime = -M ".touch_sem"; for $image (@imlist) { if ($touchtime <= -M "$image") { #rsync } } `touch .touch_sem `;
      what about the --modify-window option in rsync?
Re: Copy files by date modified
by sabari (Beadle) on Sep 23, 2009 at 06:13 UTC
    I do have one solution to this but little lengthy 1.Build unwanted (no needs to sync) file list 2.Use --exclude-from in rsync i know this is not correct way todo but we can doit for temp solution
    Best Regards, S.Sabarinathan,
Re: Copy files by date modified
by Anonymous Monk on Nov 26, 2010 at 08:50 UTC
    what heaven...??
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://768319]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2017-07-28 03:23 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (424 votes). Check out past polls.