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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting files less than 2 hours old? 3

Status
Not open for further replies.

smacattack

Technical User
Jun 25, 2001
102
GB
I can delete files which are a day old by exec
find /fred -name "*fred" -mtime -1 -exec rm {} \;

Is there any way of being more precise and saying less than 3 hours?

thanks
 
Someone just answered this for me a few days ago, so I'm happy to pass the information on!

Just use a decimal after mtime. In my case, I wanted to delete files which were more than 4 hours old. 4 hours divided by 24 hours in a day is .16, so I used mtime +.16. 3 hours would be .125 - I'm not sure how thin you can slice it, you might need to use .12 or .13.

Good luck,

Allen
 
Has this been tested on Solaris? Doesn't seem to work for me... (Solaris 8) Annihilannic.
 
My systems are a couple years old & running SunOS 5.6, but I have 4 of them using the following cron entry sucessfully:

Code:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * find /var/spool/srv_print/Karchive_iR110 -name "?f*" -mtime +.16 -exec rm -f {} \;

I haven't tried it on any newer versions, but I can't imagine why it wouldn't be backwards compatible.

Allen
 
It just seems to silently ignore anything after the decimal point, for example:

[tt]# date
Fri Mar 28 15:09:05 GMT 2003
# ls -l
total 0
-rw-r--r-- 1 root other 0 Mar 28 05:00 10hoursold
-rw-r--r-- 1 root other 0 Mar 28 00:00 15hoursold
-rw-r--r-- 1 root other 0 Mar 27 14:00 1dayold
-rw-r--r-- 1 root other 0 Mar 28 14:00 1hourold
-rw-r--r-- 1 root other 0 Mar 26 14:00 2daysold
-rw-r--r-- 1 root other 0 Mar 28 10:00 5hoursold
# find . -mtime +0
./1dayold
./2daysold
# find . -mtime +0.1
./1dayold
./2daysold
# find . -mtime +.1
./1dayold
./2daysold[/tt]

This is on a Solaris 2.6 system. It's a shame because it would be a handy feature! Annihilannic.
 
Annihilannic, you are absolutely correct. I swear I thought I tested this and had it work, but I just finally got back in front of one of these machines and looked at it a little closer. It seems to be treating any decimal < 1 as 1 - so it's still just trapping day-old stuff.

The man page is a little less clear than I would like (imagine that!), in that it says

In the descriptions, whenever n is used as a primary argument, it will be interpreted as a decimal integer optionally preceded by a plus (+) or minus (-)sign as follows ........

For some reason, when I read that, I paid more attention to the word &quot;Decimal&quot; than the word &quot;Integer&quot;, and assumed that they meant &quot;Fractional Number&quot;, rather than &quot;Base 10&quot;. I should have paid more attention to the fact that they used the word &quot;integer&quot;....

Is it just me, or is the term &quot;Decimal Integer&quot; an oxymoron?

Anyway, sorry for passing along inaccurate information.

Allen
 
It's not really an oxymoron... as you say, &quot;integer&quot; means a whole negative or positive number (which could be any base), and &quot;decimal&quot; means base 10.

In other words... it's just you. ;-) Annihilannic.
 
Getting back to the orginal problem, you could use

touch 04021200 /tmp/TIMESTAMP
find /fred -newer /tmp/TIMESTAMP -exec rm {} \;

to delete everything from noon today. CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
CaKiwi, I see how that could work, but is there a way to have that touch always equal to 4 hours ago - so that if is were run in a cron, say hourly, it would delete every file that was more that 4 hours old at that moment....?

Thanks,

Allen
 
JAFrank,

Maybe like this?

touch `date +%Y%m%d%H%M | awk -f date.awk` /tmp/TIMESTAMP

# ------ date.awk ------
BEGIN { m[1]=31;m[2]=28;m[3]=31;m[4]=30;m[5]=31;m[6]=30;
m[7]=31;m[8]=31;m[9]=30;m[10]=31;m[11]=30;m[12]=31;
}
{
yy=substr($0,1,4)
mm=substr($0,5,2)
dd=substr($0,7,2)
hh=substr($0,9,2)
mn=substr($0,11,2)

hh-=4;
if (hh<0) {
hh+=24
dd--;
if (dd==0) {
mm--;
if (mm==0) {
yy--;
mm=12;
}
dd = m[mm];
if (mm==2 && yy%4 == 0) dd=29;
}
}
printf (&quot;%2.2d%2.2d%2.2d%2.2d%d\n&quot;, mm, dd, hh, mn, yy)
}
CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Or you may like to adapt the suggestions in faq80-953
I believe the method that changes TZ only works in the GMT timezone CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Or use gawk (GNU awk) which can manipulate dates using strftime. systime gets the gurrent system time in secs since 00:00 1/1/1970.

touch `gawk 'BEGIN { print strftime(&quot;%Y%m%d%H%M&quot;,systime()-4*3600); }'` /tmp/TIMESTAMP
 
CaKiwi,

I realize that I am probably just being a moron, but I can't get your awk script to work for me - if I run it using your command line,

Code:
touch `date +%Y%m%d%H%M | awk -f date.awk` /tmp/TIMESTAMP

I end up with a new file in my local directory called
Code:
date +%Y%m%d%H%M | awk -f date.awk
and a new file in /tmp called
Code:
TIMESTAMP
.

If I run just the date part,

Code:
date +%Y%m%d%H%M | awk -f date.awk

I always get the same result - 040320002003

The result I would have expected, based on my understanding of touch, would be the current month, day, hour, minute - with year optional - so I should get something like 04021925.....Right???
At any rate, shouldn't I get a different result each time I run it?

Thanks for your help

Allen
 
Make sure you are using back tick ` quotes rather than ' single quotes.

touch has 2 time formats the one you're using expects an optional 2 digit year suffix, the awk script is returning the 4 digit year. Use yy=substr($0,3,2). The hours should be 4 hours earlier than the time at which you called it.

Command I gave earlier had wrong date format as well

touch `gawk 'BEGIN { print strftime(&quot;%m%d%H%M%y&quot;,systime()-4*3600); }'` /tmp/TIMESTAMP

gawk is on the Solaris 8 free software CD or downloadeble from
 
date +%Y%m%d%H%M | awk -f date.awk | touch -- testfile

this command works well,i have tested it.
 
I like julianbarnett's gawk program and bluelake's method of using touch

gawk 'BEGIN { print strftime(&quot;%m%d%H%M%y&quot;,systime()-4*3600); }' | touch -- /tmp/TIMESTAMP
CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
I finally figured out what was messing me up - in CaKiwi's awk script above, I changed

Code:
printf (&quot;%2.2d%2.2d%2.2d%2.2d%d\n&quot;, mm, dd, hh, mn, yy)

to

Code:
printf (&quot;%2.2d%2.2d%2.2d%2.2d%d\n&quot;, yy, mm, dd, hh, mn)

And then changed the command line from

Code:
touch `date +%Y%m%d%H%M | awk -f date.awk` /tmp/TIMESTAMP

to

Code:
touch -t `date +%Y%m%d%H%M | awk -f date.awk` /tmp/TIMESTAMP

If I use the -t, I can send a date_time using CCYYmmddHHMM. Otherwise, I have to use mmddHHMMYY. The problem there is that the script wasn't returning 03 for the year - it was dropping the leading zero, so I'd end up with 040312453 instead of 0403124503.

So all of that works now, but I have another question - can the -newer option on the find command be inverted? Remember that the original question was deleting files more than x hours old.

If we use find with the -newer option, as in CaKiwi's original post, we would delete everything less than x hours old....

Thanks to everyone who has contributed!

Allen
 
I misread the original post (the title misled me). Use

! -newer

to invert -newer
CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Thanks, that did the trick.

I ended up with the awk script, modified slightly as outlined above, and a quickie shell script called clean_archive with the following two lines:

Code:
touch -t `date +%Y%m%d%H%M | awk -f /allen/date.awk` /tmp/testfile
find /var/spool/srv_print/Karchive_iR110 -name &quot;?f*&quot; ! -newer /tmp/testfile -exec rm -f {}

The first line sets the date on /tmp/testfile to 4 hours ago, the second finds all files in the specified directory with &quot;f&quot; as the second character which are not newer than /tmp/testfile (ie: 4 hours) and deletes it.

I set up the following cron entry to run it on the hour:

Code:
0 * * * * /allen/clean_archive


Seems to work like a charm. I can't thank everyone enough for your help.

Thanks again,

Allen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top