Operating System - HP-UX
1748060 Members
5612 Online
108758 Solutions
New Discussion юеВ

Re: how to check if a file is more than 1 week old?

 
SOLVED
Go to solution
Pando
Regular Advisor

how to check if a file is more than 1 week old?

Dear Gurus,

Is there a one line command to check if the file is 1 week old or more?

Maximum points to all correct replies.
Thanks!

6 REPLIES 6
Joseph Loo
Honored Contributor

Re: how to check if a file is more than 1 week old?

hi,

use the find command:

# find /etc -mtime +6 -exec ls -ld {} \;
using /etc directory to find which file/s is modified 7 days or more.

regards.
what you do not see does not mean you should not believe
Con O'Kelly
Honored Contributor

Re: how to check if a file is more than 1 week old?

Hi

You can use the find command:
# find / -mtime +7 -type -print


Cheers
Con
Con O'Kelly
Honored Contributor

Re: how to check if a file is more than 1 week old?

sorry the -type should be "-type f"

Cheers
Con
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: how to check if a file is more than 1 week old?

No matter what you are told that is not possible unless the file itself carry that data. File creation is simply not a timestamp carried in the file's inode; if you know the file's creation time (e.g. ctime), that is only be accident because ctime is the last time the file's mode, uid, gid, etc. was changed. Mtime is the last modification of the file itself (ie the data).


Now if you are willing to change you question to something "has the file neen modified in the last 7 days?" then it becomes possible using the attached Perl script:

typeset FNAME="myfile"
typeset -i PERIOD=604800
fileage.pl -m -s ${PERIOD} "${FNAME}"
typeset -i STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "${FNAME} has not been in ${PERIOD} seconds"
else
if [[ ${STAT} -eq 1 ]]
then
echo "${FNAME} has been modified with the last ${PERIOD} seconds"
else
echo "Command failed; status ${STAT}" > &2
fi
fi

Here's the Perl script, fileage.pl. Invoke as fileage.pl for full usage.
If it ain't broke, I can fix that.
Muthukumar_5
Honored Contributor

Re: how to check if a file is more than 1 week old?

You can try as,

find /tmp \( -mtime +7 -o -atime +7 -o -ctime +7 \) -type f

hth.
Easy to suggest when don't know about the problem!
Ralph Grothe
Honored Contributor

Re: how to check if a file is more than 1 week old?

As ACS wrote this is ambigous
because you have to distinguish between
the file's last modification time (mtime),
access time (atime), and inode change time (ctime), and all of them can be changed at any time.

You get these three times in epoch secs through a stat() syscall.

I think the easiest interface to stat() is through Perl.
Apart from stat() Perl also offers a nice file test feature through -M, -A, -C that return the days of the respective last change since the (Perl) script started.
This relieves you from doing subtractions and conversions from epoch secs to days youself.

Here's a wee example

$ date
Wed Jun 29 09:03:18 METDST 2005
$ touch -m -t 06211200 /tmp/tref
$ ll /tmp/tref
-rw-r--r-- 1 saz users 0 Jun 21 12:00 /tmp/tref
$ perl -le 'print -M shift' /tmp/tref
7.87875
$ perl -le 'print -A shift' /tmp/tref
0.000844907407407407
$ perl -le 'print -C shift' /tmp/tref
0.000960648148148148
$ perl -le 'printf"atime: %d\nmtime: %d\nctime: %d\n",(stat shift)[8..10]' /tmp/tref
atime: 1120028679
mtime: 1119348000
ctime: 1120028679
$ perl -le 'print scalar localtime 1119348000'
Tue Jun 21 12:00:00 2005
Madness, thy name is system administration