Shell script to print out YESTERDAY's date: MMDDYYYY

Posted on 2008-11-12
Last Modified: 2013-12-26
Hello all,

I have a cron job that will be renaming some mysql tables daily to back them up..

I wanted to do this from a shell script but I'm not sure how to gate 'date' to show YESTERDAYS date (since the script is run at midnight)

YESTERDAY=$(date +%m%d%Y)   - todays date just fine heh

Question by:mcainc
    LVL 48

    Accepted Solution

    If you have GNU date (standard on all Linux systems), you can simply do

    YESTERDAY=$(date --date yesterday +%m%d%Y)
    LVL 7

    Expert Comment

    Same as above but:
    YESTERDAY=$(date --date='1 day ago' +%m%d%Y)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
    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 …
    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…
    This video discusses moving either the default database or any database to a new volume.

    779 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

    13 Experts available now in Live!

    Get 1:1 Help Now