6

I am looking to get previous date in unix / shell script .

I am using the following code

date -d ’1 day ago’ +’%Y/%m/%d’

But I am getting the following error.

date: illegal option -- d

As far as I've read on the inetrnet , it basically means I am using a older version of GNU. Can anyone please help with this.

Further Info

unix> uname -a

SunOS Server 5.10 Generic_147440-19 sun4v sparc SUNW,Sun-Fire-T200

Also The below command gives an error.

unix> date --version

date: illegal option -- version
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
devnull
  • 103,635
  • 29
  • 207
  • 208
misguided
  • 3,519
  • 18
  • 47
  • 88
  • 3
    if you're using a true unix, many don't support the `date -d` option. Consider editing your question to include the output of `uname -a` and `date --version`. Good luck. – shellter Apr 16 '13 at 02:33
  • Nope Unix , `SunOS 5.10 Generic_147440-19 sun4v sparc SUNW,Sun-Fire-T200System = SunOS` – misguided Apr 16 '13 at 02:35
  • +1 to shellter, an alternative being date -h and/or man date to see if your system supports the -d option. – Cargo23 Apr 16 '13 at 02:35
  • 1
    You might check if you have `gdate` (GNU date) installed. – FatalError Apr 16 '13 at 02:37
  • @FatalError Sorry , I'm a noob at shell scrpting. How do I check this? If I run this coomand directly on unix I get `ksh: gdate: not found` – misguided Apr 16 '13 at 02:52
  • @misguided: You had the right idea. You probably don't have it, so as mentioned by others you'll need to use another option or solution overall. – FatalError Apr 16 '13 at 02:59
  • Note that this is not about Linux. For Linux / GNU `date` maybe see https://stackoverflow.com/questions/15374752/get-yesterdays-date-in-bash-on-linux-dst-safe – tripleee Sep 23 '19 at 05:12

12 Answers12

16

try this:

date --date="yesterday" +%Y/%m/%d
kumarprd
  • 808
  • 2
  • 8
  • 19
  • 1
    I am using this in my shell script and working fine for me in all my shells. Have you tested it ?? – kumarprd Apr 16 '13 at 11:01
  • Then the question's code will also have worked for you, and you are not on SunOS. – tripleee Apr 16 '13 at 12:45
  • Yes that also worked. I am not using SunOS, that's why I asked to try. – kumarprd Apr 18 '13 at 03:04
  • This worked for me in Solaris 11.3, but using "gdate" instead of "date". – lamcro Dec 27 '18 at 21:25
  • search engines are sending us here for asking how to find yesterday's date in linux. @tripleee pls specify "SunOS" in your question because your use case doesn't apply to generic bash/linux where this seems to work just fine. – Nikhil VJ Sep 23 '19 at 04:37
  • @NikhilVJ Not my question and you should be able to suggest an edit to the tags yourself. But the question is already tagged [tag:solaris]. How `date` behaves is not a function of your shell (Bash) or OS (Linux), just the `date` command (though Linux typically ships with GNU `date`). – tripleee Sep 23 '19 at 05:09
  • yikes, sorry :P – Nikhil VJ Sep 23 '19 at 05:38
7
dtd="2015-06-19"
yesterday=$( date -d "${dtd} -1 days" +'%Y_%m_%d' )
echo $yesterday;
Vladislav Ross
  • 471
  • 4
  • 6
6

you can use

date -d "30 days ago" +"%d/%m/%Y"

to get the date from 30 days ago, similarly you can replace 30 with x amount of days

Dan Pickard
  • 293
  • 5
  • 13
4

Several solutions suggested here assume GNU coreutils being present on the system. The following should work on Solaris:

