Move files based on age
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix administration > Move files based on age




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Move files based on age  
dbennet@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-14-05 12:48 PM

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






[ Post a follow-up to this message ]



    Re: Move files based on age  
Bit Twister


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-14-05 11:01 PM

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.







[ Post a follow-up to this message ]



    Re: Move files based on age  
Doug Freyburger


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-14-05 11:01 PM

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.






[ Post a follow-up to this message ]



    Re: Move files based on age  
cashar@roushind.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-23-05 02:01 AM

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 webservertalk.com - Unlimited-Uncensored-Secure Usenet New
s==----
http://www.webservertalk.com The #1 Newsgroup Service in the World! 120,000+
 Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 04:53 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register