Recently I had to create a bash script to copy some files based on date.

The rule was copy yesterday’s files to …

But How to determine what was the yesterdays date?

The answer was in date man page.

Here are the parts from the script related to yesterday date:

$date -d '1 day ago' +'%Y/%m/%d'

on Internet I’ve found also:

date --date='yesterday'
Thu Jun  2 14:01:28 EDT 2011

And (Bonus!!) if you want to go in future 😉

date --date='tomorrow'
Sat Jun  4 14:04:29 EDT 2011

Happy scripting…


2 Responses to “Bash: Get yesterday’s date”

  1. sh2515 on January 25th, 2012 2:13 am

    how did you run a script against the above to check if files were of yesterdays date to enable you to do whatever to them? In my case I need to check files on server under subfolders and copy yesterdays files only to another server folder.

  2. hb on January 25th, 2012 9:13 am

    In your case you do not need the command above.

    find . -daystart -atime 1

    will find any file in current filder modified yesterday, but less than 24h ago.

    Or you can use rsync to mittor daily the files.

Leave a Reply

You must be logged in to post a comment.