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.,
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?