LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > 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

Reply
 
Search this Thread
Old 10-08-2008, 04:39 PM   #1
lrirwin
LQ Newbie
 
Registered: Jun 2006
Location: Greenville, SC USA
Distribution: Debian and RedHat currently
Posts: 3

Rep: Reputation: 1
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
 
Old 10-08-2008, 06:00 PM   #2
jailbait
Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Wheezy, Debian Jessie
Posts: 7,522

Rep: Reputation: 177Reputation: 177
You could use the logrotate command to do the same thing.

-------------------
Steve Stites
 
Old 10-08-2008, 08:44 PM   #3
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,086

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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
 
Old 05-22-2009, 07:02 AM   #4
usamantaray
LQ Newbie
 
Registered: May 2009
Posts: 1

Rep: Reputation: 9
Smile

Quote:
#!/usr/bin/perl

print int((time()-(stat("$ARGV[0]"))[9]) / 86400) . "\n";
The above perl script will also do the same thing.
 
Old 07-12-2012, 02:10 AM   #5
GodofPain
LQ Newbie
 
Registered: May 2006
Posts: 5

Rep: Reputation: 0
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.
 
Old 07-12-2012, 08:03 AM   #6
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,086

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Have you looked at the date?

jlinkels
 
  


Reply

Tags
date, days, old, script, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 26 10-04-2012 08:26 AM
Cron backup script stops running after a few days brokenpromises Linux - Server 5 04-05-2008 05:30 AM
script to auto delete files older than X days nocnoc Programming 17 12-06-2006 08:30 AM
Calculating age in days and month in a bash script jachba Programming 5 06-23-2006 01:37 PM
help with a script that deletes files more than X days old BrianK Linux - General 5 06-14-2004 09:05 PM


All times are GMT -5. The time now is 04:51 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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration