Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding files that are 2 hours or older

Status
Not open for further replies.

mh53jfe

Technical User
Nov 20, 2000
9
US
I need to write a program that removes only those files in a directory that are 2 hours or older.

Using something like this:
find . -mtime +0 -name '[A-Z]*'
This code snippet will return all files older than 1 day.

I have not found any reference finding a file that is n hours old.

Any help will be highly appreciated.

Thanks in advance for your help.
 
man touch
man find (! -newer)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
This could give you a start:

#!/usr/bin/ksh
integer CURTIME=$(date '+%m%d%H%M')
integer REQTIME=$CURTIME-200
touch -m -a -t 0$REQTIME TEMPFILE
find . ! -newer HOURFILE -type f
rm -f HOURFILE


However the above one works only if the current month is until september, and
after october you will need to take out the zero"0" in the touch command, like change it to

touch -m -a -t $REQTIME TEMPFILE

 
Correction Please

This could give you a start:

#!/usr/bin/ksh
integer CURTIME=$(date '+%m%d%H%M')
integer REQTIME=$CURTIME-200
touch -m -a -t 0$REQTIME HOURFILE
find . ! -newer HOURFILE -type f
rm -f HOURFILE


However the above one works only if the current month is until september, and
after october you will need to take out the zero"0" in the touch command, like change it to

touch -m -a -t $REQTIME TEMPFILE
 
create a cron job to run every two hours
the first time the job runs, "oldfiles" will not exist

for i in `cat /mydir/oldfiles`
do
rm $i
done
ls /mydir > /mydir/oldfiles
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top