TZ=GMT+24 date +’%Y/%m/%d’
devnull
  • 103,635
  • 29
  • 207
  • 208
  • brilliant answer. WOrks perfectly. Only query I have is , is this specific to any timezone? I live in a GMT+10 timezone, does that I mean i should upadte the qury accordingly? – misguided Apr 16 '13 at 06:16
  • 1
    If your time zone is GMT+10, use `TZ=GMT+14 date +’%Y/%m/%d’`. – devnull Apr 16 '13 at 06:36
  • ... Which again makes this answer unsuitable for a portable solution; but if it works for you, it's arguable the simplest. – tripleee Apr 16 '13 at 08:08
  • @devnull Thanks. Just want to understand the logic , if it is GMT +10 , why am I using GMT+14 in the equation? – misguided Apr 16 '13 at 08:16
  • Because 10+14=24. Consider what would happen if you run it at 01:00 local time and just blindly use GMT+24. (Or am I counting it the wrong way now? If I got it wrong, consider 23:00 instead :-) – tripleee Apr 16 '13 at 08:33
  • 1
    Another option is to say: `TZ=$TZ+24 date +’%Y/%m/%d’` – devnull Apr 16 '13 at 08:42
  • I'm assuming that would again be `TZ=$TZ+14 date +’%Y/%m/%d’` for me? – misguided Apr 16 '13 at 09:57
  • Is this independent of timezone? – misguided Apr 16 '13 at 10:50
  • This $TZ+24 approach doesn't work as really intended: The date returned is indeed 24 hours in the past, but it's in GMT, which might not be the right date. It's even weirder if your TZ is something like America/Los_Angeles or US/Pacific. – schnitz Jan 16 '14 at 00:55
3

In order to get 1 day back date using date command:

date -v -1d It will give (current date -1) means 1 day before .

date -v +1d This will give (current date +1) means 1 day after.

Similarly below written code can be used in place of d to find out year,month etc

y-Year, m-Month w-Week d-Day H-Hour M-Minute
S-Second

Joel
  • 6,735
  • 4
  • 46
  • 54
2

the following script prints the previous date to the targetDate (specified Date or given date)

targetDate=2014-06-30
count=1
startDate=$( echo `date -d "${targetDate} -${count} days" +"%Y-%m-%d"`)
echo $startDate
UpAndAdam
  • 4,155
  • 2
  • 25
  • 41
1

SunOS ships with legacy BSD userland tools which often lack the expected modern options. See if you can get the XPG add-on (it's something like /usr/xpg4/bin/date) or install the GNU coreutils package if you can.

In the meantime, you might need to write your own simple date handling script. There are many examples on the net e.g. in Perl. E.g. this one:

vnix$ perl -MPOSIX=strftime -le 'print strftime("%Y%m", localtime(time-86400))'
201304

(Slightly adapted, if you compare to the one behind the link.)

tripleee
  • 139,311
  • 24
  • 207
  • 268
0

I have used the following workaround to get to the required solution .

timeA=$(date +%Y%m)
sysD=$(date +%d)
print "Initial sysD $sysD">>LogPrint.log
sysD=$((sysD-1))
print "Final sysD $sysD">>LogPrint.log
finalTime=$timeA$sysD

I hope this is useful for people who are facing the same issue as me.

misguided
  • 3,519
  • 18
  • 47
  • 88
  • Of course, in January, 201401 minus one is 201400, not 201312. – tripleee Apr 16 '13 at 04:55
  • @tripleee agree that this is not a foolproof workaround.But at this moment I am not able to find any other solutions :(. I am still looking for a better answer. – misguided Apr 16 '13 at 05:29
0
$ date '+%m/%d/%Y' --- current date


$ TZ=Etc/GMT+24 date  '+%m/%d/%Y'  -- one dayprevious date

Use time zone appropriately

Nazik
  • 8,393
  • 26
  • 72
  • 115
0

Try This: gdate -d "1 day ago" +"%Y/%m/%d"

venki
  • 31
  • 2
0
date -d "yesterday" '+%Y-%m-%d'
showdev
  • 25,529
  • 35
  • 47
  • 67
suresh Palemoni
  • 824
  • 6
  • 12
-1

Problem

You are using backticks, rather than single quotes, for your arguments. You may also not be using GNU date, or a version of date that supports the flag you are using.

Solution

Quote your arguments properly. For example:

$ date -d '1 day ago' +'%Y/%m/%d'
2013/04/14
Todd A. Jacobs
  • 71,673
  • 14
  • 128
  • 179