Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 692
user22 Avatar asked Jul 15 '13 07:07

user22


People also ask

How do I get the last week Sunday date in python?

Use the now() method to get the current date and time. Or, If you have datetime in a string format, refer to converting a string into a datetime object. The weekday() method returns the day of the week as an integer, where Monday is 0 and Sunday is 6. For example, the date(2022, 05, 02) is a Monday.

How do you display 2 days back date in dd mm yyyy format in Linux?

To format date in DD-MM-YYYY format, use the command date +%d-%m-%Y or printf "%(%d-%m-%Y)T\n" $EPOCHSECONDS .

How do I get last Friday in Unix?

I used below code to find out yesterday and last friday 's date. It was working in server1. YESTERDAY=$(date --date="yesterday" +"%m%d%Y") echo YESTERDAY: $YESTERDAY; LASTFRIDAY=$(date --date='last Friday' +"%m%d%Y") echo LAST FRIDAY: $LASTFRIDAY; I need to move the script to Server2 where it gives below error.


2 Answers

$ date +"%b-%d-%Y" -d "last saturday"
Jul-13-2013
like image 109
Ignacio Vazquez-Abrams Avatar answered Nov 09 '22 01:11

Ignacio Vazquez-Abrams


In python script:

from datetime import date
from datetime import timedelta
today = date.today()
last_saturday = today - timedelta(days= (today.weekday() - 5) % 7)
like image 28
Sudipta Avatar answered Nov 09 '22 01:11

Sudipta