I am writing a script a check whether exist a backup file every morning Monday to Friday. These backup file is saved in the end of everyday from Monday to Friday only named as


There is a problem that if I run my script on Monday said


It won't find out the file because the file name is


rather than yesterday


Please find my date code below,

#Create variables
yday=$(date --date yesterday +"%d_%m_%y")
FileName=$(date --date yesterday +"%Y%m%d")

How can I get the date for Monday special for last Friday.

2 Answers 2

unset mon
[ $(date +%u) -eq 1 ] && mon='Fri'
yday=$(date --date="last ${mon}day" +"%d_%m_%y")

So if it is Monday date +%u produce 1 so mon variable will set to Fri and --date's string will be last Friday. Otherways it will be last day as mon remain unset(empty)

  • Could you please elaborate on how your snippet works?
    – Dubu
    Nov 9, 2015 at 10:18
  • Thank you @Costas . Your idea is pretty good but your the second line didn't work for me. I have changed them a little bit. Please find the code below: if test $(date +"%u") -eq 1 then mon='Fri' fi yday=$(date --date="last ${mon}day" +"%d_%m_%y")
    – Alan Yu
    Nov 9, 2015 at 10:51

According to the info page, GNU date accepts other forms which you can use. Here is an example:

FileName=$(date --date '1 day ago' +"%Y%m%d")
FileName=$(date --date 'last Friday' +"%Y%m%d")

However, it does not appear to support variants such as "last weekday" or "last day of week". If your script has to run only on week-days (Monday through Friday), or take account of holidays, it will need more than a single line to accomplish this.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .