LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   daysold script - how many days old is a file (http://www.linuxquestions.org/questions/linux-software-2/daysold-script-how-many-days-old-is-a-file-675135/)

lrirwin 10-08-2008 05:39 PM

daysold script - how many days old is a file
 
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

jailbait 10-08-2008 07:00 PM

You could use the logrotate command to do the same thing.

-------------------
Steve Stites

jlinkels 10-08-2008 09:44 PM

Nice piece of programming work.

However since you are using ls, I assume you want to use the date the file was last changed. You could have use the find command for that with the -mtime option.

Furthermore, you could have fed the dates coming from the ls command into date, and ask a seconds output. This substracted from todays timestamp / 86400 would have given you the number of days as well. Really.

But a good exercise it is, much better than many members do!

jlinkels

usamantaray 05-22-2009 08:02 AM

Quote:

#!/usr/bin/perl

print int((time()-(stat("$ARGV[0]"))[9]) / 86400) . "\n";
The above perl script will also do the same thing.

GodofPain 07-12-2012 03:10 AM

I like the Perl version.

Here is how you do it in bash.

If your file is called mybackup.tbz
Code:

echo \($(date +%s) - $(date +%s --date="$(stat mybackup.tbz --printf='%y\n')")\) / \(60 \* 60 \* 24\) | bc
Basiclly get the date the file was last modified, convert that to seconds, subtract that value from the current time in seconds and then devide that by the number of seconds in a day.

jlinkels 07-12-2012 09:03 AM

Have you looked at the date?

jlinkels


All times are GMT -5. The time now is 11:06 PM.