Calculate specific past date

Hello All,

I use date like the following in one of my scripts:

date=`TZ=GMT+2 date +%Y-%m-%d`
 => gives  2016-04-20 

I want to use just one argument to my script such as number of days which represents "how old the found files are" such as "14" means I need to find 14 days old files to process.. Until here it is ok works smoothly..
But when I use this number as argument I need to calculate the past date too not to ask customer to calculate it and give two arguments to the script.

abc.sh 14     => is desired script run

abc.sh 14  2016-04-06   => not desired script run..
So I need to calcualte 2016-04-06 by using  "14"

I have found some yerterday's or tomorrow's date but not spesific one.. Any help appreciated.

I think you are using the wrong tool. You appear to be using gnu coreutils, the "stat" command is available.

Filetimes (ctime, mtime, and atime plus and optional file "birth time") are kept in epoch seconds.
There are 86400 seconds in one day.
stat -c "%X filename" returns that value for mtime for the file. mtime is the last time the file was written.

So there are lot of ways to do this. One way:

z=$(( 16 * 86400 ))
when=$((  `date +%s` - $z  ))  ## epoch seconds 16 days ago
for f in path/to/files/*
do
    check=$((  stat -c "%X" $f ))
    if [ $check -le $ when ]; then
       ls -l $f
    fi
done

The above assumes 86400 * 16 seconds from RIGHT NOW. If you want the entire day starting from midnight then you have to subtract the extra seconds after midnight
e.g.,

slop=$(( $when % 86400 ))
when=$(( $when - $slop  ))

Am i missing something?

find ./ -mtime +14 -exec foobar {} \;

GNU date available?

date -d-14day
Mi 6. Apr 16:50:20 CEST 2016

Or (Free)BSD:

date -jv-14d
Wed Apr  6 16:54:16 CEST 2016

No way Rudic, it's not BSD:

[sscm@testvm3:/export/home/sscm/ ] $ date -d-14day
date: illegal option -- d
date: illegal option -- -
date: illegal option -- 1
date: illegal option -- 4
date: illegal option -- d
date: invalid argument -- y
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
[sscm@testvm3:/export/home/sscm/ ] $ date -jv-14d
date: illegal option -- j
date: illegal option -- v
date: illegal option -- -
date: illegal option -- 1
date: illegal option -- 4
date: illegal option -- d
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
[sscm@testvm3:/export/home/sscm/ ] $ uname -a
SunOS testvm3 5.10 Generic_147148-26 i86pc i386 i86pc

---------- Post updated at 03:01 PM ---------- Previous update was at 02:52 PM ----------

Hello Jim,

Thanks for your effort, bu unfortunately its SunOS 5.10 and no stat command available:

[sscm@testvm3:/export/home/sscm/ ] $ stat
-bash: stat: command not found
[sscm@testvm3:/export/home/sscm/ ] $ find / -type f -name "stat" 2>/dev/null
[sscm@testvm3:/export/home/sscm/ ] $ 

It's my bad not to answer about the GNU utils, sorry Rudic. We dont have GNU utilities installed on the site.

If you would tell us about your environment when you start a thread, volunteers trying to help you wouldn't waste their time and your time making suggestions that won't work in your environment.

Yelling at RudiC about a suggestion that didn't work in your environment when you hadn't told the readers of this thread about your environment is rude and obnoxious.

Now that we know you're using a Solaris 5/10 system, the next question is: Have you installed the GNU core utilities?

For AIX and Solaris I find this function useful to determine a files age in seconds. Perl should be available by default.

function age_seconds()
{
    fs=`perl -e 'use File::stat; print stat($ARGV[0])->mtime;' "$1"`
    cs=`date +%s`
    let age=cs-fs
    echo $age
}

You could then do:

z=$(( 14 * 86400 ))
for f in path/to/files/*
do
    check=$((  age_seconds "$f" ))
    if [ $check -le $z ]; then
       ls -l "$f"
    fi
done