find command

Hi All,
I have a problem with using the find command.In my file structure,I need to find files that are exactly 14days old to execute an archive script.I used the "find . -mtime 14" command which displays 14day old files.The problem,however,is I get files that are more than 14days .For example today is Sep 4th,14day old files should be files on August 21st.I sometimes get files that are also on August 20th.How do I solve this issue?I need only 14day old files..NO MORE.

Thanks
PS: Find -mtime n apparently displays files that are n*24hours old.

The mtime with no sign means for example, 3 (as in -mtime 3 or -atime 3), means the 24-hour period that ended exactly 3 days ago (in other words, between 96 and 72 hours ago).
The other way to find the files created after some date and time is -
create empty file with touch have date as 21-AUG 00:01

touch -t 08210001 t1

now compare the files created after this file.

find path -newer t1 -ls

Now it will give you files created after 21st Aug midnight 00.00.

Perfect!

Thanks a lot

Is there a way to know the exact date 14days ago? And have it in this format

YYYYMMDD.

I want to store this date in a variable, then touch it.

Example today is Sep 4th 20060904, 14days ago would be 20060821,then

touch -t 200608210001 t1 (I would still need to concat 0001 to the date)

Search the site for datecalc. That is a fast script written by Perderabo for date calculations.

Hi Dhruva,
One more point please, the approach you mentioned, it lists all the file newer than the touched date.However,I want only files that are 14days old i.e files that were created btw Aug 21 2006 00:01 and Aug 21 2006 23:59. The find . -newer option would list all files newer than the touched date.

First thing however,how do I get the date 14days ago?

Thank you

Ok We can find the files between two dates and time...

This touch will serve as from time 31 Aug 2006 00:01

touch -t 200608310001 t2

This touch will serve to time 31 Aug 2006 23:59

touch -t 200608312359 t3

This find will give us files created in between two files.But keep in mind this will list file t3 as well.

find ./ -newer t2 \! -newer t3

I got it what you are trying to do..i will give a try

Ok I got one solution if you want to try....
Here is Date calc script by perderabo which i would like to use here.It take care of leap yr etc.

sysMon=`date +%m`
      sysDay=`date +%d`
      sysYear=`date +%Y`
      X=`./datecalc.sh -a $sysYear $sysMon $sysDay - 14`
     echo $X
 Output::

2006 8 21
Now i guess you can cut fields form X variable and can use in find command.
You may like to share with us if you get some other solution.

Thanks Dhruva,
I will try this.But why is the output just 8 and not 08?This could cause a problem in my final script.I will find out.

Thanks a lot.

Hi,

I added this to my script, but got bad conversion message.I think it could be bcos of the 2006821 output instead of 20060821.

sysMon=`date +%m`
sysDay=`date +%d`
sysYear=`date +%Y`
X=`./datecalc.sh -a $sysYear $sysMon $sysDay - 14`
Y=`echo $X|sed 's/ //g'`
touch -t $Y t5 

My script is almost done.The only problem now is Paderabo's script outputs the diff is this format YYYYMD (200693) however I want it to output as 20060903.

Let me check out the script.. I had a similiar problem awhile back that I fixed.

Go to line 188, and insert the following line above it:

typeset -Z2 month day

Now the script will print YYYYMMDD output.

Thanks alot.It works :slight_smile:

Hi everyone that contributed to my problems :slight_smile:

This is the part of the script:

#!/bin/ksh

BIN=/interface/gunner

cd $BIN
sysMon=`date +%m`
sysDay=`date +%d`
sysYear=`date +%Y`
begDate='0001'
endDate='2359'
X=`./datecalc.sh -a $sysYear $sysMon $sysDay - 53`
Y=`echo $X$begDate|sed 's/ //g'`
Z=`echo $X$endDate|sed 's/ //g'`
touch -t $Y t1
touch -t $Z t2


find ./ -newer t1 \! -newer t2  

I have the begdate and enddate so that I will be able to touch with 0001 and 2359.
However, the find command still return files outside a particular date. For example the above script should return only files created on the 14th of July.

find ./ -newer t1 \! -newer t2 still return some files created on 15th and sometimes it doesnt even return all the files created on 14th.

Thanks

Hint :-

You can run find command with mtime option in 00:00 clock to calculate time correctly without any effort.

am try this hint & the result very good.

Try and confirm.

Have you checked with -53 value on datecalc
X=`./datecalc.sh -a $sysYear $sysMon $sysDay - 53`
please echo it and check it what out put you are getting from the above command substitution.

I did. The echo outputs 2006 07 14 which looks fine. Now I intend to list only files created on this date.No more. In order to touch, I appened 0001 and 2359 to this. But when I use find newer command, I still get some wrong outputs.
Im thinking of using a cron job to run at 00:00 and for example

find ./ -mtime +55 \! -mtime +56

To find the files older than 55 days but not older than 56days.

I want your idea to work though.

few old vesions of find command have trouble with using multiple -newer expressions in one command. If find doesn't find files that it should, try using multiple explicit -mtime expressions instead. They're not as precise, but they will work even on finds with buggy -newer handling.
This is universally known problem of find.

I have a solution, but not using find or bash. I am using Python and the time,datetime modules

#!/usr/bin/python
import os,sys,time,datetime
today = datetime.date.today() 
delta = datetime.timedelta(days=14)
diff = today-delta
day,mth,yr = diff.day, diff.month, diff.year
for root,dir,files in os.walk("/home/user"):
        for fi in files:
                t = os.stat( os.path.join(root,fi) ).st_mtime
                y,m,d = time.localtime(t)[0:3]
                if yr == y and mth == m and day == d:
                        print "Found " , os.path.join(root,fi)

when run, it should give you exactly files that are 14 days old, no more.