Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting a previous date in bash/unix

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]
like image 888
misguided Avatar asked Apr 16 '13 02:04

misguided


People also ask

How do I go back to a previous date in bash?

To get yesterday's date in bash shell you can use the Linux GNU date version with date -d yesterday or the Unix/macOS date version using date -j -v -1d .

How do I get the previously executed command?

gives the exit status of the last command executed. After a script terminates, a $? from the command-line gives the exit status of the script, that is, the last command executed in the script, which is, by convention, 0 on success or an integer in the range 1 - 255 on error. #!/bin/bash echo hello echo $?


4 Answers

try this:

date --date="yesterday" +%Y/%m/%d
like image 183
kumarprd Avatar answered Oct 26 '22 06:10

kumarprd


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

like image 29
Dan Pickard Avatar answered Oct 26 '22 06:10

Dan Pickard


dtd="2015-06-19"
yesterday=$( date -d "${dtd} -1 days" +'%Y_%m_%d' )
echo $yesterday;
like image 8
Vladislav Ross Avatar answered Oct 26 '22 06:10

Vladislav Ross


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

like image 5
user3568717 Avatar answered Oct 26 '22 07:10

user3568717