Moving Files Based on Date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moonbug
    New Member
    • Jun 2007
    • 1

    Moving Files Based on Date

    How can I move files based on date specifically for the case below:

    1. crontab runs every hour
    2. any file in directory 1 dated less than the current hour is moved to directory 2.
    eg file1 time stamp 2:59
    file2 time stamp 2:45
    file3 time stamp 2:05
    file4 time stamp 3:08
    3. crontab runs at 3:15, so file1,2,3 are moved to directory2
  • tifoso
    New Member
    • Apr 2007
    • 41

    #2
    Have the cron job call a perl script is quicker/cleaner get stat of each file on the folder(s) and compare the dates to current system time and move it properly

    Ciao

    Comment

    • prn
      Recognized Expert Contributor
      • Apr 2007
      • 254

      #3
      tifoso is right. This is much easier done in Perl than in a shell script. Here's an example:
      [code=perl]#! /usr/bin/perl
      use strict;
      my $destination_di rectory = '/foo/d2';
      foreach my $file (glob "*") {
      my $mtime = (stat("$file"))[9];
      my $now = time();
      my $diff = $now - $mtime;
      print "age of $file is $diff \n";
      # running at x:15 -- 15 minutes = 900 seconds
      if ( $diff > 900 ) {
      print "should move $file to $destination_di rectory/$file\n";
      #rename $file $destination_di rectory/$file;
      }
      }
      [/code]
      You would, of course, want to change the value of $destination_di rectory to whatever is appropriate, uncomment the "rename" line and, of course, you can delete the print statements that just show you what is going on.

      Best Regards,
      Paul

      Comment

      Working...