Professional Web Applications Themes

Move files based on age - Linux / Unix Administration

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...

  1. #1

    Default 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

    dbennet@gmail.com Guest

  2. #2

    Default Re: Move files based on age

    On 14 Jul 2005 04:31:08 -0700, com wrote: 


    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.


    Bit Guest

  3. #3

    Default Re: Move files based on age

    com wrote: 

    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.

    Doug Guest

  4. #4

    Default 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 = <INFILE>;

    # 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 =----
    cashar@roushind.com Guest

Similar Threads

  1. How to move files and dirs recursively?
    By warddc in forum Macromedia ColdFusion
    Replies: 0
    Last Post: May 5th, 12:21 PM
  2. Cannot move or delete files
    By katie_e@adobeforums.com in forum Adobe Illustrator Windows
    Replies: 5
    Last Post: June 30th, 02:15 PM
  3. how can i put swf files into a doent and be able to move it around?
    By GMDESIGNS webforumsuser@macromedia.com in forum Macromedia Flash Sitedesign
    Replies: 7
    Last Post: October 22nd, 08:43 PM
  4. FTP app to move large files between hosts?
    By Bob Harris in forum Mac Networking
    Replies: 3
    Last Post: July 24th, 06:04 AM
  5. Move dependent files
    By MGT webforumsuser@macromedia.com in forum Macromedia Dreamweaver
    Replies: 2
    Last Post: July 15th, 02:21 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139