Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to get Previous date of reference file.

What I've tried :

[rahul@testsrv]$ date +%F -r /tmp/ftpbkp.log
2013-08-27

[rahul@testsrv]$ date +%F -r /tmp/ftpbkp.log -d "1 day ago"
date: the options to specify dates for printing are mutually exclusive
Try `date --help' for more information.
share|improve this question
add comment

4 Answers

up vote 4 down vote accepted

A note of warning:

$ date -r ~/a
Sun 28 Oct 23:12:00 GMT 2012
$ LC_ALL=C date -r ~/a
Sun Oct 28 23:12:00 GMT 2012

As output, date outputs the date in the user's local format. As input to -d GNU date is more picky on the format though:

$ date -d "$(date -r ~/a) - 1 day"
date: invalid date ‘Sun 28 Oct 23:12:00 GMT 2012 - 1 day’

Fixing locale to C fixes that issue:

$ export LC_ALL=C
$ date -d "$(date -r ~/a) - 1 day"
Sun Oct 28 00:12:00 BST 2012

But note how the date is still 2012-10-28, though now in summer time. That's because 24 hours before that date in Britain, we were still the same day.

Now, if you want the day before, you'd have to write it:

date -d "$(date -r /tmp/file.ref +'%F -1 day')" +%F
share|improve this answer
add comment
LC_ALL=C date -d "$(LC_ALL=C date -r /tmp/ftpbkp.log) - 1 day" +%F

If GNU date is not available:

perl -MPOSIX -le'
  print strftime("%Y-%m-%d", localtime +(stat shift)[9] - 24*60*60)
  ' /tmp/ftpbkp.log
share|improve this answer
    
yes.. after lots of tried, Just I also found the same , thanks –  Rahul Patil Aug 28 '13 at 13:20
3  
That will not work in all locales, even English locales (you may want to call both dates with LC_ALL=C). That may not work as you want around daylight saving time change. –  Stephane Chazelas Aug 28 '13 at 13:38
    
@StephaneChazelas, as usual, thanks for your valuable comments! –  Dimitre Radoulov Aug 28 '13 at 13:44
    
Thanks Sir, If I use LC_ALL=C at top in script, is that enough ? actually I didn't understood why use LC_ALL=C. paste.ubuntu.com/6036642 –  Rahul Patil Aug 28 '13 at 13:44
    
Note that the perl one will also be affected by DST change. –  Stephane Chazelas Aug 28 '13 at 14:30
add comment
date -r /tmp/ftpbkp.log +%s | awk '{print strftime("%F", $1 - (24*60*60) );}'
share|improve this answer
add comment

The -d option is used for specifying a different time than the current time. You asked date command to format the last modification time of file /tmp/ftpbkp.log according the format %F and additionally you passed it another time specified by -d option. These option are mutually exclusive. But there is a trick how to combine it:

date -d "`date -r /tmp/ftpbkp.log`" - 1 day"
share|improve this answer
1  
The backticks are deprecated, use $() instead. Also what else does this add beyond what Dimitre's answer provided? –  slm Aug 28 '13 at 13:24
    
I wrote it at the same time so basically nothing :-) –  dsmsk80 Aug 28 '13 at 13:28
    
@slm, good comment up there. You should use $() command substitution all the time since it offers more flexibility in nesting commands and avoiding entering sometimes unnecessary " " double quotes or ' ' single quotes –  val0x00ff Aug 28 '13 at 13:45
    
@val0x00ff, would you care to expand how $() avoids entering quotes? –  Stephane Chazelas Aug 28 '13 at 14:33
    
@StephaneChazelas I am not saying it avoids entering quotes. What I am saying is: When using backticks ```` You need to use more quotes to escape other quotes which will make a line of code illegible. So nested quoting inside $() is far more convenient. Compare these two x=$(grep "$(dirname "$somepathpath")" file) x=grep "`dirname "$somepath"`" file` mywiki.wooledge.org/BashFAQ/082 –  val0x00ff Aug 28 '13 at 14:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.