Move files based on age - Unix

This is a discussion on Move files based on age - Unix ; I would like to move files from one folder to another based on the age of the file from when it was modified. I would use cron to run this script every four hours. If it has been more than ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Move files based on age

  1. Move files based on age

    I would like to move files from one folder to another based on the age
    of the file from when it was modified. I would use cron to run this
    script every four hours. If it has been more than 3 hours since a file
    was modified, it should be moved.

    I'm not sure how to use find with mtime to do this. Can anyone help?

    Thanks.

    Darryl


  2. Re: Move files based on age

    On 14 Jul 2005 04:31:08 -0700, dbennet@gmail.com wrote:
    > I would like to move files from one folder to another based on the age
    > of the file from when it was modified. I would use cron to run this
    > script every four hours. If it has been more than 3 hours since a file
    > was modified, it should be moved.
    >
    > I'm not sure how to use find with mtime to do this. Can anyone help?



    Play with this until it displays the files you like then change the ls
    -al to your move command.

    find . -mtime -40 -a -mtime +20 -exec ls -al '{}' \;

    do the command
    man find

    Hint: given example uses days not minutes.



  3. Re: Move files based on age

    dbennet@gmail.com wrote:
    >
    > I would like to move files from one folder to another based on the age
    > of the file from when it was modified. I would use cron to run this
    > script every four hours. If it has been more than 3 hours since a file
    > was modified, it should be moved.
    >
    > I'm not sure how to use find with mtime to do this. Can anyone help?


    Normal versions of find use days not hours for -mtime.

    Look at the man page for touch(1), and consider using
    -newer instead of -mtime.

    Simple strategy. touch an anchor file somewhere. Wait
    3 hours. Run your cron job doing the moves with
    \( \! -newer anchor \). touch the anchor file after
    the moves. Let cron do your next wait for you.

    Fun strategy. Read the man page for touch in fine
    detail. Read the man page for date in fine detail.
    Read the man page for expr in fine detail. Use the
    3 commands to do integer arithmatic to have your
    anchor file be exactly 3 hours old at the start of
    your run. Use \( \! -newer anchor \).

    I recommend the fun strategy. Learning more shell
    toolsmithing is always an implicit goal.


  4. Re: Move files based on age

    Hi,

    I do a similar thing with cleaning up an ftp system every hour. The script
    below can be modified for your use. It is a perl script. Instead of
    unlinking the files, just do a move of the files. The script is pretty easy
    to modify.

    Craig

    ------------------------------------------------------------snip-----------------------------------------------------------

    #!/usr/bin/perl -w

    use Time::Local;
    use File::stat;

    open OUTFILE, ">/home/admin/cleanup/ftpdirs.lst" || die "$!\n";
    open EXCLUDE, ">/home/admin/cleanup/exclude.lst" || die "$!\n";

    @DIRS = `find $ARGV[0] -type d`;

    foreach (@DIRS) {

    chomp;

    unless (/lib/ || /etc/ || /bin/ || /admin/) {

    select OUTFILE;
    print "$_\n";
    select STDOUT;
    next;

    } else {

    select EXCLUDE;
    print "$_\n";
    select STDOUT;

    }

    }

    close OUTFILE;
    close EXCLUDE;

    open INFILE, "/home/admin/cleanup/ftpdirs.lst" || die "Cannot open file
    $!\n";

    # get the path
    @path = ;

    # set max age in seconds;
    $max_age=864000;

    # get the current time in Epoch seconds
    $tm=timelocal(localtime);

    # get the directory contents
    foreach $DIR (@path) {

    chomp $DIR;

    chdir $DIR;

    @directory=glob("*");

    # process each entry in the directory
    foreach (@directory) {
    chomp; # remove new line
    $filestat=stat($_); # stat the file
    if(-f) { # if it IS a file
    $writetime=$filestat->mtime; # get the last mod'd time
    $age=$tm-$writetime; # calculate the file age
    if($age>$max_age) { # older than max_age?
    print "$DIR\/$_\n";
    unlink($_); # delete the file
    }

    }
    }
    }

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

+ Reply to Thread