I want to store yesterday's date in BASH variable to search for yesterday's files with that variable in the file-name wildcard search. I am using the following format for New York City, NY, USA (EST) time zone, and wanted to know whether it is guaranteed to fetch yesterday's date from the system date; else I can make further changes to the variable.
yesterday=$(TZ=GMT+28 date +%Y%m%d)
...
for file in $HOME_DIR/*$yesterday*.txt;
...
The text filename in HOME_DIR would be as follows:
"ABC_20171011064612.txt"
update 1: Attempt for removing daylight savings related issues:
yesterday=$(echo -e "$(TZ=GMT+28 date +%Y%m%d)\n$(TZ=GMT+18 date +%Y%m%d)"|grep -v $(date +%Y%m%d)|sort|tail -1)
1) Convert two dates to string, 24 hours and 14 (picked arbitrarily to be less than 24 hours) hours before today's date
2) Filter for dates that are not today's date
3) Sort strings from 2) in ascending order
4) Assign yesterday variable to last tail -1 entry of the list
It may not be always right due to DST, although it will not be a big issue.
You could rather say:
yesterday=$(date -d yesterday +%Y%m%d)
You attempted
yesterday=$(echo -e "$(TZ=GMT+28 date +%Y%m%d)\n$(TZ=GMT+18 date +%Y%m%d)|
grep -v $(date +%Y%m%d)|sort|tail -1)
I think it worked.
Related
Given a UNIX date variable, I need to construct a new date adding a certain number of hours (must be variable) to the initial date. I've looked at this post, which explains how to do this with the current date. For example with 1 hour, date -d '+1 hour' '+%F %T' returns the time it will be in exactly one hour. However, I need to do this with a date variable, not with the current date.
I've tried messing around with the -d flag, but if I set the date to another date variable, I can't figure out how to change it again (such as adding another X number of hours).
Is there a good way to do this? Am I on the right track with the -d flag or is there a better way?
Thanks!
You can use:
# your date variable
dt=$(date -d '2016-08-15 11:10:15')
# add 1 hour to $dt now
date -d "$dt +1 hour"
Mon Aug 15 12:10:15 EDT 2016
I want to pull some information from a website from past 4 years and each file is date based, like http://ransompull.com/pullme/2013-04-06/example.2013-04-06.txt
and it is the starting file and it ends today, so i want to pull all the txt files from last 4 years.
What I tried:
DATE=`date +%Y`
MONTH='01'
DAY='1'
for i in range(1,31);
for j in range(01,12):
do wget http://ransompull.com/pullme/$DATE$i/example.$DATE$i.txt;
done
done
But this seems to wrong as iterating over month and date is not feasible as it is not giving desired output.Any suggestions on how to pull all data from
http://ransompull.com/pullme/2013-04-06/example.2013-04-06.txt
to
http://ransompull.com/pullme/2017-08-10/example.2017-08-10.txt
Instead of counting years, months and days,
you could just count days relative to the start date.
If you have the GNU implementation of the date command,
you can use it to compute the relative date, for example:
date +%F -d '2013-04-06 + 1000 days'
This outputs 2016-01-01.
You can create a loop, generating dates by incrementing the number of days from start, until you reach the end:
start=2013-04-06
end=2017-08-10
date=$start
days=0
while [ "$date" != "$end" ]; do
date=$(date +%F -d "$start + $days days")
wget http://ransompull.com/pullme/$date/example.$date.txt
((days++))
done
try this:
$startdate=get-date 2017-08-11
$enddate=$startdate.AddYears(-4)
0..($startdate - $enddate).Days | %{wget ("http://ransompull.com/pullme/{0:yyyy-MM-dd}/example.{0:yyyy-MM-dd}.txt" -f $startdate.AddDays(-$_))}
I am new to shell scripting..
I want a script to get any date as a input from user and print date of 3 days back?
example:
If user enters date as 2013-01-01
then output should be
2012-12-29.
If you have GNU date, then this will work:
user_date=2013-01-01
date +%Y-%m-%d -d "$user_date - 3 days"
With BSD date, you'd have to do like this:
user_date=2013-01-01
date -j -v -3d +%Y-%m-%d -d "${user_date//-}0000"
because BSD date needs date to be in the format YYYYmmddHHMM.
I don't have a Solaris now to test there. If you're in Solaris then hopefully there is gdate, and you can use the first option, just replace the date command with gdate.
Whichever OS you are in, there are two important points:
In what format can you pass dates to the date command. I tested that GNU date can accept YYYY-mm-dd format (and probably many others), while BSD needs YYYYmmddHHMM.
In what format can you ask for a difference. With GNU date simply DATE - 3 days works, with BSD date it's trickier with -j -v -3d flags.
man date of your system should help you get through these hurdles. In the worst case, you could do all the date operations you need in perl or similar.
You can just do:
date --date="3 days ago"
to get get date of 3 days back.
If i give the date "20130828"(not a current date) in YYYYMMDD format, how can i get 10 days back date using shell script i.e. 20130818
Thanks in advance!
Try
date +%Y%m%d --date="20130818 -10 day"
or even
date +%Y%m%d --date="20130818 10 days ago"
+%Y%m%d is the format of your date (YYYYmmdd), and through --date you can provide a string (in a very human readable format) to specify when you want this date.
The following:
date --date="20130828 - 10 days" +"%Y%m%d"
Outputs
20130818
if you have gnu date, you can just give 10 days ago to date's -d option:
kent$ date -d'10 days ago 20130828' +%Y%m%d
20130818
At work all the days config files are generated fresh and appended with a
session number. The company went public on Feb 16, and the 86400 is seconds
in one day. The session number is generated by subtracting the company start
day from seconds_since_last_day and adding a few zero's
That is the key to interacting with the days config files. I get this - However I do not
understand the
date -ud "$distance days ago 00:00:00".
Is it the number of seconds since 1970?
if $session; then
# return the session of the last day
seconds_since_day_one=`date -ud "Feb 16 2002" +"%s"`
seconds_since_last_day=`date -ud "$distance days ago 00:00:00" +"%s"`
days_between=`printf "%010d" $(( (seconds_since_last_day - seconds_since_day_one) / 86400 ))`
# Truncate on the left to 9 bytes to leave room
# to append the engine suffix for your environment
echo $days_between | awk '{l=length($1); print substr( $1, (l-8), l )}'
date -ud "$distance days ago 00:00:00" in itself just prints the date a certain amount of days ago in a quite readable format, but when you add the FORMAT string to control the output +"%s" does indeed mean the number in so called Unix Time (number of seconds since 1970-01-01 00:00:00 UTC).
If the variable $distance is set to a number it shows the date that number of days ago, if its set to 0 it means today, 1 it means yesterday, 2 the day before yesterday and so on. To better understand these formats and relative keywords there are rather good documentations in (amongst other places) the GNU coreutils package.
Check these URLs:
http://www.gnu.org/software/coreutils/manual/html_node/Relative-items-in-date-strings.html#Relative-items-in-date-strings
http://www.gnu.org/software/coreutils/manual/html_node/Date-input-formats.html
http://www.gnu.org/software/coreutils/manual/html_node/date-invocation.html#date-invocation
Wikipedia explanation of Unix Time:
http://en.wikipedia.org/wiki/Unix_time
The option -d to date provides a generic string to obtain the date.
So, for example, date -d yesterday will print yesterday's date, and date -d 'yesterday 12:00 AM' will print yesterday's date with the time set to 12:00 AM.
So, date -d 6 days ago 00:00:00 will print the date from 6 days ago, with the time set to 00:00:00. I hope it answers your question.
The format +"%s" tells date to print the number of seconds from 1970, instead the date.
mktime and strftime in awk can be used to get the date of the time.
http://www.gnu.org/software/gawk/manual/html_node/Time-Functions.html
For instance, strftime("%A",mktime("YYYY MM DD 00 00 00"))
should give you the day.