Operating System - Linux
1753611 Members
6076 Online
108797 Solutions
New Discussion юеВ

Re: Checking if file is older than 1 week

 
SOLVED
Go to solution
James R. Ferguson
Acclaimed Contributor

Re: Checking if file is older than 1 week

Hi (again):

By the way, all you need to do to create a file with a timestamp of your choosing is to 'touch' the file:

# touch -amt 2006100500.00 myfile

...would create "myfile" or update the timestamp of "myfile" to set its both its 'atime' and 'mtime' to October 5, 2006 at 00:00:00 hours. If you don't want to alter a timestamp of an existing file but only want to create one if it *doesn't* exist, add the '-c' swithch to the 'touch':

# touch -camt 2006100500.00 myfile

Look at the manpages for 'touch' for more information.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Checking if file is older than 1 week

Hi:

It occurs to me that your "concern" is having to create and cleanup a temporary file to which you can compare another file's age.

My first choice would be a one-line Perl snippet, but since you would like to keep things in shell only, consider something like this:

# cat .fileage
#!/usr/bin/sh
MYT=${1}
MYF=${2}
REF=/var/tmp/${0##*/}.${$}

if [ ${#} -ne 2 ]; then
echo "Usage: timestamp file"
exit 2
fi

[ ! -f ${MYF} ] && { echo "${MYF} doesn't exist"; exit 2; }

trap 'rm -f ${REF}' EXIT

touch -amt ${MYT} ${REF} || exit 1

if [ ${MYF} -nt ${REF} ]; then
echo "${MYF} is newer than ${MYT}"
else
echo "${MYF} isn't newer than ${MYT}"
fi
exit 0

...run, passnig a timestamp value and the name of a file for comparison of its age; e.g.:

# ./fileage 200510131001 /etc/hosts
/etc/hosts is newer than 200510131001

# .fileage 10131001 /etc/hosts
/etc/hosts isn't newer than 10131001

The script creates a reference file for comparison purposes, but uses a 'trap' to cleanup (remove) the file. This simplifies coding.

Regards!

...JRF...
dictum9
Super Advisor

Re: Checking if file is older than 1 week

I like Perl better, how would it look in Perl?
dictum9
Super Advisor

Re: Checking if file is older than 1 week

P.S.
Thanks, the ksh is really good stuff too, I will implement it.
James R. Ferguson
Acclaimed Contributor

Re: Checking if file is older than 1 week

HI:

OK, in Perl we have the '-M' file test operator to ascertain the number of days since modification. We also have '-A' and '-C' for access and inode change times, if you want.

Again, since your original question was about a shell script, and since I suggested that at the least you could add a snippet of Perl to the shell for your purposes, here's a different variation --- without any temporary files:

# cat .fileage2
#!/usr/bin/sh
MYT=${1}
MYF=${2}

if [ ${#} -ne 2 ]; then
echo "Usage: days file"
exit 2
fi

[ ! -f ${MYF} ] && { echo "${MYF} doesn't exist"; exit 2; }

R=`perl -e '$x=(@ARGV && -f $ARGV[1] && -M $ARGV[1] > $ARGV[0]) ? 1:0;print $x'
${MYT} ${MYF}`

if [ ${R} -eq 1 ]; then
echo "${MYF} is older than ${MYT} days"
else
echo "${MYF} is too young"
fi
exit 0

...run this one ...passnig the number of days and the name of a file for comparison of its age; e.g.:

# .fileage2 100 /etc/hosts
/etc/hosts is older than 100 days

To integrate the Perl code into the shell, I elected to pass two arguments to Perl; the number of days and the file's name. The code returns a one (1) or a zero (0) which can be captured by the shell and handled there.

Regards!

...JRF...