Unix administration - Move files based on age

This is Interesting: Free IT Magazines  
Home > Archive > Unix administration > September 2005 > Move files based on age





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Move files based on age
dbennet@gmail.com

2005-07-14, 7:48 am

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

Bit Twister

2005-07-14, 6: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.


Doug Freyburger

2005-07-14, 6: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.

cashar@roushind.com

2005-09-22, 9:01 pm

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






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com