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.
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?
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.
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.
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.
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.