Shell Script , 2 days back date required.

Posted on 2011-10-14
Last Modified: 2012-05-12
Need day before yesterday's date from the system though shell command.

I have one for yesterday , but i am looking for day before yesterday.

DATE=$(date -d yesterday +"%Y%m%d")

$ export YD=$(date --date yesterday +%Y%m%d)
$ date
Fri Oct 14 15:08:28 EDT 2011
$ export YD=$(date --date yesterday +%Y%m%d)
$ echo $YD

I am looking for 20111012
Question by:ratna1234
    LVL 6

    Accepted Solution

    export YD=$(date --date '2 days ago'  +%Y%m%d)

    LVL 23

    Expert Comment

    date -d "2 days ago" +%Y%m%d

    Open in new window

    LVL 2

    Expert Comment

    Worth noting that the -d option to date is not available on all systems (solaris for example).

    Try this when not available:

    perl -e 'print scalar localtime(time - 86400 * 2)'


    Author Closing Comment


    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
    Recently, an awarded photographer, Selina De Maeyer (, completed a photo shoot of a beautiful event ( in An…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now