LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Tags used in this thread
Popular LQ Tags , , , , ,

Reply
 
Thread Tools
Old 10-08-2008, 05:39 PM   #1
lrirwin
LQ Newbie
 
Registered: Jun 2006
Location: Greenville, SC USA
Distribution: Debian and RH (2.4 and 2.6)
Posts: 2
Thanked: 0
daysold script - how many days old is a file


[Log in to get rid of this advertisement]
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
lrirwin is offline  
Tag This Post , , , , ,
Reply With Quote
Old 10-08-2008, 07:00 PM   #2
jailbait
Guru
 
Registered: Feb 2003
Location: atop the Blue Ridge
Distribution: Debian Lenny, CentOS 5.2
Posts: 7,250
Thanked: 31
You could use the logrotate command to do the same thing.

-------------------
Steve Stites
jailbait is offline     Reply With Quote
Old 10-08-2008, 09:44 PM   #3
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Etch/Lenny/Squeeze
Posts: 2,301
Thanked: 89
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
jlinkels is offline     Reply With Quote
Old 05-22-2009, 08:02 AM   #4
usamantaray
LQ Newbie
 
Registered: May 2009
Posts: 1
Thanked: 0
Smile

Quote:
#!/usr/bin/perl

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

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to remove files older than 3 days rust8y Linux - General 8 11-09-2009 09:09 AM
Cron backup script stops running after a few days brokenpromises Linux - Server 5 04-05-2008 06:30 AM
script to auto delete files older than X days nocnoc Programming 17 12-06-2006 09:30 AM
Calculating age in days and month in a bash script jachba Programming 5 06-23-2006 02:37 PM
help with a script that deletes files more than X days old BrianK Linux - General 5 06-14-2004 10:05 PM


All times are GMT -5. The time now is 08:38 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration