Operating System - HP-UX
1748170 Members
4084 Online
108758 Solutions
New Discussion юеВ

Re: Deleting files older than 20 minutes

 
SOLVED
Go to solution
Salman Ahmed Qureshi
Frequent Advisor

Deleting files oler than 20 minutes

Hi,
On my database server, archives are created in a file system very frequently and i move those archives to another file system so that this file system doesn't get full. My current job moves all archives to the other file system but some times it corrupts my archives because it also moves the archive currently being written and has not finished yet, so the solution in my mind is to move only archive files older than, let say 15 minutes so i will be sure that only finished archives are being moved to the other file system. Can anyone tell me how to edit my current script to accomplish this. Please see my script.
##########################################
date
for fstr in `find /odsdata1/archive/ods_* -type f -print`
do
echo "Moving $fstr"
mv $fstr /odsarchive2/backup/ods/archive/
done
date
echo "End of transfer"
######################################
Thanks

Salman
15 REPLIES 15
Jeeshan
Honored Contributor

Re: Deleting files oler than 20 minutes

find files older than 1 hr

# perl -MFile::Find -le 'find(sub{print $File::Find::name if -f $_ && -M _ <= 1/24},@ARGV)' /path
a warrior never quits
Dennis Handly
Acclaimed Contributor

Re: Deleting files older than 20 minutes

You'll need to create a reference file with touch for 20 minutes ago. (Or use gnu find, -mmin +20.)

The problem with 20 minutes ago is that you need to do date arithmetic on touch. If now is 16:00, then the reference file would be:
touch 02091540 ref_file

You can also solve the reference file issue by creating it manually the first time then just use your sleep or cron to remember 20 minutes ago:
touch ref_file_new # for next time
for fstr in $(find /odsdata1/archive/ods_* -type f ! -newer ref_file); do
done
mv ref_file_new ref_file
sleep $(( 20 * 60 ))

I suppose you could put the touch at the bottom if the find and mv doesn't take too long.

Salman Ahmed Qureshi
Frequent Advisor

Re: Deleting files oler than 20 minutes

Hi Dennis,
Thanks for your reply. I would be rather interested in, "find -mmin" command. Can you please edit my script to add this portion. If you think that i can't achieve 20 mins, you can make it 1 hour.
Thanks in advance

Salman
Steven E. Protter
Exalted Contributor
Solution

Re: Deleting files oler than 20 minutes

Shalom Salman,

Example script:
http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=131801

How to overcome issues that may come up in such a script:
http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=1227620

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Dennis Handly
Acclaimed Contributor

Re: Deleting files older than 20 minutes

>"find -mmin" command. Can you please edit my script to add this portion?

Unless you install GNU find, you can't. HP-UX's find only has the standard day granularity with -mtime. If you don't want to use GNU, you will have to use a reference file or use ahsan's perl solution.

>you can make it 1 hour.

That is still 1/24 too small. :-)

James R. Ferguson
Acclaimed Contributor

Re: Deleting files oler than 20 minutes

Hi Salman:

To find your candidates, use Perl like this (sorry Ahsan, you can't just copy and paste previous solutions):

# perl -MFile::Find -le '@ARGV=(".") unless @ARGV;find(sub{print $File::Find::name if -f $_ && -M _ >= 1/96},@ARGV)' /path

You can pass multiple /path arguments if you like. RUnning without any argument ('/path') infers the current directory.

The "1/96" is "1/96" of one day or one 15-miunute period of age.

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor

Re: Deleting files oler than 20 minutes

Hi (atain) Salman:

Actually, it is just as easy to do everything in one Perl script:

# cat ./mymover
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use File::Copy;
my $dstdir='/odsarchive2/backup/ods/archive';
@ARGV = (".") unless @ARGV;
print STDERR "Begin @ ", scalar localtime, "\n";
find(
sub {
if ( -f $_ && -M _ >= 1 / 96 ) {
print STDERR "Moving '$_'\n";
move( $File::Find::name, $dstdir ) or die "$!\n";
}
},
@ARGV
);
print STDERR "Ended @ ", scalar localtime, "\n";
1;

...Again, pass the source directory name or "." or nothing at all to denote the current working directory for the source path.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Deleting files older than 20 minutes

>JRF: The "1/96" is one 15-minute period

You might want to use an expression so you don't have to explain it:
15 / (24 * 60)

James R. Ferguson
Acclaimed Contributor

Re: Deleting files oler than 20 minutes

Hi (again):

> Dennis: You might want to use an expression so you don't have to explain it:
15 / (24 * 60)

Yes, I agree, in this case that would be good. So change:

if ( -f $_ && -M _ >= 1 / 96 ) {

...to:

if ( -f $_ && -M _ >= ( 15 / ( 24 * 60 ) ) ) {

...which makes the 15-minute interval much clearer and allows leveraging further:

...
my $age=15
@ARGV = (".") unless @ARGV;
...
if ( -f $_ && -M _ >= ( $age / ( 24 * 60 ) ) ) {
...

Regards!

...JRF...