Calculate specific past date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate specific past date
# 1  
Old 04-20-2016
Calculate specific past date

Hello All,

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

Code:
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.

Code:
abc.sh 14     => is desired script run

abc.sh 14  2016-04-06   => not desired script run..

Code:
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.
# 2  
Old 04-20-2016
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:
Code:
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.,
Code:
slop=$(( $when % 86400 ))
when=$(( $when - $slop  ))

# 3  
Old 04-20-2016
Am i missing something?
Code:
find ./ -mtime +14 -exec foobar {} \;

# 4  
Old 04-20-2016
GNU date available?
Code:
date -d-14day
Mi 6. Apr 16:50:20 CEST 2016

# 5  
Old 04-20-2016
Or (Free)BSD:
Code:
date -jv-14d
Wed Apr  6 16:54:16 CEST 2016

# 6  
Old 04-21-2016
Quote:
Originally Posted by RudiC
Or (Free)BSD:
Code:
date -jv-14d
Wed Apr  6 16:54:16 CEST 2016

No way Rudic, it's not BSD:

Code:
[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]

Code:
[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 ----------

Quote:
Originally Posted by jim mcnamara
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:
Code:
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.,
Code:
slop=$(( $when % 86400 ))
when=$(( $when - $slop  ))


Hello Jim,

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

Code:
[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.

Last edited by EAGL€; 04-22-2016 at 03:28 AM..
# 7  
Old 04-21-2016
Quote:
Originally Posted by EAGL€
No way Rudic, it's not BSD:

... ... ...

Hello Jim,

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

... ... ...
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

2. Shell Programming and Scripting

Touch a file with creation date as 30 min past.

Hi All, I would like to create a file say test.txt. And the requirement is its creation date should be 30 minutes past. For Example : If i am creating a file at 2:00 am means. Creation time should be 01:30 am. likewise if i am creating a file at 5 pm means. Creation date should be... (5 Replies)
Discussion started by: Girish19
5 Replies

3. Shell Programming and Scripting

Seven days past date from current date

hi all.. i want 2 know how 2 find 7days past date from current date.. when i used set datetime = `date '+%m%d%y'` i got 060613.. i just want to know hw to get 053013.. i tried using date functions but couldnt get it :( i use c shell and there is no chance that i can change that ..... (3 Replies)
Discussion started by: Rahul619
3 Replies

4. Linux

date of a specific past day

Can I print the date of a specific date? Let say, today is 20 and I want to print in date format the day 14 I know that I can do this: date --date='last Mon' but How can I do this?: date --date='last 14' ?? (7 Replies)
Discussion started by: kernel_panic
7 Replies

5. Shell Programming and Scripting

Get Previous date of a past date

Hi all, I have a variable where it has a past date value in the format YYYY-MM-DD eg-> pcontromModate="2008-11-31" How can i get the date 1 day before the pcontromModate. The required date is 2008-11-30. ..Plz reply since its an urgent one (2 Replies)
Discussion started by: morbid_angel
2 Replies

6. Shell Programming and Scripting

How to find a date which is 7 days past when given current date

hii all. I have to get the date of the 7th day past from the current date. if i give the current date as sep 3 then i must get the date as 27th of august. can we get the values from the "cal" command. cal | awk '{print $2}' will this type of command work. actually my need is if today is... (17 Replies)
Discussion started by: ladtony
17 Replies

7. Shell Programming and Scripting

Converting past date to seconds

Need a little help developing a ksh script. Have read through Perderabo's datecalc routine and it does not seem to fit the function I am looking for. Basically what I am trying to do is find any file (in a specific directory) that was created within the last five minutes. I am not a programming... (3 Replies)
Discussion started by: synergy_texas
3 Replies

8. Shell Programming and Scripting

Past date display ( today - N) in shell

Hi, Any idea to get display of date - n ( n=1,2,3,4 etc) ? For example , dtoudt will easily execute by dtoudt -3 and result is >dtoudt -3 1234852529 Date 2009-2-17 time 14:35:29 day 47 How to get it display in 20090217 instead ? Maybe you have other suggestion to display date... (0 Replies)
Discussion started by: rauphelhunter
0 Replies

9. Shell Programming and Scripting

calculate the date of next satureday of current date.

I want to calculate the date of next satureday of current date using shell script. Suppose, today is 27-feb-08 I want to get the date of next satureday, which means 01-mar-08, in the formate '' YYMMDD ". I do this in ksh.. Please tell me any type of command which help me out. Thanks in... (3 Replies)
Discussion started by: rinku
3 Replies

10. Shell Programming and Scripting

Get date and time for past 1 hour from current date

Hi, I need to get the date and time for past 1 hour from the current date. Anyone know how to do so? Thanks (5 Replies)
Discussion started by: spch2o
5 Replies
Login or Register to Ask a Question