How To Get Yesterday's Date using BASH Shell Scripting

The new short way:
$ date -d '1 day ago' +'%Y/%m/%d'
2009/07/21

Or the longer way:
Yesterday in epoch seconds
$ yesterday=$((`date +'%s'` - 86400))

Get default formatted yesterday's date
$ date -d "1970-01-01 $yesterday sec"
Tue Feb 17 01:27:32 PST 2009

Same thing in YY-MM-DD
$ date -d "1970-01-01 $yesterday sec" +"%Y-%m-%d"
2009-02-17

This entry was posted in Bash, Linux. Bookmark the permalink.

2 Responses to How To Get Yesterday's Date using BASH Shell Scripting

  1. Yo says:

    It can be done like this, if you prefer a one-liner:

    date -d @`echo $((\`date +%s\` – 86400))` +”%Y-%m-%d”

  2. xeno says:

    With newer versions of GNU date you may also write:

    date -d ’1 day ago’

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>