Ever needed to figure out the number of days old a file is so you could delete old logs and saved copies of posted transactions???
I had to write it today... Couldn't find one anywhere, so I thought I'd post it here... I called it daysold..
I have not tested it using something like: daysold `find / -print`
But it works really well for a directory full of files...
=================================================
Code:
# !/bin/sh
# daysold - shell script to show how many days old a given file is
#
# Internal functions
yeardays() {
# return the number of days in a year
# usage: yeardays ccyy
if [ $# = 0 ]
then
echo 0
return
else
y=$1
fi
# a year is a leap year if it is even divisible by 4
# but not evenly divisible by 100
# unless it is evenly divisible by 400
# if it is evenly divisible by 400 it must be a leap year
a=`expr $y % 400`
if [ $a = 0 ]
then
echo 366
return
fi
#if it is evenly divisible by 100 it must not be a leap year
a=`expr $y % 100`
if [ $a = 0 ]
then
echo 365
return
fi
# if it is evenly divisible by 4 it must be a leap year
a=`expr $y % 4`
if [ $a = 0 ]
then
echo 366
return
fi
# otherwise it is not a leap year
echo 365
return
}
getdays() {
IYear=`echo $1 | cut -c1-4`
IMonth=`echo $1 |cut -c5-6`
IDay=`echo $1 |cut -c7-8`
if [ "${IYear}" = "${SysYear}" ]
then
FDays=`date --date $1 +%j`
DDays=`expr ${JulDate} - ${FDays}`
echo ${DDays}
return
fi
DaysInFYear=`yeardays ${IYear}`
IJulian=`date --date $1 +%j`
FDays=`expr $DaysInFYear - $IJulian`
# FDays=`expr $FDays + 1`
YTmp=`expr $IYear + 1`
while [ $YTmp -lt $SysYear ]
do
DTmp=`yeardays $YTmp`
FDays=`expr $FDays + $DTmp`
YTmp=`expr $YTmp + 1`
done
FDays=`expr $FDays + $JulDate`
echo $FDays
return
}
SysDate=`date +%Y%m%d`
JulDate=`date +%j`
SysMonth=`date +%m`
SysYear=`date +%Y`
TmpYear=`date +%Y`
YM=""
for i in 0 1 2 3 4 5 6 7
do
Tmp=`expr $SysMonth - $i`
[ ${Tmp} -le 0 ] && {
Tmp=`expr ${Tmp} + 12`
TmpYear=`expr ${SysYear} - 1`
}
case ${Tmp} in
1) TmpMonth="01";;
2) TmpMonth="02";;
3) TmpMonth="03";;
4) TmpMonth="04";;
5) TmpMonth="05";;
6) TmpMonth="06";;
7) TmpMonth="07";;
8) TmpMonth="08";;
9) TmpMonth="09";;
10) TmpMonth="10";;
11) TmpMonth="11";;
12) TmpMonth="12";;
*) echo "Code isn't working.... Exiting."; exit 1;;
esac
[ "${YM}" = "" ] && {
YM="${TmpYear}${TmpMonth}"
continue
}
YM="${YM} ${TmpYear}${TmpMonth}"
done
for Line in `ls -l $* | sed -e 's/ * / /g' -e 's/^ //' | tr " " "#"`
do
Line="$Line#"
case $Line in
tot*) continue;;
*) ;;
esac
File=`echo ${Line} | cut -f9 -d"#"`
Month=`echo ${Line} | cut -f6 -d"#"`
case $Month in
"") continue;;
Jan) MM="01";;
Feb) MM="02";;
Mar) MM="03";;
Apr) MM="04";;
May) MM="05";;
Jun) MM="06";;
Jul) MM="07";;
Aug) MM="08";;
Sep) MM="09";;
Oct) MM="10";;
Nov) MM="11";;
Dec) MM="12";;
*) echo "Invalid Month on file $File. Exiting."
exit 1;;
esac
Day=`echo ${Line} | cut -f7 -d"#"`
case ${Day} in
?) Day="0${Day}";;
*) ;;
esac
Year=`echo ${Line} | cut -f8 -d"#"`
case ${Year} in
??:??) Year="TIME";;
*) ;;
esac
[ "${Year}" = "TIME" ] && {
for j in ${YM}
do
TmpMonth=`echo $j | cut -c5,6`
[ "${MM}" = "${TmpMonth}" ] && {
TmpYear=`echo $j | cut -c1-4`
Year=${TmpYear}
break
}
done
}
Tmp="${Year}${MM}${Day}"
Days=`getdays ${Tmp}`
echo "$Days $File"
# debugging area...
# echo $Tmp
# echo "YM Conv=${YM}"
# echo "System Gregorian Date=${SysDate}"
# echo "System Julian Date=${JulDate}"
# echo "Month=${MM}"
# echo "Day=${Day}"
# echo "Year=${Year}"
# echo "Days Old = ${Days}"
# echo $Line
# echo "FileDate=${Tmp} GetDate=${From}"
# read ans gbg
done
=================================================
Enjoy!
Larry Irwin