how to check if a file is more than 1 week old? (55 Views)
Reply
Regular Advisor
Pando
Posts: 201
Registered: ‎04-15-2004
Message 1 of 7 (55 Views)
Accepted Solution

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!

Please use plain text.
Honored Contributor
Joseph Loo
Posts: 1,981
Registered: ‎10-23-2000
Message 2 of 7 (55 Views)

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
Please use plain text.
Honored Contributor
Con O'Kelly
Posts: 645
Registered: ‎03-18-2003
Message 3 of 7 (55 Views)

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
Please use plain text.
Honored Contributor
Con O'Kelly
Posts: 645
Registered: ‎03-18-2003
Message 4 of 7 (55 Views)

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

sorry the -type should be "-type f"

Cheers
Con
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 5 of 7 (55 Views)

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.
Please use plain text.
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 6 of 7 (55 Views)

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!
Please use plain text.
Honored Contributor
Ralph Grothe
Posts: 2,631
Registered: ‎04-09-2000
Message 7 of 7 (55 Views)

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
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation