How to get last Week's date in bash SunOS - bash

Here is my issue: I have a backup bash script that needs to access a folder with a date in its name for example : backup_01072022 .
I used date=`TZ=GMT+24 date +%d%m%Y` when i needed to access the backup folder of yesterday.
Now I want to access the backup folder of last week :
date=`TZ=GMT+168 date +%d%m%Y` , it doesn't work , it show today's date.
I read that TZ doesn't work for a value above +144.
Is there any other way of manipulating dates in SunOS 6.8 ?
Notes :
SunOS 6.8
version of the date util : 8.5
version of bash : 4.1.11(2)-release

This'll depend on the version of date on your system.
With GNU date (v 8.26):
$ TZ=GMT date '+%d%m%Y'
06072022 # today
$ TZ=GMT date '+%d%m%Y' -d 'last week'
29062022
$ TZ=GMT date '+%d%m%Y' -d '7 days ago'
29062022
NOTE: I'll leave it up to OP to determine if the explicit TZ setting should be adjusted (or used at all)

I seem to recall SunOS comes with Perl, so if you don't have a date that supports --date="...", you should be able to do:
date=$(perl -MPOSIX -e '
print POSIX::strftime "%d%m%Y", localtime time-(60*60*24*7)
')

Thanks to the helpful commments and answers I was able to make it work using :
/usr/gnu/bin/date -d "last week" '+%d%m%Y'
It turns out I was not using the GNU date util until I specified it explicitly, and that's neither --date nor -d was working for me.
I still can't figure out what date util I was using by default if not GNU date.

Related

Unix shell scripting: date format syntax

I m trying to get yesterday date, it's not working in hp ux server.
Prev_date=$(date +"y%m%d" -d "1 day ago")
For this I m still getting current date only.
20210811
Could you please help on the same.
You missed a percentage in front of the 'y': this is working fine for me:
echo $(date +"%y%m%d" -d "1 day ago")
You can use below command, if you want.
date --date=' 1 days ago' '+%Y-%m-%d'
It will give result like
2021-09-06
I prefer this format since most of the time my scripts include SQL queries for data fetch and hence date is required to filter out data on daily basis.

Why do I always get extra one day with date difference in bash?

I'm trying to calculate the seconds between a date from now, however, I always get extra one day when adding the seconds from now.
echo $(($(date -ud "2020-03-15 19:13" +'%s') - $(date +'%s')))
As of posting, the result is 1744204
Using this website to check, I gets 16 March, not 15 March as expected. Any idea why?
verify the result of your date commands and verify the timezone of both.
The first result of the command shows the timestamp in UTC, and the second one shows the timestamp using the timezone of the system.
Here is the difference:
$ date -ud "2020-03-15 19:13" +'%s'
1584299580
With UTC-3 in my system:
$ date -d "2020-03-15 19:13" +'%s'
1584310380
I hope that help you.

How subtract days from date in bash shell on Solaris 11?

I've been trying subtrac some days $days from a date $date with format yyyy-MM-dd, but nothing has worked on Solaris 11. Some solution is a 'trick' with the timezone, but it depends on the timezone and I think that is exactly that, a trick.
I would like a cheaper solution, because the only thing I can think is to convert the date to julian representation and then subtract one day and again obtain yyyy-MM-dd representation, for example:
date=2000-12-31
days=1
julian=$(toJulian $date)
resultJulian=$(subtractDays $julian $days)
resultGregorian=toGregorian $resultJulian
So, how can I do it without all this proccess? Thanks.
If you don't have GNU date or GNU awk, consider perl:
subtractDays() {
local date numDays
date=$1
numDays=$2
date=$date days=$numDays perl -e '
use Env qw(date days);
use Time::Piece;
use Time::Seconds;
my $start_time = Time::Piece->strptime($date, "%Y-%m-%d");
my $end_time = $start_time - (ONE_DAY * $days);
print $end_time->ymd . "\n";'
}
...thereafter:
subtractDays 2000-12-31 1
...emits...
2000-12-30
Here's the solution for Solaris 11:
#!/bin/bash
mydate="2016-08-01"
days=3
date_in_the_past=$(gdate -d "${mydate} - ${days} days")
You may wonder why I use gdate when you say it is not available?
Well, GNU Date is in fact available by default on a Solaris 11 install. It is accessed by the gdate command (full path is /usr/bin/gdate).
Some background: GNU Date is part of "GNU Coreutils" package and this package gets installed on any Solaris 11 server unless your customer has actively selected to exclude it. I doubt that is the case.
So gdate is there somewhere, but you say you can't find it? The reason is probably that you are in a local zone, not the global zone. This particular package doesn't get propagated by default into local zones when they are created. This is done in a (misguided, if you ask me) effort to save disk space. This difference between global zones and local zones is somewhat unknown to many Solaris admins.
When you explain to Solaris admins that in fact the binary already physically reside on the disk then it suddenly becomes more acceptable for them to execute the command required:
from the local zone:
pkg install file/gnu-coreutils
The above command doesn't actually go outside the server. It doesn't fetch the package from a remote package repository. The command will work even if you execute it when the server is detached from any network because the package is already there on the disk. Once you explain that to your Solaris admin he's typically okay with executing the command.

Bash Shell Current Date Minus Number of Days

I am new to bash and shell but I am running a debian install and I am trying to make a script which can find a date in the past without having to install any additional packages. From tutorials I have got to this stage:
#!/bin/sh
#
# BACKUP DB TO S3
#
# VARIABLES
TYPE="DATABASE"
DAYS="30"
# GET CURRENT DATETIME
CURRENTDATE="$(date +%Y%m%d%H%M%S)"
# GENERATE PAST DATE FROM DAYS CONTSTANT
OLDERDATE=`expr $CURRENTDATE - $DAYS'
# CALL PYTHON SCRIPT WITH OLDERDATE ARGUMENT
python script.py $OLDERDATE
Where I am getting stuck is the fact that my "days" is just the number 30 and isnt datetime formattted, so when I come to minus it from the currentdate variable it obviously isnt compatible.
Would anyone be kind enough to help me find a way to get this working as it should?
Try
date -d '30 days ago'
should do on debian.
Try doing this :
#!/bin/sh
#
# BACKUP DB TO S3
#
# VARIABLES
TYPE="DATABASE"
DAYS="30"
# GET CURRENT DATETIME
CURRENTDATE="$(date +%Y%m%d%H%M%S)"
# GENERATE PAST DATE FROM DAYS CONSTANT
OLDERDATE="$(date "+%Y%m%d%H%M%S" -d "$DAYS days ago")"
# CALL PYTHON SCRIPT WITH OLDERDATE ARGUMENT
python script.py "$OLDERDATE"
See info coreutils 'date invocation' | less +/28.7\ Relative\ items\ in\ date\ strings
You can use the following script:
#!/bin/bash
days=73
while [ ${days} -ge 0 ]; do
date -d "${days} days ago" +'%F'
days=$((days-1))
done
You could modify the python script instead -- that way you would not depend on particular implementation of date

Subtracting time from file creation/modification date in OSX

I am trying to shift the dates of a series of files by 9 hours. I've reached as far as this:
for i in *.MOV; do touch -r "$i" -d "-9 hours" "$i"; done
This should work in recent systems, but the touch command in OSX seems to be a bit outdated and not to support the -d switch.
I'm using Snow Leopard. Any idea on the best option for doing this with a single line command? I don't want to create a script for this.
Ok, sorted it out. OSX comes with a gtouch command, that knows the -d switch. It's part of GNU coreutils. See the comments below for information regarding availability on specific MacOS versions.
For more information on using relative dates with the -d switch see the manual.
Looking at the Wikipedia Page for Touch, it appears you're accustomed to the GNU version of Touch. Which MacOS isn't using.
For what you want to do, look into the "SetFile" command, which gets installed with XCode tools. You have -d and -m options, which reset the Created and Modified dates & times respectively.
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/SetFile.1.html
Donno OS X, but it should be easy enough to
get curr time stamp on the file
convert it to seconds
subtract 9 hours (9*60*60 secs) from it
convert it back to the format accepted by touch's -t option
run touch command
All this of course can be done in a single for loop on command line.
Here are simple examples from WikiPedia showing back and forth conversion.
# To convert a specific time stamp to Unix epoch time (seconds since 1970-01-01):
date +"%s" -d "Fri Apr 24 13:14:39 CDT 2009"
# 1240596879
# To convert Unix epoch time (seconds since 1970-01-01) to a human readable format:
date -d "UTC 1970-01-01 1240596879 secs"
# Fri Apr 24 13:14:39 CDT 2009
# Or:
date -ud #1000000000
# Sun Sep 9 01:46:40 UTC 2001
# or: Haven't tested this but should work..
date -d #1000000000 +%y%m%d%%H%M%S
# 010909014640

Resources