user22
user22

Reputation: 1259

How to find out the date of the last Saturday in Linux shell script or python?

I have python script which i need to run daily for backups. Now i need to find the date of last saturday because i need that in my script to get the backups i did on last saturdy. Suppose

on saturday i made this file

weekly_user1_Jul-13-2013.sql

I ned to get that name in script which i run daily. so for script running on saturday i need to get todays date , buif its on sunday then i need to get last saturday date.

how can i do that

Upvotes: 8

Views: 8444

Answers (3)

dan
dan

Reputation: 1002

On Mac OS it would be:

$ date -v -sat +"%b-%d-%Y"
Mar-28-2015

Upvotes: 1

Sudipta
Sudipta

Reputation: 4971

In python script:

from datetime import date
from datetime import timedelta
today = date.today()
last_saturday = today - timedelta(days= (today.weekday() - 5) % 7)

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

$ date +"%b-%d-%Y" -d "last saturday"
Jul-13-2013

Upvotes: 24

Related Questions