1751792 Members
5002 Online
108781 Solutions
New Discussion юеВ

More Time

 
SOLVED
Go to solution
Rob Blake
New Member

More Time

I'm trying to figure out if there is a way to write a ksh script that will find and delete all directories and their sub-files that are more than 5 minutes old. Anyone have an idea how/if I can use mtime or ctime or something like that to make something that works? Basically a clean-up type of ksh script to put into crontab to run every 5 min.
17 REPLIES 17
Ivan Ferreira
Honored Contributor

Re: More Time

Hi Rob, ctime and mtime uses multiples of 24 hours.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Pete Randall
Outstanding Contributor

Re: More Time

Rob,

You need to look at a combination of the touch and find commands. Use touch to create a file with a time stamp of 5 minutes prior. Then use find to detect and remove any files older than your reference file.

touch -t [[CC]YY]MMDDhhmm[.SS] ref_file
find /start_dir -type f ! -newer ref_file -exec rm {} \;
-or-
find /start_dir -type f ! -newer ref_file | xargs rm


Pete

Pete
Victor Fridyev
Honored Contributor

Re: More Time

Hi,

You can use command
touch -t time file_stamp
time can be set up somehow as now -5 minutes
After you run
find dirname -newer file_stamp -exec rm {} \;

HTH
Entities are not to be multiplied beyond necessity - RTFM
James R. Ferguson
Acclaimed Contributor
Solution

Re: More Time

Hi Rob:

First compute a timestamp that is 5-minutes less than the current time:

Use the following perl script called "/tmp/time":

#!/usr/bin/perl
$t=((time)-300);
($mon,$day,$hr,$min)=(localtime($t))[4,3,2,1];
$mon++;
printf("%02d%02d%02d%02d\n",$mon,$day,$hr,$min)

In your cron task do:

# TS=`/tmp/time`

# touch -amt ${TS} /tmp/myref
# find /path -xdev -type f ! -newer /tmp/myref | xargs rm
# find /path -xdev -type d -newer /tmp/myref | xargs rmdir
exit 0

This does a pass for files first, and then secondarily for empty directories. Since deletion of the files in a subdirectory will update the directory's timestampe ('mtime') I chose the positive form of '-newer' whereas the negated '-newer' argument was applied to the files.

Regards!

...JRF...
Arturo Galbiati
Esteemed Contributor

Re: More Time

Hi,
I use this command to remove teh core file:
It founds the core file in the current modified from 24 hours ago to now

find . -name "core" -mtime -1 -type f -exec rm {} \;

I scheduled it in crontab to run every 5 minutes and teh result is that I remove teh core file every 5 minutes.
HTH,
Art
Stephen Keane
Honored Contributor

Re: More Time

Or (proving yet again that there is more than one way to skin a cat in shell) ...

Your cron job (which runs every 5 minutes) should look for a reference file in a known location.

If the file is absent, then create it (its timestamp will be set to now) do nething else this time.

If the file is present, then it was created by your cron job 5 minutes ago, so any file older than it can be removed. Then touch your reference file (its timestamp will be set to now) so that the next time the cron job runs it will be 5 minutes old.

The advtange of doing it this way is you can change how old the files need to be before deleting them by changing the cron job frequency rather than having to edit the script each time.

Just my ├В┬г0.02 worth.

Rob Blake
New Member

Re: More Time

JRF suggests:

# find /path -xdev -type f ! -newer /tmp/myref | xargs rm
# find /path -xdev -type d -newer /tmp/myref | xargs rmdir

James, just looking at it, the above seems to delete all files that are younger than the reference file /tmp/myref rather than older.

Am I reading this wrong? What can I put on the end to replace the xargs rm and xargs rmdir that will show me which files are fixing to be deleted before I actually try and run a test of this.

So far, the script looks like this ...

rm /tmp/myref
START=${HOME}
echo ${START}
TARGET=${HOME}/targetdir
echo ${TARGET}
cd ${TARGET}

#####
# Run temp_time to get the date time less 5 minutes
#####
TS=`${PS_HOME}/temp_time`

#####
# Touch to create a reference file in tmp directory
#####
touch -amt ${TS} /tmp/myref

#####
# find all files and directories in the target directory that are older
# than the reference file.
#####
find ${TARGET} -xdev -type f ! -newer /tmp/myref | xargs rm
find ${TARGET} -xdev -type d -newer /tmp/myref | xargs rmdir

#####
# Exit script
#####
cd ${START}
exit 0
Pete Randall
Outstanding Contributor

Re: More Time

The "! -newer" syntax says not newer, i.e. older.


Pete

Pete
Stephen Keane
Honored Contributor

Re: More Time

You do realise that when you delete a file from a directory, you update the directory (and hence its access time) as well?